SlideShare a Scribd company logo
Graphs > Discrete structures , Data Structures & Algorithums
Minimum Spanning Tree:
Prim's Algorithm
Prim's algorithm for finding an MST is a greedy

algorithm.
Start by selecting an arbitrary vertex, include it into
the current MST.
Grow the current MST by inserting into it the vertex
closest to one of the vertices already in current MST.
Minimum Spanning Tree
Problem: given a connected, undirected, weighted

graph, find a spanning tree using edges that minimize
the total weight
6

4
5

9

14

2

10

15
3

8
Prim’s Algorithm
MST-Prim(G, w, r)
Q = V[G];
for each u ∈ Q
key[u] = ∞;
key[r] = 0;
p[r] = NULL;
while (Q not empty)
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Prim’s Algorithm
MST-Prim(G, w, r)
6
Q = V[G];
5
for each u ∈ Q
key[u] = ∞;
key[r] = 0;
14
10
p[r] = NULL;
while (Q not empty)
u = ExtractMin(Q);
for each v ∈ Adj[u]
3
if (v ∈ Q and w(u,v) < key[v])
Run
p[v] = u;
key[v] = w(u,v);

4

9
2
15

8

on example graph
Prim’s Algorithm
MST-Prim(G, w, r)
∞
6
Q = V[G];
5
for each u ∈ Q
∞
key[u] = ∞;
key[r] = 0;
14
10
p[r] = NULL;
while (Q not empty)
∞
u = ExtractMin(Q);
for each v ∈ Adj[u]
3
∞
if (v ∈ Q and w(u,v) < key[v])
Run
p[v] = u;
key[v] = w(u,v);

4

∞

9

∞

2

∞

15

8

on example graph

∞
Prim’s Algorithm

MST-Prim(G, w, r)
∞
6
Q = V[G];
5
for each u ∈ Q
∞
key[u] = ∞;
key[r] = 0;
14
10
p[r] = NULL;
while (Q not empty)
0
r
u = ExtractMin(Q);
for each v ∈ Adj[u]
3
∞
if (v ∈ Q and w(u,v) < key[v])
Pick
p[v] = u;
key[v] = w(u,v);

4

∞

9

∞

2

∞
8

a start vertex r

15

∞
Prim’s Algorithm
MST-Prim(G, w, r)
∞
6
4
Q = V[G];
5
for each u ∈ Q
∞
∞
key[u] = ∞;
key[r] = 0;
14
2
10
p[r] = NULL;
while (Q not empty)
0
∞
u
u = ExtractMin(Q);
for each v ∈ Adj[u]
3
8
∞
if (v ∈ Q and w(u,v) < key[v])
Black vertices have been
p[v] = u;
key[v] = w(u,v);

9

15

∞

∞

removed from Q
Prim’s Algorithm

MST-Prim(G, w, r)
∞
6
4
Q = V[G];
5
for each u ∈ Q
∞
key[u] = ∞;
14
key[r] = 0;
10
p[r] = NULL;
0
u
while (Q not empty)
u = ExtractMin(Q);
3
8
3
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
Black arrows
key[v] = w(u,v);

∞

9

∞

2

∞

15

∞

indicate parent pointers
Prim’s Algorithm

6

∞

5
MST-Prim(G, w, r)
14
Q = V[G];
for each u ∈ Q
14
10
key[u] = ∞;
key[r] = 0;
0
u
p[r] = NULL;
3
3
while (Q not empty)
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);

4

∞

9

∞

2

∞
8

15

∞
Prim’s Algorithm

6

∞

5
MST-Prim(G, w, r)
14
Q = V[G];
for each u ∈ Q
14
10
key[u] = ∞;
key[r] = 0;
0
p[r] = NULL;
3
3
while (Q not empty)
u
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);

4

∞

9

∞

2

∞
8

15

∞
Prim’s Algorithm

6

∞

5
MST-Prim(G, w, r)
14
Q = V[G];
for each u ∈ Q
14
10
key[u] = ∞;
key[r] = 0;
0
p[r] = NULL;
3
3
while (Q not empty)
u
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);

4

∞

9

∞

2

8
8

15

∞
Prim’s Algorithm

6

∞

5
MST-Prim(G, w, r)
10
Q = V[G];
for each u ∈ Q
14
10
key[u] = ∞;
key[r] = 0;
0
p[r] = NULL;
3
3
while (Q not empty)
u
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);

