際際滷

際際滷Share a Scribd company logo
1
Lecture 15:
Graph Searching and
Traversing
DATA STRUCTURES AND ALGORITHMS
1. Graph Traversal
Definition:
 A graph traversal means visiting all the vertices (nodes) of the
graph.
- Traversal is the facility to move through a structure visiting each of
the vertices once.
 Two traversal methods for a graph are breadth-first and depth-first.
2. Breadth-First Graph Traversal
 Breadth-first traversal makes use of a queue data structure. The
queue holds a list of vertices which have not been visited yet but
which should be visited soon.
 Since a queue is a first-in first-out structure, vertices are visited in
the order in which they are added to the queue.
 Visiting a vertex involves, for example, outputting the data stored
in that vertex, and also adding its neighbours to the queue.
 Neighbours are not added to the queue if they are already in the
queue, or have already been visited.
 In BFS;
- One node is selected as a start position. Its visited and marked.
- Then, all unvisited nodes adjacent of the next node are visited and
marked in some sequential order.
2. Breadth-First Search [Example-1] (1/5)
Step-1:
Step-2:
4
A
B
S
C
D
E
F
G H
Queue Status
Output: A
B
S
A
B
S
C
D
E
F
G H
Output: A, B,
Queue Status
S
2. Breadth-First Search [Example-1] (2/5)
Step-3:
Step-4:
5
A
B
S
C
D
E
F
G H
Queue Status
Output: A, B, S,
C
G
A
B
S
C
D
E
F
G H
Output: A, B, S, C,
Queue Status
G
D
E
F
2. Breadth-First Search [Example-1] (3/5)
Step-5:
Step-6:
6
A
B
S
C
D
E
F
G H
Queue Status
Output: A, B, S, C, G,
D
E
F
H
A
B
S
C
D
E
F
G H
Output: A, B, S, C, G, D,
Queue Status
E
F
H
2. Breadth-First Search [Example-1] (4/5)
Step-7:
Step-8:
7
A
B
S
C
D
E
F
G H
Queue Status
Output: A, B, S, C, G, D, E,
F
H
A
B
S
C
D
E
F
G H
Output: A, B, S, C, G, D, E, F
Queue Status
H
2. Breadth-First Search [Example-1] (5/5)
Step-9:
8
A
B
S
C
D
E
F
G H
Queue Status
Output: A, B, S, C, G, D, E, F, H [Hence Proved]
If we start at vertex 1 then a valid breadth-first order
would be:
9
2
5
3
0
6
4
7
1
1,2,3,7,6,0,5,4
2. Breadth-First Search [Example-2]
10
Breadth First Search:
B
C
D
E
F
H
G
A
I
A B D E
A B D E F
A B D E F C H
A B D E F C H G I
As was true with depth-first search, a breadth-first search from some vertices
may fail to locate all the vertices.
A breadth-first search from B:
B F G I H C D
2. Breadth-First Search [Example-3]
A
H
B
F
E
D
C
G
A
B
C
D
E
F
G
H
How is this accomplished? Simply replace the stack with a queue!
Rules: (1) Maintain an enqueued array. (2) Visit node when
dequeued.
Q 
2. Breadth-First Search [Example-4] (1/11)
A
H
B
F
E
D
C
G
A
B
C
D 
E
F
G
H
Enqueue D. Notice, D not yet visited.
Q  D
Nodes visited:
2. Breadth-First Search [Example-4] (2/11)
A
H
B
F
E
D
C
G
A
B
C 
D 
E 
F 
G
H
Dequeue D. Visit D. Enqueue unenqueued nodes adjacent to D.
Q  C  E  F
Nodes visited: D
2. Breadth-First Search [Example-4] (3/11)
A
H
B
F
E
D
C
G
A
B
C 
D 
E 
F 
G
H
Dequeue C. Visit C. Enqueue unenqueued nodes adjacent to C.
Q  E  F
Nodes visited: D, C
2. Breadth-First Search [Example-4] (4/11)
A
H
B
F
E
D
C
G
A
B
C 
D 
E 
F 
G
H
Dequeue E. Visit E. Enqueue unenqueued nodes adjacent to
E.
Q  F  G
Nodes visited: D, C, E
2. Breadth-First Search [Example-4] (5/11)
A
H
B
F
E
D
C
G
A
B
C 
D 
E 
F 
G 
H
Dequeue F. Visit F. Enqueue unenqueued nodes adjacent to F.
Q  G
Nodes visited: D, C, E, F
2. Breadth-First Search [Example-4] (6/11)
A
H
B
F
E
D
C
G
A
B
C 
D 
E 
F 
G 
H 
Dequeue G. Visit G. Enqueue unenqueued nodes adjacent to G.
Q  H
Nodes visited: D, C, E, F, G
2. Breadth-First Search [Example-4] (7/11)
A
H
B
F
E
D
C
G
A 
B 
C 
D 
E 
F 
G 
H 
Dequeue H. Visit H. Enqueue unenqueued nodes adjacent to H.
Q  A  B
Nodes visited: D, C, E, F, G, H
2. Breadth-First Search [Example-4] (8/11)
A
H
B
F
E
D
C
G
A 
B 
C 
D 
E 
F 
G 
H 
Dequeue A. Visit A. Enqueue unenqueued nodes adjacent to
A.
Q  B
Nodes visited: D, C, E, F, G, H, A
2. Breadth-First Search [Example-4] (9/11)
A
H
B
F
E
D
C
G
A 
B 
C 
D 
E 
F 
G 
H 
Dequeue B. Visit B. Enqueue unenqueued nodes adjacent to B.
Q empty
Nodes visited: D, C, E, F, G, H, A, B
2. Breadth-First Search [Example-4] (10/11)
A
H
B
F
E
D
C
G
A 
B 
C 
D 
E 
F 
G 
H 
Q empty. Algorithm done.
Q empty
Nodes visited: D, C, E, F, G, H, A, B
2. Breadth-First Search [Example-4] (11/11)
1. Visit the start vertex
2. Initialize queue to contain the start vertex
3. While queue not empty do
a. Remove a vertex v from the queue
b. For all vertices w adjacent to v do:
If w has not been visited then:
i. Visit w
ii. Add w to queue
End while
22
2. Breadth-First Search [Algorithm]
3. Depth-First Graph Traversal
23
 In DFS;
