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

lecture_graph_greedy

The document provides an overview of non-linear data structures, specifically focusing on graph traversal techniques such as Breadth First Search (BFS) and Depth First Search (DFS). It outlines the procedures for each traversal method, including their algorithms and examples, as well as discusses shortest path problems in graphs. The content is intended for a Computer Science and Engineering course at Jaypee Institute of Information Technology, covering essential graph algorithms and their applications.

Uploaded by

9922103035
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)
2 views

lecture_graph_greedy

The document provides an overview of non-linear data structures, specifically focusing on graph traversal techniques such as Breadth First Search (BFS) and Depth First Search (DFS). It outlines the procedures for each traversal method, including their algorithms and examples, as well as discusses shortest path problems in graphs. The content is intended for a Computer Science and Engineering course at Jaypee Institute of Information Technology, covering essential graph algorithms and their applications.

Uploaded by

9922103035
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/ 71

Data Structures (15B11CI311)

Odd Semester 2020

3rd Semester , Computer Science and Engineering


Jaypee Institute Of Information Technology (JIIT), Noida

Data Structures (ODD SEM 2020)


Non Linear Data Structures : Graphs
Traversal and Graph Application Algorithms

Data Structures (ODD SEM 2020)


3

Graph Traversal and Graph Algorithms


• Traversal
Given G=(V,E) and vertex v, find all wV, such that w connects v.
• Breadth First Search (BFS)
level order tree traversal
• Depth First Search (DFS)
preorder tree traversal
• Shortest Path Finding
• Spanning Trees

Data Structures (ODD SEM 2020)


4

Breadth First Search (BFS)

• Breadth First Traversal (or Search) for a graph is similar to Breadth First
Traversal of a tree The only catch here is, unlike trees, graphs may
contain cycles, so we may come to the same node again. To avoid
processing a node more than once, we use a Boolean visited array. For
simplicity, it is assumed that all vertices are reachable from the starting
vertex.
• Breadth-first search is a way to find all the vertices reachable from a
given source vertex, s.
• As the name BFS suggests, you are required to traverse the graph
breadth wise : First move horizontally and visit all the nodes of the
current level then move to the next level

Data Structures (ODD SEM 2020)


5
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition (The MIT Press) 3rd
Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest , Clifford Stein

Breadth First Search (BFS) : Search Procedure

1. Start at a given vertex s, which is at level 0.


2. Visit all the vertices that are at the distance of one edge away(adjacent), paint them
as "visited," vertices and placed into level 1.
3. Visit all the new vertices we can reach at the distance of two edges away from the
source vertex s. These new vertices, which are adjacent to level 1 vertices and not
previously assigned to a level, are placed into level 2, and so on.
4. The BFS traversal terminates when every vertex has been visited.
The state of a vertex, u, is stored in a color variable as follows:
color[u] = White - for the "undiscovered" state,
color [u] = Gray - for the "discovered but not fully explored" state, and
color [u] = Black - for the "fully explored" state.

Data Structures (ODD SEM 2020)


