Docsity

Prim's and Dijkstra's Algorithms for Spanning Tree and Shortest Path, Slides of Aeronautical Engineering

Partial preview of the text

Download Prim's and Dijkstra's Algorithms for Spanning Tree and Shortest Path and more Slides Aeronautical Engineering in PDF only on Docsity!

Step 2

A E

C F

D G

B 1 1 H

A E

C F

D G

B^1 H

Step 3

A E

C F

D G

B H

A E

C F

D G

B H

Step 4

A E

C F

D G

B H

A E

C F

D G

B H

Step 5

A E

C F

D G

B H

A E

C F

D G

B H

docsity.com

Slide 6

A E

C F

D G

B H

A E

C F

D G

B H

Slide 7

A E

C F

D G

B H

A E

C F

D G

B H

Minimum Spanning Tree

• Prim’s Algorithm– Finds a subset of the edges (that form a tree)

including every vertex and the total weight ofall the edges in tree is minimized• Choose starting vertex• Create the Fringe Set• Loop until the MST contains all the vertices in thegraph– Remove edge with minimum weight from Fringe Set– Add the edge to MST– Update the Fringe Set

Initialization

Body

Prim – Initialization

• Pick any vertex

x^ as the starting vertex

• Place

x^ in the Minimum Spanning Tree

(MST)• For each vertex

y^ in the graph that is

adjacent to

x

– Add

y^ to the Fringe Set

• For each vertex

y^ in the Fringe Set

– Set weight of

y^ to weight of the edge

connecting

y^ to

x

– Set

x^ to be parent of

y

docsity.com

Prim’s Body

MSTA

A 4 E

Parent Fringe Set

B 1

A E

C F

D G

B H

D 1 C

2 D^3 F

Prim’s Body

MSTA

A 4 E

Parent Fringe Set

B 1

A E

C F

D G

B H

D 1 C

2 D

25 FG

Prim’s Body

MSTA

A 4 E

Parent Fringe Set

B 1

A E

C F

D G

B H

D

2 D

25 FG

1 C

Prim’s Body

MSTA Parent Fringe Set

B 1

A E

C F

D G

B H

2 D

1 C

E

C

G

D 2 F

docsity.com

Prim’s Body

MSTA Parent Fringe Set

B 1

A E

C F

D G

B H

2 D

1 C

E

C

G

D 2 F

Prim’s Body

MSTA Parent Fringe Set

B 1

A E

C F

D G

B H

2 D

1 C

E

C 2

D 2 F

1 G

Prim’s Body

MSTA Parent Fringe Set

B 1

A E

C F

D G

B H

2 D

1 C

1 G

G 3 H

E

C 2

D 2 F

Prim’s Body

MSTA Parent Fringe Set

B 1

A E

C F

D G

B H

2 D

1 C

1 G

G 3 H

D 2 F

2 E

docsity.com

Inserting into Ordered Binary Tree -- insert

procedure

procedure

Insert

(Root

in^ out

Nodeptr;

Element

:^ in

Integer

)^ is

New_Node

:^ Nodeptr;

beginif

Root

=^ null

then

New_Node:=

new^

Node;

New_Node.Element

:=^ Element;

Root^

:=^ New_Node;

elseif

Root.Element

<^ Element

then

Insert(Root.Right_Child,

Element);

else^ Insert(Root.Left_Child,

Element);

end^ if

end^ if

end^ Insert;

Inserting into a Binary node • Insert 10, 11, 9, 7, 8, 12• Insert 10• Insert 11

if^ Root.Element < Element

then

Insert(Root.Right_Child, Element);

else

Insert(Root.Left_Child, Element);

end if

Inserting into Ordered Binary Tree • Insert 9

if^ Root.Element

<^ Element

then

Insert(Root.Right_Child,

Element);

else

Insert(Root.Left_Child,

Element);

end^ if

Inserting into Ordered Binary Tree • Insert 7

docsity.com

Inserting into Ordered Binary Tree • Insert 8

Inserting into Ordered Binary Tree • Insert 12

Shortest Path Problems

•^ Dijkstra’s algorithm^ – Finds shortest path for a directed and

connected graph G(V,E) which has non-negative weights.– Applications:• Internet routing• Road generation within a geographic region• …

Dijkstra’s Algorithm

•^ Dijkstra(G,w,s)

Init_Source(G,s)S := empty setQ := set of all verticeswhile Q is not an empty set loopu := Extract_Min(Q)S := S union {u}for each vertex v which is a neighbor of u loop

Relax(u,v,w)

docsity.com

Dijkstra’s Algorithm 8

s^5

a^

b d

c

S = {s, c, d}Q = {a, b}

Extract_Min (Q)

Æ^ d

Neighbors of d = bRelax (d,b,6)

0814 d = 5 7 0 c c s c

prev =

0 8 5 7 0 c s c

13 Æ Æ

d

Dijkstra’s Algorithm 8

s^5

a^

b d

c

S = {s, c, d, a}Q = {b}

Extract_Min (Q)

Æ^ a

Neighbors of a = b, cRelax (a,b,1)Relax (a,c,3)

0813 d = 5 7 0 c d s c

prev =

0 8 7 0 c c

Æ Æ

9 5 a s

Dijkstra’s Algorithm 8

s^5

a^

b d

c

S = {s, c, d, a, b}Q = {}

Extract_Min (Q)

Æ^ b

Neighbors of b = dRelax (b, d, 4)

089 d = 5 7 0 c a s c

prev =

0 8 9 5 0 c a s

Æ Æ

7 c

docsity.com