UNIT V - Graphs
UNIT V - Graphs
GRAPHS
Definition
A graph G is defined as an ordered set (V, E), where V(G) represents the set ofvertices
and E(G) represents the edges that connect these vertices.
The figure below shows a graph with V(G) = {A, B, C, D and E} and E(G) = {(A, B), (B,
C), (A, D), (B, D), (D, E), (C, E)}. Note that there are five vertices or nodes and six
edges in the graph.
Undirected graph
A graph can be directed or undirected. In an undirected graph, edges do not have any
direction associated with them. That is, if an edge is drawn between nodes A and B, then
the nodes can be traversed from A to B as well as from B to A.
Directed graph
The figure above shows a directed graph. In a directed graph, edges form an ordered
pair. If there is an edge from A to B, then there is a path from A to B but not from B to A.
The edge (A, B) is said to initiate from node A (also known as initial node) and terminate
at node B (terminal node).
Representationof Graphs
There are three common ways of storing graphs inthe computer’s memory. They are:
• Sequential representation by using an adjacencymatrix.
• Linked representation by using an adjacency list that stores the neighbors of a
node usinga linked list.
• Adjacency multi-list which is an extension of linked representation.
• Consider the graph G given in the figure.The adjacency list of G is also given.
(b) Dequeue a node by setting FRONT = FRONT + 1 (remove the FRONT element of
QUEUE) and enqueue the neighbors of A. Also, add A as the ORIG of its neighbors.
(c) Dequeue a node by setting FRONT = FRONT + 1 and enqueue the neighbors of B. Also,
addB asthe ORIG of its neighbors.
(d) Dequeue a node by setting FRONT = FRONT + 1 and enqueue the neighbors of C. Also,
add C as the ORIG of its neighbors. Note that C has two neighborsB and G. Since B has
already beenadded to the queue
(e) Dequeue a node by setting FRONT = FRONT + 1 and enqueue the neighbors of D. Also,
add D as the ORIG of its neighbors. Note that D has two neighborsC and G. Since both of
them havealready been added to the queue
(f) Dequeue a node by setting FRONT = FRONT + 1 and enqueue the neighbors of E. Also,
add E as the ORIG of its neighbors. Note that E has two neighborsC and F. Since C has
alreadybeenadded to the queue
(g) Dequeue a node by setting FRONT = FRONT + 1 and enqueue the neighbors of G. Also,
add G as the ORIG of its neighbors. Note that G has three neighborsF, H, and I.
Now, backtrack from I using ORIG to find the minimum path P. Thus, we have
P as A C G I.
Data Structures Dr. V. Saishanmuga raja SEC
Features of Breadth First Search
Space complexity :In the breadth-first search algorithm, all the nodes at a particular level must
be saved until their child nodes in the next level have been generated. The space complexity is
therefore proportional to the number of nodes at the deepest level of the graph.
Optimality Breadth-first search is optimal for a graph that has edges of equal length, since it
always returns the result with the fewest edges between the start node and the goal node.
In this algorithm, edges that lead to a new vertex are called discovery edges and edges
that lead to an already visited vertex are called back edges.
Consider the graph G given in the Figure. The adjacency list of G is also given. Suppose
we want to print all the nodes that can be reached from the node H (including H itself).
Use a depth-first search of G starting at node H.
H, I, F, C, G, B, E -These are the nodes which are reachable from the node H.
Data Structures Dr. V. Saishanmuga raja SEC
Applications of Depth-First Search Algorithm
Depth-first search is useful for:
• Finding a path between two specified nodes, u and v, of an unweighted graph.
• Finding a path between two specified nodes, u and v, of a weighted graph.
• Finding whether a graph is connected or not.
• Computing the spanning tree of a connected graph.
Prim’s Algorithm
Prim’s algorithm is a greedy algorithm that is used to form a minimum spanning tree for
a connected weighted undirected graph. In other words, the algorithm builds a tree that
includes every vertex and a subset of the edges in such a way that the total weight of all
the edges in the tree is minimized. For this, the algorithm maintains three sets of vertices
which can be given as below:
• Tree vertices: Vertices that are a part of the minimum spanning tree T.
• Fringe vertices: Vertices that are currently not a part of T, but are adjacent to
some tree vertex.
• Unseen vertices:Vertices that are neither tree vertices nor fringe vertices fall
under this category.
consider a graph given below. Construct a minimum spanning tree of the graph given
• Kruskal’s algorithm is used to find the minimum spanning tree for a connected
weighted graph.
• The algorithm aims to find a subset of the edges that forms a tree that includes
every vertex.
• The total weight of all the edges in the tree is minimized.
• In the algorithm, we use a priority queue Q in which edges that have minimum
weight takes a priority over any other edge in the graph.
• When the Kruskal’s algorithm terminates, the forest has only one component and
forms a minimum spanning tree of the graph.
Algorithm
Initially, we have
F = {{A}, {B}, {C}, {D}, {E}, {F}}
MST = {}
Q = {(A, D), (E, F), (C, E), (E, D), (C, D), (D, F), (A, C), (A, B), (B, C)}
AD EF CE ED CD DF AC AB BC
1 2 3 4 5 5 6 7 8
Step 1: Remove the edge (A, D) from Q and make the following changes:
F = {{A, D}, {B}, {C}, {E}, {F}}
MST = {A, D}
Q = {(E, F), (C, E), (E, D), (C, D), (D, F), (A, C), (A, B), (B, C)}
Data Structures Dr. V. Saishanmuga raja SEC
Step 2: Remove the edge (E, F) from Q and make the following changes:
F = {{A, D}, {B}, {C}, {E, F}}
MST = {(A, D), (E, F)}
Q = {(C, E), (E, D), (C, D), (D, F), (A, C), (A, B), (B, C)}
Step 3: Remove the edge (C, E) from Q and make the following changes:
F = {{A, D}, {B}, {C, E, F}}
MST = {(A, D), (C, E), (E, F)}
Q = {(E, D), (C, D), (D, F), (A, C), (A, B), (B, C)}
Step 4: Remove the edge (E, D) from Q and make the following changes:
F = {{A, C, D, E, F}, {B}}
MST = {(A, D), (C, E), (E, F), (E, D)}
Q = {(C, D), (D, F), (A, C), (A, B), (B, C)}
Step 5: Remove the edge (C, D) from Q. Note that this edge creates a loop, so simply
discard this edge.
F = {{A, C, D, E, F}, {B}}
MST = {(A, D), (C, E), (E, F), (E, D)}
Q = {(D, F), (A, C), (A, B), (B, C)}
Step 6: Remove the edge (D, F) from Q. Note that this edge creates a loop, so simply
discard this edge.
F = {{A, C, D, E, F}, {B}}
MST = {(A, D), (C, E), (E, F), (E, D)}
Q = {(A, C), (A, B), (B, C)}
Step 7: Remove the edge (A, C) from Q. Note that this edge creates a loop, so simply
discard this edge.
F = {{A, C, D, E, F}, {B}}
MST = {(A, D), (C, E), (E, F), (E, D)}
Q = {(A, B), (B, C)}
Step 8: Remove the edge (A, B) from Q and make the following changes:
F = {A, B, C, D, E, F}
MST = {(A, D), (C, E), (E, F), (E, D), (A, B)}
Q = {(B, C)}
Step 9: The algorithm continues until Q is empty. Since the entire forest has become one
tree, all the remaining edges will simply be discarded.
F = {A, B, C, D, E, F}
MST = {(A, D), (C, E), (E, F), (E, D), (A, B)}
Q = {}
Algorithm
Example 1: Consider the graph given to find the shortest path to all other nodes
using Dijkstra’s algorithm. Take D as initial node.
A B C D E F G
D 19 15 14 0 & 5 18
A B C D E F G H I
A 0 4 12 19 21 11 9 8 14
Bi-Connected Graph
A bi-connected graph (shown in Fig. 13.10) is defined as a connected graph that has no
articulation vertices. That is, a bi-connected graph is connected and non-separable in the
sense that even if we remove any vertex from the graph, the resultant graph is still
connected. By definition,
• A bi-connected undirected graph is a connected graph that cannot be broken into
disconnected pieces by deleting any single vertex.
• In a bi-connected directed graph, for any two vertices v and w, there are two
directed paths from v to w which have no vertices in common other than v and w.
Cut Vertex
A vertex which when deleted would disconnect the remaining graph.
The algorithm for the topological sort of a graph that has no cycles focuses on selecting a node N with
zero in-degree, that is, a node that has no predecessor. The two main steps involved in the topological
sort algorithm include:
• Selecting a node N with zero in-degree
• Deleting N from the graph along with its edges
We use a QUEUE to hold the nodes with zero in-degree. The order in which the nodes will be
deleted from the graph will depend on the sequence in which the nodes are inserted in the QUEUE.
Then, we will use a variable INDEG, where INDEG(N) will represent the in-degree of node N.
Consider a directed acyclic graph G given in Fig. We use the algorithm given above to find a
topological sort T of G. The steps are given as below:
Step 5: Remove the front element G from the queue by setting FRONT = FRONT + 1, so
FRONT = 2 REAR = 3 QUEUE = A, G, B
Step 6: Set INDEG(D) = INDEG(D) – 1, since D is the neighbour of G. Now, INDEG(C) = 1
INDEG(D) = 1 INDEG(E) = 3 INDEG(F) = 1
Delete the edge from G to D. The graph now becomes as shown in the figure below
Step 10: Remove the front element C from the queue by setting FRONT = FRONT + 1, so
FRONT = 5 REAR = 5 QUEUE = A, G, B, C, D
Step 11: Set INDEG(E) = INDEG(E) – 1, since E is the neighbour of C. Now, INDEG(E) = 1
The graph now becomes as shown in the figure below
Step 12: Remove the front element D from the queue by setting FRONT = FRONT + 1, so
FRONT = 6 REAR = 5 QUEUE = A, B, G, C, D
Step 13: Set INDEG(E) = INDEG(E) – 1, since E is the neighbour of D. Now, INDEG(E) = 0, so
add E to the queue.
The queue now becomes. FRONT = 6 REAR = 6 QUEUE = A, G, B, C, D, E
Step 14: Delete the edge between D an E. The graph now becomes as shown in the figure below
There are no more edges in the graph and all the nodes have been added to the queue, so the
Topological sort T of G can be given as: A, G, B, C, D, E, F.