Source for Example and algorithms :Introduction to Algorithms, 3rd Edition (The MIT
Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest , Clifford
Stein
6

Breadth-First Search Traversal : Algorithm


BFS(V, E, s) // develops a breadth-first search tree with the source vertex, s, as its root.
1. for each u in V − {s} ▷ for each vertex u in V[G] except s.
2. do color[u] ← WHITE
3. d[u] ← infinity
4. π[u] ← NIL
5. color[s] ← GRAY ▷ Source vertex discovered
6. d[s] ← 0 ▷ initialize
7. π[s] ← NIL ▷ initialize
8. Q ← {} ▷ Clear queue Q
9. ENQUEUE(Q, s)
10 while Q is non-empty
11. do u ← DEQUEUE(Q) ▷ That is, u = head[Q]
12. for each v adjacent to u ▷ for loop for every node along with edge.
13. do if color[v] ← WHITE ▷ if color is white you've never seen it before
14. then color[v] ← GRAY
15. d[v] ← d[u] + 1
16. π[v] ← u
17. ENQUEUE(Q, v)
18. DEQUUE(Q)
19. color[u] ← BLACK

Data Structures (ODD SEM 2020)


Source for Example and algorithms :Introduction to Algorithms, 3rd Edition (The MIT Press) 3rd Edition
by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest , Clifford Stein 7

Breadth-First Search Traversal : Example

The following figure illustrates the progress of breadth-first search on the


undirected sample graph.
a. After initialization (paint every vertex white, set d[u] to infinity for each
vertex u, and set the parent of every vertex to be NIL), the source vertex is
discovered in line 5. Lines 8-9 initialize Q to contain just the source vertex s.

Data Structures (ODD SEM 2020)


Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
8
onald L. Rivest , Clifford Stein

Breadth-First Search Traversal : Example


Cond…
b. The algorithm discovers all vertices 1 edge from s i.e., discovered all
vertices (w and r) at level 1.

c.

Data Structures (ODD SEM 2020)


Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
9
onald L. Rivest , Clifford Stein

Breadth-First Search Traversal : Example


Cond…
d. The algorithm discovers all vertices 2 edges from s i.e., discovered all
vertices (t, x, and v) at level 2.

e.

Data Structures (ODD SEM 2020)


Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
10
onald L. Rivest , Clifford Stein

Breadth-First Search Traversal : Example


Cond…
f.

g. The algorithm discovers all vertices 3 edges from s i.e., discovered all
vertices (u and y) at level 3.

Data Structures (ODD SEM 2020)


Source for Example and algorithms :Introduction to Algorithms, 3rd Edition (The MIT
Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest , Clifford
Stein
11

Breadth-First Search Traversal : Example


Cond…

h.

i. The algorithm terminates when every vertex has been fully explored.

Data Structures (ODD SEM 2020)


Source for Example and algorithms :Introduction to Algorithms, 3rd Edition (The MIT
Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest , Clifford
Stein
12

Depth First Search (DFS)

Depth First Traversal (or Search) for a graph is similar to Depth First
Traversal of a tree. The only catch here is, unlike trees, graphs may contain
cycles, a node may be visited twice. To avoid processing a node more than
once, use a Boolean visited array.
Like BFS, to keep track of progress depth-first-search colors each vertex.
Each vertex of the graph is in one of three states:
• Undiscovered
• Discovered but not finished (not done exploring from it); and
• Finished (have found everything reachable from it) i.e. fully explored.

Data Structures (ODD SEM 2020)


Source for Example and algorithms :Introduction to Algorithms, 3rd Edition (The MIT
Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest ,
Clifford Stein
13

Depth First Search (DFS) : Search Procedure

Depth-first search is an algorithm for traversing or searching tree or


graph data structures. The algorithm starts at the root node (selecting
some arbitrary node as the root node in the case of a graph) and explores
as far as possible along each branch before backtracking.
So the basic idea is to start from the root or any arbitrary node and mark
the node and move to the adjacent unmarked node and continue this
loop until there is no unmarked adjacent node. Then backtrack and check
for other unmarked nodes and traverse them. Finally print the nodes in
the path.

Data Structures (ODD SEM 2020)


Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
14
onald L. Rivest , Clifford Stein

Depth First Search (DFS) : Search Procedure Cond


…The state of a vertex, u, is stored in a color variable as follows:
1. color[u] = White - for the "undiscovered" state,
2. color[u] = Gray - for the "discovered but not finished" state, and
3. color[u] = Black - for the "finished" state.
Like BFS, depth-first search uses π[v] to record the parent of vertex v.
We have π[v] = NIL if and only if vertex v is the root of a depth-first tree.
DFS time-stamps each vertex when its color is changed.
1. When vertex v is changed from white to gray the time is recorded in d[v].
2. When vertex v is changed from gray to black the time is recorded in f[v].
The discovery and the finish times are unique integers, where for each vertex the finish
time is always after the discovery time. That is, each time-stamp is an unique integer in
the range of 1 to 2|V| and for each vertex v, d[v] < f[v]. In other words, the following
inequalities hold:
1 ≤ d[v] < f[v] ≤ 2|V|

Data Structures (ODD SEM 2020)


Source for Example and algorithms :Introduction to Algorithms, 3rd Edition (The MIT
Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest , Clifford
Stein
15

Depth-First Search Traversal : Algorithm

The DFS forms a depth-first forest comprised of more than one depth-first
trees. Each tree is made of edges (u, v) such that u is gray and v is white when
edge (u, v) is explored. The following pseudocode for DFS uses a global
timestamp time.
DFS (V, E)
1. for each vertex u in V[G]
2. do color[u] ← WHITE
3. π[u] ← NIL
4. time ← 0
5. for each vertex u in V[G]
6. do if color[u] ← WHITE
7. then DFS-Visit(u)

Data Structures (ODD SEM 2020)


Source for Example and algorithms :Introduction to Algorithms, 3rd Edition (The MIT
Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest ,
Clifford Stein
16

Depth-First Search Traversal : Algorithm


Cond…
DFS-Visit(u)
1. color[u] ← GRAY ▷ discover u
2. time ← time + 1
3. d[u] ← time
4. for each vertex v adjacent to u ▷ explore (u, v)
5. do if color[v] ← WHITE
6. then π[v] ← u
7. DFS-Visit(v)
8. color[u] ← BLACK
9. time ← time + 1
10. f[u] ← time ▷ we are done with u

Data Structures (ODD SEM 2020)


Source for Example and algorithms :Introduction to Algorithms, 3rd Edition (The MIT
Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest , Clifford 17
Stein

Example
In the following figure, the solid edge represents discovery or tree edge and the
dashed edge shows the back edge. Furthermore, each vertex has two time stamps:
the first time-stamp records when vertex is first discovered and second time-stamp
records when the search finishes examining adjacency list of vertex.

Data Structures (ODD SEM 2020)


Source for Example and algorithms :Introduction to Algorithms, 3rd Edition (The MIT
Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest , Clifford
Stein
18

Example Cond…

Data Structures (ODD SEM 2020)


Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
19
onald L. Rivest , Clifford Stein

Shortest Path Problems


• What is shortest path ?
 shortest length between two vertices for an unweighted graph:
 smallest cost between two vertices for a weighted graph:

B 210 B
A A
450
60 190
unweighted
graph
C C weighted
200 graph
130
D D
E E

Data Structures (ODD SEM 2020)


Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
20
onald L. Rivest , Clifford Stein

Shortest Path Problems Cond…

• How can we find the shortest route between two points on a map?
• Model the problem as a graph problem:
• Road map is a weighted graph:
vertices = cities
edges = road segments between cities
edge weights = road distances
• Goal: find a shortest path between two vertices (cities)

Data Structures (ODD SEM 2020)


21
Shortest Path Problems Cond…
• Input: t x
6
3 9
• Directed graph G = (V, E)
3
• Weight function w : E → R 4
2 1
s 0
2 7
• Weight of path p = v0, v1, . . . , vk 5 3
5 11
k 6
y z
w( p )  w( vi  1 , vi )
i 1

• Shortest-path weight
p from u to v:

δ(u, v) = min w(p) : u v if there exists a path from u to v

∞ otherwise

• Shortest path u to v is any path p such that w(p) = δ(u, v)


Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
22
onald L. Rivest , Clifford Stein

Variants of Shortest Paths

• Single-source shortest path


• G = (V, E)  find a shortest path from a given source vertex s to each vertex v  V

• Single-destination shortest path


• Find a shortest path to a given destination vertex t from each vertex v
• Reverse the direction of each edge  single-source

• Single-pair shortest path


• Find a shortest path from u to v for given vertices u and v
• Solve the single-source problem

• All-pairs shortest-paths
• Find a shortest path from u to v for every pair of vertices u and v

Data Structures (ODD SEM 2020)


Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
23
onald L. Rivest , Clifford Stein

Optimal Substructure of Shortest Paths

Given: vj
pij pjk
• A weighted, directed graph G = (V, E) v1
p1i
• A weight function w: E  R, pij’ vk

• A shortest path p = v1, v2, . . . , vk from v1 to vk vi

• A subpath of p: pij = vi, vi+1, . . . , vj, with 1  i  j  k

Then: pij is a shortest path from vi to vj

Data Structures (ODD SEM 2020)


Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
24
onald L. Rivest , Clifford Stein

Shortest-Path Representation

For each vertex v  V: t x


6
3 9
• d[v] = δ(s, v): a shortest-path estimate 3
4
• Initially, d[v]= ∞ 2 1
s 0
2 7
• Reduces as algorithms progress
5 3
• [v] = predecessor of v on a shortest path from s 5 11
6
• If no predecessor, [v] = NIL y z
•  induces a tree—shortest-path tree
• Shortest paths & shortest path trees are not unique

Data Structures (ODD SEM 2020)


Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
25
onald L. Rivest , Clifford Stein

Initialization

Alg.: INITIALIZE-SINGLE-SOURCE(V, s)
1. for each v  V
2. do d[v] ← 
3. [v] ← NIL
4. d[s] ← 0

All the shortest-paths algorithms start with INITIALIZE-SINGLE-SOURCE

Data Structures (ODD SEM 2020)


Source for Example and algorithms :Introduction to Algorithms, 3rd Edition (The MIT Press) 3rd
Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest , Clifford Stein 26

Relaxation
• Relaxing an edge (u, v) = testing whether we can improve the shortest path to v
found so far by going through u
If d[v] > d[u] + w(u, v)
we can improve the shortest path to v
 update d[v] and [v]
s s
u v u v
2 2
5 9 5 6 After relaxation:
d[v]  d[u] + w(u,
RELAX(u, v, w) v)
RELAX(u, v, w)
u v u v
2 2
5 7 5 6

Data Structures (ODD SEM 2020)


Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
27
onald L. Rivest , Clifford Stein

RELAX(u, v, w)

1. if d[v] > d[u] + w(u, v)


2. then d[v] ← d[u] + w(u, v)
3. [v] ← u

• All the single-source shortest-paths algorithms


• start by calling INIT-SINGLE-SOURCE
• then relax edges

• The algorithms differ in the order and how many times they relax each edge

Data Structures (ODD SEM 2020)


Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
28
onald L. Rivest , Clifford Stein

Dijkstra’s Algorithm
• Single-source shortest path problem:
• No negative-weight edges: w(u, v) > 0  (u, v)  E

• Maintains two sets of vertices:


• S = vertices whose final shortest-path weights have already been
determined
• Q = vertices in V – S: min-priority queue
• Keys in Q are estimates of shortest-path weights (d[v])

• Repeatedly select a vertex u  V – S, with the minimum shortest-path estimate


d[v]

Data Structures (ODD SEM 2020)


Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
29
onald L. Rivest , Clifford Stein

Dijkstra (G, w, s) t x
1
1. INITIALIZE-SINGLE-SOURCE(V, s)  
10 9
2. S←  2 4
s 3 6
0
3. Q ← V[G]
5 7
4. while Q   
2

y z
5. do u ← EXTRACT-MIN(Q) t x
1
6. S ← S  {u} 
10 
10 9
7. for each vertex v  Adj[u] 2 3 4 6
s 0
8. do RELAX(u, v, w) 5 7

5 
2
y z

Data Structures (ODD SEM 2020)


Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
30
onald L. Rivest , Clifford Stein

Example
t x t 1 x
1
8
10 14
 8 13
14
10 9 10 9
2 3 4 6 2 3 4 6
s 0 s 0
5 7 5 7
5 7
 5 7
2 2
y z y z

t x t 1 x
1
8 13
9 8 9
10 9 10 9

2 4 2 3 4 6
s 0 3 6 s 0

7 5 7
5
5 7 5 7
2 2
y z y z

Data Structures (ODD SEM 2020)


Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
onald L. Rivest , Clifford Stein

Dijkstra's Shortest Path Algorithm

• Find shortest path from s to t.

24
2 3
9

s
18
14
2 6
6
30 4 19
11
15 5
5
6
20 16

t
7 44

31 Data Structures (ODD SEM 2020)


Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
onald L. Rivest , Clifford Stein

Dijkstra's Shortest Path Algorithm

S={ }
PQ = { s, 2, 3, 4, 5, 6, 7, t } 

24
2 3
0 9

s
18
14  2 6
6 
30  4 19
11
15 5
5
6
20 16

t
7 44
distance label  
32 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
onald L. Rivest , Clifford Stein

Dijkstra's Shortest Path Algorithm


S={ }
PQ = { s, 2, 3, 4, 5, 6, 7, t }
delmin


24
2 3
0 9

s
18
14  2 6
6 
30  4 19
11
15 5
5
6
20 16

t
7 44
distance label  
33 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
onald L. Rivest , Clifford Stein

Dijkstra's Shortest Path Algorithm

decrease key S={s}


PQ = { 2, 3, 4, 5, 6, 7, t } 
 9
X
24
2 3
0 9

s
18
14 X
 14 6
2
6 
30  4 19
11
15 5
5
6
20 16

t
7 44
distance label  15
X 
34 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
onald L. Rivest , Clifford Stein

Dijkstra's Shortest Path Algorithm


S={s}
PQ = { 2, 3, 4, 5, 6, 7, t }
delmin

 9
X
24
2 3
0 9

s
18
14 X
 14 6
2
6 
30  4 19
11
15 5
5
6
20 16

t
7 44
distance label  15
X 
35 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
onald L. Rivest , Clifford Stein

Dijkstra's Shortest Path Algorithm


S = { s, 2 }
PQ = { 3, 4, 5, 6, 7, t }

 9
X
24
2 3
0 9

s
18
14 X
 14 6
2
6 
30  4 19
11
15 5
5
6
20 16

t
7 44

 15
X 
36 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
onald L. Rivest , Clifford Stein

Dijkstra's Shortest Path Algorithm


S = { s, 2 }
PQ = { 3, 4, 5, 6, 7, t } decrease key

X 33
 9
X
24
2 3
0 9

s
18
14 X
 14 6
2
6 
30  4 19
11
15 5
5
6
20 16

t
7 44

 15
X 
37 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
onald L. Rivest , Clifford Stein

Dijkstra's Shortest Path Algorithm


S = { s, 2 }
PQ = { 3, 4, 5, 6, 7, t }

X 33
 9
X
24
2 3
0 9
delmin
s
18
14 X
 14 6
2
6 
30  4 19
11
15 5
5
6
20 16

t
7 44

 15
X 
38 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
onald L. Rivest , Clifford Stein

Dijkstra's Shortest Path Algorithm


S = { s, 2, 6 }
PQ = { 3, 4, 5, 7, t }
32
X 33
X
 9
X
24
2 3
0 9

s
18
14 X
 14 6
2
6 
44
30 X
 4 19
11
15 5
5
6
20 16

t
7 44

 15
X 
39 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
onald L. Rivest , Clifford Stein

Dijkstra's Shortest Path Algorithm


S = { s, 2, 6, 7 }
PQ = { 3, 4, 5, t }
32
X 33
X
 9
X
24
2 3
0 9

s
18
14 X
 14 6
2
6 
44
30 X
 4 19
11
15 5
5
6
20 16

t
7 44

 15
X delmin 
40 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
onald L. Rivest , Clifford Stein

Dijkstra's Shortest Path Algorithm


S = { s, 2, 6, 7 }
PQ = { 3, 4, 5, t }
32
X 33
X
 9
X
24
2 3
0 9

s
18
14 X
 14 6
2
6 
X 35
44
30 X
 4 19
11
15 5
5
6
20 16

t
7 44

 15
X 
59 X
41 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
onald L. Rivest , Clifford Stein

Dijkstra's Shortest Path Algorithm


delmin
S = { s, 2, 6, 7 }
PQ = { 3, 4, 5, t }
32
X 33
X
 9
X
24
2 3
0 9

s
18
14 X
 14 6
2
6 
X 35
44
30 X
 4 19
11
15 5
5
6
20 16

t
7 44

 15
X 
59 X
42 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition (The MIT
Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest , Clifford
Stein

Dijkstra's Shortest Path Algorithm


S = { s, 2, 3, 6, 7 }
PQ = { 4, 5, t }
32
X 33
X
 9
X
24
2 3
0 9

s
18
14 X
 14 6
2
6 
X 35
44 X 34
30 X
 4 19
11
15 5
5
6
20 16

t
7 44

 15
X 51 59
X X
43 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition (The MIT
Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest , Clifford
Stein

Dijkstra's Shortest Path Algorithm


S = { s, 2, 3, 6, 7 }
PQ = { 4, 5, t } 32
X 33
X
 9
X
24
2 3
0 9

s
18
14 X
 14 6
2
6 
X 35
44 X 34
30 X
 4 19
11
15 5
5
6
20 16
delmin

t
7 44

 15
X 51 59
X X
44 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition (The MIT
Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest , Clifford
Stein

Dijkstra's Shortest Path Algorithm


S = { s, 2, 3, 5, 6, 7 }
PQ = { 4, t }
32
X 33
X
 9
X
24
2 3
0 9

s
18
14 X
 14 6
2
6 
45 X
X 35
44 X 34
30 X
 4 19
11
15 5
5
6
20 16

t
7 44

 15
X 50 51
X 59 
X X
45 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition (The MIT
Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest , Clifford
Stein

Dijkstra's Shortest Path Algorithm


S = { s, 2, 3, 5, 6, 7 }
PQ = { 4, t }
32
X 33
X
 9
X
24
2 3
0 9

s
18
14 X
 14 6
2
6 
45 X
X 35
44 X 34
30 X
 4 19
11
15 5 delmin
5
6
20 16

t
7 44

 15
X 50 51
X 59 
X X
46 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
onald L. Rivest , Clifford Stein

Dijkstra's Shortest Path Algorithm


S = { s, 2, 3, 4, 5, 6, 7 }
PQ = { t }
32
X 33
X
 9
X
24
2 3
0 9

s
18
14 X
 14 6
2
6 
45 X
X 35
44 X 34
30 X
 4 19
11
15 5
5
6
20 16

t
7 44

 15
X 50 51
X 59 
X X
47 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
onald L. Rivest , Clifford Stein

Dijkstra's Shortest Path Algorithm


S = { s, 2, 3, 4, 5, 6, 7 }
PQ = { t }
32
X 33
X
 9
X
24
2 3
0 9

s
18
14 X
 14 6
2
6 
45 X
X 35
44 X 34
30 X
 4 19
11
15 5
5
6
20 16

t
7 44

 15
X
delmin 50 51
X 59 
X X
48 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition (The MIT Press) 3rd
Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest , Clifford Stein

Dijkstra's Shortest Path Algorithm


S = { s, 2, 3, 4, 5, 6, 7, t }
PQ = { }
32
X 33
X
 9
X
24
2 3
0 9

s
18
14 X
 14 6
2
6 
45 X
X 35
44 X 34
30 X
 4 19
11
15 5
5
6
20 16

t
7 44

 15
X 50 51
X 59 
X X
49 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition (The MIT Press) 3rd
Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest , Clifford Stein

Dijkstra's Shortest Path Algorithm


S = { s, 2, 3, 4, 5, 6, 7, t }
PQ = { }
32
X 33
X
 9
X
24
2 3
0 9

s
18
14 X
 14 6
2
6 
45 X
X 35
44 X 34
30 X
 4 19
11
15 5
5
6
20 16

t
7 44

 15
X 50 51
X 59 
X X
50 Data Structures (ODD SEM 2020)
Source for Example and algorithms :Introduction to Algorithms, 3rd Edition (The MIT
Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest , Clifford
Stein
51

Dijkstra (G, w, s)
1. INITIALIZE-SINGLE-SOURCE(V, s)
(V)
2. S← 
3. Q ← V[G] O(V) build min-heap
4. while Q   Executed O(V) times
5. do u ← EXTRACT-MIN(Q) O(lgV)
6. S ← S  {u}
7. for each vertex v  Adj[u]
8. do RELAX(u, v, w)
Running time: O(VlgV + ElgV) = O(ElgV) O(E) times; O(lgV)

Data Structures (ODD SEM 2020)


Source for Example and algorithms :Introduction to Algorithms, 3rd Edition
(The MIT Press) 3rd Edition by Thomas H. Cormen, Charles E. Leiserson, R
52
onald L. Rivest , Clifford Stein

Dijkstra’s Running Time


• Extract-Min executed |V| time
• Decrease-Key executed |E| time
• Time = |V| TExtract-Min + |E| TDecrease-Key
• T depends on different Q implementations

Q T(Extract- T(Decrease- Total


Min) Key)
array O(V) O(1) O(V 2)
binary heap O(lg V) O(lg V) O(E lg V)
Fibonacci heap O(lg V) O(1) (amort.) O(V lgV + E)

Data Structures (ODD SEM 2020)


53

Applications of Dijkstra's Algorithm


- Traffic Information Systems are most prominent use

- Mapping (Map Quest, Google Maps)

- Routing Systems

Data Structures (ODD SEM 2020)


54

Spanning Tree
• Spanning Trees: A sub graph T of a undirected graph G= {V,E } is a
spanning tree if it is a tree and contains every vertex of G.

Data Structures (ODD SEM 2020)


55

Minimum Spanning Tree (MST)


• A Minimum Spanning Tree in an undirected connected weighted graph
is a spanning tree of minimum weight (among all spanning trees).

Remark: The minimum spanning tree may not be unique. However, if the weights
of all the edges are pairwise distinct, it is indeed unique.

Data Structures (ODD SEM 2020)


56

MST Problem

• MST Problem: Given a connected weighted undirected graph G , design


an algorithm that outputs a minimum spanning tree (MST) of G.
• Generic approach: A tree is an acyclic graph. The idea is to start with
an empty graph and try to add edges one at a time, always making
sure that what is built remains acyclic. And if we are sure every time
the resulting graph always is a subset of some minimum spanning tree,
we are done.

Data Structures (ODD SEM 2020)


57

Generic MST
• Let A be a set of edges such that A ⊆ T, where T is a MST. An edge (u,v) is a safe edge
for A, if A ∪ {(u,v)} is also a subset of some MST. If at each step, we can find a safe
edge , we can ’grow’ a MST. This leads to the following generic approach:

• Generic-MST(G, w) :
Let A=EMPTY;
while A does not form a spanning tree find an edge (u, v) that is safe for A add (u, v).
return A
* Safe edge :
Let G = (V,E) be a connected, undirected graph with a real-valued weight function w
defined on E . Let A be a subset of E that is included in some minimum spanning tree for
G, let (S, S-V) be any cut of G that respects A , and let (u,v) be a light edge crossing
the cut (S,S-V). Then, edge (u,v) is safe for A.

Data Structures (ODD SEM 2020)


58

Prim’s Algorithm

The generic algorithm gives us an idea how to ’grow’ a MST. However


the choice of a cut (and hence the corresponding light edge) in each
iteration is immaterial. We can select any cut (that respects the
selected edges) and find the light edge crossing that cut to proceed.
The Prim’s algorithm makes a nature choice of the cut in each iteration
– it grows a single tree and adds a light edge in each iteration.

Data Structures (ODD SEM 2020)


59

Prim’s Algorithm Cond…

Grow a Tree
• Start by picking any vertex r to be the root of the tree.
• While the tree does not contain all vertices in the graph find shortest edge
leaving the tree and add it to the tree .

Running time is O((|V|+|E|)log|V|)

Data Structures (ODD SEM 2020)


60

Prim’s Algorithm Cond…


The idea:
expand the current tree by adding the lightest (shortest) edge leaving it
and its endpoints.

• Step 0: Choose any element r ; set S = {r} and A = 

(Take r as the root of our spanning tree.)


• Step 1: Find a lightest edge such that one endpoint is in S and the other is in V\S .
Add this edge to A and its (other) endpoint to S.
• Step 2: If V\S =  , then stop & output (minimum) spanning tree (S,A) . Otherwise
go to Step 1.

Data Structures (ODD SEM 2020)


61

Prim’s Algorithm Cond…

Data Structures (ODD SEM 2020)


62

Prim’s Algorithm : Example Cond…

Data Structures (ODD SEM 2020)


63

Prim’s Algorithm : Example Cond…

Data Structures (ODD SEM 2020)


64

Prim’s Algorithm : Example Cond…

Data Structures (ODD SEM 2020)


65

Prim’s Algorithm : Example Cond…

Data Structures (ODD SEM 2020)


66

Analysis of Prim’s Algorithm

Let n = |V| and e = |E|.

1. O(log n) to extract each vertex from the queue.


Done once for each vertex = O(n log n).

2. O(log n) to decrease the key value of neighboring vertex.


Done at most once for each edge = O(e log n)

Total cost = O ( (n+e) log n)

Data Structures (ODD SEM 2020)


67

Kruskal’s Algorithm

The Kruskal’s Algorithm is based directly on the generic algorithm.


Unlike Prim’s algorithm, we make a different choices of cuts. Initially,
trees of the forest are the vertices (no edges). In each step add the
cheapest edge that does not create a cycle. Observe that unlike Prim’s
algorithm, which only grows one tree, Kruskal’s algorithm grows a
collection of trees (a forest). Continue until the forest ’merge to’ a
single tree. This is a minimum spanning tree

Data Structures (ODD SEM 2020)


68

Outline of Algorithm by Example

Data Structures (ODD SEM 2020)


69

Kruskal’s Algorithm : Key Concepts

Question: How does algorithm choose edge e  F with minimum weight?


Answer: Start by sorting edges in E in order of increasing weight. Walk through the edges in
this order. (Once edge causes a cycle it will always cause a cycle so it can be thrown away
Question : How to check for cycles ?
Answer :Observation: At each step of the outlined algorithm, (V,A) is acyclic so it is a forest. If
u and v are in the same tree, then adding edge (u,v) to A creates a cycle. If u and v are not
in the same tree, then adding edge (u,v) to A does not create a cycle.
Question: How to test whether u and v are in the same tree?
Answer: Use a disjoint-set data structure Vertices in a tree are considered to be in same set.
Test if Find-Set(u) = Find-Set(v)?

Data Structures (ODD SEM 2020)


70

Kruskal’s Algorithm : the details

Remark: With a proper implementation of


UNION-FIND, Kruskal’s algorithm has
running time O(|E|log|E|

Data Structures (ODD SEM 2020)


71

References

1. Introduction to Algorithms, 3rd Edition (The MIT Press) 3rd


Edition
by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest ,
Clifford Stein
(Presented Examples and Pseudo codes are according to ref. 1)

Data Structures (ODD SEM 2020)

You might also like