- Depth first search (DFS) follows first a path from the starting node to an
ending node.
- Then, another path from the start to the End and so forth until all nodes
have been visited.
 Data stored in DFS as stack (working as Push and Pop).
Steps of DFGT:
- stack works as LIFO (Last In First Out).
 This method visits all the vertices, beginning with a specified start vertex.
 This strategy proceeds along a path from vertex V as deeply into the graph as
possible.
 This means that after visiting V, the algorithm tries to visit any unvisited
vertex adjacent to V.
 When the traversal reaches a vertex which has no adjacent vertex, it back
tracks and visits an unvisited adjacent vertex.
3. Depth-First Search [Example-1] (1/6)
Step-1:
Step-2:
24
A
B
S
C
D
E
F
G H
Stack
Output: A
A
A
B
S
C
D
E
F
G H
Output: A, B,
Stack
B
A
No adjacent. So
B become
Pop
3. Depth-First Search [Example-1] (2/6)
Step-3:
Step-4:
25
A
B
S
C
D
E
F
G H
Stack
Output: A, B, S,
S
A
A
B
S
C
D
E
F
G H
Output: A, B, S, C,
Stack
C
S
A
3. Depth-First Search [Example-1] (3/6)
Step-5:
Step-6:
26
A
B
S
C
D
E
F
G H
Stack
Output: A, B, S, C, D
A
B
S
C
D
E
F
G H
Output: A, B, S, C, D
Stack
D
C
S
A
D
C
S
A
No adjacent. So
D become
Pop
3. Depth-First Search [Example-1] (4/6)
Step-7:
Step-8:
27
A
B
S
C
D
E
F
G H
Stack
Output: A, B, S, C, D, E
A
B
S
C
D
E
F
G H
Output: A, B, S, C, D, E, H,
Stack
H
E
C
S
A
E
C
S
A
3. Depth-First Search [Example-1] (5/6)
Step-9:
Step-10:
28
A
B
S
C
D
E
F
G H
Stack
Output: A, B, S, C, D, E, H, G,
A
B
S
C
D
E
F
G H
Output: A, B, S, C, D, E, H,G, F
Stack
G
H
E
C
S
A
F
G
H
E
C
S
A
3. Depth-First Search [Example-1] (6/6)
Step-10:
29
A
B
S
C
D
E
F
G H
Output: A, B, S, C, D, E, H,G, F
Stack
No adjacent. So
F become
Pop.
No adjacent. So
G become
Pop.
No adjacent. So
H become
Pop.
No adjacent. So
E become
Pop.
No adjacent. So
C become
Pop.
No adjacent. So
S become
Pop.
No adjacent. So
A become
Pop.
If we start at vertex 1 then a valid depth-first order
would be:
30
2
5
3
0
6
4
7
1
1, 2, 3, 6, 4, 5, 7, 0
3. Depth-First Search [Example-2]
A
D E
B
F
C
A B C F E D
3. Depth-First Search [Example-3]
32
B
C
D
E
F
H
G
A
I
 Design Starting depth first search at start vertices as;
(i) Vertices A
(ii) Vertices C
(iii) Vertices G
3. Depth-First Search [Class Participation]
A
H
B
F
E
D
C
G
A
B
C
D
E
F
G
H
Visited Array
Task: Conduct a depth-first search of the graph
starting with node D
3. Depth-First Search [Class Participation]
1. Shortest Paths
2. Minimum Spanning Trees
58
4. Applications of Graph
4.1 Shortest Paths
 Definition: The shortest path problem is about finding a path
between vertices in a graph such that the total sum of the edges
weights is minimum.
 Problem: Given some vertex s, find the shortest path from s to every
other vertex in G.
 Suppose we have a weighted graph G:
 The cost of traversing edge (i, j) is ci,j .
 The cost of traversing a path is the sum of the edge costs.
59
 Phone/Internet communications: to find the cheapest/fastest path
between two computers.
 Transportation/Travelling: to find the cheapest/fastest path between
two cities.
60
4.1 Shortest Paths [Applications]
 Dijkstras algorithm is an algorithm for finding the shortest paths between
nodes in a graph which may represent.
 For example; Road networks.
Steps of Dijkstras Algorithm:
1. Initialize distances according to the algorithm.
2. Pick 1st
node and calculate distances to adjacent nodes.
3. Pick next node with minimal distance; repeat adjacent node distance
calculations.
61
4.2 Shortest Paths [Dijkstras Algorithm]
 Ex
Step-1: Start with source node (i.e., a). And Initialize it with 0.
Other nodes become infinite ().
62
4.2 Dijkstras Algorithm [Example-1] (1/4)
a
b
c
d
e
f
4
2
1
5
8
10
6
5
2
a
b
c
d
e
f
4
2
1
5
8
10
6
5
2
0
Step-2: Source node called as u. and destination as v. Find shortest path.
Step-3: Applying formula. c=2.
63
4.2 Dijkstras Algorithm [Example-1] (2/4)
a
b
c
d
e
f
4
2
1
5
8
10
6
5
2
0
 
 

u
v
Formula:
If (d (u) + c (u, v) < d(v)}
d (v) = d(u) + c (u, v)
If (0 + 2 < )
2 <  [True]
d (v) = d(u) + c (u, v)
= 0 + 2 = 2
a
b
c
d
e
f
4
2
1
5
8
10
6
5
2
0
 
2
Step-4: Find shortest path from c. u and v changes.
Step-5: Find shortest path from b. u and v changes.
64
4.2 Dijkstras Algorithm [Example-1] (3/4)
a
b
c
d
e
f
4
2
1
5
8
10
6
5
2
0
3 
2 

u
v
a
b
c
d
e
f
4
2
1
5
8
10
6
5
2
0
3
2 