4

∞

9

∞

2

8
8

15

∞
Prim’s Algorithm

6

∞

5
MST-Prim(G, w, r)
10
Q = V[G];
for each u ∈ Q
14
10
key[u] = ∞;
key[r] = 0;
0
p[r] = NULL;
3
3
while (Q not empty)
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);

4

∞

9

∞

2

8
8

u

15

∞
Prim’s Algorithm

6

∞

5
MST-Prim(G, w, r)
10
Q = V[G];
for each u ∈ Q
14
10
key[u] = ∞;
key[r] = 0;
0
p[r] = NULL;
3
3
while (Q not empty)
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);

4

9

2

∞

2

8
8

u

15

∞
Prim’s Algorithm

6

∞

5
MST-Prim(G, w, r)
10
Q = V[G];
for each u ∈ Q
14
10
key[u] = ∞;
key[r] = 0;
0
p[r] = NULL;
3
3
while (Q not empty)
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);

4

9

2

∞

2

8
8

u

15

15
Prim’s Algorithm

6

∞

5
MST-Prim(G, w, r)
10
Q = V[G];
for each u ∈ Q
14
10
key[u] = ∞;
key[r] = 0;
0
p[r] = NULL;
3
3
while (Q not empty)
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);

u

4

9

2

∞

2

8
8

15

15
Prim’s Algorithm

6

4

5
MST-Prim(G, w, r)
10
Q = V[G];
for each u ∈ Q
14
10
key[u] = ∞;
key[r] = 0;
0
p[r] = NULL;
3
3
while (Q not empty)
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);

u

4

9

2

∞

2

8
8

15

15
Prim’s Algorithm

6

4

5
MST-Prim(G, w, r)
5
Q = V[G];
for each u ∈ Q
14
10
key[u] = ∞;
key[r] = 0;
0
p[r] = NULL;
3
3
while (Q not empty)
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);

u

4

9

2

∞

2

8
8

15

15
Prim’s Algorithm

6

4

5
MST-Prim(G, w, r)
5
Q = V[G];
for each u ∈ Q
14
10
key[u] = ∞;
key[r] = 0;
0
p[r] = NULL;
3
3
while (Q not empty)
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);

u

4

9

2

9

2

8
8

15

15
Prim’s Algorithm

6

4

5
MST-Prim(G, w, r)
5
Q = V[G];
for each u ∈ Q
14
10
key[u] = ∞;
key[r] = 0;
0
p[r] = NULL;
3
3
while (Q not empty)
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);

4

u
9

2

9

2

8
8

15

15
Prim’s Algorithm
u

6

4

5
MST-Prim(G, w, r)
5
Q = V[G];
for each u ∈ Q
14
10
key[u] = ∞;
key[r] = 0;
0
p[r] = NULL;
3
3
while (Q not empty)
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);

4

9

2

9

2

8
8

15

15
Prim’s Algorithm

6

4

5
MST-Prim(G, w, r)
5
Q = V[G];
for each u ∈ Q
14
10
key[u] = ∞;
key[r] = 0;
0
p[r] = NULL;
3
3
while (Q not empty)
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);

u

4

9

2

9

2

8
8

15

15
Prim’s Algorithm

6

4

5
MST-Prim(G, w, r)
5
Q = V[G];
for each u ∈ Q
14
10
key[u] = ∞;
key[r] = 0;
0
p[r] = NULL;
3
3
while (Q not empty)
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);

4

9

2

u

2

8
8

9

15

15
Review: Prim’s Algorithm
MST-Prim(G, w, r)
Q = V[G];
for each u ∈ Q
key[u] = ∞;
key[r] = 0;
p[r] = NULL;
while (Q not empty)
u = ExtractMin(Q);
for each v ∈ Adj[u]
if (v ∈ Q and w(u,v) < key[v])
p[v] = u;
key[v] = w(u,v);
Dijkstra’s Algorithm
Similar to (BFS) Breadth-First Search
Grow a tree gradually, advancing from vertices taken from a

queue

Also similar to Prim’s algorithm for MST
Use a priority queue keyed on d[v]
Shortest Path for Weighted Graphs
Given a graph G

= (V, E) with edge costs
c(e), and a vertex s ∈ V, find the shortest
(lowest cost) path from s to every vertex in V

