Docsity

Prim's and Kruskal's Algorithms for Minimum Spanning Trees, Slides of Aeronautical Engineering

Toggle sidebar
bg1
Trees
• A tree is a connected graph without
cycles
• A connected graph is a tree iff it has N
vertices and N-1 edges
• A graph is a tree iff there is one and
only one path joining any two of its
vertices
Spanning Trees
• A Spanning tree of a graph G, is a tree
that includes all the vertices from G.
SFO
Boston
LA Dallas
Wash DC
Airline Routes
SFO
Boston
LA Dallas
Wash DC
The resulting spanning tree
is not unique
The resulting spanning tree
is not unique
Minimum Spanning Tree
• Prim’s Algorithm
– Finds a subset of the edges (that form a tree)
including every vertex and the total weight of
all the edges in tree is minimized
• Choose starting vertex
• Create the Fringe Set
• Loop until the MST contains all the vertices in the
graph
– Remove edge with minimum weight from Fringe Set
– Add the edge to MST
– Update the Fringe Set
Initialization
Body
Prim – Initialization
• Pick any vertex xas the starting vertex
•Place xin the Minimum Spanning Tree
(MST)
• For each vertex yin the graph that is
adjacent to x
– Add yto the Fringe Set
• For each vertex yin the Fringe Set
–Set weight of yto weight of the edge
connecting yto x
–Set xto be parent of y
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Prim's and Kruskal's Algorithms for Minimum Spanning Trees and more Slides Aeronautical Engineering in PDF only on Docsity!

Trees

• A tree is a connected graph withoutcycles• A connected graph is a tree iff it has Nvertices and N-1 edges• A graph is a tree iff there is one andonly one path joining any two of itsvertices

Spanning Trees

• A Spanning tree of a graph G, is a treethat includes

all^ the vertices from G.

SFO

Boston LA^

Dallas

Wash DC

Airline Routes

SFO

Boston LA^

Dallas

Wash DC

The resulting spanning tree

is not unique The resulting spanning tree

is not unique

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 – Body

While number of vertices in MST < vertices inthe graphFind vertex

y^ with minimum weight in the Fringe Set Add vertex and the edge

x,y^ to the MST

Remove

y^ from the Fringe Set For all vertices

z^ adjacent to

y

If^ z^ is not in the Fringe SetAdd

z^ to the Fringe SetSet parent to

y Set weight of

z^ to weight of the edge connecting

z^ to^ y

ElseIf Weight(

y,z)^ < Weight(

z ) then Set parent to

y Set weight of

z^ to weight of the edge connecting

z^ to^ y

BOS JFK

ATL

MIA

SFO^ LAX

DEN

ORD

BOS 191 JFK MIA

ATL 595

ORD^606

908 DEN

SFO^834349 LAX

Minimum spanning tree – Prim

Minimum Spanning Tree

• Kruskal’s Algorithm– Finds a minimum spanning tree for aconnected weighted graph

  • Create a set of trees, where each vertex in thegraph is a separate tree• Create set S containing all edges in the graph• While S not empty– Remove edge with minimum weight from S– if that edge connects two different trees, then addit to the forest, combining two trees into a singletree– Otherwise discard that edge

BOS JFK

ATL

MIA

SFO^ LAX

DEN

ORD

BOS 191 JFK MIA

ATL 595

ORD^606

908 DEN

834 LAX

SFO^349

Minimum spanning tree – Kruskal

docsity.com

Simple Search Algorithm

Let Q be a list of partial paths,Let S be the start node andLet G be the Goal node.1.^ Initialize Q with partial path (S)2.^ If Q is empty, fail.

Else, pick a partial path N from Q

3.^ If head(N) = G, return N

(goal reached!)

4.^ Else:a)^

Remove N from Qb) Find all children of head(N) andcreate all the one-step extensions of N to each child.c) Add all extended paths to Qd) Go to step 2.

C

S

B

G

A

D

Q 1 (S) 2 3 4 5

Depth-First

Pick first element of Q;

Add path extensions to front of Q

C

S

B

G

A

D

Q 1 (S) 2 (A S) 3 4 5

Added paths in blue

Depth-First

Pick first element of Q;

Add path extensions to front of Q

C

S

B

G

A

D

Q 1 (S) 2 (A S) (B S) 3 4 5

Added paths in blue

Depth-First

Pick first element of Q; Add path

extensions

to front of Q

docsity.com

Simple Search Algorithm

Let Q be a list of partial paths,Let S be the start node andLet G be the Goal node.1.^ Initialize Q with partial path (S)2.^ If Q is empty, fail.

Else, pick a partial path N from Q

3.^ If head(N) = G, return N

(goal reached!)

4.^ Else:a)^

Remove N from Qb) Find all children of head(N) andcreate all the one-step extensions of N to each child.c) Add all extended paths to Qd) Go to step 2.

C

S

B

G

A

D

Q 1 (S) 2 (A S) (B S) 3 4 5

Added paths in blue

Depth-First

Pick first element of Q;