u
8 v
Step-6: Find shortest path from d. u and v changes.
Step-7: Find shortest path from e. u and v changes.
65
4.2 Dijkstras Algorithm [Example-1] (4/4)
a
b
c
d
e
f
4
2
1
5
8
10
6
5
2
0
3
2

u
8
v
10
a
b
c
d
e
f
4
2
1
5
8
10
6
5
2
0
3
2
15
10
Marked (red line) is
most shortest path to
reach destination.
Total cost 15.
4.2 Dijkstras Algorithm [Example-2] (1/15)
Find the shortest path from vertex A to every
other vertex?
Step-1: Create Shortest distance from A and Previous vertex columns.
4.2 Dijkstras Algorithm [Example-2] (2/15)
Step-2:
4.2 Dijkstras Algorithm [Example-2] (3/15)
Step-3:
4.2 Dijkstras Algorithm [Example-2] (4/15)
Step-4:
4.2 Dijkstras Algorithm [Example-2] (5/15)
Step-5:
4.2 Dijkstras Algorithm [Example-2] (6/15)
Step-6:
4.2 Dijkstras Algorithm [Example-2] (7/15)
Step-7:
4.2 Dijkstras Algorithm [Example-2] (8/15)
Step-8:
4.2 Dijkstras Algorithm [Example-2] (9/15)
Step-9:
4.2 Dijkstras Algorithm [Example-2] (10/15)
Step-10:
4.2 Dijkstras Algorithm [Example-2] (11/15)
Step-11:
4.2 Dijkstras Algorithm [Example-2] (12/15)
Step-12:
4.2 Dijkstras Algorithm [Example-2] (13/15)
Step-13:
4.2 Dijkstras Algorithm [Example-2] (14/15)
Step-14:
4.2 Dijkstras Algorithm [Example-2] (15/15)
Step-15: Finally; Short distance from A to all other vertices as;
10
1
5
2
6
4
9
7
2 3
4.2 Dijkstras Algorithm [Class Participation]
Find the shortest path in the given weighted
graph by Dijkstra algorithm ?
91
Applications of Graph
1. Shortest Paths
2. Minimum Spanning Trees
5. Applications of Graph [Minimum Spanning Trees]
92
Problem: Laying Telephone Wire
Central office
93
Wiring: First Approach
Central office
Simple but Expensive!
94
Wiring: Better Approach
Central office
Minimize the total length of wire connecting the customers
95
5.1 Minimum Spanning Tree (MST)
 it is a tree (i.e., it is acyclic)
 it covers all the vertices V
 contains |V| - 1 edges
 the total cost associated with tree edges is the
minimum among all possible spanning trees
 not necessarily unique
A minimum spanning tree is a subgraph of an
undirected weighted graph G, such that
96
 Prim's algorithm is a famous greedy algorithm.
- It is used for finding the Minimum Spanning Tree (MST) of a
given graph.
 To apply Prims algorithm,
- the given graph must be weighted, connected and undirected.
Steps of Prims Algorithm:
1. Remove all loop edges.
2. Remove all parallel edges.
3. Choose any arbitrary node as root node.
5.2 Minimum Spanning Tree (Prims Algorithm)
97
5.2 Minimum Spanning Tree (Prims Algorithm)
[Example-1]
B
A
C
D
E
F
7
8
3
6
4
3
8
8
2
5
2
Step-1: Remove all loop edges
B
A
C
D
E
F
7
8
3
6
4
3
8
2
5
2
Step-2: Remove all Parallel edges.
If more than edges between 2 nodes;
remove high weighted nodes.
B
A
C
D
E
F
7
8
3
6
4
3
2
5
2
98
5.2 Minimum Spanning Tree (Prims Algorithm)
[Example-1]
Step-3: Choose any arbitrary node as root node. We selected A vertices.
B
A
C
D
E
F
7
8
3
6
4
3
8
2
5
2
A
Step-4: Check outgoing edges (A) and select the one with less cost.
B
A
7 Remaining edges: AC(8)
Step-5: Check outgoing edges (B) and select the one with less cost.
B
A
7
C
3
Selected edges: AC(8), BD(6), BC(3)
Remaining edges: AC(8), BD(6)
Selected edges: AC(8), AB(7)
99
5.2 Minimum Spanning Tree (Prims Algorithm)
[Example-1]
Step-6: Check outgoing edges (C) and select the one with less cost.
B
A
7
C
3
Selected edges: AC(8), BD(6), CD(4), CE(3)
Remaining edges: AC(8), BD(6), CD(4),
E
3
Step-7: Check outgoing edges (C) and select the one with less cost.
B
A
7
C
3
E
3
Selected edges: AC(8), BD(6), CD(4), ED(2), EF(2)
Remaining edges: AC(8), BD(6), CD(4), EF(2)
Select anyone.
Both are minimum
values.
D
2
100
5.2 Minimum Spanning Tree (Prims Algorithm)
[Example-1]
Step-7: Check outgoing edges (C) and select the one with less cost.
Selected edges: AC(8), BD(6), CD(4), EF(2), DF(5)
Remaining edges: AC(8), BD(6), CD(4), DF(5)
EF(2) is selected
because of
minimum value in
queue..
B
A
7
C
3
E
3
D
2 F
2
Total vertices (V)= 6, Edge (E) = | V | - 1
= 6  1 = 5 [Hence Proof, Minimum Spanning Tree]
Final conclusion
Kruskals Algorithm
101
Initialization
a. Create a forest of vertices F
b. Initialize the set of safe edges T
comprising the MST to the empty set
c. Sort edges by increasing weight
a
c
e
d
b
2
4
5
9
6
4
5
5
F = {a}, {b}, {c}, {d}, {e}
T = 
E = {(a,d), (c,d), (d,e), (a,c),
(b,e), (c,e), (b,d), (a,b)}
Kruskals Algorithm
102
a
c
e
d
b
2
4
5
9
6
4
5
5
2 4 4 5 5 5 6 9
(a,d) (c,d) (d,e) (a,c) (b,e) (c,e) (b,d) (a,b)
E = {(a,d), (c,d), (d,e), (b,e)}

More Related Content

Similar to Lec 15-graph Searching in data structure and lgorithm.pptx (20)

Analysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsAnalysis of Pathfinding Algorithms
Analysis of Pathfinding Algorithms
SigSegVSquad
130210107039 2130702
130210107039 2130702130210107039 2130702
130210107039 2130702
Ketaki_Pattani
Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7
Traian Rebedea
algoritmagraph_breadthfirstsearch_depthfirstsearch.pptx
algoritmagraph_breadthfirstsearch_depthfirstsearch.pptxalgoritmagraph_breadthfirstsearch_depthfirstsearch.pptx
algoritmagraph_breadthfirstsearch_depthfirstsearch.pptx
AzwanAlghifari
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
Afaq Mansoor Khan
Depth First Search and Breadth First Search
Depth First Search and Breadth First SearchDepth First Search and Breadth First Search
Depth First Search and Breadth First Search
Nisha Soms
Problem Solving through Search - Uninformed Search
Problem Solving through Search - Uninformed SearchProblem Solving through Search - Uninformed Search
Problem Solving through Search - Uninformed Search
DuraiRaj315751
Breadth First Search (BFS)
Breadth First Search (BFS)Breadth First Search (BFS)
Breadth First Search (BFS)
Ashish Ranjan
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
Masud Parvaze
DFS & BFS Graph
DFS & BFS GraphDFS & BFS Graph
DFS & BFS Graph
Mahfuzul Yamin
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
Muhammad Hammad Waseem
Analysis and design of algorithms part 3
Analysis and design of algorithms part 3Analysis and design of algorithms part 3
Analysis and design of algorithms part 3
Deepak John
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
chandrashekarr799
DATA STRUCTURES.pptx
DATA STRUCTURES.pptxDATA STRUCTURES.pptx
DATA STRUCTURES.pptx
KENNEDY GITHAIGA
Algorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptxAlgorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptx
zerihunnana
Data Structures-Non Linear DataStructures-Graphs
Data Structures-Non Linear DataStructures-GraphsData Structures-Non Linear DataStructures-Graphs
Data Structures-Non Linear DataStructures-Graphs
sailaja156145
Breadth first search and depth first search
Breadth first search and  depth first searchBreadth first search and  depth first search
Breadth first search and depth first search
Hossain Md Shakhawat
Introduction of Depth First Search with example
Introduction of Depth First Search with exampleIntroduction of Depth First Search with example
Introduction of Depth First Search with example
Dr. Anand Bihari
Algorithms Analysis & Design - Lecture 7
Algorithms Analysis & Design - Lecture 7Algorithms Analysis & Design - Lecture 7
Algorithms Analysis & Design - Lecture 7
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
logic.pptx
logic.pptxlogic.pptx
logic.pptx
KENNEDY GITHAIGA
Analysis of Pathfinding Algorithms
Analysis of Pathfinding AlgorithmsAnalysis of Pathfinding Algorithms
Analysis of Pathfinding Algorithms
SigSegVSquad
130210107039 2130702
130210107039 2130702130210107039 2130702
130210107039 2130702
Ketaki_Pattani
Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7
Traian Rebedea
algoritmagraph_breadthfirstsearch_depthfirstsearch.pptx
algoritmagraph_breadthfirstsearch_depthfirstsearch.pptxalgoritmagraph_breadthfirstsearch_depthfirstsearch.pptx
algoritmagraph_breadthfirstsearch_depthfirstsearch.pptx
AzwanAlghifari
Depth First Search and Breadth First Search
Depth First Search and Breadth First SearchDepth First Search and Breadth First Search
Depth First Search and Breadth First Search
Nisha Soms
Problem Solving through Search - Uninformed Search
Problem Solving through Search - Uninformed SearchProblem Solving through Search - Uninformed Search
Problem Solving through Search - Uninformed Search
DuraiRaj315751
Breadth First Search (BFS)
Breadth First Search (BFS)Breadth First Search (BFS)
Breadth First Search (BFS)
Ashish Ranjan
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
Muhammad Hammad Waseem
Analysis and design of algorithms part 3
Analysis and design of algorithms part 3Analysis and design of algorithms part 3
Analysis and design of algorithms part 3
Deepak John
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
chandrashekarr799
Algorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptxAlgorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptx
zerihunnana
Data Structures-Non Linear DataStructures-Graphs
Data Structures-Non Linear DataStructures-GraphsData Structures-Non Linear DataStructures-Graphs
Data Structures-Non Linear DataStructures-Graphs
sailaja156145
Breadth first search and depth first search
Breadth first search and  depth first searchBreadth first search and  depth first search
Breadth first search and depth first search
Hossain Md Shakhawat
Introduction of Depth First Search with example
Introduction of Depth First Search with exampleIntroduction of Depth First Search with example
Introduction of Depth First Search with example
Dr. Anand Bihari

Recently uploaded (20)

K-Circle-Weekly-Quiz-May2025_12345678910
K-Circle-Weekly-Quiz-May2025_12345678910K-Circle-Weekly-Quiz-May2025_12345678910
K-Circle-Weekly-Quiz-May2025_12345678910
PankajRodey1
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
RVSPSOA
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
SweetytamannaMohapat
QUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptx
QUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptxQUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptx
QUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptx
Sourav Kr Podder
LET卒S PRACTICE GRAMMAR USING SIMPLE PAST TENSE
LET卒S PRACTICE GRAMMAR USING SIMPLE PAST TENSELET卒S PRACTICE GRAMMAR USING SIMPLE PAST TENSE
LET卒S PRACTICE GRAMMAR USING SIMPLE PAST TENSE
OlgaLeonorTorresSnch
Odoo 18 Point of Sale PWA - Odoo 際際滷s
Odoo 18 Point of Sale PWA  - Odoo  際際滷sOdoo 18 Point of Sale PWA  - Odoo  際際滷s
Odoo 18 Point of Sale PWA - Odoo 際際滷s
Celine George
Writing Research Papers: Guidance for Research Community
Writing Research Papers: Guidance for Research CommunityWriting Research Papers: Guidance for Research Community
Writing Research Papers: Guidance for Research Community
Rishi Bankim Chandra Evening College, Naihati, North 24 Parganas, West Bengal, India
Exploring Identity Through Colombian Companies
Exploring Identity Through Colombian CompaniesExploring Identity Through Colombian Companies
Exploring Identity Through Colombian Companies
OlgaLeonorTorresSnch
How to Configure Add to Cart in Odoo 18 Website
How to Configure Add to Cart in Odoo 18 WebsiteHow to Configure Add to Cart in Odoo 18 Website
How to Configure Add to Cart in Odoo 18 Website
Celine George
"Dictyoptera: The Order of Cockroaches and Mantises" Or, more specifically: ...
"Dictyoptera: The Order of Cockroaches and Mantises"  Or, more specifically: ..."Dictyoptera: The Order of Cockroaches and Mantises"  Or, more specifically: ...
"Dictyoptera: The Order of Cockroaches and Mantises" Or, more specifically: ...
Arshad Shaikh
How to Use Owl Slots in Odoo 17 - Odoo 際際滷s
How to Use Owl Slots in Odoo 17 - Odoo 際際滷sHow to Use Owl Slots in Odoo 17 - Odoo 際際滷s
How to Use Owl Slots in Odoo 17 - Odoo 際際滷s
Celine George
CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...
CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...
CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...
Sritoma Majumder
Order: Odonata Isoptera and Thysanoptera.pptx
Order: Odonata Isoptera and Thysanoptera.pptxOrder: Odonata Isoptera and Thysanoptera.pptx
Order: Odonata Isoptera and Thysanoptera.pptx
Arshad Shaikh
How to Manage Orders in Odoo 18 Lunch - Odoo 際際滷s
How to Manage Orders in Odoo 18 Lunch - Odoo 際際滷sHow to Manage Orders in Odoo 18 Lunch - Odoo 際際滷s
How to Manage Orders in Odoo 18 Lunch - Odoo 際際滷s
Celine George
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdfForestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
ChalaKelbessa
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
Arshad Shaikh
"Hymenoptera: A Diverse and Fascinating Order".pptx
"Hymenoptera: A Diverse and Fascinating Order".pptx"Hymenoptera: A Diverse and Fascinating Order".pptx
"Hymenoptera: A Diverse and Fascinating Order".pptx
Arshad Shaikh
Order Lepidoptera: Butterflies and Moths.pptx
Order Lepidoptera: Butterflies and Moths.pptxOrder Lepidoptera: Butterflies and Moths.pptx
Order Lepidoptera: Butterflies and Moths.pptx
Arshad Shaikh
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
Cloud Computing ..PPT ( Faizan ALTAF )..
Cloud Computing ..PPT ( Faizan ALTAF )..Cloud Computing ..PPT ( Faizan ALTAF )..
Cloud Computing ..PPT ( Faizan ALTAF )..
faizanaltaf231
K-Circle-Weekly-Quiz-May2025_12345678910
K-Circle-Weekly-Quiz-May2025_12345678910K-Circle-Weekly-Quiz-May2025_12345678910
K-Circle-Weekly-Quiz-May2025_12345678910
PankajRodey1
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
SEM II 3202 STRUCTURAL MECHANICS, B ARCH, REGULATION 2021, ANNA UNIVERSITY, R...
RVSPSOA
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
IDSP(INTEGRATED DISEASE SURVEILLANCE PROGRAMME...
SweetytamannaMohapat
QUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptx
QUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptxQUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptx
QUIZ-O-FORCE FINAL SET BY SUDIPTA & SUBHAM.pptx
Sourav Kr Podder
LET卒S PRACTICE GRAMMAR USING SIMPLE PAST TENSE
LET卒S PRACTICE GRAMMAR USING SIMPLE PAST TENSELET卒S PRACTICE GRAMMAR USING SIMPLE PAST TENSE
LET卒S PRACTICE GRAMMAR USING SIMPLE PAST TENSE
OlgaLeonorTorresSnch
Odoo 18 Point of Sale PWA - Odoo 際際滷s
Odoo 18 Point of Sale PWA  - Odoo  際際滷sOdoo 18 Point of Sale PWA  - Odoo  際際滷s
Odoo 18 Point of Sale PWA - Odoo 際際滷s
Celine George
Exploring Identity Through Colombian Companies
Exploring Identity Through Colombian CompaniesExploring Identity Through Colombian Companies
Exploring Identity Through Colombian Companies
OlgaLeonorTorresSnch
How to Configure Add to Cart in Odoo 18 Website
How to Configure Add to Cart in Odoo 18 WebsiteHow to Configure Add to Cart in Odoo 18 Website
How to Configure Add to Cart in Odoo 18 Website
Celine George
"Dictyoptera: The Order of Cockroaches and Mantises" Or, more specifically: ...
"Dictyoptera: The Order of Cockroaches and Mantises"  Or, more specifically: ..."Dictyoptera: The Order of Cockroaches and Mantises"  Or, more specifically: ...
"Dictyoptera: The Order of Cockroaches and Mantises" Or, more specifically: ...
Arshad Shaikh
How to Use Owl Slots in Odoo 17 - Odoo 際際滷s
How to Use Owl Slots in Odoo 17 - Odoo 際際滷sHow to Use Owl Slots in Odoo 17 - Odoo 際際滷s
How to Use Owl Slots in Odoo 17 - Odoo 際際滷s
Celine George
CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...
CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...
CBSE - Grade 11 - Mathematics - Ch 2 - Relations And Functions - Notes (PDF F...
Sritoma Majumder
Order: Odonata Isoptera and Thysanoptera.pptx
Order: Odonata Isoptera and Thysanoptera.pptxOrder: Odonata Isoptera and Thysanoptera.pptx
Order: Odonata Isoptera and Thysanoptera.pptx
Arshad Shaikh
How to Manage Orders in Odoo 18 Lunch - Odoo 際際滷s
How to Manage Orders in Odoo 18 Lunch - Odoo 際際滷sHow to Manage Orders in Odoo 18 Lunch - Odoo 際際滷s
How to Manage Orders in Odoo 18 Lunch - Odoo 際際滷s
Celine George
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdfForestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
Forestry Model Exit Exam_2025_Wollega University, Gimbi Campus.pdf
ChalaKelbessa
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
"Orthoptera: Grasshoppers, Crickets, and Katydids pptx
Arshad Shaikh
"Hymenoptera: A Diverse and Fascinating Order".pptx
"Hymenoptera: A Diverse and Fascinating Order".pptx"Hymenoptera: A Diverse and Fascinating Order".pptx
"Hymenoptera: A Diverse and Fascinating Order".pptx
Arshad Shaikh
Order Lepidoptera: Butterflies and Moths.pptx
Order Lepidoptera: Butterflies and Moths.pptxOrder Lepidoptera: Butterflies and Moths.pptx
Order Lepidoptera: Butterflies and Moths.pptx
Arshad Shaikh
Cloud Computing ..PPT ( Faizan ALTAF )..
Cloud Computing ..PPT ( Faizan ALTAF )..Cloud Computing ..PPT ( Faizan ALTAF )..
Cloud Computing ..PPT ( Faizan ALTAF )..
faizanaltaf231

Lec 15-graph Searching in data structure and lgorithm.pptx

  • 1. 1 Lecture 15: Graph Searching and Traversing DATA STRUCTURES AND ALGORITHMS
  • 2. 1. Graph Traversal Definition: A graph traversal means visiting all the vertices (nodes) of the graph. - Traversal is the facility to move through a structure visiting each of the vertices once. Two traversal methods for a graph are breadth-first and depth-first.
  • 3. 2. Breadth-First Graph Traversal Breadth-first traversal makes use of a queue data structure. The queue holds a list of vertices which have not been visited yet but which should be visited soon. Since a queue is a first-in first-out structure, vertices are visited in the order in which they are added to the queue. Visiting a vertex involves, for example, outputting the data stored in that vertex, and also adding its neighbours to the queue. Neighbours are not added to the queue if they are already in the queue, or have already been visited. In BFS; - One node is selected as a start position. Its visited and marked. - Then, all unvisited nodes adjacent of the next node are visited and marked in some sequential order.
  • 4. 2. Breadth-First Search [Example-1] (1/5) Step-1: Step-2: 4 A B S C D E F G H Queue Status Output: A B S A B S C D E F G H Output: A, B, Queue Status S
  • 5. 2. Breadth-First Search [Example-1] (2/5) Step-3: Step-4: 5 A B S C D E F G H Queue Status Output: A, B, S, C G A B S C D E F G H Output: A, B, S, C, Queue Status G D E F
  • 6. 2. Breadth-First Search [Example-1] (3/5) Step-5: Step-6: 6 A B S C D E F G H Queue Status Output: A, B, S, C, G, D E F H A B S C D E F G H Output: A, B, S, C, G, D, Queue Status E F H
  • 7. 2. Breadth-First Search [Example-1] (4/5) Step-7: Step-8: 7 A B S C D E F G H Queue Status Output: A, B, S, C, G, D, E, F H A B S C D E F G H Output: A, B, S, C, G, D, E, F Queue Status H
  • 8. 2. Breadth-First Search [Example-1] (5/5) Step-9: 8 A B S C D E F G H Queue Status Output: A, B, S, C, G, D, E, F, H [Hence Proved]
  • 9. If we start at vertex 1 then a valid breadth-first order would be: 9 2 5 3 0 6 4 7 1 1,2,3,7,6,0,5,4 2. Breadth-First Search [Example-2]
  • 10. 10 Breadth First Search: B C D E F H G A I A B D E A B D E F A B D E F C H A B D E F C H G I As was true with depth-first search, a breadth-first search from some vertices may fail to locate all the vertices. A breadth-first search from B: B F G I H C D 2. Breadth-First Search [Example-3]
  • 11. A H B F E D C G A B C D E F G H How is this accomplished? Simply replace the stack with a queue! Rules: (1) Maintain an enqueued array. (2) Visit node when dequeued. Q 2. Breadth-First Search [Example-4] (1/11)
  • 12. A H B F E D C G A B C D E F G H Enqueue D. Notice, D not yet visited. Q D Nodes visited: 2. Breadth-First Search [Example-4] (2/11)
  • 13. A H B F E D C G A B C D E F G H Dequeue D. Visit D. Enqueue unenqueued nodes adjacent to D. Q C E F Nodes visited: D 2. Breadth-First Search [Example-4] (3/11)
  • 14. A H B F E D C G A B C D E F G H Dequeue C. Visit C. Enqueue unenqueued nodes adjacent to C. Q E F Nodes visited: D, C 2. Breadth-First Search [Example-4] (4/11)
  • 15. A H B F E D C G A B C D E F G H Dequeue E. Visit E. Enqueue unenqueued nodes adjacent to E. Q F G Nodes visited: D, C, E 2. Breadth-First Search [Example-4] (5/11)
  • 16. A H B F E D C G A B C D E F G H Dequeue F. Visit F. Enqueue unenqueued nodes adjacent to F. Q G Nodes visited: D, C, E, F 2. Breadth-First Search [Example-4] (6/11)
  • 17. A H B F E D C G A B C D E F G H Dequeue G. Visit G. Enqueue unenqueued nodes adjacent to G. Q H Nodes visited: D, C, E, F, G 2. Breadth-First Search [Example-4] (7/11)
  • 18. A H B F E D C G A B C D E F G H Dequeue H. Visit H. Enqueue unenqueued nodes adjacent to H. Q A B Nodes visited: D, C, E, F, G, H 2. Breadth-First Search [Example-4] (8/11)
  • 19. A H B F E D C G A B C D E F G H Dequeue A. Visit A. Enqueue unenqueued nodes adjacent to A. Q B Nodes visited: D, C, E, F, G, H, A 2. Breadth-First Search [Example-4] (9/11)
  • 20. A H B F E D C G A B C D E F G H Dequeue B. Visit B. Enqueue unenqueued nodes adjacent to B. Q empty Nodes visited: D, C, E, F, G, H, A, B 2. Breadth-First Search [Example-4] (10/11)
  • 21. A H B F E D C G A B C D E F G H Q empty. Algorithm done. Q empty Nodes visited: D, C, E, F, G, H, A, B 2. Breadth-First Search [Example-4] (11/11)
  • 22. 1. Visit the start vertex 2. Initialize queue to contain the start vertex 3. While queue not empty do a. Remove a vertex v from the queue b. For all vertices w adjacent to v do: If w has not been visited then: i. Visit w ii. Add w to queue End while 22 2. Breadth-First Search [Algorithm]
  • 23. 3. Depth-First Graph Traversal 23 In DFS; - Depth first search (DFS) follows first a path from the starting node to an ending node. - Then, another path from the start to the End and so forth until all nodes have been visited. Data stored in DFS as stack (working as Push and Pop). Steps of DFGT: - stack works as LIFO (Last In First Out). This method visits all the vertices, beginning with a specified start vertex. This strategy proceeds along a path from vertex V as deeply into the graph as possible. This means that after visiting V, the algorithm tries to visit any unvisited vertex adjacent to V. When the traversal reaches a vertex which has no adjacent vertex, it back tracks and visits an unvisited adjacent vertex.
  • 24. 3. Depth-First Search [Example-1] (1/6) Step-1: Step-2: 24 A B S C D E F G H Stack Output: A A A B S C D E F G H Output: A, B, Stack B A No adjacent. So B become Pop
  • 25. 3. Depth-First Search [Example-1] (2/6) Step-3: Step-4: 25 A B S C D E F G H Stack Output: A, B, S, S A A B S C D E F G H Output: A, B, S, C, Stack C S A
  • 26. 3. Depth-First Search [Example-1] (3/6) Step-5: Step-6: 26 A B S C D E F G H Stack Output: A, B, S, C, D A B S C D E F G H Output: A, B, S, C, D Stack D C S A D C S A No adjacent. So D become Pop
  • 27. 3. Depth-First Search [Example-1] (4/6) Step-7: Step-8: 27 A B S C D E F G H Stack Output: A, B, S, C, D, E A B S C D E F G H Output: A, B, S, C, D, E, H, Stack H E C S A E C S A
  • 28. 3. Depth-First Search [Example-1] (5/6) Step-9: Step-10: 28 A B S C D E F G H Stack Output: A, B, S, C, D, E, H, G, A B S C D E F G H Output: A, B, S, C, D, E, H,G, F Stack G H E C S A F G H E C S A
  • 29. 3. Depth-First Search [Example-1] (6/6) Step-10: 29 A B S C D E F G H Output: A, B, S, C, D, E, H,G, F Stack No adjacent. So F become Pop. No adjacent. So G become Pop. No adjacent. So H become Pop. No adjacent. So E become Pop. No adjacent. So C become Pop. No adjacent. So S become Pop. No adjacent. So A become Pop.
  • 30. If we start at vertex 1 then a valid depth-first order would be: 30 2 5 3 0 6 4 7 1 1, 2, 3, 6, 4, 5, 7, 0 3. Depth-First Search [Example-2]
  • 31. A D E B F C A B C F E D 3. Depth-First Search [Example-3]
  • 32. 32 B C D E F H G A I Design Starting depth first search at start vertices as; (i) Vertices A (ii) Vertices C (iii) Vertices G 3. Depth-First Search [Class Participation]
  • 33. A H B F E D C G A B C D E F G H Visited Array Task: Conduct a depth-first search of the graph starting with node D 3. Depth-First Search [Class Participation]
  • 34. 1. Shortest Paths 2. Minimum Spanning Trees 58 4. Applications of Graph
  • 35. 4.1 Shortest Paths Definition: The shortest path problem is about finding a path between vertices in a graph such that the total sum of the edges weights is minimum. Problem: Given some vertex s, find the shortest path from s to every other vertex in G. Suppose we have a weighted graph G: The cost of traversing edge (i, j) is ci,j . The cost of traversing a path is the sum of the edge costs. 59
  • 36. Phone/Internet communications: to find the cheapest/fastest path between two computers. Transportation/Travelling: to find the cheapest/fastest path between two cities. 60 4.1 Shortest Paths [Applications]
  • 37. Dijkstras algorithm is an algorithm for finding the shortest paths between nodes in a graph which may represent. For example; Road networks. Steps of Dijkstras Algorithm: 1. Initialize distances according to the algorithm. 2. Pick 1st node and calculate distances to adjacent nodes. 3. Pick next node with minimal distance; repeat adjacent node distance calculations. 61 4.2 Shortest Paths [Dijkstras Algorithm]
  • 38. Ex Step-1: Start with source node (i.e., a). And Initialize it with 0. Other nodes become infinite (). 62 4.2 Dijkstras Algorithm [Example-1] (1/4) a b c d e f 4 2 1 5 8 10 6 5 2 a b c d e f 4 2 1 5 8 10 6 5 2 0
  • 39. Step-2: Source node called as u. and destination as v. Find shortest path. Step-3: Applying formula. c=2. 63 4.2 Dijkstras Algorithm [Example-1] (2/4) a b c d e f 4 2 1 5 8 10 6 5 2 0 u v Formula: If (d (u) + c (u, v) < d(v)} d (v) = d(u) + c (u, v) If (0 + 2 < ) 2 < [True] d (v) = d(u) + c (u, v) = 0 + 2 = 2 a b c d e f 4 2 1 5 8 10 6 5 2 0 2
  • 40. Step-4: Find shortest path from c. u and v changes. Step-5: Find shortest path from b. u and v changes. 64 4.2 Dijkstras Algorithm [Example-1] (3/4) a b c d e f 4 2 1 5 8 10 6 5 2 0 3 2 u v a b c d e f 4 2 1 5 8 10 6 5 2 0 3 2 u 8 v
  • 41. Step-6: Find shortest path from d. u and v changes. Step-7: Find shortest path from e. u and v changes. 65 4.2 Dijkstras Algorithm [Example-1] (4/4) a b c d e f 4 2 1 5 8 10 6 5 2 0 3 2 u 8 v 10 a b c d e f 4 2 1 5 8 10 6 5 2 0 3 2 15 10 Marked (red line) is most shortest path to reach destination. Total cost 15.
  • 42. 4.2 Dijkstras Algorithm [Example-2] (1/15) Find the shortest path from vertex A to every other vertex? Step-1: Create Shortest distance from A and Previous vertex columns.
  • 43. 4.2 Dijkstras Algorithm [Example-2] (2/15) Step-2:
  • 44. 4.2 Dijkstras Algorithm [Example-2] (3/15) Step-3:
  • 45. 4.2 Dijkstras Algorithm [Example-2] (4/15) Step-4:
  • 46. 4.2 Dijkstras Algorithm [Example-2] (5/15) Step-5:
  • 47. 4.2 Dijkstras Algorithm [Example-2] (6/15) Step-6:
  • 48. 4.2 Dijkstras Algorithm [Example-2] (7/15) Step-7:
  • 49. 4.2 Dijkstras Algorithm [Example-2] (8/15) Step-8:
  • 50. 4.2 Dijkstras Algorithm [Example-2] (9/15) Step-9:
  • 51. 4.2 Dijkstras Algorithm [Example-2] (10/15) Step-10:
  • 52. 4.2 Dijkstras Algorithm [Example-2] (11/15) Step-11:
  • 53. 4.2 Dijkstras Algorithm [Example-2] (12/15) Step-12:
  • 54. 4.2 Dijkstras Algorithm [Example-2] (13/15) Step-13:
  • 55. 4.2 Dijkstras Algorithm [Example-2] (14/15) Step-14:
  • 56. 4.2 Dijkstras Algorithm [Example-2] (15/15) Step-15: Finally; Short distance from A to all other vertices as;
  • 57. 10 1 5 2 6 4 9 7 2 3 4.2 Dijkstras Algorithm [Class Participation] Find the shortest path in the given weighted graph by Dijkstra algorithm ?
  • 58. 91 Applications of Graph 1. Shortest Paths 2. Minimum Spanning Trees 5. Applications of Graph [Minimum Spanning Trees]
  • 59. 92 Problem: Laying Telephone Wire Central office
  • 60. 93 Wiring: First Approach Central office Simple but Expensive!
  • 61. 94 Wiring: Better Approach Central office Minimize the total length of wire connecting the customers
  • 62. 95 5.1 Minimum Spanning Tree (MST) it is a tree (i.e., it is acyclic) it covers all the vertices V contains |V| - 1 edges the total cost associated with tree edges is the minimum among all possible spanning trees not necessarily unique A minimum spanning tree is a subgraph of an undirected weighted graph G, such that
  • 63. 96 Prim's algorithm is a famous greedy algorithm. - It is used for finding the Minimum Spanning Tree (MST) of a given graph. To apply Prims algorithm, - the given graph must be weighted, connected and undirected. Steps of Prims Algorithm: 1. Remove all loop edges. 2. Remove all parallel edges. 3. Choose any arbitrary node as root node. 5.2 Minimum Spanning Tree (Prims Algorithm)
  • 64. 97 5.2 Minimum Spanning Tree (Prims Algorithm) [Example-1] B A C D E F 7 8 3 6 4 3 8 8 2 5 2 Step-1: Remove all loop edges B A C D E F 7 8 3 6 4 3 8 2 5 2 Step-2: Remove all Parallel edges. If more than edges between 2 nodes; remove high weighted nodes. B A C D E F 7 8 3 6 4 3 2 5 2
  • 65. 98 5.2 Minimum Spanning Tree (Prims Algorithm) [Example-1] Step-3: Choose any arbitrary node as root node. We selected A vertices. B A C D E F 7 8 3 6 4 3 8 2 5 2 A Step-4: Check outgoing edges (A) and select the one with less cost. B A 7 Remaining edges: AC(8) Step-5: Check outgoing edges (B) and select the one with less cost. B A 7 C 3 Selected edges: AC(8), BD(6), BC(3) Remaining edges: AC(8), BD(6) Selected edges: AC(8), AB(7)
  • 66. 99 5.2 Minimum Spanning Tree (Prims Algorithm) [Example-1] Step-6: Check outgoing edges (C) and select the one with less cost. B A 7 C 3 Selected edges: AC(8), BD(6), CD(4), CE(3) Remaining edges: AC(8), BD(6), CD(4), E 3 Step-7: Check outgoing edges (C) and select the one with less cost. B A 7 C 3 E 3 Selected edges: AC(8), BD(6), CD(4), ED(2), EF(2) Remaining edges: AC(8), BD(6), CD(4), EF(2) Select anyone. Both are minimum values. D 2
  • 67. 100 5.2 Minimum Spanning Tree (Prims Algorithm) [Example-1] Step-7: Check outgoing edges (C) and select the one with less cost. Selected edges: AC(8), BD(6), CD(4), EF(2), DF(5) Remaining edges: AC(8), BD(6), CD(4), DF(5) EF(2) is selected because of minimum value in queue.. B A 7 C 3 E 3 D 2 F 2 Total vertices (V)= 6, Edge (E) = | V | - 1 = 6 1 = 5 [Hence Proof, Minimum Spanning Tree] Final conclusion
  • 68. Kruskals Algorithm 101 Initialization a. Create a forest of vertices F b. Initialize the set of safe edges T comprising the MST to the empty set c. Sort edges by increasing weight a c e d b 2 4 5 9 6 4 5 5 F = {a}, {b}, {c}, {d}, {e} T = E = {(a,d), (c,d), (d,e), (a,c), (b,e), (c,e), (b,d), (a,b)}
  • 69. Kruskals Algorithm 102 a c e d b 2 4 5 9 6 4 5 5 2 4 4 5 5 5 6 9 (a,d) (c,d) (d,e) (a,c) (b,e) (c,e) (b,d) (a,b) E = {(a,d), (c,d), (d,e), (b,e)}