Adjacency List represents a graph as an array of linked list.
The array index represents a vertex and each element in its linked list, and also represents other vertices that form an edge with the vertex.
Representing adjacency list
The graph and its equivalent adjacency list representation are shown below.
The adjacency list is storage efficient because we only need to store values for edges. For a sparse graph with millions of vertices and edges, this can mean a lot of space saved.
The structure of the adjacency list
The simplest adjacency list requires a node data structure to store the vertex and a graph data structure to organize the nodes.
We are approaching the basic definition of a graph - the collection of vertices and edges {V, E}. For simplicity, we use an unlabeled graph, not a labeled one, that is, the vertices are identified by their indices 0,1,2,3.
Let's take a look at the data structure below.
struct node { int vertex; struct node* next; }; struct Graph { int numVertices; struct node** adjLists; };
Don't let struct node*
adjLists overwhelm you.
We need to save the struct node
pointer. Because we don't know how many vertices there will be in the graph, we can't create an array of linked lists at compile time.
Adjacency List C++
It's the same structure, but with C++'s built-in list of STL data structures, we make the structure a bit cleaner. We can also abstract away implementation details.
class Graph { int numVertices; list<int> *adjLists; public: Graph(int V); void addEdge(int src, int dest); };
Java adjacency list
We are using Java Collections to store an array of linked lists.
class Graph { private int numVertices; private LinkedList<integer> adjLists[]; }
The LinkedList type determines what kind of data you want to store in it. For a labeled graph, you can store a dictionary instead of an integer value.
Python adjacency list
There's a reason why Python gets so much "love". A simple dictionary of vertices and its edges is a sufficient representation of a graph. You can make the top as complex as you like.
graph = {'A': set(['B', 'C']), 'B': set(['A', 'D', 'E']), 'C': set(['A', 'F']), 'D': set(['B']), 'E': set(['B', 'F']), 'F': set(['C', 'E'])}
C adjacency list code
#include <stdio.h> #include <stdlib.h> struct node { int vertex; struct node* next; }; struct node* createNode(int); struct Graph { int numVertices; struct node** adjLists; }; struct Graph* createGraph(int vertices); void addEdge(struct Graph* graph, int src, int dest); void printGraph(struct Graph* graph); int main() { struct Graph* graph = createGraph(6); addEdge(graph, 0, 1); addEdge(graph, 0, 2); addEdge(graph, 1, 2); addEdge(graph, 1, 4); addEdge(graph, 1, 3); addEdge(graph, 2, 4); addEdge(graph, 3, 4); addEdge(graph, 4, 6); addEdge(graph, 5, 1); addEdge(graph, 5, 6); printGraph(graph); return 0; } struct node* createNode(int v) { struct node* newNode = malloc(sizeof(struct node)); newNode->vertex = v; newNode->next = NULL; return newNode; } struct Graph* createGraph(int vertices) { struct Graph* graph = malloc(sizeof(struct Graph)); graph->numVertices = vertices; graph->adjLists = malloc(vertices * sizeof(struct node*)); int i; for (i = 0; i < vertices; i++) graph->adjLists[i] = NULL; return graph; } void addEdge(struct Graph* graph, int src, int dest) { // Add edge from src to dest struct node* newNode = createNode(dest); newNode->next = graph->adjLists[src]; graph->adjLists[src] = newNode; // Add edge from dest to src newNode = createNode(src); newNode->next = graph->adjLists[dest]; graph->adjLists[dest] = newNode; } void printGraph(struct Graph* graph) { int v; for (v = 0; v < graph->numVertices; v++) { struct node* temp = graph->adjLists[v]; printf("\n Adjacency list of vertex %d\n ", v); while (temp) { printf("%d -> ", temp->vertex); temp = temp->next; } printf("\n"); } }
C++ adjacency list code
#include <iostream> #include <list> using namespace std; class Graph { int numVertices; list *adjLists; public: Graph(int V); void addEdge(int src, int dest); }; Graph::Graph(int vertices) { numVertices = vertices; adjLists = new list[vertices]; } void Graph::addEdge(int src, int dest) { adjLists[src].push_front(dest); } int main() { Graph g(4); g.addEdge(0, 1); g.addEdge(0, 2); g.addEdge(1, 2); g.addEdge(2, 3); return 0; }
Java adjacency list code
import java.io.*; import java.util.*; class Graph { private int numVertices; private LinkedList<Integer> adjLists[]; Graph(int vertices) { numVertices = vertices; adjLists = new LinkedList[vertices]; for (int i = 0; i < vertices; i++) adjLists[i] = new LinkedList(); } void addEdge(int src, int dest) { adjLists[src].add(dest); } public static void main(String args[]) { Graph g = new Graph(4); g.addEdge(0, 1); g.addEdge(0, 2); g.addEdge(1, 2); g.addEdge(2, 3); } }