Add path extensions to front of Q

C

S

B

G

A

D

Q 1 (S) 2 (A S) (B S) 3 (C A S) (D A S) (B S) 4 5

Depth-First

Pick first element of Q;

Add path extensions to front of Q

Added paths in blue

C

S

B

G

A

D

Q 1 (S) 2 (A S) (B S) 3 (C A S) (D A S) (B S) 4 5

Depth-First

Pick first element of Q;

Add path extensions to front of Q

Added paths in blue

docsity.com

Simple Search Algorithm

Let Q be a list of partial paths,Let S be the start node andLet G be the Goal node.1.^ Initialize Q with partial path (S)2.^ If Q is empty, fail.

Else, pick a partial path N from Q

3.^ If head(N) = G, return N

(goal reached!)

4.^ Else:a)^

Remove N from Qb) Find all children of head(N) andcreate all the one-step extensions of N to each child.c) Add all extended paths to Qd) Go to step 2.

C

S

B

G

A

D

Q 1 (S) 2 (A S) (B S) 3 (C A S) (D A S) (B S) 4 (D A S) (B S)(C D A S)(G D A S) 5 (B S)

Depth-First

Pick first element of Q;

Add path extensions to front of Q

C

S

B

G

A

D

Q 1 (S) 2 (A S) (B S) 3 (C A S) (D A S) (B S) 4 (D A S) (B S)(C D A S)(G D A S) 5 (B S) (G D A S)(B S) 6

Depth-First

Pick first element of Q;

Add path extensions to front of Q

C

S

B

G

A

D

Q 1 (S) 2 (A S) (B S) 3 (C A S) (D A S) (B S) 4 (D A S) (B S)(C D A S)(G D A S) 5 (B S) (G D A S)(B S) 6

Depth-First

Pick first element of Q;

Add path extensions to front of Q

docsity.com

More about Trees

• Spanning trees– Prim’s algorithm– Kruskal’s algorithm• Generic search algorithm– Depth-first search example– Handling cycles– Breadth-first search example

C

S

B

G

A

D

Issue: Starting at S and moving top to bottom,will depth-first search ever reach G?

C

S

B

G

A

D

Q 1 (S) 2 (A S) (B S) 3 (C A S) (D A S) (B S) 4 (D A S) (B S)(C D A S)(G D A S) 5 (B S) (G D A S)(B S) 6

Depth-First

  • C visited multiple times• Multiple paths to C, D & G

Effort can be wasted in more mild casesHow much wasted effort can be incurred in the worst case?

How Do We Avoid Repeat Visits?Idea^ : • Keep track of nodes already visited.• Do not place visited nodes on Q.Does this maintain correctness?• Any goal reachable from a node that was visited asecond time would be reachable from that node thefirst time.Does it always improve efficiency?• Guarantees each node appears at most once atthe head of a path in Q.

docsity.com

Breadth-First

Pick first element of Q; Add path extensions to end of Q

C

S

B

G

A

D

Visited

Q 1 2 3 4 5

S

(S)

Breadth-First

Pick first element of Q; Add path extensions to end of Q

C

S

B

G

A

D

Visited

Q 1 2 3 4 5

A,B,S

(A S) (B S)

S

(S)

Breadth-First

Pick first element of Q; Add path extensions to end of Q

C

S

B

G

A

D

Visited

Q 1 2 3 4 5

A,B,S

(A S) (B S)

S

(S)

Breadth-First

Pick first element of Q; Add path extensions to end of Q

C

S

B

G

A

D

Visited

Q 1 2 3 4 5

C,D,B,A,S

(B S) (C A S) (D A S)

A,B,S

(A S) (B S)

S

(S)

docsity.com

Breadth-First

Pick first element of Q; Add path extensions to end of Q

C

S

B

G

A

D

Visited

Q 1 2 3 4 5

C,D,B,A,S

(B S) (C A S) (D A S)

A,B,S

(A S) (B S)

S

(S)

Breadth-First

Pick first element of Q; Add path extensions to end of Q

C

S

B

G

A

D

G,C,D,B,A,S

(G B S) 6

Visited

Q 1 2 3 4 5

G,C,D,B,A,S

(D A S) (G B S)

G,C,D,B,A,S

(C A S) (D A S) (G B S)*

C,D,B,A,S

(B S) (C A S) (D A S)

A,B,S

(A S) (B S)

S

(S)

Depth First Search (DFS)

S

D

B

A C^

G

C^

G

D C G

S

D

B

A C^

G

C^

G

D C G

Breadth First Search (BFS)

Test_ordered_binary.adb Depth-first:^ Add path extensions to

front^ of Q

Pick first element of Q Breadth-first:^ Add path extensions to

back^ of Q

Pick first element of Q

Summary

• Most problem solving tasks may beformulated as state space search.• Mathematical representations for searchare graphs and search trees.• Depth-first and breadth-first searchmay be framed, among others, asinstances of a generic search strategy.• Cycle detection is required to achieveefficiency and completeness.

docsity.com