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

  • Un-directed Graph – when you can traverse either direction between two nodes.
  • 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:

  • Adjacency Matrix
  • Adjacency List

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

Complete Code for Graph Using 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

Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.

You may also like...

%d bloggers like this: