0% found this document useful (0 votes)
16 views28 pages

ds-12 13

The document discusses shortest paths in graphs, specifically focusing on Dijkstra's algorithm for finding single-source shortest paths. It outlines the algorithm's steps, including maintaining a set of known vertices and updating distance estimates. Additionally, it covers spanning trees and minimum spanning trees, introducing Kruskal's and Prim's algorithms for obtaining the minimum spanning tree.

Uploaded by

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

ds-12 13

The document discusses shortest paths in graphs, specifically focusing on Dijkstra's algorithm for finding single-source shortest paths. It outlines the algorithm's steps, including maintaining a set of known vertices and updating distance estimates. Additionally, it covers spanning trees and minimum spanning trees, introducing Kruskal's and Prim's algorithms for obtaining the minimum spanning tree.

Uploaded by

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

Lecture

Graphs
Course Instructor
Engr. Anum Raza
Shortest paths
A shortest path from u to v is a path of
minimum weight from u to v. The shortest-
path weight from u to v is defined as
(u, v) = min{w(p) : p is a path from u to v}.

Note: (u, v) =  if no path from u to v exists.


Single-source shortest paths
Problem. From a given source vertex s  V, find
the shortest-path weights (s, v) for all v  V.
IDEA:
1. Maintain a set S of vertices whose shortest-
path distances from s are known.
2. At each step add to S the vertex v  V – S
whose distance estimate from s is minimal.
3. Update the distance estimates of vertices
adjacent to v.
Dijkstra's algorithm-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 d[v] ←d[u] + w(u, v) (set new value of shortest path)
(if desired, add traceback code)
return dist
Execution of Dijkstra’s algorithm
2 2 ✓
1 3 1 1 3 1
6 6 ✓
5 2 5 2
3 3
1 4 1 4✓ 2
2
3 3
2 2
✓ 4 5 4 5

Iteratio N D2 D3 D4 D5 D6
n ✓
Initial {1} 3✓ 2 5  
1 {1,3} 3 2 4  ✓
3
2 {1,2,3} 3 2 4✓ 7 3
3 {1,2,3,6} 3 2 4 5✓ 3
4 {1,2,3,4,6} 3 2 4 5 3
5 {1,2,3,4,5,6} 3 2 4 5 3 5
Shortest2 Paths in Dijkstra’s2 Algorithm
1 3 1 1 3 1
6 6
5 2 5 2
3 3
1 4 2 1 4 2
3 3
2 2
4 5 4 5
2 3 1 2 3 1
1 1
6 6
5 2 5 2
3 3
1 4 2 1 4 2
3 3
2 2
4 5 4 5

2 3 1 2 3 1
1 1
6 6
5 2 5 2
3 3
1 4 2 1 4 2
3 3
2 2
4 5 4 5
6
Spanning Trees
A spanning tree of a graph is just a subgraph that
contains all the vertices and is a tree.
A graph may have many spanning trees.

Graph A Some Spanning Trees from Graph A

or or or
Complete Graph All 16 of its Spanning Trees
Minimum Spanning Trees
The Minimum Spanning Tree for a given graph is the
Spanning Tree of minimum cost for that graph.
Complete Graph Minimum Spanning Tree
7

2 2
5 3 3

1 1
Algorithms for Obtaining the
Minimum Spanning Tree
 Kruskal's Algorithm

 Prim's Algorithm
Kruskal's Algorithm
This algorithm creates a forest of trees. Initially the forest
consists of n single node trees (and no edges). At each
step, we add one edge (the cheapest one) so that it joins
two trees together. If it were to form a cycle, it would
simply link two nodes that were already part of a single
connected tree, so that this edge would not be needed.
The steps are:

1. The forest is constructed - with each node in a separate


tree.
2. The edges are placed in a priority queue.
3. Until we've added n-1 edges,
1. Extract the cheapest edge from the queue,
2. If it forms a cycle, reject it,
3. Else add it to the forest. Adding it to the forest will join
two trees together.

Every step will have joined two trees in the forest together, so
that at the end, there will only be one tree in T.
Complete Graph

B 4 C
4
2 1

A 4 E
1 F

D 2 3
10
G 5

5 6 3

4
I
H

2 3
J
A 4 B A 1 D

B 4 C B 4 D

B 4 C B 10 J C 2 E
4
2 1
C 1 F D 5 H
A 4 E
1 F

2 D 6 J E 2 G
D 3
10
G 5
F 3 G F 5 I
5 6 3

4
I G 3 I G 4 J
H

2 3
J H 2 J I 3 J
Sort Edges A 1 D C 1 F

(in reality they are placed in a priority


queue - not sorted - but sorting them C 2 E E 2 G
makes the algorithm easier to visualize)

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J
Add Edge A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J
Add Edge A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J
Add Edge A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J
Add Edge A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J
Add Edge A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J
Cycle A 1 D C 1 F
Don’t Add Edge

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J
Add Edge A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J
Add Edge A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J
Add Edge A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J
Cycle A 1 D C 1 F
Don’t Add Edge

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J
Add Edge A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J
Minimum Spanning Tree Complete Graph

B 4 C 4
B C
4 4
2 1 2 1
A E A 4
1 F E F
1

D 2 2
D 3
10
G G 5

3 5 6 3

4
I I
H H
2 3 3
J 2 J

You might also like