SlideShare ist ein Scribd-Unternehmen logo
1 von 52
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 …
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.

Weitere ähnliche Inhalte

Was ist angesagt?

3 capitulo-iii-matriz-asociada-sem-13-t-l-c
3 capitulo-iii-matriz-asociada-sem-13-t-l-c3 capitulo-iii-matriz-asociada-sem-13-t-l-c
3 capitulo-iii-matriz-asociada-sem-13-t-l-cFernandoDanielMamani1
 
Capitulo 2 corripio
Capitulo 2 corripioCapitulo 2 corripio
Capitulo 2 corripioomardavid01
 
IIT JAM PHYSICS - PH 2022 Question Paper | Sourav Sir's Classes
IIT JAM PHYSICS - PH 2022 Question Paper | Sourav Sir's ClassesIIT JAM PHYSICS - PH 2022 Question Paper | Sourav Sir's Classes
IIT JAM PHYSICS - PH 2022 Question Paper | Sourav Sir's ClassesSOURAV DAS
 
MinFill_Presentation
MinFill_PresentationMinFill_Presentation
MinFill_PresentationAnna Lasota
 
3 capitulo-iii-matriz-asociada-sem-14-t-l-d
3 capitulo-iii-matriz-asociada-sem-14-t-l-d3 capitulo-iii-matriz-asociada-sem-14-t-l-d
3 capitulo-iii-matriz-asociada-sem-14-t-l-dFernandoDanielMamani1
 
IIT JAM MATH 2021 Question Paper | Sourav Sir's Classes
IIT JAM MATH 2021 Question Paper | Sourav Sir's ClassesIIT JAM MATH 2021 Question Paper | Sourav Sir's Classes
IIT JAM MATH 2021 Question Paper | Sourav Sir's ClassesSOURAV DAS
 
Common derivatives integrals
Common derivatives integralsCommon derivatives integrals
Common derivatives integralsolziich
 
SPSF04 - Euler and Runge-Kutta Methods
SPSF04 - Euler and Runge-Kutta MethodsSPSF04 - Euler and Runge-Kutta Methods
SPSF04 - Euler and Runge-Kutta MethodsSyeilendra Pramuditya
 
Cauchy integral theorem &amp; formula (complex variable & numerical method )
Cauchy integral theorem &amp; formula (complex variable & numerical method )Cauchy integral theorem &amp; formula (complex variable & numerical method )
Cauchy integral theorem &amp; formula (complex variable & numerical method )Digvijaysinh Gohil
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back trackingTech_MX
 
University of manchester mathematical formula tables
University of manchester mathematical formula tablesUniversity of manchester mathematical formula tables
University of manchester mathematical formula tablesGaurav Vasani
 
Complex analysis
Complex analysisComplex analysis
Complex analysissujathavvv
 
Orthogonal basis and gram schmidth process
Orthogonal basis and gram schmidth processOrthogonal basis and gram schmidth process
Orthogonal basis and gram schmidth processgidc engineering college
 

Was ist angesagt? (20)

3 capitulo-iii-matriz-asociada-sem-13-t-l-c
3 capitulo-iii-matriz-asociada-sem-13-t-l-c3 capitulo-iii-matriz-asociada-sem-13-t-l-c
3 capitulo-iii-matriz-asociada-sem-13-t-l-c
 
corripio
corripio corripio
corripio
 
Capitulo 2 corripio
Capitulo 2 corripioCapitulo 2 corripio
Capitulo 2 corripio
 
IIT JAM PHYSICS - PH 2022 Question Paper | Sourav Sir's Classes
IIT JAM PHYSICS - PH 2022 Question Paper | Sourav Sir's ClassesIIT JAM PHYSICS - PH 2022 Question Paper | Sourav Sir's Classes
IIT JAM PHYSICS - PH 2022 Question Paper | Sourav Sir's Classes
 
MinFill_Presentation
MinFill_PresentationMinFill_Presentation
MinFill_Presentation
 
Inner product spaces
Inner product spacesInner product spaces
Inner product spaces
 
3 capitulo-iii-matriz-asociada-sem-14-t-l-d
3 capitulo-iii-matriz-asociada-sem-14-t-l-d3 capitulo-iii-matriz-asociada-sem-14-t-l-d
3 capitulo-iii-matriz-asociada-sem-14-t-l-d
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
 
Conference ppt
Conference pptConference ppt
Conference ppt
 
IIT JAM MATH 2021 Question Paper | Sourav Sir's Classes
IIT JAM MATH 2021 Question Paper | Sourav Sir's ClassesIIT JAM MATH 2021 Question Paper | Sourav Sir's Classes
IIT JAM MATH 2021 Question Paper | Sourav Sir's Classes
 
