Unit 4 Graphs - Algorithms
Unit 4 Graphs - Algorithms
Weighted Graphs
• Graphs that have a number assigned to each edge are called
weighted graphs.
• Weighted graphs are used to model computer networks.
Communications costs, the response times of the computers over
these lines, or the distance between computers, can all be studied
using weighted graphs.
10
9
2 3
s 0 4 6
5 7
2
x y
Example 2
u v
1
10
10
9
2 3
s 0 4 6
5 7
5
2
x y
Example 2
u v
1
8 14
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
Example 2
u v
1
8 13
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
Example 2
u v
1
8 9
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
Example 2
u v
1
8 9
10
9
2 3
s 0 4 6
5 7
5 7
2
x y
Minimum Spanning Tree(MST)
• A company plans to build a communications network connecting its five
computer centers. Any pair of these centers can be linked with a leased
telephone line. Which links should be made to ensure that there is a path
between any two computer centers so that the total cost of the network is
minimized?
• We can solve this problem by finding a spanning tree so that the sum of
the weights of the edges of the tree is minimized. Such a spanning tree is
called a minimum spanning tree.
Central office
Wiring: Naïve Approach
Central office
Expensive!
Wiring: Better Approach
Central office
18
Algorithm Explained
MAKE-SET(x)
p[x] ← x
rank[x] ← 0
FIND-SET(x)
if x != p[x]
then p[x] ← FIND-SET(p[x])
return p[x]
Department of CSE 19
Algorithm Explained
Department of CSE 20
Example.
Department of CSE 21
Example.
Department of CSE 22
Example..
Department of CSE 23
Example.
Department of CSE 24
Minimum Spanning Tree(Example 2)
Kruskal’s Algorithm
1 3
2 F = {0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}
1
S = { 1: (0 - 2),
0 3 2 4 1: (1 – 3),
3 1: (3 – 7),
1
3 1: (4 – 7),
1 2: (0 – 1),
1
2 2: (3 – 4),
2
3 2 2: (2 – 7),
2: (4 – 6),
2 7 2: (5 – 6),
3 3 2: (2 – 5),
3: (1 – 2),
5 2 6 3: (1 – 4),
3: (2 – 3),
3: (6 – 7),
3: (0 – 5),
3: (5 – 7) } 25
Minimum Spanning Tree
Kruskal’s Algorithm
1 3
2 F = {0, 2}, {1}, {3}, {4}, {5}, {6}, {7}
1
S = { 1: (0 - 2),
0 3 2 4 1: (1 – 3),
3 1: (3 – 7),
1
3 1: (4 – 7),
1 2: (0 – 1),
1
2 2: (3 – 4),
2
3 2 2: (2 – 7),
2: (4 – 6),
2 7 2: (5 – 6),
3 3 2: (2 – 5),
3: (1 – 2),
5 2 6 3: (1 – 4),
3: (2 – 3),
3: (6 – 7),
3: (0 – 5),
3: (5 – 7) } 26
Minimum Spanning Tree
Kruskal’s Algorithm
1 3
2 F = {0, 2} {1, 3}, {4}, {5}, {6}, {7}
1
S = { 1: (0 - 2),
0 3 2 4 1: (1 – 3),
3 1: (3 – 7),
1
3 1: (4 – 7),
1 2: (0 – 1),
1
2 2: (3 – 4),
2
3 2 2: (2 – 7),
2: (4 – 6),
2 7 2: (5 – 6),
3 3 2: (2 – 5),
3: (1 – 2),
5 2 6 3: (1 – 4),
3: (2 – 3),
3: (6 – 7),
3: (0 – 5),
3: (5 – 7) } 27
Minimum Spanning Tree
Kruskal’s Algorithm
1 3
2 F = {0, 2} {1, 3, 7}, {4}, {5}, {6}
1
S = { 1: (0 - 2),
0 3 2 4 1: (1 – 3),
3 1: (3 – 7),
1
3 1: (4 – 7),
1 2: (0 – 1),
1
2 2: (3 – 4),
2
3 2 2: (2 – 7),
2: (4 – 6),
2 7 2: (5 – 6),
3 3 2: (2 – 5),
3: (1 – 2),
5 2 6 3: (1 – 4),
3: (2 – 3),
3: (6 – 7),
3: (0 – 5),
3: (5 – 7) } 28
Minimum Spanning Tree
Kruskal’s Algorithm
1 3
2 F = {0, 2} {1, 3, 4, 7}, {5}, {6}
1
S = { 1: (0 - 2),
0 3 2 4 1: (1 – 3),
3 1: (3 – 7),
1
3 1: (4 – 7),
1 2: (0 – 1),
1
2 2: (3 – 4),
2
3 2 2: (2 – 7),
2: (4 – 6),
2 7 2: (5 – 6),
3 3 2: (2 – 5),
3: (1 – 2),
5 2 6 3: (1 – 4),
3: (2 – 3),
3: (6 – 7),
3: (0 – 5),
3: (5 – 7) } 29
Minimum Spanning Tree
Kruskal’s Algorithm
1 3
2 F = {0,1, 2, 3, 4, 7}, {5}, {6}
1
S = { 1: (0 - 2),
0 3 2 4 1: (1 – 3),
3 1: (3 – 7),
1
3 1: (4 – 7),
1 2: (0 – 1),
1
2 2: (3 – 4), Rejected
2
3 2 2: (2 – 7),
2: (4 – 6),
2 7 2: (5 – 6),
3 3 2: (2 – 5),
3: (1 – 2),
5 2 6 3: (1 – 4),
3: (2 – 3),
3: (6 – 7),
3: (0 – 5),
3: (5 – 7) } 30
Minimum Spanning Tree
Kruskal’s Algorithm
1 3
2 F = {0,1, 2, 3, 4, 7}, {5}, {6}
1
S = { 1: (0 - 2),
0 3 2 4 1: (1 – 3),
3 1: (3 – 7),
1
3 1: (4 – 7),
1 2: (0 – 1),
1
2 2: (3 – 4), Rejected
2
3 2 2: (2 – 7), Rejected
2: (4 – 6),
2 7 2: (5 – 6),
3 3 2: (2 – 5),
3: (1 – 2),
5 2 6 3: (1 – 4),
3: (2 – 3),
3: (6 – 7),
3: (0 – 5),
3: (5 – 7) } 31
Minimum Spanning Tree
Kruskal’s Algorithm
1 3
2 F = {0,1, 2, 3, 4, 7}, {5}, {6}
1
S = { 1: (0 - 2),
0 3 2 4 1: (1 – 3),
3 1: (3 – 7),
1
3 1: (4 – 7),
1 2: (0 – 1),
1
2 2: (3 – 4), Rejected
2
3 2 2: (2 – 7), Rejected
2: (4 – 6),
2 7 2: (5 – 6),
3 3 2: (2 – 5),
3: (1 – 2),
5 2 6 3: (1 – 4),
3: (2 – 3),
3: (6 – 7),
3: (0 – 5),
3: (5 – 7) } 32
Minimum Spanning Tree
Kruskal’s Algorithm
1 3
2 F = {0,1, 2, 3, 4, 6, 7}, {5}
1
S = { 1: (0 - 2),
0 3 2 4 1: (1 – 3),
3 1: (3 – 7),
1
3 1: (4 – 7),
1 2: (0 – 1),
1
2 2: (3 – 4), Rejected
2
3 2 2: (2 – 7), Rejected
2: (4 – 6),
2 7 2: (5 – 6),
3 3 2: (2 – 5),
3: (1 – 2),
5 2 6 3: (1 – 4),
3: (2 – 3),
3: (6 – 7),
3: (0 – 5),
3: (5 – 7) } 33
Minimum Spanning Tree
Kruskal’s Algorithm
1 3
2 F = {0,1, 2, 3, 4, 5, 6, 7}
1
S = { 1: (0 - 2),
0 3 2 4 1: (1 – 3),
3 1: (3 – 7),
1
3 1: (4 – 7),
1 2: (0 – 1),
1
2 2: (3 – 4), Rejected
2
3 2 2: (2 – 7), Rejected
2: (4 – 6),
2 7 2: (5 – 6),
3 3 2: (2 – 5), Rejected
3: (1 – 2),
5 2 6 3: (1 – 4),
3: (2 – 3),
3: (6 – 7),
3: (0 – 5),
3: (5 – 7) } 34
Minimum Spanning Tree
Kruskal’s Algorithm
1 3
2 F = {0,1, 2, 3, 4, 5, 6, 7}
1
S = { 1: (0 - 2),
0 3 2 4 1: (1 – 3),
3 1: (3 – 7),
1
3 1: (4 – 7),
1 2: (0 – 1),
1
2 2: (3 – 4), Rejected
2
3 2 2: (2 – 7), Rejected
2: (4 – 6),
2 7 2: (5 – 6),
3 3 2: (2 – 5), Rejected
3: (1 – 2), Rejected
5 2 6 3: (1 – 4),
3: (2 – 3),
3: (6 – 7),
3: (0 – 5),
3: (5 – 7) } 35
Minimum Spanning Tree
Kruskal’s Algorithm
1 3
2 F = {0,1, 2, 3, 4, 5, 6, 7}
1
S = { 1: (0 - 2),
0 3 2 4 1: (1 – 3),
3 1: (3 – 7),
1
3 1: (4 – 7),
1 2: (0 – 1),
1
2 2: (3 – 4), Rejected
2
3 2 2: (2 – 7), Rejected
2: (4 – 6),
2 7 2: (5 – 6),
3 3 2: (2 – 5), Rejected
3: (1 – 2), Rejected
5 2 6 3: (1 – 4), Rejected
3: (2 – 3),
3: (6 – 7),
3: (0 – 5),
3: (5 – 7) } 36
Minimum Spanning Tree
Kruskal’s Algorithm
1 3
2 F = {0,1, 2, 3, 4, 5, 6, 7}
1
S = { 1: (0 - 2),
0 3 2 4 1: (1 – 3),
3 1: (3 – 7),
1
3 1: (4 – 7),
1 2: (0 – 1),
1
2 2: (3 – 4), Rejected
2
3 2 2: (2 – 7), Rejected
2: (4 – 6),
2 7 2: (5 – 6),
3 3 2: (2 – 5), Rejected
3: (1 – 2), Rejected
5 2 6 3: (1 – 4), Rejected
3: (2 – 3), Rejected
3: (6 – 7),
3: (0 – 5),
3: (5 – 7) } 37
Minimum Spanning Tree
Kruskal’s Algorithm
1 3
2 F = {0,1, 2, 3, 4, 5, 6, 7}
1
S = { 1: (0 - 2),
0 3 2 4 1: (1 – 3),
3 1: (3 – 7),
1
3 1: (4 – 7),
1 2: (0 – 1),
1
2 2: (3 – 4), Rejected
2
3 2 2: (2 – 7), Rejected
2: (4 – 6),
2 7 2: (5 – 6),
3 3 2: (2 – 5), Rejected
3: (1 – 2), Rejected
5 2 6 3: (1 – 4), Rejected
3: (2 – 3), Rejected
3: (6 – 7), Rejected
3: (0 – 5),
3: (5 – 7) } 38
Minimum Spanning Tree
Kruskal’s Algorithm
1 3
2 F = {0,1, 2, 3, 4, 5, 6, 7}
1
S = { 1: (0 - 2),
0 3 2 4 1: (1 – 3),
3 1: (3 – 7),
1
3 1: (4 – 7),
1 2: (0 – 1),
1
2 2: (3 – 4), Rejected
2
3 2 2: (2 – 7), Rejected
2: (4 – 6),
2 7 2: (5 – 6),
3 3 2: (2 – 5), Rejected
3: (1 – 2), Rejected
5 2 6 3: (1 – 4), Rejected
3: (2 – 3), Rejected
3: (6 – 7), Rejected
3: (0 – 5), Rejected
3: (5 – 7) } 39
Minimum Spanning Tree
Kruskal’s Algorithm
1 3
2 F = {0,1, 2, 3, 4, 5, 6, 7}
1
S = { 1: (0 - 2),
0 3 2 4 1: (1 – 3),
3 1: (3 – 7),
1
3 1: (4 – 7),
1 2: (0 – 1),
1
2 2: (3 – 4), Rejected
2
3 2 2: (2 – 7), Rejected
2: (4 – 6),
2 7 2: (5 – 6),
3 3 2: (2 – 5), Rejected
3: (1 – 2), Rejected
5 2 6 3: (1 – 4), Rejected
3: (2 – 3), Rejected
3: (6 – 7), Rejected
3: (0 – 5), Rejected
3: (5 – 7) Rejected } 40
Prim’s algorithm
( minimum spanning tree)
41
• MST-PRIM(G,w, r)
• 1 for each u ∈ V[G]
• 2 do key[u]←∞
• 3 π[u]← NIL
• 4 key[r] ← 0
• 5 Q ← V[G]
• 6 while Q! = ∅
• 7 do u ← EXTRACT-MIN(Q)
• 8 for each v ∈ Adj[u]
• 9 do if v ∈ Q and w(u, v) < key[v]
• 10 then π[v]← u
• 11 key[v] ← w(u, v)
Department of CSE 42
MST
Minimum spanning tree: (Prim’s algorithm)
Start anywhere and repeatedly choose the next-
smallest edge out from your current tree.
43
MST
Minimum spanning tree: (Prim’s algorithm)
Start anywhere and repeatedly choose the next-
smallest edge out from your current tree.
44
MST
Minimum spanning tree: (Prim’s algorithm)
Start anywhere and repeatedly choose the next-
smallest edge out from your current tree.
45
MST
Minimum spanning tree: (Prim’s algorithm)
Start anywhere and repeatedly choose the next-
smallest edge out from your current tree.
46
MST
Minimum spanning tree: (Prim’s algorithm)
Start anywhere and repeatedly choose the next-
smallest edge out from your current tree.
47
MST
Minimum spanning tree: (Prim’s algorithm)
Start anywhere and repeatedly choose the next-
smallest edge out from your current tree.
48
MST
Minimum spanning tree: (Prim’s algorithm)
Start anywhere and repeatedly choose the next-
smallest edge out from your current tree.
49
MST
Minimum spanning tree: (Prim’s algorithm)
Start anywhere and repeatedly choose the next-
smallest edge out from your current tree.
50
MST
Minimum spanning tree: (Prim’s algorithm)
Start anywhere and repeatedly choose the next-
smallest edge out from your current tree.
Done! 51