Graph is a data structure that consists of following two components:

1. A finite set of vertices also called as nodes.
2. A finite set of ordered pair of the form (u, v) called as edge. The pair is ordered because (u, v) is not same as (v, u) in case of directed graph(di-graph). The pair of form (u, v) indicates that there is an edge from vertex u to vertex v. The edges may contain weight/value/cost.

Graphs are used to represent many real life applications: Graphs are used to represent networks. The networks may include paths in a city or telephone network or circuit network. Graphs are also used in social networks like linkedIn, facebook. For example, in facebook, each person is represented with a vertex(or node). Each node is a structure and contains information like person id, name, gender and locale. See this for more applications of graph.

[ad type=”banner”]

Following is an example undirected graph with 5 vertices.

Following two are the most commonly used representations of graph.
1. Adjacency Matrix
2. Adjacency List
There are other representations also like, Incidence Matrix and Incidence List. The choice of the graph representation is situation specific. It totally depends on the type of operations to be performed and ease of use.

Adjacency Matrix:
Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. Let the 2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j. Adjacency matrix for undirected graph is always symmetric. Adjacency Matrix is also used to represent weighted graphs. If adj[i][j] = w, then there is an edge from vertex i to vertex j with weight w.

Pros: Representation is easier to implement and follow. Removing an edge takes O(1) time. Queries like whether there is an edge from vertex ‘u’ to vertex ‘v’ are efficient and can be done O(1).

Cons: Consumes more space O(V^2). Even if the graph is sparse(contains less number of edges), it consumes the same space. Adding a vertex is O(V^2) time.
Adjacency List:
An array of linked lists is used. Size of the array is equal to number of vertices. Let the array be array[]. An entry array[i] represents the linked list of vertices adjacent to the ith vertex. This representation can also be used to represent a weighted graph. The weights of edges can be stored in nodes of linked lists. Following is adjacency list representation of the above graph.

 

Below is C code for adjacency list representation of an undirected graph:

[pastacode lang=”c” manual=”%2F%2F%20A%20C%20Program%20to%20demonstrate%20adjacency%20list%20representation%20of%20graphs%0A%20%0A%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstdlib.h%3E%0A%20%0A%2F%2F%20A%20structure%20to%20represent%20an%20adjacency%20list%20node%0Astruct%20AdjListNode%0A%7B%0A%20%20%20%20int%20dest%3B%0A%20%20%20%20struct%20AdjListNode*%20next%3B%0A%7D%3B%0A%20%0A%2F%2F%20A%20structure%20to%20represent%20an%20adjacency%20list%0Astruct%20AdjList%0A%7B%0A%20%20%20%20struct%20AdjListNode%20*head%3B%20%20%2F%2F%20pointer%20to%20head%20node%20of%20list%0A%7D%3B%0A%20%0A%2F%2F%20A%20structure%20to%20represent%20a%20graph.%20A%20graph%20is%20an%20array%20of%20adjacency%20lists.%0A%2F%2F%20Size%20of%20array%20will%20be%20V%20(number%20of%20vertices%20in%20graph)%0Astruct%20Graph%0A%7B%0A%20%20%20%20int%20V%3B%0A%20%20%20%20struct%20AdjList*%20array%3B%0A%7D%3B%0A%20%0A%2F%2F%20A%20utility%20function%20to%20create%20a%20new%20adjacency%20list%20node%0Astruct%20AdjListNode*%20newAdjListNode(int%20dest)%0A%7B%0A%20%20%20%20struct%20AdjListNode*%20newNode%20%3D%0A%20%20%20%20%20%20%20%20%20%20%20%20(struct%20AdjListNode*)%20malloc(sizeof(struct%20AdjListNode))%3B%0A%20%20%20%20newNode-%3Edest%20%3D%20dest%3B%0A%20%20%20%20newNode-%3Enext%20%3D%20NULL%3B%0A%20%20%20%20return%20newNode%3B%0A%7D%0A%20%0A%2F%2F%20A%20utility%20function%20that%20creates%20a%20graph%20of%20V%20vertices%0Astruct%20Graph*%20createGraph(int%20V)%0A%7B%0A%20%20%20%20struct%20Graph*%20graph%20%3D%20(struct%20Graph*)%20malloc(sizeof(struct%20Graph))%3B%0A%20%20%20%20graph-%3EV%20%3D%20V%3B%0A%20%0A%20%20%20%20%2F%2F%20Create%20an%20array%20of%20adjacency%20lists.%20%20Size%20of%20array%20will%20be%20V%0A%20%20%20%20graph-%3Earray%20%3D%20(struct%20AdjList*)%20malloc(V%20*%20sizeof(struct%20AdjList))%3B%0A%20%0A%20%20%20%20%20%2F%2F%20Initialize%20each%20adjacency%20list%20as%20empty%20by%20making%20head%20as%20NULL%0A%20%20%20%20int%20i%3B%0A%20%20%20%20for%20(i%20%3D%200%3B%20i%20%3C%20V%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20graph-%3Earray%5Bi%5D.head%20%3D%20NULL%3B%0A%20%0A%20%20%20%20return%20graph%3B%0A%7D%0A%20%0A%2F%2F%20Adds%20an%20edge%20to%20an%20undirected%20graph%0Avoid%20addEdge(struct%20Graph*%20graph%2C%20int%20src%2C%20int%20dest)%0A%7B%0A%20%20%20%20%2F%2F%20Add%20an%20edge%20from%20src%20to%20dest.%20%20A%20new%20node%20is%20added%20to%20the%20adjacency%0A%20%20%20%20%2F%2F%20list%20of%20src.%20%20The%20node%20is%20added%20at%20the%20begining%0A%20%20%20%20struct%20AdjListNode*%20newNode%20%3D%20newAdjListNode(dest)%3B%0A%20%20%20%20newNode-%3Enext%20%3D%20graph-%3Earray%5Bsrc%5D.head%3B%0A%20%20%20%20graph-%3Earray%5Bsrc%5D.head%20%3D%20newNode%3B%0A%20%0A%20%20%20%20%2F%2F%20Since%20graph%20is%20undirected%2C%20add%20an%20edge%20from%20dest%20to%20src%20also%0A%20%20%20%20newNode%20%3D%20newAdjListNode(src)%3B%0A%20%20%20%20newNode-%3Enext%20%3D%20graph-%3Earray%5Bdest%5D.head%3B%0A%20%20%20%20graph-%3Earray%5Bdest%5D.head%20%3D%20newNode%3B%0A%7D%0A%20%0A%2F%2F%20A%20utility%20function%20to%20print%20the%20adjacenncy%20list%20representation%20of%20graph%0Avoid%20printGraph(struct%20Graph*%20graph)%0A%7B%0A%20%20%20%20int%20v%3B%0A%20%20%20%20for%20(v%20%3D%200%3B%20v%20%3C%20graph-%3EV%3B%20%2B%2Bv)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20struct%20AdjListNode*%20pCrawl%20%3D%20graph-%3Earray%5Bv%5D.head%3B%0A%20%20%20%20%20%20%20%20printf(%22%5Cn%20Adjacency%20list%20of%20vertex%20%25d%5Cn%20head%20%22%2C%20v)%3B%0A%20%20%20%20%20%20%20%20while%20(pCrawl)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20printf(%22-%3E%20%25d%22%2C%20pCrawl-%3Edest)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20pCrawl%20%3D%20pCrawl-%3Enext%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20printf(%22%5Cn%22)%3B%0A%20%20%20%20%7D%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20functions%0Aint%20main()%0A%7B%0A%20%20%20%20%2F%2F%20create%20the%20graph%20given%20in%20above%20fugure%0A%20%20%20%20int%20V%20%3D%205%3B%0A%20%20%20%20struct%20Graph*%20graph%20%3D%20createGraph(V)%3B%0A%20%20%20%20addEdge(graph%2C%200%2C%201)%3B%0A%20%20%20%20addEdge(graph%2C%200%2C%204)%3B%0A%20%20%20%20addEdge(graph%2C%201%2C%202)%3B%0A%20%20%20%20addEdge(graph%2C%201%2C%203)%3B%0A%20%20%20%20addEdge(graph%2C%201%2C%204)%3B%0A%20%20%20%20addEdge(graph%2C%202%2C%203)%3B%0A%20%20%20%20addEdge(graph%2C%203%2C%204)%3B%0A%20%0A%20%20%20%20%2F%2F%20print%20the%20adjacency%20list%20representation%20of%20the%20above%20graph%0A%20%20%20%20printGraph(graph)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Output:

 Adjacency list of vertex 0
 head -> 4-> 1

 Adjacency list of vertex 1
 head -> 4-> 3-> 2-> 0

 Adjacency list of vertex 2
 head -> 3-> 1

 Adjacency list of vertex 3
 head -> 4-> 2-> 1

 Adjacency list of vertex 4
 head -> 3-> 1-> 0

Pros: Saves space O(|V|+|E|) . In the worst case, there can be C(V, 2) number of edges in a graph thus consuming O(V^2) space. Adding a vertex is easier.

Cons: Queries like whether there is an edge from vertex u to vertex v are not efficient and can be done O(V).

[ad type=”banner”]