Common derivatives integrals
Common derivatives integralsCommon derivatives integrals
Common derivatives integrals
 
SPSF04 - Euler and Runge-Kutta Methods
SPSF04 - Euler and Runge-Kutta MethodsSPSF04 - Euler and Runge-Kutta Methods
SPSF04 - Euler and Runge-Kutta Methods
 
Unit1
Unit1Unit1
Unit1
 
Cauchy integral theorem &amp; formula (complex variable & numerical method )
Cauchy integral theorem &amp; formula (complex variable & numerical method )Cauchy integral theorem &amp; formula (complex variable & numerical method )
Cauchy integral theorem &amp; formula (complex variable & numerical method )
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
 
University of manchester mathematical formula tables
University of manchester mathematical formula tablesUniversity of manchester mathematical formula tables
University of manchester mathematical formula tables
 
Fuzzy graph
Fuzzy graphFuzzy graph
Fuzzy graph
 
Complex analysis
Complex analysisComplex analysis
Complex analysis
 
Orthogonal basis and gram schmidth process
Orthogonal basis and gram schmidth processOrthogonal basis and gram schmidth process
Orthogonal basis and gram schmidth process
 
Unit 4 jwfiles
Unit 4 jwfilesUnit 4 jwfiles
Unit 4 jwfiles
 

Andere mochten auch

Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9Traian Rebedea
 
Skiena algorithm 2007 lecture13 minimum spanning trees
Skiena algorithm 2007 lecture13 minimum spanning treesSkiena algorithm 2007 lecture13 minimum spanning trees
Skiena algorithm 2007 lecture13 minimum spanning treeszukun
 
Application of graph theory in drug design
Application of graph theory in drug designApplication of graph theory in drug design
Application of graph theory in drug designReihaneh Safavi
 
Applications of graphs
Applications of graphsApplications of graphs
Applications of graphsTech_MX
 
Interesting applications of graph theory
Interesting applications of graph theoryInteresting applications of graph theory
Interesting applications of graph theoryTech_MX
 
Slideshare Powerpoint presentation
Slideshare Powerpoint presentationSlideshare Powerpoint presentation
Slideshare Powerpoint presentationelliehood
 

Andere mochten auch (8)

Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9Algorithm Design and Complexity - Course 9
Algorithm Design and Complexity - Course 9
 
Skiena algorithm 2007 lecture13 minimum spanning trees
Skiena algorithm 2007 lecture13 minimum spanning treesSkiena algorithm 2007 lecture13 minimum spanning trees
Skiena algorithm 2007 lecture13 minimum spanning trees
 
Feature Selection
Feature Selection Feature Selection
Feature Selection
 
Application of graph theory in drug design
Application of graph theory in drug designApplication of graph theory in drug design
Application of graph theory in drug design
 
Applications of graphs
Applications of graphsApplications of graphs
Applications of graphs
 
Interesting applications of graph theory
Interesting applications of graph theoryInteresting applications of graph theory
Interesting applications of graph theory
 
Graph theory
Graph theoryGraph theory
Graph theory
 
Slideshare Powerpoint presentation
Slideshare Powerpoint presentationSlideshare Powerpoint presentation
Slideshare Powerpoint presentation
 

Ähnlich wie Graphs > Discrete structures , Data Structures & Algorithums

lecture 20
lecture 20lecture 20
lecture 20sajinsc
 
2.3 shortest path dijkstra’s
2.3 shortest path dijkstra’s 2.3 shortest path dijkstra’s
2.3 shortest path dijkstra’s Krish_ver2
 
Unit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.pptUnit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.pptprithivr1
 
lecture 22
lecture 22lecture 22
lecture 22sajinsc
 
module4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdfmodule4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdfShiwani Gupta
 
lecture 21
lecture 21lecture 21
lecture 21sajinsc
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithmSrikrishnan Suresh
 
Dijkstra’s algorithm
Dijkstra’s algorithmDijkstra’s algorithm
Dijkstra’s algorithmfaisal2204
 
Prims & kruskal algorithms
Prims & kruskal algorithmsPrims & kruskal algorithms
Prims & kruskal algorithmsAyesha Tahir
 
Algorithms explained
Algorithms explainedAlgorithms explained
Algorithms explainedPIYUSH Dubey
 
深層生成モデルを用いたマルチモーダルデータの半教師あり学習
深層生成モデルを用いたマルチモーダルデータの半教師あり学習深層生成モデルを用いたマルチモーダルデータの半教師あり学習
深層生成モデルを用いたマルチモーダルデータの半教師あり学習Masahiro Suzuki
 
