0% found this document useful (0 votes)
6 views127 pages

7002 Ds Graph Traversal

Uploaded by

srk712906
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views127 pages

7002 Ds Graph Traversal

Uploaded by

srk712906
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 127

Graph Traversal

Dr. Bibhudatta Sahoo


Communication & Computing Group
Department of CSE, NIT Rourkela
Email: [email protected], 9937324437, 2462358
Graph Traversal
1.Depth-First Search (DFS): Depth-First Search explores a graph by moving as far from the
starting point as possible before backtracking. It uses a stack (either implicitly through recursion
or explicitly using a data structure) to keep track of the path as it searches. DFS is particularly
useful for tasks such as topological sorting, checking for connected components in a graph, and
solving puzzles with only one solution.

2. Breadth-First Search (BFS): Breadth-First Search explores the graph level by level,
starting from the given source node. It uses a queue to track all the vertices that need to be
explored next. BFS is ideal for finding the shortest path on unweighted graphs and is used in
algorithms that need to explore all vertices at the present depth before moving on to vertices at
the next depth level.

3. Bidirectional Search: Bidirectional search is used primarily for searching a path from a
start vertex to a target vertex in unweighted graphs. It runs two simultaneous breadth-first
searches—one from the start vertex and the other from the target vertex, meeting in the
middle.This can drastically reduce the search space and is efficient for finding the shortest path.

4. Dijkstra’s Algorithm: While technically a shortest path algorithm, Dijkstra’s uses a BFS
approach to traverse the graph. It is used for finding the shortest paths from a source node to all
other nodes in a graph with non-negative edge weights. The algorithm uses a priority queue to
greedily select the next vertex with the smallest distance.

2 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Graph Traversal
5. Bellman-Ford Algorithm: This algorithm is another shortest path
method but, unlike Dijkstra's, it can handle graphs with negative weight edges
and can detect negative weight cycles in the graph. It also uses a form of BFS
but with more iterations over all edges to ensure all shortest paths are found
even when negative weights are involved.

6. Floyd-Warshall Algorithm : An all-pairs shortest path algorithm, Floyd-


Warshall is a dynamic programming solution that calculates the shortest paths
between all pairs of vertices. It is able to handle negative weights but not
negative cycles. The algorithm iteratively improves the path lengths between all
pairs of vertices.

7. A* Search Algorithm: A* Search is an extension and optimization of


Dijkstra’s Algorithm that adds a heuristic into the mix to improve the efficiency
of the search, particularly in pathfinding and graph traversal problems. The
heuristic helps estimate the cost of the cheapest path from any vertex to the
target, allowing the algorithm to explore only the most promising paths.

3 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Minimum Spanning tree
Graph & Spanning tree
 A spanning tree T of an undirected graph G is a subgraph that is
a tree which includes all of the vertices of G, with a minimum
possible number of edges.

5 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


What is a Spanning Tree?
 Given an undirected and connected graph G=(V, E), a spanning
tree of the graph G is a tree that spans G (that is, it includes
every vertex of G) and is a subgraph of G (every edge in the tree
belongs to G).
 A spanning tree (V, E’) is a subset of Graph G, which has all the
vertices covered with minimum possible number of edges.
Hence, a spanning tree does not have cycles and it cannot be
disconnected.
 Let G=(V, E) be an undirected connected graph, A sub-graph t=
(V, E’) of G is an spanning tree of G if and only if t is a tree.

6 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


What is a Spanning Tree?
General Properties of Spanning Tree
1. A connected graph G can have more than one spanning tree.
2. All possible spanning trees of graph G, have the same number
of edges and vertices.
3. The spanning tree does not have any cycle (loops).
4. Removing one edge from the spanning tree will make the
graph disconnected, i.e. the spanning tree is minimally
connected.
5. Adding one edge to the spanning tree will create a circuit or
loop, i.e. the spanning tree is maximally acyclic.
6. A spanning tree cannot have any cycles and consist of (n-1) edges
(where n is the number of vertices of the graph) because “it uses the
minimum number of edges”.

