0% found this document useful (0 votes)
75 views

UNIT V - Graphs

The document defines graphs and different ways to represent graphs in a computer's memory. It discusses directed and undirected graphs. Adjacency matrix and adjacency list representations of graphs are described. Graph traversal algorithms like breadth-first search and depth-first search are explained along with pseudocode. Applications of these algorithms are provided. Shortest path algorithms like Dijkstra's algorithm, minimum spanning tree, and Prim's algorithm for finding minimum spanning trees are summarized with examples.

Uploaded by

bala
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

UNIT V - Graphs

The document defines graphs and different ways to represent graphs in a computer's memory. It discusses directed and undirected graphs. Adjacency matrix and adjacency list representations of graphs are described. Graph traversal algorithms like breadth-first search and depth-first search are explained along with pseudocode. Applications of these algorithms are provided. Shortest path algorithms like Dijkstra's algorithm, minimum spanning tree, and Prim's algorithm for finding minimum spanning trees are summarized with examples.

Uploaded by

bala
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

UNIT IV

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.

Adjacency Matrix Representation


An adjacency matrix is used to represent which nodes are adjacent to one another.
Bydefinition,two nodes are said to be adjacent if there is an edge connecting them.
• In a directed graph G, if node v is adjacent to node u, then there is definitely an
edge from uto v.
• That is, if v is adjacent to u, we can get from u to v by traversing one edge. For
any graph Ghaving n nodes, the adjacency matrix will have the dimension of n ¥ n.
• In an adjacency matrix, the rows and columns are labeled by graph vertices.

Data Structures Dr. V. Saishanmuga raja SEC


• An entry aij in theadjacency matrix will contain 1, if vertices viand vj are adjacent
to each other. However, if the nodes arenot adjacent, aij will be set to zero.

Adjacency List Representation


An adjacency list is another way in which graphs can be represented in the computer’s
memory.This structure consists of a list of all nodes in G. Furthermore, every node is in
turn linked to itsown list that contains the names of all other nodes that are adjacent to
it.
The key advantages of using an adjacency list are:
• It is easy to follow and clearly shows the adjacent nodes of a particular node.
• It is often used for storing graphs that have a small-to-moderate number of edges.

Data Structures Dr. V. Saishanmuga raja SEC


GRAPH TRAVERSAL ALGORITHMS
There are two standard methods of graph traversal
1. Breadth-first search
2. Depth-first search
While breadth-first search uses a queue as an auxiliary data structure to store nodes for
furtherprocessing, the depth-first search scheme uses a stack. But both these algorithms
make use ofa variable STATUS.

Value of status and its significance

Breadth-First Search Algorithm


Breadth-first search (BFS) is a graph search algorithm that begins at the root node and
explores allthe neighboring nodes. Then for each of those nearest nodes, the algorithm
explorestheir unexplored neighbor nodes, and so on, until it finds the goal.

• Consider the graph G given in the figure.The adjacency list of G is also given.

Data Structures Dr. V. Saishanmuga raja SEC


• Assume that we want to find the minimum path P between A to I, given that every
edge has a length of 1.
• The minimum path P can be found by applying thebreadth-first search algorithm
that begins at A andends when I is encountered.
• During the execution of thealgorithm, we use two arrays:QUEUE and ORIG.
• While QUEUE is used to hold the nodes that have to be processed, ORIG is used to
keep track ofthe origin of each edge.

(a) Add A to QUEUE and add NULL to ORIG.

