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

Unit IV

The document outlines key concepts in graph theory, focusing on Minimum Spanning Trees and Shortest Path Algorithms. It details algorithms such as Kruskal’s, Prim’s, and Dijkstra’s, providing explanations and pseudocode for each. Additionally, it discusses applications of these algorithms in various fields, emphasizing their importance in optimizing network design and pathfinding.

Uploaded by

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

Unit IV

The document outlines key concepts in graph theory, focusing on Minimum Spanning Trees and Shortest Path Algorithms. It details algorithms such as Kruskal’s, Prim’s, and Dijkstra’s, providing explanations and pseudocode for each. Additionally, it discusses applications of these algorithms in various fields, emphasizing their importance in optimizing network design and pathfinding.

Uploaded by

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

Unit IV

Graph concepts
Scs1201 – advanced data structures
Semester iii / year ii
Syllabus
• Minimum Spanning Tree
– Kruskal’s Algorithm
– Prim’s Algorithm
– Sollin’s Algorithm
• Shortest Path Algorithms
– Dijkstra’s Algorithm
– Bellman Ford
– Floyd Warshall Algorithm
Minimum Spanning Tree
• A tree T is a spanning tree of G if T is a
spanning sub-graph of G.
• Every spanning tree of a connected n-
node graph G has (n-1) arcs.
• Acyclic Tree.
• The arcs belonging to a spanning tree are
called tree arcs and the rest are called
non-tree arcs.
Example – Undirected Graph
v1 v2 v1 v2 v1 v2

v3 v4 v3 v4 v3 v4

v1 v2 v1 v2 v1 v2

v3 v4 v3 v4 v3 v4
Example – Directed Graph
2 4 2 4

1 5 7 1 5 7

3 6 3 6
Applications
• Designing Physical Systems (E.g. Pipeline)
• Optimal Message Passing
• Reducing Data Storage
• Cluster Analysis
• All Pairs minimax Path Problem
Minimum Cost Spanning Trees
• The cost of a spanning tree of a weighted
undirected graph is the sum of the costs of
the edges in the spanning tree
• A minimum cost spanning tree is a
spanning tree of least cost
• Select n-1 edges from a weighted graph of
‘n’ vertices with minimum cost
Algorithms
• Kruskal’s Algorithm
• Prim’s Algorithm
• Sollin’s Algorithm
Greedy Strategy
 An optimal solution is constructed in stages
 At each stage, the best decision is made at this
time
 Since this decision cannot be changed later, it
is ensured that the decision will result in a
feasible solution
 Typically, the selection of an item at each stage
is based on a least cost or a highest profit
criterion
Kruskal’s Approach
 Build a minimum cost spanning tree T by
adding edges to T one at a time
 Select the edges for inclusion in T in the
non-decreasing order of the cost
 An edge is added to T if it does not form a
cycle
 Since G is connected and has n > 0 vertices,
exactly n-1 edges will be selected
0 10 5

Example 2 12 3

0 1 14 6
2 0
8 16
1 1 1 1 2
0 1 1
4 6 18
5 6 2 5 6 2 3 6
2
2 4 22
1 1 3 4
5 4 8 2 4
2 24
2 3 3 4 6
25
4 5
28
0 1
Kruskals Algorithm
Contd… 0 10 5

2 12 3

0 0 0 1 14 6
+ 3 6
10 1 10 1 1
cycle 16
10 1 2
14
5 6 2 5 6 2 6 3
18
6
5 2
12 22
4 4 4
12 3 4
3 3 3 24
4 6
25
4 5
28
0 1
Contd… 0 10 5

2 12 3
0 0
+ 4 6
1 14 6
10 1 cycle
10 1
14 16 14 16 16
1 2
5 6 2 5 6 2
18
25 3 6
12 12
4 4 22
22 3 4
22
3 3
24
4 6

cost = 10 + 25 + 22 + 12 + 16 + 4
25
5
14
28
0 1
MST_Kruskal(G, W)
• A←Ø
• for each vertex v ϵ V[G]
– do MAKE_SET(v)
• Sort the edges of E into non-decreasing order
by weight w
– do if find_set(u) ≠ find_set(v) then
• A ← A u { (u, v) }
• UNION(u, v)
• Return A
Pseudocode
• KRUSKAL(E, cost, n, t)
• Construct a queue with edge costs  mincost = mincost +
such that they are in ascending cost[u, v]
order  Union(j, k)