7 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Properties of a spanning tree
 Let’s list out a couple of properties of a spanning tree. As a minimum
spanning tree is also a spanning tree, these properties will also be true
for a minimum spanning tree.
1. In a spanning tree, the number of edges will always be .
2. A spanning tree doesn’t contain any loops or cycles.
3. If we remove any one edge from the spanning tree, it will
make it disconnected.
4. If we add one edge in a spanning tree, then it will create a
cycle.

8 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Properties of a spanning tree
 A spanning tree whose overall resultant weight value is minimal is
considered to be a Minimal SpanningTree.
 A connected graph can have more than one spanning tree.
 All Spanning trees must contain the same number of vertices as of
graph, and the number of edges must be equal to |V| - 1.
 The spanning tree must not contain any cycle.
 If the given graph is a cycle graph, then the number of possible
spanning trees will be equal to the number of vertices of the given
graph.
 If the given graph is a complete graph with n vertices, then the
number of possible spanning trees can be calculated using Cayley’s
Formula. [ (n + 1)n − 1 ]

9 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Spanning tree of a complete graph
 A complete graph is a graph in which each pair of graph vertices is
connected by an edge. The complete graph with n graph vertices is
𝑛𝑛
denoted Kn and has = n(n-1) /2 (the triangular numbers)
2
undirected edges. In older literature, complete graphs are
sometimes called universal graphs.

10 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


A complete graph with 10 nodes and 45 edges.

11 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


No of Spanning Tree of K4 (complete graph of order 4)

12 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Depth-First Search (DFS)
DFS (Depth First Search)
 DFS traversal of a graph produces a spanning tree as final result.
 SpanningTree is a graph without loops.
 The algorithm uses Stack data structure with maximum size of
total number of vertices in the graph to implement DFS traversal.

14 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


DFS (Depth First Search)
 Step 1 - Define a Stack of size total number of vertices in the graph.
 Step 2 - Select any vertex as starting point for traversal. Visit that
vertex and push it on to the Stack.
 Step 3 - Visit any one of the non-visited adjacent vertices of a
vertex which is at the top of stack and push it on to the stack.
 Step 4 - Repeat step 3 until there is no new vertex to be visited from
the vertex which is at the top of the stack.
 Step 5 - When there is no new vertex to visit then use back
tracking and pop one vertex from the stack.
 Step 6 - Repeat steps 3, 4 and 5 until stack becomes Empty.
 Step 7 - When stack becomes Empty, then produce final spanning
tree by removing unused edges from the graph

15 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


DFS (Depth First Search)

16 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


DFS (Depth First Search)

17 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


DFS (Depth First Search)

18 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


DFS (Depth First Search)

19 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


DFS (Depth First Search)

20 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


DFS (Depth First Search)

21 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


DFS (Depth First Search)

22 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


23 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024
24 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024
DFS (Depth First Search)

25 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


DFS (Depth First Search)

26 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


DFS (Depth First Search)

27 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


DFS (Depth First Search)

28 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


DFS (Depth First Search)

29 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


DFS (Depth First Search)

30 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


DFS (Depth First Search)
The step by step process to implement the DFS traversal
1. First, create a stack with the total number of vertices in the graph.
2. Now, choose any vertex as the starting point of traversal, and push
that vertex into the stack.
3. After that, push a non-visited vertex (adjacent to the vertex on the
top of the stack) to the top of the stack.
4. Now, repeat steps 3 and 4 until no vertices are left to visit from the
vertex on the stack's top.
5. If no vertex is left, go back and pop a vertex from the stack.
6. Repeat steps 2, 3, and 4 until the stack is empty.

32 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


33 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024
34 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024
35 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024
36 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024
37 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024
38 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024
Complexity of DFS Algorithm with an Adjacency Matrix
In an adjacency matrix:
 V: the number of vertices (nodes)
 The matrix has a size of V×V , where each cell matrix[i][j] indicates the presence
(or absence) of an edge between vertices i and j.
 Checking the neighbors of a node requires scanning an entire row, which takes
O(V) time.

39 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Complexity of DFS Algorithm with an Adjacency List
Let the graph G(V,E) is with
 V: the number of vertices (nodes)
 E: the number of edges

40 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Breadth-First Search (BFS)
Breadth-First Search (BFS)
 BFS traversal of a graph produces a spanning tree as final result.
 SpanningTree is a graph without loops.
 Queue data structure with maximum size of total number of
vertices in the graph to implement BFS traversal.

42 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Breadth-First Search (BFS)
 Step 1 - Define a Queue of size total number of vertices in the graph.
 Step 2 - Select any vertex as starting point for traversal. Visit that
vertex and insert it into the Queue.
 Step 3 - Visit all the non-visited adjacent vertices of the vertex
which is at front of the Queue and insert them into the Queue.
 Step 4 - When there is no new vertex to be visited from the vertex
which is at front of the Queue then delete that vertex.
 Step 5 - Repeat steps 3 and 4 until queue becomes empty.
 Step 6 - When queue becomes empty, then produce final spanning
tree by removing unused edges from the graph

43 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Breadth-First Search (BFS)

44 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Breadth-First Search (BFS)

45 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Breadth-First Search (BFS)

46 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Breadth-First Search (BFS)

47 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Breadth-First Search (BFS)

48 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Complexity of BFS Algorithm with an Adjacency Matrix
In an ADJACENCY MATRIX:
 V: the number of vertices (nodes)
 The matrix has a size of V×V , where each cell matrix[i][j] indicates the presence
(or absence) of an edge between vertices i and j.
 Checking the neighbors of a node requires scanning an entire row, which takes
O(V) time.

49 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Complexity of BFS Algorithm with an Adjacency List
Let the graph G(V,E) is with
 V: the number of vertices (nodes)
 E: the number of edges

50 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Traversing a Graph

51 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Breadth first traversal algorithm on graph G

52 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Breadth-First Search (BFS)

53 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Depth first traversal on a graph G

54 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Depth First Search (DFS)

55 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


56 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024
Summary of Complexities with an Adjacency Matrix

57 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Summary of Complexities with an Adjacency List

58 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Graph Traversal with other data structures
1. Edge List

60 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


2. Incidence Matrix

61 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


3. Compressed Sparse Row (CSR) and Compressed Sparse
Column (CSC) Formats

62 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


4. Adjacency Array (Edge Array)

63 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Summary of Complexities

64 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Parameters BFS DFS
Stands for BFS stands for Breadth First Search. DFS stands for Depth First Search.

BFS(Breadth First Search) uses Queue data structure


Data Structure DFS(Depth First Search) uses Stack data structure.
for finding the shortest path.

DFS is also a traversal approach in which the


BFS is a traversal approach in which we first walk
traverse begins at the root node and proceeds
Definition through all nodes on the same level before moving on
through the nodes as far as possible until we reach
to the next level.
the node with no unvisited nearby nodes.

Conceptual
BFS builds the tree level by level. DFS builds the tree sub-tree by sub-tree.
Difference

It works on the concept of LIFO (Last In First


Approach used It works on the concept of FIFO (First In First Out).
Out).

BFS is more suitable for searching vertices closer to DFS is more suitable when there are solutions away
Suitable for
the given source. from source.

DFS is used in various applications such as acyclic


BFS is used in various applications such as bipartite
graphs and finding strongly connected components
graphs, shortest paths, etc. If weight of every edge is
Applications etc. There are many applications where both BFS
same, then BFS gives shortest pat from source to
and DFS can be used like Topological Sorting,
every other vertex.
Cycle Detection, etc.

65 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Minimum spanning tree
Mathematical Properties of Spanning Tree
 Spanning tree has n-1 edges, where n is the number of nodes
(vertices).
 From a complete graph, by removing maximum e - n + 1 edges, we
can construct a spanning tree.
 A complete graph can have maximum nn-2 number of spanning trees.
 Spanning trees are a subset of connected Graph G and disconnected
graphs do not have spanning tree.

67 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Minimum spanning tree

The sum of the edges of the above graph is 16

68 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Different algorithms for finding the Minimum Spanning Tree (MST) of a graph.
1. Prim's Algorithm
 Approach: Starts with a single vertex and grows the MST by repeatedly adding
the smallest edge that connects a vertex in the MST to a vertex outside the MST.
 Best For: Dense graphs (graphs with many edges).
 Key Features:
 Greedy algorithm.
 Requires a priority queue or a simple array to find the smallest edge.
2. Kruskal's Algorithm
 Approach: Adds edges in ascending order of weight, ensuring no cycles are
formed.
 Best For: Sparse graphs (graphs with fewer edges).
 Key Features:
 Greedy algorithm.
 Uses the Disjoint-Set (Union-Find) data structure for cycle detection.

69 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Different algorithms for finding the Minimum Spanning Tree (MST) of a graph.
3. Borůvka’s Algorithm
 Approach: Repeatedly adds the smallest edge for each connected component of the graph until a
single connected component (the MST) is formed.
 Best For: Graphs with distributed parallel computing, as edges can be processed in parallel.
 Key Features:
 Greedy algorithm.
 Simple implementation but often less efficient for dense graphs compared to Prim’s and
Kruskal’s.
4. Reverse-Delete Algorithm
 Approach:
 Start with all edges in the graph.
 Sort edges in descending order of weight.
 Remove edges one by one, but only if removing an edge does not disconnect the graph.
 Best For:Theoretical applications or when removing edges is easier than adding.
 Key Features:
 Works in reverse compared to Kruskal's algorithm.
 Less practical due to inefficiency.

70 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Different algorithms for finding the Minimum Spanning Tree (MST) of a graph.

5. Dijkstra-Based Variants
 Approach: Though not strictly designed for MST, Dijkstra's algorithm can be
adapted to find spanning trees by modifying its relaxation criteria to focus on edges
connecting to unvisited vertices.
 Best For: Specific graph configurations or as part of hybrid algorithms.

71 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Kruskal's Algorithm
Kruskal's Algorithm
Kruskal's algorithm is a greedy algorithm that finds a minimum spanning
tree for a connected weighted graph. It finds a tree of that graph
which includes every vertex and the total weight of all the edges in the
tree is less than or equal to every possible spanning tree.
Algorithm
 Step 1 − Arrange all the edges of the given graph G(V,E) in
ascending order as per their edge weight.
 Step 2 − Choose the smallest weighted edge from the graph and
check if it forms a cycle with the spanning tree formed so far.
 Step 3 − If there is no cycle, include this edge to the spanning tree
else discard it.
 Step 4 − Repeat Step 2 and Step 3 until (|V|−1) number of edges
are left in the spanning tree.
73 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024
Kruskal's Algorithm
Joseph Bernard Kruskal, Jr in 1956

Kruskal Approach:
 Select the minimum weight edge that does not form a cycle

Kruskal's Algorithm:
sort the edges of G in increasing order by length
keep a subgraph S of G, initially empty
for each edge e in sorted order
if the endpoints of e are disconnected in S
add e to S
return S

74 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Steps in Kruskal's Algorithm

75 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Kruskal's Algorithm

76 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Kruskal’s Minimum Spanning Tree Algorithm #1/7
 The algorithm is a Greedy Algorithm. The Greedy Choice is to pick
the smallest weight edge that does not cause a cycle in the MST
constructed so far.

 The graph contains 9 vertices and 14 edges. So, the minimum


spanning tree formed will be having (9 – 1) = 8 edges.

77 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Kruskal’s Minimum Spanning Tree Algorithm #2/7
Weight Source Destination
1 7 6
2 8 2
2 6 5
4 0 1
4 2 5
6 8 6
7 2 3
7 7 8
8 0 7
8 1 2
9 3 4
10 5 4
11 1 7
14 3 5
78 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024
Kruskal’s Minimum Spanning Tree Algorithm #3/7

 1. Pick edge 7-6: No cycle is formed, include it.


 2. Pick edge 8-2: No cycle is formed, include it.
 3. Pick edge 6-5: No cycle is formed, include it.

79 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Kruskal’s Minimum Spanning Tree Algorithm #4/7
 4. Pick edge 0-1: No cycle is formed, include it.
 5. Pick edge 2-5: No cycle is formed, include it.
 6. Pick edge 8-6: Since including this edge results in cycle, discard it.

80 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Kruskal’s Minimum Spanning Tree Algorithm #5/7
 7. Pick edge 2-3: No cycle is formed, include it.
 8. Pick edge 7-8: Since including this edge results in cycle, discard it.
9. Pick edge 0-7: No cycle is formed, include it.

81 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Kruskal’s Minimum Spanning Tree Algorithm #6/7
 10. Pick edge 1-2: Since including this edge results in cycle, discard it.
11. Pick edge 3-4: No cycle is formed, include it.

82 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Kruskal’s Minimum Spanning Tree Algorithm #7/7
 Since the number of edges included equals (V – 1), the algorithm
stops here.

83 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Time Complexity : Kruskal’s Minimum Spanning Tree Algorithm

 Time Complexity: O(E log E) or O(E log V).


1. Sorting of edges takes O(E log E) time.
2. After sorting, we iterate through all edges and apply find-union
algorithm. The find and union operations can take atmost O(logV)
time. So overall complexity is O(E log E + E logV) time.
3. The value of E can be atmost O(V2), so O(logV) are O(logE) same.
Therefore, overall time complexity is O(E log E) or O(E logV)

84 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Kruskal’s minimum cost spanning tree algorithm

85 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Kruskal’s Algorithm
 Proposition: Kruskal's algorithm computes the MST of any
connected edge-weighted graph with E edges and V vertices using
extra space proportional to E and time proportional to E log E (in
the worst case).

86 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Prim’s Minimum Spanning Tree
Prim’s Minimum Spanning Tree (MST)
 Prim's algorithm to find minimum cost spanning tree (as
Kruskal's algorithm) uses the greedy approach.
 Prim's algorithm, in contrast with Kruskal's algorithm, treats the
nodes as a single tree and keeps on adding new nodes to
the spanning tree from the given graph.
 It starts with an empty spanning tree. The idea is to maintain two sets
of vertices.
 The first set contains the vertices already included in the MST, the
other set contains the vertices not yet included.
 At every step, it considers all the edges that connect the two sets, and
picks the minimum weight edge from these edges.
 After picking the edge, it moves the other endpoint of the edge to the
set containing MST.

88 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Prim’s Algorithm
Robert Clay Prim

 Prim Approach:
 Choose an arbitrary start node v
 At any point in time, we have connected component N containing v
and other nodes V-N
 Choose the minimum weight edge from N to V-N

Prim's Algorithm:
let T be a single vertex x
while (T has fewer than n vertices)
{
find the smallest edge connecting T to G-T
add it to T
}
89 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024
Steps in Prim's Algorithm

90 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Pseudocode for Prim's Algorithm
1. Initialize an empty MST.
2. Select an arbitrary starting vertex and mark it as visited.
3.While there are vertices not in the MST:
 [a] Find the minimum weight edge that connects a visited vertex to
an unvisited vertex.
 [b] Add this edge and the corresponding unvisited vertex to the MST.
4. Return the MST.

91 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Prim's Algorithm - Example

8 12

13 9
2
11 20 40 14
7
50 6
10

1 3

92 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Prim's Algorithm - Example

8 12

13 9
2
11 20 40 14
7
50 6
10

1 3

93 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Prim's Algorithm - Example

8 12

13 9
2
11 20 40 14
7
50 6
10

1 3

94 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Prim's Algorithm - Example

8 12

13 9
2
11 20 40 14
7
50 6
10

1 3

95 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Prim's Algorithm - Example

8 12

13 9
2
11 20 40 14
7
50 6
10

1 3

96 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Prim's Algorithm - Example

8 12

13 9
2
11 20 40 14
7
50 6
10

1 3

97 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Prim's Algorithm - Example

8 12

13 9
2
11 20 40 14
7
50 6
10

1 3

98 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Prim's Algorithm - Example

8 12

13 9
2
11 20 40 14
7
50 6
10

1 3

99 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Prim's Algorithm - Example

8 12

13 9
2
11 20 40 14
7
50 6
10

1 3

100 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Key Differences Between Prim’s and Kruskal’s Algorithm for MST

101 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Key Differences Between Prim’s and Kruskal’s Algorithm for MST

102 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Summary on MST
How to Choose the Right Algorithm

104 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Steps of Prim’s Algorithm
1. Initialization: Start with an arbitrary vertex and mark it as part of
the MST.
2. Edge Selection: From the set of edges that connect vertices in the
MST to vertices outside the MST, select the edge with the minimum
weight.
3. Update: Add the selected edge and the connected vertex to the
MST.
4. Repeat: Repeat the edge selection and update steps until all
vertices are included in the MST.

105 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Steps of Kruskal’s Algorithm:
 Initialization: Sort all the edges in the graph by their weight in non-
decreasing order.
 Edge Selection: Starting from the smallest edge, add the edge to the
MST if it doesn’t form a cycle with the already included edges.
 Cycle Detection: Use a union-find data structure to detect and
prevent cycles.
 Repeat: Continue adding edges until the MST contains exactly (V-1)
edges, whereV is the number of vertices.

106 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Prim’s Algorithm & Kruskal’s Algorithm

 Prim’s and Kruskal’s algorithms are both efficient for finding Minimum
Spanning Trees, with Prim’s being more suited for dense graphs due to
its vertex-focused approach and Kruskal’s excelling in sparse graphs
with its edge-centric strategy.
 The choice between them depends on the graph's density and
representation, making both valuable tools in different scenarios.

107 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Real world problem that can be
represented using graph
Problem: Urban Traffic Management
Overview:
 Urban traffic management is a complex issue faced by cities
worldwide.
 Efficiently managing traffic flow, optimizing routes, and reducing
congestion are critical challenges that can be effectively addressed
using graph data structures.
Graph Representation:
 In this scenario, a city's road network can be modeled as a graph
where intersections and endpoints are represented as vertices, and the
roads connecting these intersections are the edges.
 This graph can be either directed or undirected depending on
whether the roads have specific directional traffic flows, and it can be
weighted if the edges represent parameters like road length, traffic
density, or average travel time.

109 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Road network graph

110 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Problem: Urban Traffic Management
Applications Using Graph Theory:
 Route Optimization: Finding the shortest path from one vertex to
another using algorithms like Dijkstra’s or A*. This can help in real-time
GPS navigation to provide the fastest or shortest route to drivers.
 Traffic Congestion Analysis: Using flow algorithms like Ford-Fulkerson
to determine the maximum traffic flow that can be accommodated on
certain key routes and identifying bottlenecks in the network.
 Dynamic Traffic Light Management: Utilizing the graph model to
simulate and optimize traffic light timings based on traffic flow data to
minimize waiting times and reduce congestion.
 Evacuation Planning: Graphs can be used to plan efficient evacuation
routes in emergencies, determining multiple paths from various points in
the city to safe zones, considering factors like road capacity and predicted
congestion.

111 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


1. Social Network Analysis
Problem Overview: Social networks such as Facebook, Twitter, and
LinkedIn consist of users and the relationships between them. Understanding
the structure and dynamics of these networks can help in targeting
advertisements, recommending friends, or identifying influential users.
Graph Representation: In this scenario, each user is a vertex, and each
relationship or connection between users is an edge. The graph can be directed
(indicating the direction of a relationship like following on Twitter) or
undirected (mutual friendships on Facebook).
Applications:
 Community Detection: Identifying closely-knit groups or communities
within the network using clustering techniques.
 Influence Spread: Studying how information, like news or memes,
spreads through the network, using algorithms to trace paths and measure
influence.
 Recommendation Systems: Suggesting new friends or connections
based on shortest paths or mutual friends.

112 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


2. Supply Chain Management
Problem Overview: Supply chains consist of suppliers, manufacturers,
distributors, retailers, and customers. Managing the flow of goods and services
efficiently across this network is critical for business operations.
Graph Representation: Each entity (supplier, factory, distribution center,
etc.) in the supply chain is a vertex, and the connections (shipping routes,
contracts) between them are edges. These edges can have weights representing
costs, distances, or shipping times.
Applications:
 Route Optimization: Finding the most cost-effective routes for
transporting goods.
 Risk Assessment: Identifying critical points in the supply chain where a
disruption could cause major impacts, enabling better contingency planning.
 Inventory Management: Optimizing stock levels across different
locations to ensure efficient distribution of goods.

113 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


3. Internet Routing
Problem Overview: The Internet is a massive network of routers and
computers connected by various types of links. Efficiently routing data
packets from one computer to another is fundamental to network
performance.
Graph Representation: Routers and switches are vertices, and the
physical links between them are edges. These edges can carry weights
that represent bandwidth, latency, or packet loss.
Applications:
 Path Finding: Determining the shortest or fastest path for data to
travel from source to destination.
 Load Balancing: Distributing data flows to avoid overloading any
single part of the network.
 Fault Tolerance: Automatically finding alternative paths in case of a
link or node failure.

114 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


4. Protein Interaction Networks
Problem Overview: In bioinformatics, understanding how different
proteins interact with each other is crucial for drug discovery and
understanding cellular processes.
Graph Representation: Proteins are vertices, and interactions
between them are edges. These interactions can be physical bindings or
functional associations.
Applications:
 Pathway Analysis: Identifying which proteins interact to carry out
a specific cellular function.
 Disease Modeling: Understanding how changes in protein
interactions can lead to diseases.
 Target Identification: Finding key proteins in a network that, if
targeted with drugs, could disrupt a disease pathway.

115 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


sample questions on Graph Traversal
Basic Conceptual Questions
1. What is graph traversal, and why is it important in graph theory?
2. Explain the difference between Depth-First Search (DFS) and
Breadth-First Search (BFS). When would you choose one over the
other?
3. In a directed graph, what would be the result of applying DFS versus
BFS starting from the same node? How do the traversal paths differ?
4. Why does BFS provide the shortest path in an unweighted graph,
while DFS does not necessarily do so?
5. What are the time and space complexities of DFS and BFS in a graph
with VVV vertices and EEE edges?

117 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Algorithm Implementation Questions
6. Implement a function to perform Depth-First Search (DFS) on a
graph represented as an adjacency list.
7. Write a function to perform Breadth-First Search (BFS) on a graph
represented as an adjacency matrix.
8. Implement a function to detect cycles in a directed graph using
Depth-First Search (DFS).
9. Write a BFS-based algorithm to find the shortest path from a given
source node to all other nodes in an unweighted graph.
10. Implement an algorithm using DFS to check if a given graph is
connected (for undirected graphs).

118 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Application-Based Questions
11. In social network analysis, friends of friends are often considered.
Explain how BFS could be used to find all connections within a
certain number of steps.
12. Given a maze represented as a grid with walls and open paths, use
BFS to find the shortest path from the entrance to the exit.
13. In a map where cities are nodes and roads are edges, explain how
DFS could be used to find all cities reachable from a particular city.
14. Consider a web crawler that visits pages on the internet. Would you
use DFS or BFS for crawling, and why?
15. For a game map represented as a grid, implement BFS to find the
shortest route from a starting position to a target position.

119 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Analytical and Problem-Solving Questions
16. Given an undirected graph, write an algorithm to find and count all
connected components using BFS or DFS.
17. Explain how BFS can be modified to find the shortest path between
two nodes in an unweighted graph. Provide pseudocode or an
example.
18. In a directed acyclic graph (DAG), explain how DFS can be used to
perform topological sorting.
19. Given a binary tree (special case of a graph), implement BFS to
traverse the tree level by level.
20. Using BFS, write an algorithm to detect if there exists a path
between two nodes in an undirected graph.

120 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Advanced and Theoretical Questions
21. How can DFS be used to detect back edges in a directed graph, and
why do back edges indicate cycles?
22. Explain the concept of articulation points and bridges in a graph.
How can DFS be used to identify them?
23. Describe how BFS can be extended to find all nodes within a given
distance ddd from a starting node in an unweighted graph.
24. In a directed graph, explain how DFS can be used to identify
strongly connected components (SCCs) using Kosaraju’s or Tarjan’s
algorithm.
25. Discuss the limitations of BFS and DFS in weighted graphs. Why are
Dijkstra’s and A algorithms preferred for shortest-path calculations
in weighted graphs?*

121 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Coding Challenge Questions
26. Implement a function to determine if an undirected graph is
bipartite using BFS.
27. Write a program to count the number of nodes at each level in a
binary tree using BFS.
28. Given a grid with obstacles and open spaces, implement BFS to find
the shortest path from the top-left corner to the bottom-right
corner while avoiding obstacles.
29. Write a function to check if a graph is a tree. Use DFS or BFS to
verify that the graph is connected and contains no cycles.
30. Implement an algorithm to find all possible paths between two
nodes in a directed acyclic graph using DFS.

122 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Practical Scenario Questions
31. Consider a networking application where devices are connected in a
mesh topology. Describe how you could use BFS to find the shortest
path between two devices.
32. In a recommendation system, find users within three degrees of
connection from a target user (friends, friends of friends, etc.).
Would you use BFS or DFS? Explain your choice.
33. In a grid-based puzzle game, use BFS to find the minimum number
of moves required to reach the goal from the start.
34. Given a transportation network where each station is a node and
each route is an edge, use BFS to determine the minimum number
of stops required to travel between two stations.
35. Design a DFS-based algorithm to explore all possible moves in a
game board where each cell represents a moveable position.
123 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024
Graph Theory and Proof-Based Questions
36. Prove that the shortest path in an unweighted graph from a source
node to any other node can be found using BFS.
37. Show that if an undirected graph is connected and has V vertices and
V−1 edges, it must be a tree. How would DFS or BFS help in
verifying this?
38. Explain why DFS cannot always find the shortest path in an
unweighted graph. Provide an example to illustrate.
39. In a bipartite graph, prove that it can be 2-colored if no two adjacent
nodes have the same color. How would BFS help in determining this
property?
40. If BFS traversal from a node A reaches a node B in an unweighted
graph, prove that this is the shortest path from A to B.

124 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Thanks for Your Attention!

125 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


References
 https://www.enjoyalgorithms.com/blog/types-of-graphs-in-data-structures
 https://www.simplilearn.com/tutorials/data-structure-tutorial/graphs-in-data-
structure
 http://www.btechsmartclass.com/data_structures/graph-traversal-dfs.html
 https://www.algotree.org/algorithms/tree_graph_traversal/depth_first_search/
 https://www.geeksforgeeks.org/difference-between-prims-and-kruskals-
algorithm-for-mst/?ref=lbp

126 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024


Comparison and Use Cases
 Adjacency Matrix: Best for dense graphs, or when frequent edge
existence checks are necessary.
 Adjacency List: Suitable for most scenarios, particularly when the
graph is sparse and memory efficiency is a priority.
 Incidence Matrix: Useful in edge-centric problems or when both
vertex and edge traversal are common.
 Edge List: Ideal for situations where the graph is extremely sparse
and the primary operation is to iterate over the edges.
 Object-Oriented Representation: Perfect when the graph’s
structure is complex, involving multiple attributes for vertices and
edges, or when using object-oriented languages.

127 Graph Algorithms , Prof. Bibhudatta Sahoo@NIT Rourkela 11/16/2024

You might also like