(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.

Completeness Breadth-first search is said to be a complete algorithm because if there is a


solution, breadth-first search will find it regardless of the kind of graph. But in case of an infinite
graph where there is no possible solution, it will diverge.

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.

Applications of Breadth-First Search Algorithm


Breadth-first search can be used to solve many problems such as:
• Finding all connected components in a graph G.
• Finding all nodes within an individual connected component.
• Finding the shortest path between two nodes, u and v, of an unweighted graph.
• Finding the shortest path between two nodes, u and v, of a weighted graph.

Depth-first Search Algorithm


The depth-first search algorithm progresses by expanding the starting node of G and then
going deeper and deeper until the goal node is found, or until a node that has no children
is encountered. When a dead-end is reached, the algorithm backtracks, returning to the
most recent node that has not been completely explored.

Algorithm of Depth First Search

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.

Data Structures Dr. V. Saishanmuga raja SEC


Solution:

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.

Data Structures Dr. V. Saishanmuga raja SEC


Shortest Path Algorithms

• Minimum spanning tree


• Dijkstra’s algorithm
• Warshall’s algorithm

Minimum spanning tree

• A spanning tree of a connected, undirected graph G is a sub-graph of G which is a


tree that connects all the vertices together.
• A minimum spanning tree (MST) is defined as a spanning tree with weight less
than or equal to the weight of every other spanning tree.
• In other words, a minimum spanning tree is a spanning tree that has weights
associated with its edges, and the total weight of the tree (the sum of the weights
of its edges) is at a minimum.

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.

Algorithm for Prim’s Algorithm

consider a graph given below. Construct a minimum spanning tree of the graph given

Data Structures Dr. V. Saishanmuga raja SEC


Example 2: Construct a minimum spanning tree of the graph given. Start the Prim’s
algorithm from vertex D.

Data Structures Dr. V. Saishanmuga raja SEC


Kruskal’s Algorithm

• 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

Example 1: Apply Kruskal’s algorithm on the graph given

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 = {}

Data Structures Dr. V. Saishanmuga raja SEC


Dijkstra’s Algorithm
• Given a graph G and a source node A, the algorithm is used to find the shortest path
(one having the lowest cost) between A(source node) and every other node.
• Moreover, Dijkstra’s algorithm is also used for finding the costs of the shortest paths
from a source node to a destination node.
• Dijkstra’s algorithm is used to find the length of an optimal path between two nodes
in a graph.
• The term optimal can mean anything, shortest, cheapest, or fastest.

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

Data Structures Dr. V. Saishanmuga raja SEC


Example 2: Consider the graph. Determine the shortest distance to all other
nodes using Dijkstra’s Algorithm starting from A.

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.

Non bi-connected graph

Cut Vertex
A vertex which when deleted would disconnect the remaining graph.

Data Structures Dr. V. Saishanmuga raja SEC


APPLICATIONS OF GRAPHS
Graphs are constructed for various types of applications such as:
• In circuit networks where points of connection are drawn as vertices and component
wires become the edges of the graph.
• In transport networks where stations are drawn as vertices and routes become the edges
of the graph.
• In maps that draw cities/states/regions as vertices and adjacency relations as edges.
• In program flow analysis where procedures or modules are treated as vertices and calls
to these procedures are drawn as edges of the graph.
• Once we have a graph of a particular concept, they can be easily used for finding
shortestpaths, project planning, etc.
• In flowcharts or control-flow graphs, the statements and conditions in a program are
represented as nodes and the flow of control is represented by the edges.
• In state transition diagrams, the nodes are used to represent states and the edges
represent legal moves from one state to the other.
• Graphs are also used to draw activity network diagrams. These diagrams are extensively
used as a project management.

Data Structures Dr. V. Saishanmuga raja SEC


TOPOLOGICAL SORTING
• Topological sort of a directed acyclic graph (DAG) G is defined as a linear ordering of its nodes
in which each node comes before all nodes to which it has outbound edges.
• Every DAG has one or more number of topological sorts.
• Topological sort is possible only on directed acyclic graphs that do not have any cycles.
• In simple words, a topological ordering of a DAG G is an ordering of its vertices such that any
directed path in G traverses the vertices in increasing order.
• Topological sorting is widely used in scheduling applications, jobs, or tasks. The jobs that have to
be completed are represented by nodes, and there is an edge from node u to v if job u must be
completed before job v can be started.

Algorithm for topological sorting

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:

Data Structures Dr. V. Saishanmuga raja SEC


Step 1: Find the in-degree INDEG(N) of every node in the graph
INDEG(A) = 0 INDEG(B) = 1 INDEG(C) = 1 INDEG(D) = 2
INDEG(E) = 3 INDEG(F) = 1 INDEG(G) = 0
Step 2: Enqueue all the nodes with a zero in-degree
FRONT = 1 REAR = 2 QUEUE = A, G
Step 3: Remove the front element A from the queue by setting FRONT = FRONT + 1, so
FRONT = 2 REAR = 2 QUEUE = A, G
Step 4: Set INDEG(B) = INDEG(B) – 1, since B is the neighbour of A. Note that INDEG(B) is 0, so
add it on the queue. The queue now becomes FRONT = 2 REAR = 3 QUEUE = A, G, B
Delete the edge from A to B. The graph now becomes as shown in the figure 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

Data Structures Dr. V. Saishanmuga raja SEC


Step 7: Remove the front element B from the queue by setting FRONT = FRONT + 1, so
FRONT = 4 REAR = 3 QUEUE = A, G, B
Step 8: Set INDEG(C) = INDEG(C) – 1, INDEG(D) = INDEG(D) – 1,
INDEG(E) = INDEG(E) – 1, since C, D, and E are the neighbours of B.
Now, INDEG(C) = 0, INDEG(D) = 1 and INDEG(E) = 2
Step 9: Since the in-degree of node c and D is zero, add C and D at the rear of the queue. The queue can
be given as below:
FRONT = 4 REAR = 5 QUEUE = A, G, B, C, 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

Data Structures Dr. V. Saishanmuga raja SEC


Step 15: Remove the front element E from the queue by setting FRONT = FRONT + 1, so
FRONT = 7 REAR = 6 QUEUE = A, G, B, C, D, E
Step 16: Set INDEG(F) = INDEG(F) – 1, since F is the neighbour of E. Now INDEG(F) = 0, so
add F to the queue. The queue now becomes,
FRONT = 7 REAR = 7 QUEUE = A, G, B, C, D, E, F
Step 17: Delete the edge between E and F. 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.

Data Structures Dr. V. Saishanmuga raja SEC

You might also like