• i = 0, mincost = 0  End if
• while i < n – 1 and queue is not  End while
empty
– Delete minimum cost edge (u, v) from
 If i ≠ n – 1
queue  Print “Not a spanning
– j = Find(u), tree”
– k = Find(v)  Else
– If j ≠ k 

Return minimum_cost
i=i+1
• t[i, 1] = u  End if
• t[i, 2] = v
• minimum_cost = minimum_cost + cost[u,  End KRUSKAL
v]
Example 2
Solution
Minimum Cost Spanning Tree

cost = 4+ 8 + 1 + 2 + 2 + 4 + 7 +
9
Example 3
Example 4
1 14
8 1 14 0
1 3 5 7 1 3 5 7
0 7 3
7 4 12 6 3 2 4 6
2
2 4 6 8
2 4 6 8
9
Example 5
Prim’s Algorithm
• T = ;
• U={1}
• while (U ≠ V)
– let (u, v) be the lowest cost edge such that u ∈ U
and v ∈ V – U
– T = T ∪ {(u, v)}
– U = U ∪ {v}
Example 1

Cost = 37
4+8+1+2+2+4+7
+9
Example 2
Example 3
1 14
8 1 14 0
1 3 5 7 1 3 5 7
0 7 3
7 4 12 6 3 2 4 6
2
2 4 6 8
2 4 6 8
9
Example 4
Prims Algorithm (Cormen)
Sollins Algorithm (or Boruvka)
1) Input is a connected, weighted and directed graph.
2) Initialize all vertices as individual components (or
sets).
3) Initialize MST as empty.
4) While there are more than one components, do
following for each component.
a) Find the closest weight edge that connects this component
to any other component.
b) Add this closest edge to MST if not already added.
5) Return MST.
Example
Component Low Cost
Edge
{0} 0-1
{1} 0-1
{2} 2-8
{3} 2-3
{4} 3-4
{5} 5-6
{6} 6-7
{7} 6-7
{8} 2-8
Low Cost Edges (Level 1)

{0-1, 2-8, 2-3, 3-4, 5-6, 6-7}


Level 2

{{0,1}, {2,3,4,8}, {5,6,7}}


Component Low Cost Edge
{0,1} 1-2 (or 0-7)
{2,3,4,8} 2-5
{5,6,7} 2-5
Level 3

{0-1, 2-8, 2-3, 3-4, 5-6, 6-7, 1-2, 2-5}


Example 2
0 2
8
1 1
0 1 1
4 6
5 6 2
2
2 4 1 1
5 4 8 2
2
2 3
Problem – Minimum Cost Spanning Tree
SHORTEST PATH ALGORITHMS
Algorithms
• Single Source Shortest Path
– Dijkstras Algorithm
– Bellman Ford Algorithm
• All Pairs Shortest Path
– Floyd Warshall
Edsger Wybe Dijkstra
- May 11, 1930 – August 6,
2002

- Received the 1972 A. M.


Turing Award, widely
considered the most prestigious
award in computer science.

- The Schlumberger Centennial


Chair of Computer Sciences at
The University of Texas at
Austin from 1984 until 2000

- Made a strong case against


use of the GOTO statement in
programming languages and
helped lead to its deprecation.

- Known for his many essays on


programming.
Dijkstra’s Algorithm
• Solution to the single-source shortest path
problem in graph theory.
• Works on both directed and undirected graphs.
• All edges must have nonnegative weights.

• Approach: Greedy
• Input: Weighted graph G={E,V} and source vertex
v∈V, such that all edge weights are nonnegative
• Output: Lengths of shortest paths from a given
source vertex v∈V to all other vertices
Pseudocode
dist[s] ←0 (distance to source vertex is zero)
for all v ∈ V–{s}

do dist[v] ← ∞ (set all other distances to infinity)


S←∅ (S, the set of visited vertices is initially empty)
Q←V (Q, the queue initially contains all vertices)
while Q ≠ ∅ (while the queue is not empty)
do u ← mindistance(Q,dist) (select the element of Q with the
min. distance)
S←S ∪ {u} (add u to list of visited vertices)
for all v ∈ neighbors[u]
do if dist[v] > dist[u] + w(u, v) (if new shortest path
found)
then dist[v] ←dist[u] + w(u, v) (set new value of
shortest path)
return dist
Example
 
Initialize B
2
D
10
A B C D E
0 A 14 8 79
0    
3
2
S={} C E
 
Visit A
10 
A B C D E 2
B D
0     10
1 3 0 A 14 8 79
0
3
2
S={A} C E
3 
Visit C
10 
A B C D E 2
B D
0     10
1 3 0 A 8
14 79
0
3
2
C E
S={A, C} 3 
Start from C
A B C D E 7 11
0     2
B D
1 3 10
0
0 A 14 8 79
7 1 5
1 3
2
S={A, C} C E
3 5
Visit E
A B C D E 7 11
0     2
B D
1 3 10
0
0 A 14 8 79
7 1 5
1 3
2
S={A, C, E} C E
3 5
Start from E
A B C D E 7 11
0     2
B D
1 3 10
0
0 A 14 8 79
7 1 5
1 3
2
1
S={A, C, E} C E
4 3 5
Backtrack to C
A B C D E 7 11
0     2
B D
1 3 10
0
0 A 14 8 79
7 1 5
1 3
2
7 C, E}
S={A, 1 C E
1 3 5
Visit B
A B C D E 7 11
0     2
B D
1 3 10
0
0 A 14 8 79
7 1 5
1 3
2
7 1
S={A, C, E, B} C E
1 3 5
Start from B
A B C D E 7 11
0     2
B D
1 3 10
0
0 A 14 8 79
7 1 5
1 3
2
7 C, E,1B}
S={A, C E
1 3 5
Visit D
A B C D E 7 9
0     2
B D
1 3 10
0
0 A 14 8 79
7 1 5
1 3
2
7 C, E,9B, D}
S={A, C E
3 5
Problem – Source Vertex: B

2
B D
10
A 14 8 79
3
2
C E
Problem
Construct the Directed Graph for the given adjacency matrix. Consider 0
as the source vertex and find the shortest path using Dijkstra’s Algorithm.

0 1 2 3 4 5 6 7
0 0 7 8 0 2 0 6 0
1 0 0 4 0 0 8 0 3
2 8 0 0 0 0 0 5 0
3 0 9 0 0 0 2 0 0
4 0 1 0 2 0 0 9 7
5 9 0 0 3 0 0 2 2
6 0 0 8 0 4 4 0 3
7 6 0 4 0 0 0 9 0
Bellman Ford Algorithm
• Uses Relaxation to find Single Source Shortest
Paths on directed graphs that may contain
negative weight edges
• Also detects negative weight cycles. (no
solution)
Exercise: Source Vertex : 1
Exercise
Exercise: Source Vertex: A
Algorithm
Bellman-Ford(G, w, s) Initialize_Single_Source(G,
s)
• Initialize_Single_Sourc
• for each vertex v V[G]
e(G, s)
• do d[v]  
• for i  1 to |V[G]| - 1 • [v]  nil
– do for each edge (u,v)
• d[s]  0
 E[G] Relax(u, v, w)
• do Relax(u, v, w)
• if d[v] > d[u] + w(u, v)
• for each edge (u, v)  • then d[v]  d[u] +
E[G] w(u, v)
– do if d[v] > d[u] + w(u, • [v]  u
v)
• then return false
Initialize
1 2 3 4 5
d     0 3
 / / / / / 2 1
4
3
(1,3)= 5 1 6
6 2
(1,4)= 1
4 3
3 2
(2,1)=
3
(3,4)=
2
(4,2)=
Iteration I
1 2 3 4 5
d  4  2 0 3
 / 5 / 5 / 2 1
4
3
(1,3)= 5 1 6
6 2
(1,4)= 1
4 3
3 2
(2,1)=
3
(3,4)=
2
(4,2)=
Iteration II
1 2 3 4 5
d 7 3 3 2 0 3
 2 4 4 5 / 2 1
4
3
(1,3)= 5 1 6
6 2
(1,4)= 1
4 3
3 2
(2,1)=
3
(3,4)=
2
(4,2)=
Iteration III
1 2 3 4 5
d 6 3 3 2 0 3
 2 4 4 5 / 2 1
4
3
(1,3)= 5 1 6
6 2
(1,4)= 1
4 3
3 2
(2,1)=
3
(3,4)=
2 No edges relaxes in Iteration 4.
(4,2)=
Negative Weight Cycle Check
• d[3] > d[1] + w(1, 3)  3 > 6+6=12
• d[4] > d[1] + w(1, 4)  2 > 6+3=9
• d[1] > d[2] + w(2, 1)  6 > 3+3=6
• d[4] > d[3] + w(3, 4)  2 > 3+2=5
• d[2] > d[4] + w(4, 2)  3 > 2+1=3
• d[3] > d[4] + w(4, 3)  3 > 2+1=3
• d[2] > d[5] + w(5, 2)  3 > 0+4=4
• d[4] > d[5] + w(5, 4)  2 > 0+2=2
Problem
Construct the Directed Graph for the given adjacency matrix. Consider 0
as the source vertex and find the shortest path using Dijkstra’s and
Bellman Ford Algorithm.
0 1 2 3 4 5 6 7
0 0 7 8 0 2 0 -6 0
1 0 0 4 0 0 8 0 3
2 8 0 0 0 0 0 5 0
3 0 9 0 0 0 2 0 0
4 0 1 0 -2 0 0 9 7
5 9 0 0 3 0 0 2 2
6 0 0 8 0 4 4 0 3
7 6 0 4 0 0 0 -9 0
Floyd-Warshall Algorithm
• The Floyd-Warshall algorithm works based on
a property of intermediate vertices of a
shortest path.
• An intermediate vertex for a path
– p = <v1, v2, ..., vj>
– is any vertex other than v1 or vj.
Procedure
• If the vertices of a graph G are indexed by {1, 2, ..., n}, then
• consider a subset of vertices {1, 2, ..., k}.
• Assume p is a minimum weight path from vertex i to
vertex j whose intermediate vertices are drawn from the
subset {1, 2, ..., k}.
• If we consider vertex k on the path then either:
– k is not an intermediate vertex of p
– all intermediate vertices are in {1, 2, ..., k-1}
• else
– k is an intermediate vertex of p
– Divide p at k giving two subpaths p1 and p2 giving vi ↝ k ↝ vj
Procedure
• Define a quantity d(k)ij as the minimum weight
of the path from vertex i to vertex j with
intermediate vertices drawn from the set {1,
2, ..., k}
• The above properties give the following
recursive solution:
Predecessor Matrix (П)
• П is a sequence of matrices П(0), П(1), …,П(r)
• where П=П(r) and Пij(k) defined to be the
predecessor of vertex j on a shortest path
from vertex i with all intermediate vertices in
the set {1,2, …, k}.
Predecessor Matrix
• Recursive formulation of Пij(k) when k = 0,

• When k ≥ 1,
FLOYD-WARSHALL(W) Algorithm
1.n = W.rows
2. D(0) = W
3. Π(0) = π(0)ij = NIL if i=j or wij = ∞
4. Π(0) = π(0)ij = i if i≠j and wij < ∞
5. for k = 1 to n
6. let D(k) = (d(k)ij) be a new nxn
matrix
7. for i = 1 to n
8. for j = 1 to n
9. dkij = min(d(k-1)ij,
d(k-1)ik + d(k-1)kj)
10. if d(k-1)ij ≤ d(k-1)ik +
d(k-1)kj
11. π(k)ij = π(k-1)ij
Example
Example
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
Initialization (k=0)
Iteration 1: k = 1

Shorter paths from 2 ↝ 3 and 2 ↝ 4 are found through


vertex 1
Iteration 2: k = 2

Shorter paths from 4 ↝ 1, 5 ↝ 1, and 5 ↝ 3 are found through


vertex 2
Iteration 2: k = 3

No shorter paths are found through vertex 3


Iteration 2: k = 4

Shorter paths from the vertex 4 are:


1 ↝ 2, 1 ↝ 3, 2 ↝ 3, 3 ↝ 1, 3 ↝ 2, 5 ↝ 1, 5 ↝ 2, 5 ↝ 3, and
5↝4
Iteration 2: k = 5

No shorter paths are found through vertex 5


Shortest Path

You might also like