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

Unit 4 Graphs - Algorithms

Uploaded by

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

Unit 4 Graphs - Algorithms

Uploaded by

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

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.

Figure: Weighted Graphs Modeling a Computer Network.


Example
• Another important problem involving weighted graphs asks for a
circuit of shortest total length that visits every vertex of a complete
graph exactly once.
• E.g 1: Travelling Salesman Problem
• E.g.2: What is the length of a shortest path between a and z?
Single-source shortest-path problem
• Given a graph G =(V,E), we want to find a shortest path from a given
source vertex s ∈ V to each vertex v ∈ V .
• Dijkstra’s algorithm solves the single-source shortest-paths problem
on a weighted, connected simple graph G(V,E) for the case in which all
edge weights are nonnegative.
• Let Lk(v) is the length of a shortest path from a to v
• Updation:
• Lk(a, v) = min{L k-1(a, v),L k-1(a, u) + w(u, v)}
where w(u, v) is the length of the edge with u and v as endpoints
Example
Use Dijkstra’s algorithm to find the length of a shortest path between the vertices a and z in the
weighted graph displayed in Figure (a).
Example 2
u v
1
 

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.

• A minimum spanning tree in a connected weighted graph is a spanning


tree that has the smallest possible sum of weights of its edges.
Problem: Laying Telephone Wire

Central office
Wiring: Naïve Approach

Central office

Expensive!
Wiring: Better Approach

Central office

Minimize the total length of wire connecting the customers


MST Algorithms
• Two greedy approach algorithms for constructing MST :
• Prim’s Algorithm
• Kruskal Algorithm
Kruskal’s algorithm -
• Select the edges in order of smallest weight and accept an edge
if it does not cause a cycle
MST-KRUSKAL(G,w)
1A←∅
2 for each vertex v ∈ V[G]
3 do MAKE-SET(v)
4 sort the edges of E into non decreasing order by weight w
5 for each edge (u, v) ∈ E, taken in non decreasing order by
weight
6 do if FIND-SET(u) != FIND-SET(v)
7 then A ← A ∪ {(u, v)}
8 UNION(u, v)
9 return A

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)

• Grow the tree in successive stages


• At each stage, a new vertex is added to the tree by
choosing the edge (u, v) such that the cost of (u, v) is the
smallest among all edges where u is in the tree and v is not.

• EXTRACT-MIN(Q) deletes the node from array/queue Q


whose key is minimum, returning a pointer to the node.

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

You might also like