Assume: only positive edge costs
Dijkstra’s Algorithm for
Single Source Shortest Path
Similar to breadth-first search, but uses a heap

instead of a queue:

Always select (expand) the vertex that has a lowest-cost

path to the start vertex

Correctly handles the case where the lowest-cost

(shortest) path to a vertex is not the one with fewest
edges
Dijkstra, Edsger Wybe
Legendary figure in computer science; was
a professor at University of Texas.
Supported teaching introductory
computer courses without computers
(pencil and paper programming)
Supposedly wouldn’t (until very late in
life) read his e-mail; so, his staff had to
print out messages and put them in his
box.

E.W. Dijkstra (1930-2002)

1972 Turning Award Winner,
Programming Languages, semaphores, and …
Graphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & Algorithums
Final Table
Dijkstra’s Algorithm: Idea
Adapt BFS to handle
weighted graphs
Two kinds of vertices:
–

Finished or known
vertices
•

–

Shortest distance has
been computed

Unknown vertices
•

Have tentative
distance

38
Dijkstra’s Algorithm: Idea
At each step:
Pick closest unknown
vertex
2) Add it to known
vertices
3) Update distances
1)

39
Dijkstra’s Algorithm: Pseudocode
Initialize the cost of each node to ∞
Initialize the cost of the source to 0
While there are unknown nodes left in the graph
Select an unknown node b with the lowest cost
Mark b as known
For each node a adjacent to b
a’s cost = min(a’s old cost, b’s cost + cost of (b, a))
a’s prev path node = b

40
Important Features
Once a vertex is made known, the cost of the shortest

path to that node is known
While a vertex is still not known, another shorter
path to it might still be found

41
Dijkstra’s Algorithm in action
0



2

2

B

A



2

 D

C

3


E

Vertex

Visited?

Cost

A

??

F

??

G

??

H

??

Found by

??

E



??

D

1

??

C



0

B

1

G

11

7

H

2

10

9

3

F

1
4



42
Dijkstra’s Algorithm in action
0

2

2

2

B

A



C

2

4 D

3

1
E

Vertex

Visited?

Cost

A

Y

1

G

11

7

H

2

10

9

3

F

1
4





1


Found by

0

B

<=2

A

C

<=1

A

D

<=4

A

E

??

F

??

G

??

H

??

43
Dijkstra’s Algorithm in action
0

2

2

2

B

A



C

2

4 D

3

1
E

Vertex

Visited?

Cost

A

Y

1

G

11

7

H

2

10

9

3

F

1
4



12



1

0

B

Found by

<=2

A

1

A

D

<=4

A

E

<=12

C

F

??

G

??

H

??

C

Y

44
Dijkstra’s Algorithm in action
0

2

2

2

B

A

4

C

2

4 D

3

1
E

1

G

11

7

H

2

10

9

3

F

1
4



12



1

Vertex

Visited?

Cost

Found by

A

Y

0

B

Y

2

A

C

Y

1

A

D

<=4

A

E

<=12

C

F

<=4

B

G

??

H

??

45
Dijkstra’s Algorithm in action
0

2

2

2

B

A

4

C

2

4 D

3

1
E

1

G

11

7

H

2

10

9

3

F

1
4



12



1

Vertex

Visited?

Cost

Found by

A

Y

0

B

Y

2

A

C

Y

1

A

D

Y

4

A

E

<=12

C

F

<=4

B

G

??

H

??

46
Dijkstra’s Algorithm in action
0

2

2

2

B

A

4

C

2

4 D

3

1
E

1

G

11

7

H

2

10

9

3

F

1
4

7

12



1

Vertex

Visited?

Cost

A

Y

0

B

Y

2

A

C

Y

1

A

D

Y

4

A

<=12

C

4

B

E
F

Y

G

??

H

<=7

Found by

F

47
Dijkstra’s Algorithm in action
0

2

2

2

B

A

4

C

2

4 D

2

10

9

11

E

H
1

G 8

3

1

7

3

F

1
4

7

12

1

Vertex

Visited?

Cost

A

Y

0

B

Y

2

A

C

Y

1

A

D

Y

4

A

<=12

C

4

B

<=8

H

7

F

E
F

Y

G
H

Y

Found by

48
Dijkstra’s Algorithm in action
0

2

2

