This post is completed by 2 users

  • 0
Add to List

264. Graph Implementation – Adjacency List - Better| Set 2

Earlier we had discussed in Graph Representation – Adjacency Matrix and Adjacency List about Graph and its different representations. In this article we will see the better (use inbuilt class of LinkedList) way to implement graph using adjacency list.

Let’s revise few basic concepts:

What is Graph:

G = (V,E)

Graph is a collection of nodes or vertices (V) and edges(E) between them. We can traverse these nodes using the edges. These edges might be weighted or non-weighted.

There can be two kinds of Graphs

  1. Un-directed Graph – when you can traverse either direction between two nodes.
  2. Directed Graph – when you can traverse only in the specified direction between two nodes.

Now how do we represent a Graph, There are two common ways to represent it:

    1. Adjacency Matrix
    2. Adjacency List
  1. Adjacency List:

Adjacency List is the Array[] of Linked List, where array size is same as number of Vertices in the graph. Every Vertex has a Linked List. Each Node in this Linked list represents the reference to the other vertices which share an edge with the current vertex. The weights can also be stored in the Linked List Node.

Adjacency List
Adjacency List


Vertex 0 is connected to: 4 1
Vertex 1 is connected to: 4 3 2 0
Vertex 2 is connected to: 3 1
Vertex 3 is connected to: 4 2 1
Vertex 4 is connected to: 3 1 0