Unit IV - Graph
Unit IV - Graph
Introduction
1. Define a graph.
A graph is a pair of sets (V, E), where V is the set of vertices and E is the set of edges, connecting the
pairs of vertices.
Each node of the graph is represented as a vertex,
Edge represents a path between two vertices or a line between two vertices.
In the above example, the lines from A to B, A to C, and so on represents edges.
By Graph representation, we simply mean the technique which is to be used in order to store some graph into
the computer's memory.
There are two ways to store Graph into the computer's memory.
1. Sequential representation
2. Linear representation
1. Sequential Representation
In sequential representation, we use adjacency matrix to store the mapping represented by vertices and edges.
In adjacency matrix, the rows and columns are represented by the graph vertices. A graph having n vertices,
will have a dimension n x n.
An entry Mij in the adjacency matrix representation of an undirected graph G will be 1 if there exists an edge
between Vi and Vj.
An undirected graph and its adjacency matrix representation is shown in the following figure.
in the above figure, we can see the mapping among the vertices (A, B, C, D, E) is represented by using the
adjacency matrix which is also shown in the figure.
There exists different adjacency matrices for the directed and undirected graph. In directed graph, an entry
Aij will be 1 only when there is an edge directed from Vi to Vj.
A directed graph and its adjacency matrix representation is shown in the following figure.
Representation of weighted directed graph is different. Instead of filling the entry by 1, the Non- zero entries
of the adjacency matrix are represented by the weight of respective edges.
The weighted directed graph along with the adjacency matrix representation is shown in the following figure.
2. Linked Representation
In the linked representation, an adjacency list is used to store the Graph into the computer's memory.
Consider the undirected graph shown in the following figure and check the adjacency list representation.
An adjacency list is maintained for each node present in the graph which stores the node value and a pointer
to the next adjacent node to the respective node. If all the adjacent nodes are traversed then store the NULL in
the pointer field of last node of the list. The sum of the lengths of adjacency lists is equal to the twice of the
number of edges present in an undirected graph.
Consider the directed graph shown in the following figure and check the adjacency list representation of the
graph.
In a directed graph, the sum of lengths of all the adjacency lists is equal to the number of edges present in the
graph.
In the case of weighted directed graph, each node contains an extra field that is called the weight of the node.
The adjacency list representation of a directed graph is shown in the following figure.
Traversing a Graph
Graph Traversal Algorithm
In this part of the tutorial we will discuss the techniques by using which, we can traverse all the vertices of the
graph.
Traversing the graph means examining all the nodes and vertices of the graph. There are two standard
methods by using which, we can traverse the graphs. Lets discuss each one of them in detail.
Breadth first search is a graph traversal algorithm that starts traversing the graph from root node and
explores all the neighbouring nodes. Then, it selects the nearest node and explore all the unexplored nodes.
The algorithm follows the same process for each of the nearest node until it finds the goal.
The algorithm of breadth first search is given below. The algorithm starts with examining the node A
and all of its neighbours. In the next step, the neighbours of the nearest node of A are explored and process
continues in the further steps. The algorithm explores all neighbours of all the nodes and ensures that each
node is visited exactly once and no node is visited twice.
Algorithm
Example
Consider the graph G shown in the following image, calculate the minimum path p from node A to node E.
Given that each edge has a length of 1.
Solution:
Minimum Path P can be found by applying breadth first search algorithm that will begin at node A and will
end at E. the algorithm uses two queues, namely QUEUE1 and QUEUE2. QUEUE1 holds all the nodes that
are to be processed while QUEUE2 holds all the nodes that are processed and deleted from QUEUE1.
1. QUEUE1 = {A}
2. QUEUE2 = {NULL}
2. Delete the Node A from QUEUE1 and insert all its neighbours. Insert Node A into QUEUE2
1. QUEUE1 = {B, D}
2. QUEUE2 = {A}
3. Delete the node B from QUEUE1 and insert all its neighbours. Insert node B into QUEUE2.
1. QUEUE1 = {D, C, F}
2. QUEUE2 = {A, B}
4. Delete the node D from QUEUE1 and insert all its neighbours. Since F is the only neighbour of it which
has been inserted, we will not insert it again. Insert node D into QUEUE2.
1. QUEUE1 = {C, F}
2. QUEUE2 = { A, B, D}
5. Delete the node C from QUEUE1 and insert all its neighbours. Add node C to QUEUE2.
1. QUEUE1 = {F, E, G}
2. QUEUE2 = {A, B, D, C}
6. Remove F from QUEUE1 and add all its neighbours. Since all of its neighbours has already been added, we
will not add them again. Add node F to QUEUE2.
1. QUEUE1 = {E, G}
2. QUEUE2 = {A, B, D, C, F}
7. Remove E from QUEUE1, all of E's neighbours has already been added to QUEUE1 therefore we will not
add them again. All the nodes are visited and the target node i.e. E is encountered into QUEUE2.
1. QUEUE1 = {G}
2. QUEUE2 = {A, B, D, C, F, E}
The data structure which is being used in DFS is stack. The process is similar to BFS algorithm. In
DFS, the edges that leads to an unvisited node are called discovery edges while the edges that leads to an
already visited node are called block edges.
Algorithm
Example :
Consider the graph G along with its adjacency list, given in the figure below. Calculate the order to print all
the nodes of the graph starting from node H, by using depth first search (DFS) algorithm.
Solution :
1. STACK : H
POP the top element of the stack i.e. H, print it and push all the neighbours of H onto the stack that are is
ready state.
1. Print H
2. STACK : A
Pop the top element of the stack i.e. A, print it and push all the neighbours of A onto the stack that are in
ready state.
1. Print A
2. Stack : B, D
Pop the top element of the stack i.e. D, print it and push all the neighbours of D onto the stack that are in
ready state.
1. Print D
2. Stack : B, F
Pop the top element of the stack i.e. F, print it and push all the neighbours of F onto the stack that are in ready
state.
1. Print F
2. Stack : B
Pop the top of the stack i.e. B and push all the neighbours
1. Print B
2. Stack : C
Pop the top of the stack i.e. C and push all the neighbours.
1. Print C
2. Stack : E, G
Pop the top of the stack i.e. G and push all its neighbours.
1. Print G
2. Stack : E
Pop the top of the stack i.e. E and push all its neighbours.
1. Print E
2. Stack :
Hence, the stack now becomes empty and all the nodes of the graph have been traversed.
H → A → D → F → B → C → G → E