2

B

A

4

C

2

4 D

2

10

9

11

E

H
1

G 8

3

1

7

3

F

1
4

7

11

1

Vertex

Visited?

Cost

A

Y

0

B

Y

2

A

C

Y

1

A

D

Y

4

A

<=11

G

E

Found by

F

Y

4

B

G

Y

8

H

H

Y

7

F

49
Dijkstra’s Algorithm in action
0

2

2

2

B

A

4

C

2

4 D

2

10

9

11

E

H
1

G 8

3

1

7

3

F

1
4

7

11

1

Vertex

Visited?

Cost

Found by

A

Y

0

B

Y

2

A

C

Y

1

A

D

Y

4

A

E

Y

11

G

F

Y

4

B

G

Y

8

H

H

Y

7

F

50
Time Complexity of Dijskrs Algorithm

The efficiency of the Dijskras’s algorithm is analyzed by the iteration of the
loop structures. The while loop iteration n – 1 times to visit the minimum
weighted edge. Potentially loop must be repeated n times to examine every
vertices in the graph. So the time complexity is O(n2).
QUIZ

1. Find the BREATH-FIRST spanning tree and depth-first spanning tree of the
graph GA shown above.

More Related Content

PPTX
Prims and kruskal algorithms
PDF
Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm
PPT
Operational research
PPT
PDF
Sect2 1
PPTX
Inner Product Space
PDF
IIT JAM Mathematical Statistics - MS 2022 | Sourav Sir's Classes
PPTX
Kuhn munkres algorithm
Prims and kruskal algorithms
Bellman-Ford-Moore Algorithm and Dijkstra’s Algorithm
Operational research
Sect2 1
Inner Product Space
IIT JAM Mathematical Statistics - MS 2022 | Sourav Sir's Classes
Kuhn munkres algorithm

What's hot (20)

DOCX
3 capitulo-iii-matriz-asociada-sem-13-t-l-c
PDF
corripio
PDF
Capitulo 2 corripio
PDF
IIT JAM PHYSICS - PH 2022 Question Paper | Sourav Sir's Classes
PDF
MinFill_Presentation
PPTX
Inner product spaces
DOCX
3 capitulo-iii-matriz-asociada-sem-14-t-l-d
PPT
Randomized algorithms ver 1.0
PPT
Conference ppt
PDF
IIT JAM MATH 2021 Question Paper | Sourav Sir's Classes
PDF
Common derivatives integrals
PDF
SPSF04 - Euler and Runge-Kutta Methods
PPTX
PPTX
Cauchy integral theorem &amp; formula (complex variable & numerical method )
PPTX
8 queens problem using back tracking
PDF
University of manchester mathematical formula tables
PPTX
Fuzzy graph
PPTX
Complex analysis
PPT
Orthogonal basis and gram schmidth process
DOC
Unit 4 jwfiles
3 capitulo-iii-matriz-asociada-sem-13-t-l-c
corripio
Capitulo 2 corripio
IIT JAM PHYSICS - PH 2022 Question Paper | Sourav Sir's Classes
MinFill_Presentation
Inner product spaces
3 capitulo-iii-matriz-asociada-sem-14-t-l-d
Randomized algorithms ver 1.0
Conference ppt
IIT JAM MATH 2021 Question Paper | Sourav Sir's Classes
Common derivatives integrals
SPSF04 - Euler and Runge-Kutta Methods
Cauchy integral theorem &amp; formula (complex variable & numerical method )
8 queens problem using back tracking
University of manchester mathematical formula tables
Fuzzy graph
Complex analysis
Orthogonal basis and gram schmidth process
Unit 4 jwfiles
Ad

Viewers also liked (8)

PPT
Algorithm Design and Complexity - Course 9
PDF
Skiena algorithm 2007 lecture13 minimum spanning trees
PPTX
Feature Selection
PPTX
Application of graph theory in drug design
PPT
Applications of graphs
PPTX
Interesting applications of graph theory
PPT
Graph theory
PPT
Slideshare Powerpoint presentation
Algorithm Design and Complexity - Course 9
Skiena algorithm 2007 lecture13 minimum spanning trees
Feature Selection
Application of graph theory in drug design
Applications of graphs
Interesting applications of graph theory
Graph theory
Slideshare Powerpoint presentation
Ad

Similar to Graphs > Discrete structures , Data Structures & Algorithums (20)

PPT
Lec-35Graph - Copy Graph therory in Data strucure
PPT
lecture 20
PDF
algoritm prim kruskal review time complexity .pdf
PPTX
Prim's algorithm
PPT
prims algorithm for algorithms for cs st
PPT
2.3 shortest path dijkstra’s
PPT
Unit 5 session 2 MinimumSpanningTrees.ppt
PPT
lecture 22
PDF
Shortest Path in Graph
PPT
algorthm analysis from computer scince.ppt
PDF
module4_dynamic programming_2022.pdf
PPT
lecture 21
PPTX
lec6.pptx
PDF
All pairs shortest path algorithm
PPTX
Dijkstra’s algorithm
PPT
Greedy Approach in Design Analysis and Algorithms
PDF
Minimum spanning tree
PPT
lecture2faafffffffffffffffaaaaaaaaaaa4.ppt
PPTX
Prims & kruskal algorithms
PDF
Algorithms explained
Lec-35Graph - Copy Graph therory in Data strucure
lecture 20
algoritm prim kruskal review time complexity .pdf
Prim's algorithm
prims algorithm for algorithms for cs st
2.3 shortest path dijkstra’s
Unit 5 session 2 MinimumSpanningTrees.ppt
lecture 22
Shortest Path in Graph
algorthm analysis from computer scince.ppt
module4_dynamic programming_2022.pdf
lecture 21
lec6.pptx
All pairs shortest path algorithm
Dijkstra’s algorithm
Greedy Approach in Design Analysis and Algorithms
Minimum spanning tree
lecture2faafffffffffffffffaaaaaaaaaaa4.ppt
Prims & kruskal algorithms
Algorithms explained

More from Ain-ul-Moiz Khawaja (17)

PPT
Algo>Queues
PPT
Application of Stacks
PPT
Algo>Stacks
PPT
Analysis of Algorithum
PPT
Algo>ADT list & linked list
PPT
Algo>Arrays
PPT
Algo>Abstract data type
PPT
Algorithum Analysis
PPT
Sorting algorithums > Data Structures & Algorithums
PPT
Sorting algos > Data Structures & Algorithums
PPT
Huffman > Data Structures & Algorithums
PPT
Data Structures & Algorithms
PPTX
PPTX
Attribution Theories
PPTX
Attribution Theory
PPTX
Absenteeism
PPT
HRM Employee Turnover
Algo>Queues
Application of Stacks
Algo>Stacks
Analysis of Algorithum
Algo>ADT list & linked list
Algo>Arrays
Algo>Abstract data type
Algorithum Analysis
Sorting algorithums > Data Structures & Algorithums
Sorting algos > Data Structures & Algorithums
Huffman > Data Structures & Algorithums
Data Structures & Algorithms
Attribution Theories
Attribution Theory
Absenteeism
HRM Employee Turnover

Recently uploaded (20)

PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Lesson notes of climatology university.
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Insiders guide to clinical Medicine.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Complications of Minimal Access Surgery at WLH
Renaissance Architecture: A Journey from Faith to Humanism
Module 4: Burden of Disease Tutorial Slides S2 2025
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Microbial diseases, their pathogenesis and prophylaxis
Lesson notes of climatology university.
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
TR - Agricultural Crops Production NC III.pdf
Cell Structure & Organelles in detailed.
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
human mycosis Human fungal infections are called human mycosis..pptx
Insiders guide to clinical Medicine.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Computing-Curriculum for Schools in Ghana
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf

Graphs > Discrete structures , Data Structures & Algorithums

  • 2. Minimum Spanning Tree: Prim's Algorithm Prim's algorithm for finding an MST is a greedy algorithm. Start by selecting an arbitrary vertex, include it into the current MST. Grow the current MST by inserting into it the vertex closest to one of the vertices already in current MST.
  • 3. Minimum Spanning Tree Problem: given a connected, undirected, weighted graph, find a spanning tree using edges that minimize the total weight 6 4 5 9 14 2 10 15 3 8
  • 4. Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u ∈ Q key[u] = ∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
  • 5. Prim’s Algorithm MST-Prim(G, w, r) 6 Q = V[G]; 5 for each u ∈ Q key[u] = ∞; key[r] = 0; 14 10 p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] 3 if (v ∈ Q and w(u,v) < key[v]) Run p[v] = u; key[v] = w(u,v); 4 9 2 15 8 on example graph
  • 6. Prim’s Algorithm MST-Prim(G, w, r) ∞ 6 Q = V[G]; 5 for each u ∈ Q ∞ key[u] = ∞; key[r] = 0; 14 10 p[r] = NULL; while (Q not empty) ∞ u = ExtractMin(Q); for each v ∈ Adj[u] 3 ∞ if (v ∈ Q and w(u,v) < key[v]) Run p[v] = u; key[v] = w(u,v); 4 ∞ 9 ∞ 2 ∞ 15 8 on example graph ∞
  • 7. Prim’s Algorithm MST-Prim(G, w, r) ∞ 6 Q = V[G]; 5 for each u ∈ Q ∞ key[u] = ∞; key[r] = 0; 14 10 p[r] = NULL; while (Q not empty) 0 r u = ExtractMin(Q); for each v ∈ Adj[u] 3 ∞ if (v ∈ Q and w(u,v) < key[v]) Pick p[v] = u; key[v] = w(u,v); 4 ∞ 9 ∞ 2 ∞ 8 a start vertex r 15 ∞
  • 8. Prim’s Algorithm MST-Prim(G, w, r) ∞ 6 4 Q = V[G]; 5 for each u ∈ Q ∞ ∞ key[u] = ∞; key[r] = 0; 14 2 10 p[r] = NULL; while (Q not empty) 0 ∞ u u = ExtractMin(Q); for each v ∈ Adj[u] 3 8 ∞ if (v ∈ Q and w(u,v) < key[v]) Black vertices have been p[v] = u; key[v] = w(u,v); 9 15 ∞ ∞ removed from Q
  • 9. Prim’s Algorithm MST-Prim(G, w, r) ∞ 6 4 Q = V[G]; 5 for each u ∈ Q ∞ key[u] = ∞; 14 key[r] = 0; 10 p[r] = NULL; 0 u while (Q not empty) u = ExtractMin(Q); 3 8 3 for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; Black arrows key[v] = w(u,v); ∞ 9 ∞ 2 ∞ 15 ∞ indicate parent pointers
  • 10. Prim’s Algorithm 6 ∞ 5 MST-Prim(G, w, r) 14 Q = V[G]; for each u ∈ Q 14 10 key[u] = ∞; key[r] = 0; 0 u p[r] = NULL; 3 3 while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 ∞ 9 ∞ 2 ∞ 8 15 ∞
  • 11. Prim’s Algorithm 6 ∞ 5 MST-Prim(G, w, r) 14 Q = V[G]; for each u ∈ Q 14 10 key[u] = ∞; key[r] = 0; 0 p[r] = NULL; 3 3 while (Q not empty) u u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 ∞ 9 ∞ 2 ∞ 8 15 ∞
  • 12. Prim’s Algorithm 6 ∞ 5 MST-Prim(G, w, r) 14 Q = V[G]; for each u ∈ Q 14 10 key[u] = ∞; key[r] = 0; 0 p[r] = NULL; 3 3 while (Q not empty) u u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 ∞ 9 ∞ 2 8 8 15 ∞
  • 13. Prim’s Algorithm 6 ∞ 5 MST-Prim(G, w, r) 10 Q = V[G]; for each u ∈ Q 14 10 key[u] = ∞; key[r] = 0; 0 p[r] = NULL; 3 3 while (Q not empty) u u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 ∞ 9 ∞ 2 8 8 15 ∞
  • 14. Prim’s Algorithm 6 ∞ 5 MST-Prim(G, w, r) 10 Q = V[G]; for each u ∈ Q 14 10 key[u] = ∞; key[r] = 0; 0 p[r] = NULL; 3 3 while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 ∞ 9 ∞ 2 8 8 u 15 ∞
  • 15. Prim’s Algorithm 6 ∞ 5 MST-Prim(G, w, r) 10 Q = V[G]; for each u ∈ Q 14 10 key[u] = ∞; key[r] = 0; 0 p[r] = NULL; 3 3 while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 9 2 ∞ 2 8 8 u 15 ∞
  • 16. Prim’s Algorithm 6 ∞ 5 MST-Prim(G, w, r) 10 Q = V[G]; for each u ∈ Q 14 10 key[u] = ∞; key[r] = 0; 0 p[r] = NULL; 3 3 while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 9 2 ∞ 2 8 8 u 15 15
  • 17. Prim’s Algorithm 6 ∞ 5 MST-Prim(G, w, r) 10 Q = V[G]; for each u ∈ Q 14 10 key[u] = ∞; key[r] = 0; 0 p[r] = NULL; 3 3 while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u 4 9 2 ∞ 2 8 8 15 15
  • 18. Prim’s Algorithm 6 4 5 MST-Prim(G, w, r) 10 Q = V[G]; for each u ∈ Q 14 10 key[u] = ∞; key[r] = 0; 0 p[r] = NULL; 3 3 while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u 4 9 2 ∞ 2 8 8 15 15
  • 19. Prim’s Algorithm 6 4 5 MST-Prim(G, w, r) 5 Q = V[G]; for each u ∈ Q 14 10 key[u] = ∞; key[r] = 0; 0 p[r] = NULL; 3 3 while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u 4 9 2 ∞ 2 8 8 15 15
  • 20. Prim’s Algorithm 6 4 5 MST-Prim(G, w, r) 5 Q = V[G]; for each u ∈ Q 14 10 key[u] = ∞; key[r] = 0; 0 p[r] = NULL; 3 3 while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u 4 9 2 9 2 8 8 15 15
  • 21. Prim’s Algorithm 6 4 5 MST-Prim(G, w, r) 5 Q = V[G]; for each u ∈ Q 14 10 key[u] = ∞; key[r] = 0; 0 p[r] = NULL; 3 3 while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 u 9 2 9 2 8 8 15 15
  • 22. Prim’s Algorithm u 6 4 5 MST-Prim(G, w, r) 5 Q = V[G]; for each u ∈ Q 14 10 key[u] = ∞; key[r] = 0; 0 p[r] = NULL; 3 3 while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 9 2 9 2 8 8 15 15
  • 23. Prim’s Algorithm 6 4 5 MST-Prim(G, w, r) 5 Q = V[G]; for each u ∈ Q 14 10 key[u] = ∞; key[r] = 0; 0 p[r] = NULL; 3 3 while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); u 4 9 2 9 2 8 8 15 15
  • 24. Prim’s Algorithm 6 4 5 MST-Prim(G, w, r) 5 Q = V[G]; for each u ∈ Q 14 10 key[u] = ∞; key[r] = 0; 0 p[r] = NULL; 3 3 while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v); 4 9 2 u 2 8 8 9 15 15
  • 25. Review: Prim’s Algorithm MST-Prim(G, w, r) Q = V[G]; for each u ∈ Q key[u] = ∞; key[r] = 0; p[r] = NULL; while (Q not empty) u = ExtractMin(Q); for each v ∈ Adj[u] if (v ∈ Q and w(u,v) < key[v]) p[v] = u; key[v] = w(u,v);
  • 26. Dijkstra’s Algorithm Similar to (BFS) Breadth-First Search Grow a tree gradually, advancing from vertices taken from a queue Also similar to Prim’s algorithm for MST Use a priority queue keyed on d[v]
  • 27. Shortest Path for Weighted Graphs Given a graph G = (V, E) with edge costs c(e), and a vertex s ∈ V, find the shortest (lowest cost) path from s to every vertex in V Assume: only positive edge costs
  • 28. Dijkstra’s Algorithm for Single Source Shortest Path Similar to breadth-first search, but uses a heap instead of a queue: Always select (expand) the vertex that has a lowest-cost path to the start vertex Correctly handles the case where the lowest-cost (shortest) path to a vertex is not the one with fewest edges
  • 29. Dijkstra, Edsger Wybe Legendary figure in computer science; was a professor at University of Texas. Supported teaching introductory computer courses without computers (pencil and paper programming) Supposedly wouldn’t (until very late in life) read his e-mail; so, his staff had to print out messages and put them in his box. E.W. Dijkstra (1930-2002) 1972 Turning Award Winner, Programming Languages, semaphores, and …
  • 38. Dijkstra’s Algorithm: Idea Adapt BFS to handle weighted graphs Two kinds of vertices: – Finished or known vertices • – Shortest distance has been computed Unknown vertices • Have tentative distance 38
  • 39. Dijkstra’s Algorithm: Idea At each step: Pick closest unknown vertex 2) Add it to known vertices 3) Update distances 1) 39
  • 40. Dijkstra’s Algorithm: Pseudocode Initialize the cost of each node to ∞ Initialize the cost of the source to 0 While there are unknown nodes left in the graph Select an unknown node b with the lowest cost Mark b as known For each node a adjacent to b a’s cost = min(a’s old cost, b’s cost + cost of (b, a)) a’s prev path node = b 40
  • 41. Important Features Once a vertex is made known, the cost of the shortest path to that node is known While a vertex is still not known, another shorter path to it might still be found 41
  • 42. Dijkstra’s Algorithm in action 0  2 2 B A  2  D C 3  E Vertex Visited? Cost A ?? F ?? G ?? H ?? Found by ?? E  ?? D 1 ?? C  0 B 1 G 11 7 H 2 10 9 3 F 1 4  42
  • 43. Dijkstra’s Algorithm in action 0 2 2 2 B A  C 2 4 D 3 1 E Vertex Visited? Cost A Y 1 G 11 7 H 2 10 9 3 F 1 4   1  Found by 0 B <=2 A C <=1 A D <=4 A E ?? F ?? G ?? H ?? 43
  • 44. Dijkstra’s Algorithm in action 0 2 2 2 B A  C 2 4 D 3 1 E Vertex Visited? Cost A Y 1 G 11 7 H 2 10 9 3 F 1 4  12  1 0 B Found by <=2 A 1 A D <=4 A E <=12 C F ?? G ?? H ?? C Y 44
  • 45. Dijkstra’s Algorithm in action 0 2 2 2 B A 4 C 2 4 D 3 1 E 1 G 11 7 H 2 10 9 3 F 1 4  12  1 Vertex Visited? Cost Found by A Y 0 B Y 2 A C Y 1 A D <=4 A E <=12 C F <=4 B G ?? H ?? 45
  • 46. Dijkstra’s Algorithm in action 0 2 2 2 B A 4 C 2 4 D 3 1 E 1 G 11 7 H 2 10 9 3 F 1 4  12  1 Vertex Visited? Cost Found by A Y 0 B Y 2 A C Y 1 A D Y 4 A E <=12 C F <=4 B G ?? H ?? 46
  • 47. Dijkstra’s Algorithm in action 0 2 2 2 B A 4 C 2 4 D 3 1 E 1 G 11 7 H 2 10 9 3 F 1 4 7 12  1 Vertex Visited? Cost A Y 0 B Y 2 A C Y 1 A D Y 4 A <=12 C 4 B E F Y G ?? H <=7 Found by F 47
  • 48. Dijkstra’s Algorithm in action 0 2 2 2 B A 4 C 2 4 D 2 10 9 11 E H 1 G 8 3 1 7 3 F 1 4 7 12 1 Vertex Visited? Cost A Y 0 B Y 2 A C Y 1 A D Y 4 A <=12 C 4 B <=8 H 7 F E F Y G H Y Found by 48
  • 49. Dijkstra’s Algorithm in action 0 2 2 2 B A 4 C 2 4 D 2 10 9 11 E H 1 G 8 3 1 7 3 F 1 4 7 11 1 Vertex Visited? Cost A Y 0 B Y 2 A C Y 1 A D Y 4 A <=11 G E Found by F Y 4 B G Y 8 H H Y 7 F 49
  • 50. Dijkstra’s Algorithm in action 0 2 2 2 B A 4 C 2 4 D 2 10 9 11 E H 1 G 8 3 1 7 3 F 1 4 7 11 1 Vertex Visited? Cost Found by A Y 0 B Y 2 A C Y 1 A D Y 4 A E Y 11 G F Y 4 B G Y 8 H H Y 7 F 50
  • 51. Time Complexity of Dijskrs Algorithm The efficiency of the Dijskras’s algorithm is analyzed by the iteration of the loop structures. The while loop iteration n – 1 times to visit the minimum weighted edge. Potentially loop must be repeated n times to examine every vertices in the graph. So the time complexity is O(n2).
  • 52. QUIZ 1. Find the BREATH-FIRST spanning tree and depth-first spanning tree of the graph GA shown above.

Editor's Notes

  • #41: Here’s pseudocode for how dijkstra’s actually works. Speak the algorithm. Notice that we need to be able to find a minimum node and also update costs to nodes.