Unit 5 Graphs (ECE)
Unit 5 Graphs (ECE)
Graphs 7
Directed vs. undirected graphs
5 is adjacent to 7
7 is adjacent from 5
• Path: a sequence of vertices/edges that connect
two nodes in a graph
• Complete graph: a graph in which every vertex is
directly connected to every other vertex
Graph terminology (cont.)
• Weighted graph: a graph in which each edge
carries a value
Sequential Graph Representation
There are different ways to represent a graph
• Adjacency matrix.
• Adjacency List for each vertex
Graph Representations
Graph representation is a technique to store graph into the memory of
computer.
1. Adjacency Matrix
It is used to represent which nodes are adjacent to each other.
In this representation, we have to construct a nXn matrix A. If there is
any edge from a vertex i to vertex j, then the corresponding element of
A, aij = 1, otherwise aij = 0.
Note, even if the graph on 100 vertices contains only 1 edge, we still
have to have a 100x100 matrix with lots of zeroes.
If there is any weighted graph then instead of 1s and 0s, we can store
the weight of the edge.
Graph Representations (Cont.)
Adjacency Matrix Examples
Undirected graph representation
Graph Representations (Cont.)
Adjacency Matrix Examples
Directed graph representation
Draw
Adjacency
Matrix
Graph Representations (Cont.)
Adjacency Matrix Examples
Undirected weighted graph representation
Graph Representations (Cont.)
Example 3:
B
represent following graph by adjacency matrix.
Pros and Cons of Adjacency Matrix
Pros:
• Easy method of representing graph
• Storing dense graphs is easy.
Cons:
• Memory space is wasted
• Insertion and deletion operations are difficult as
we need to resize the matrix and update the
values.
• May lead to sparse matrix if sparse graph is there.
Graph Representations (Cont.)
3. Adjacency List
Adjacency list is a linked representation.
In this representation, for each vertex in the graph, we maintain
the list of its neighbors. It means, every vertex of the graph
contains list of its adjacent vertices.
We have an array of vertices which is indexed by the vertex
number and for each vertex v, the corresponding array element
points to a singly linked list of neighbors of v.
Graph Representations (Cont.)
Adjacency List Example
Let's see the following directed graph representation
implemented using linked list:
Adjacency List Representation
Vertex 1 2 4
1 4 5
Vertex 2
1 30
Vertex 3
2 4 5
Vertex 4
2 3
Vertex 5 1 3 5
4 3
Implementing Adjacency list
Breadth First Search ( Level Order)
r s t u
v w x y
Q: s
Example (BFS)
Visiting Order: s
r s t u
w x y
v
Q: w r
Example (BFS)
Visiting Order: s, w
r s t u
v w x y
Q: r t x
Example (BFS)
Visiting Order: s, w, r
r s t u
v w x y
Q: t x v
Example (BFS)
Visiting Order: s, w, r, t
r s t u
v w x y
Q: x v u
Example (BFS)
Visiting Order: s, w, r, t, x
r s t u
v w x y
Q: v u y
Example (BFS)
Visiting Order: s, w, r, t, x, v
r s t u
v w x y
Q: u y
Example (BFS)
Visiting Order: s, w, r, t, x, v, u
r s t u
v w x y
Q: y
Example (BFS)Breadth First Search-
Equivalant to Level Order traversal
Traversal Order: s, w, r, t, x, v, u, y
r s t u
v w x y
Q:
BFS(G,s)
BFS(G,s)
1.
1. for
foreach
eachvertex
vertexuuininV[G]
V[G]–– {s}
{s}
22 do
docolor[u]
color[u] white/1
white/1
33 QQ white: undiscovered
gray: discovered
44 color[s] gray
color[s] gray black: finished/print/Deqeued
55 enqueue(Q,s)
enqueue(Q,s)
66 while
whileQQ Q: a queue of discovered
77 do
douudequeue(Q)
dequeue(Q)
vertices
color[v]: color of v
88 for
foreach
eachvvin
inAdj[u]
Adj[u]
99 do
doififcolor[v]
color[v]==white
white
10
10 then
thencolor[v]
color[v]
gray/2
gray/2 Example: animation in last slide
11
11 enqueue(Q,v)
enqueue(Q,v)
12
12 color[u]
color[u]black/3
black/3
Question for Practice
Breadth-First Search Example
Apply BFS.
Breadth-First Search Example
• During the execution, the BFS algorithm uses two arrays: QUEUE
and ORIG.
• While QUEUE is used to hold the nodes that have to be processed,
ORIG is used to keep track of the origin of each edge. Initially,
FRONT = REAR = –1.
Breadth-First Search Example
• (a) Add A to QUEUE and add NULL to ORIG.
Breadth-First Search Example
• (b) Dequeue a node by setting FRONT = FRONT + 1 (remove the
FRONT element of QUEUE) and enqueue the neighbours of A.
Also, add A as the ORIG of its neighbours.
A
Breadth-First Search Example
• (c) Dequeue a node by setting FRONT = FRONT + 1 and enqueue
the neighbours of B. Also, add B as the ORIG of its neighbours.
AB
Breadth-First Search Example
• (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 neighbors B
and G. Since B has already been added to the queue and it is not in the Ready
state, we will not add B and only add G.
ABC
Breadth-First Search Example
• (e) Dequeue a node by setting FRONT = FRONT + 1 and enqueue the neighbours
of D. Also, add D as the ORIG of its neighbours. Note that D has two neighbours
C and G. Both are already added.
ABCD
Breadth-First Search Example
• (f) Dequeue a node by setting FRONT = FRONT + 1 and enqueue the neighbors
of E. Also, add E as the ORIG of its neighbors. We will not add C and add only F.
ABCDE
Breadth-First Search Example
• (g) Dequeue a node by setting FRONT = FRONT + 1 and enqueue the neighbours
of G. Also, add G as the ORIG of its neighbors. Note that G has three neighbors
F, H, and I.
• Now, backtrack from I using ORIG to find the minimum path P. Thus, we have P
as A -> C -> G -> I.
ABCDEGFHI
Breadth-First Search Example
• (g) Dequeue a node by setting FRONT = FRONT + 1 and enqueue the neighbours
of G. Also, add G as the ORIG of its neighbors. Note that G has three neighbors
F, H, and I.
• Now, backtrack from I using ORIG to find the minimum path P. Thus, we have P
as A -> C -> G -> I.
ABCDEGFHI
Depth First Search (Pre-order)
1 2 3
8 4
7 6 5
David Luebke
64
Review: DFS Example
source Stack:1 Initially Push source on
vertex to stack and make it yellow
1 2 3
8 4
7 6 5
David Luebke
65
Review: DFS Example
source Stack: 2,6,8 after Pop(1) and add to output
vertex then push all white adjacent on stack and
make them yellow
1 2 3
8 4
7 6 5
1
David Luebke
66
Review: DFS Example
source Stack: 2,6,7 after Pop(8) and add to output
vertex then push all white adjacent on stack and
make them yellow
1 2 3
8 4
7 6 5
1 8
Review: DFS Example
source Stack: 2,6 after Pop(7) and add to output
vertex then Now No Adjacent white of 7 so make as
black.(Fully Processed)
1 2 3
8 4
7 6 5
1 8 7
Review: DFS Example
source Stack: 2 after Pop(6) and add to output then
vertex Now No Adjacent white of 6 so make as
black.(Fully Processed)
1 2 3
8 4
7 6 5
1 8 7 6
Review: DFS Example
source Stack: 4 after Pop(2) and add to output then
vertex push all Adjacent white nodes of 2 to stack
and make it yellow(Partially Processed)
1 2 3
8 4
7 6 5
1 8 7 6 2
Review: DFS Example
source Stack: Pop(4) and add to output , make it
vertex black. Start again for remaining white nodes.
1 2 3
8 4
7 6 5
1 8 7 6 2 4
Review: DFS Example
source Stack:3
vertex
1 2 3
8 4
7 6 5
1 8 7 6 2 4 3
David Luebke
72
Review: DFS Example
source Stack:5 Pop 3 make it black and push
vertex adjacent white on the stack make it yellow
1 2 3
8 4
7 6 5
1 8 7 6 2 4 3
David Luebke
73
Review: DFS Example
source Stack: Pop 5 make it black and No more node
vertex to process.
1 2 3
8 4
7 6 5
1 8 7 6 2 4 3 5
David Luebke
74
Apply BFS and DFS from
(Considering Numerical Order ie adjacent nodes to be listed in
increasing order)
• 1,2,3,4,5,6,7,8
• 13785462
Question for Practice
Question for Practice
Question for Practice
Show all steps to traverse the following graph by
Breadth First Search. Start from node A.
Apply DFS and BFS
BFS and DFS Comparison
S.NO BFS DFS
BFS stands for Breadth First
1. Search. DFS stands for Depth First Search.
BFS uses Queue data structure for DFS(Depth First Search) uses Stack data
2. finding the shortest path. structure.
BFS is more suitable for searching
3. vertices which are closer to the DFS is more suitable when there are
given source. solutions away from source.
BFS considers all neighbors first DFS is more suitable for game or puzzle
and therefore not suitable for problems. We decide, then explore all
4. decision making trees used in paths through this decision. And if this
games or puzzles. decision leads to win situation, we stop.
The Time complexity of BFS is The Time complexity of DFS is also O(V +
5. O(V + E), where V stands for E), where V stands for vertices and E
vertices and E stands for edges. stands for edges
Spanning Tree
• It is sub graph of a graph where sub graph
must have all the vertices and v-1 edges. If
there are v vertices in graph.
• There are different possibility of spanning tree
of a same graph.
• In terms of number of nodes of complete graph
formula is n .
(n-2)
Max. number of Spanning
trees.
1. Calculate and Draw all spanning trees of complete graph of 3 vertices
David Luebke
85
Draw any one Spanning Tree & calculate
overall cost
• Problem: given a connected, undirected,
weighted graph:
6 4
5 9
14 2
10
15
3 8
Can we calculate Min. Cost Spanning tree?
• How-> ??
• Prims and Kruskal's
Minimum Spanning Tree
• Problem: given a connected, undirected,
weighted graph:
6 4
5 9
14 2
10
15
3 8
Minimum Spanning Tree
• Problem: given a connected, undirected,
weighted graph, find a spanning tree using
edges that minimize the total weight
6 4
5 9
14 2
10
15
3 8
Minimum Spanning Tree
• Which edges form the minimum spanning tree
(MST) of the below graph?
A
6 4
5 9
H B C
14 2
10
15
G E D
3 8
F
Minimum Spanning Tree
• Answer:
A
6 4
5 9
H B C
14 2
10
15
G E D
3 8
F
Prims
• Select the min. cost edge
• Select next min. edge which should be
connected with pre selected edges’s vertices
• Always maintain a tree at every stage.
Prim’s Algorithm
MST-Prim(G, w, r)
Q = V[G];
for each u Q
key[u] = ;
key[r] = 0;
p[r] = NULL;
while (Q not empty)
u = ExtractMin(Q);
for each v Adj[u]
if (v Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 4
5 9
Q = V[G];
for each u Q
key[u] = ; 14 2
10
key[r] = 0; 15
p[r] = NULL;
3 8
while (Q not empty)
u = ExtractMin(Q); Run on example graph
for each v Adj[u]
if (v Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 4
5 9
Q = V[G];
for each u Q
key[u] = ; 14 2
10
key[r] = 0; 15
p[r] = NULL;
3 8
while (Q not empty)
u = ExtractMin(Q); Run on example graph
for each v Adj[u]
if (v Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 4
5 9
Q = V[G];
for each u Q
key[u] = ; 14 2
10
key[r] = 0; 15
r 0
p[r] = NULL;
3 8
while (Q not empty)
u = ExtractMin(Q); Pick a start vertex r
for each v Adj[u]
if (v Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 4
5 9
Q = V[G];
for each u Q
key[u] = ; 14 2
10
key[r] = 0; 15
u 0
p[r] = NULL;
3 8
while (Q not empty)
u = ExtractMin(Q); Red vertices have been removed from Q
for each v Adj[u]
if (v Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 4
5 9
Q = V[G];
for each u Q
key[u] = ; 14 2
10
key[r] = 0; 15
u 0
p[r] = NULL;
3 3 8
while (Q not empty)
u = ExtractMin(Q); Red arrows indicate parent pointers
for each v Adj[u]
if (v Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 4
5 9
Q = V[G]; 14
for each u Q
key[u] = ; 14 2
10
key[r] = 0; 15
u 0
p[r] = NULL;
3 3 8
while (Q not empty)
u = ExtractMin(Q);
for each v Adj[u]
if (v Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 4
5 9
Q = V[G]; 14
for each u Q
key[u] = ; 14 2
10
key[r] = 0; 15
0
p[r] = NULL;
3 3 8
while (Q not empty) u
u = ExtractMin(Q);
for each v Adj[u]
if (v Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 4
5 9
Q = V[G]; 14
for each u Q
key[u] = ; 14 2
10
key[r] = 0; 15
0 8
p[r] = NULL;
3 3 8
while (Q not empty) u
u = ExtractMin(Q);
for each v Adj[u]
if (v Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 4
5 9
Q = V[G]; 10
for each u Q
key[u] = ; 14 2
10
key[r] = 0; 15
0 8
p[r] = NULL;
3 3 8
while (Q not empty) u
u = ExtractMin(Q);
for each v Adj[u]
if (v Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 4
5 9
Q = V[G]; 10
for each u Q
key[u] = ; 14 2
10
key[r] = 0; 15
0 8
p[r] = NULL;
3 3 8
while (Q not empty) u
u = ExtractMin(Q);
for each v Adj[u]
if (v Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 4
5 9
Q = V[G]; 10 2
for each u Q
key[u] = ; 14 2
10
key[r] = 0; 15
0 8
p[r] = NULL;
3 3 8
while (Q not empty) u
u = ExtractMin(Q);
for each v Adj[u]
if (v Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 4
5 9
Q = V[G]; 10 2
for each u Q
key[u] = ; 14 2
10
key[r] = 0; 15
0 8 15
p[r] = NULL;
3 3 8
while (Q not empty) u
u = ExtractMin(Q);
for each v Adj[u]
if (v Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
u
MST-Prim(G, w, r) 6 4
5 9
Q = V[G]; 10 2
for each u Q
key[u] = ; 14 2
10
key[r] = 0; 15
0 8 15
p[r] = NULL;
3 3 8
while (Q not empty)
u = ExtractMin(Q);
for each v Adj[u]
if (v Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
u
MST-Prim(G, w, r) 6 4
5 9
Q = V[G]; 10 2 9
for each u Q
key[u] = ; 14 2
10
key[r] = 0; 15
0 8 15
p[r] = NULL;
3 3 8
while (Q not empty)
u = ExtractMin(Q);
for each v Adj[u]
if (v Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
4 u
MST-Prim(G, w, r) 6 4
5 9
Q = V[G]; 10 2 9
for each u Q
key[u] = ; 14 2
10
key[r] = 0; 15
0 8 15
p[r] = NULL;
3 3 8
while (Q not empty)
u = ExtractMin(Q);
for each v Adj[u]
if (v Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
4 u
MST-Prim(G, w, r) 6 4
5 9
Q = V[G]; 5 2 9
for each u Q
key[u] = ; 14 2
10
key[r] = 0; 15
0 8 15
p[r] = NULL;
3 3 8
while (Q not empty)
u = ExtractMin(Q);
for each v Adj[u]
if (v Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
u
MST-Prim(G, w, r) 6 4 4
5 9
Q = V[G]; 5 2 9
for each u Q
key[u] = ; 14 2
10
key[r] = 0; 15
0 8 15
p[r] = NULL;
3 3 8
while (Q not empty)
u = ExtractMin(Q);
for each v Adj[u]
if (v Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
u 4
MST-Prim(G, w, r) 6 4
5 9
Q = V[G]; 5 2 9
for each u Q
key[u] = ; 14 2
10
key[r] = 0; 15
0 8 15
p[r] = NULL;
3 3 8
while (Q not empty)
u = ExtractMin(Q);
for each v Adj[u]
if (v Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
u
MST-Prim(G, w, r) 6 4 4
5 9
Q = V[G]; 5 2 9
for each u Q
key[u] = ; 14 2
10
key[r] = 0; 15
0 8 15
p[r] = NULL;
3 3 8
while (Q not empty)
u = ExtractMin(Q);
for each v Adj[u]
if (v Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r) 6 4 4
5 9
Q = V[G]; 5 2 9
for each u Q
key[u] = ; 14 2 u
10
key[r] = 0; 15
0 8 15
p[r] = NULL;
3 3 8
while (Q not empty)
u = ExtractMin(Q);
for each v Adj[u]
if (v Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Review: Prim’s Algorithm
MST-Prim(G, w, r)
Q = V[G];
for each u Q
key[u] = ;
key[r] = 0;
p[r] = NULL;
while (Q not empty)
u = ExtractMin(Q);
for each v Adj[u]
if (v Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Construct minimum spanning tree for the below given graph
using Prim’s algorithm (Source node = a)
Draw MST using Prim’s
Kruskal’s Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
14 17
T = ; 8 25
5
for each v V
21 13 1
MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
} David Luebke
117
Kruskal’s Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
14 17
T = ; 8 25
5
for each v V
21 13 1?
MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
} David Luebke
118
Kruskal’s Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
14 17
T = ; 8 25
5
for each v V
21 13 1
MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
} David Luebke
119
Kruskal’s Algorithm
Run the algorithm:
Kruskal() 2? 19
{ 9
14 17
T = ; 8 25
5
for each v V
21 13 1
MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
} David Luebke
120
Kruskal’s Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
14 17
T = ; 8 25
5
for each v V
21 13 1
MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
} David Luebke
121
Kruskal’s Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
14 17
T = ; 8 25
5?
for each v V
21 13 1
MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
} David Luebke
122
Kruskal’s Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
14 17
T = ; 8 25
5
for each v V
21 13 1
MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
} David Luebke
123
Kruskal’s Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
14 17
T = ; 8? 25
5
for each v V
21 13 1
MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
} David Luebke
124
Kruskal’s Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
14 17
T = ; 8 25
5
for each v V
21 13 1
MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
} David Luebke
125
Kruskal’s Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9?
14 17
T = ; 8 25
5
for each v V
21 13 1
MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
} David Luebke
126
Kruskal’s Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
14 17
T = ; 8 25
5
for each v V
21 13 1
MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
} David Luebke
127
Kruskal’s Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
14 17
T = ; 8 25
5
for each v V
21 13? 1
MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
} David Luebke
128
Kruskal’s Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
14 17
T = ; 8 25
5
for each v V
21 13 1
MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
} David Luebke
129
Kruskal’s Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
14? 17
T = ; 8 25
5
for each v V
21 13 1
MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
} David Luebke
130
Kruskal’s Algorithm
Run the algorithm:
Kruskal() 2 19
{ 9
14 17
T = ; 8 25
5
for each v V
21 13 1
MakeSet(v);
sort E by increasing edge weight w
for each (u,v) E (in sorted order)
if FindSet(u) FindSet(v)
T = T U {{u,v}};
Union(FindSet(u), FindSet(v));
} David Luebke
131
Question