Randomized algorithms all pairs shortest path
Randomized algorithms  all pairs shortest pathRandomized algorithms  all pairs shortest path
Randomized algorithms all pairs shortest pathMohammad Akbarizadeh
 
Maximum clique detection algorithm
Maximum clique detection algorithmMaximum clique detection algorithm
Maximum clique detection algorithmAbhishek Kona
 
Dijkstra Searching Algorithms Shortest.pptx
Dijkstra Searching Algorithms Shortest.pptxDijkstra Searching Algorithms Shortest.pptx
Dijkstra Searching Algorithms Shortest.pptxsandeep54552
 

Ähnlich wie Graphs > Discrete structures , Data Structures & Algorithums (20)

lecture 20
lecture 20lecture 20
lecture 20
 
Prim's algorithm
Prim's algorithmPrim's algorithm
Prim's algorithm
 
2.3 shortest path dijkstra’s
2.3 shortest path dijkstra’s 2.3 shortest path dijkstra’s
2.3 shortest path dijkstra’s
 
Unit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.pptUnit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.ppt
 
lecture 22
lecture 22lecture 22
lecture 22
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
 
module4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdfmodule4_dynamic programming_2022.pdf
module4_dynamic programming_2022.pdf
 
lecture 21
lecture 21lecture 21
lecture 21
 
lec6.pptx
lec6.pptxlec6.pptx
lec6.pptx
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
 
Dijkstra’s algorithm
Dijkstra’s algorithmDijkstra’s algorithm
Dijkstra’s algorithm
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Prims & kruskal algorithms
Prims & kruskal algorithmsPrims & kruskal algorithms
Prims & kruskal algorithms
 
Algorithms explained
Algorithms explainedAlgorithms explained
Algorithms explained
 
Aaex2 group2
Aaex2 group2Aaex2 group2
Aaex2 group2
 
深層生成モデルを用いたマルチモーダルデータの半教師あり学習
深層生成モデルを用いたマルチモーダルデータの半教師あり学習深層生成モデルを用いたマルチモーダルデータの半教師あり学習
深層生成モデルを用いたマルチモーダルデータの半教師あり学習
 
Randomized algorithms all pairs shortest path
Randomized algorithms  all pairs shortest pathRandomized algorithms  all pairs shortest path
Randomized algorithms all pairs shortest path
 
Maximum clique detection algorithm
Maximum clique detection algorithmMaximum clique detection algorithm
Maximum clique detection algorithm
 
Dijkstra Searching Algorithms Shortest.pptx
Dijkstra Searching Algorithms Shortest.pptxDijkstra Searching Algorithms Shortest.pptx
Dijkstra Searching Algorithms Shortest.pptx
 
12_Graph.pptx
12_Graph.pptx12_Graph.pptx
12_Graph.pptx
 

Mehr von Ain-ul-Moiz Khawaja (17)

Algo>Queues
Algo>QueuesAlgo>Queues
Algo>Queues
 
Application of Stacks
Application of StacksApplication of Stacks
Application of Stacks
 
Algo>Stacks
Algo>StacksAlgo>Stacks
Algo>Stacks
 
Analysis of Algorithum
Analysis of AlgorithumAnalysis of Algorithum
Analysis of Algorithum
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
Algo>Arrays
Algo>ArraysAlgo>Arrays
Algo>Arrays
 
Algo>Abstract data type
Algo>Abstract data typeAlgo>Abstract data type
Algo>Abstract data type
 
Algorithum Analysis
Algorithum AnalysisAlgorithum Analysis
Algorithum Analysis
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
 
Sorting algos > Data Structures & Algorithums
Sorting algos  > Data Structures & AlgorithumsSorting algos  > Data Structures & Algorithums
Sorting algos > Data Structures & Algorithums
 
Huffman > Data Structures & Algorithums
Huffman > Data Structures & AlgorithumsHuffman > Data Structures & Algorithums
Huffman > Data Structures & Algorithums
 
Data Structures & Algorithms
Data Structures & AlgorithmsData Structures & Algorithms
Data Structures & Algorithms
 
Turn over
Turn overTurn over
Turn over
 
Attribution Theories
Attribution TheoriesAttribution Theories
Attribution Theories
 
Attribution Theory
Attribution TheoryAttribution Theory
Attribution Theory
 
Absenteeism
AbsenteeismAbsenteeism
Absenteeism
 
HRM Employee Turnover
HRM Employee TurnoverHRM Employee Turnover
HRM Employee Turnover
 

Kürzlich hochgeladen

Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 

Kürzlich hochgeladen (20)

Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 

Graphs > Discrete structures , Data Structures & Algorithums

  • 1.
  • 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 …
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 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.

Hinweis der Redaktion

  1. 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.