# Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select.

## Presentation on theme: "Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select."— Presentation transcript:

Spanning Trees

Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select an edge of minimum weight between a tree vertex and a fringe vertex. 2)add the selected edge and the fringe vertex to the tree. end.

Prims Algorithm Minimal Spanning Tree 1 6 24 35 3 2 1 4 4 2 5 3 4 78 1 6 2 5 2 6

1 6 24 35 3 2 1 4 4 2 5 3 4 78 1 6 2 5 2 6 Example: start with 7

Prims Algorithm Minimal Spanning Tree 1 6 24 35 3 2 1 4 4 2 5 3 4 78 1 6 2 5 2 6

1 6 24 35 3 2 1 4 4 2 5 3 4 78 1 6 2 5 2 6

1 6 24 35 3 2 1 4 4 2 5 3 4 78 1 6 2 5 2 6

1 6 24 35 3 2 1 4 4 2 5 3 4 78 1 2 5 2 6

1 6 24 35 3 2 1 4 4 2 5 3 4 78 1 2 5 2 6

1 6 24 35 3 2 1 4 4 2 5 3 4 78 1 2 5 6

1 6 24 35 3 2 1 4 4 2 5 3 4 78 1 2 5 6

1 6 24 35 2 1 4 4 2 5 3 4 78 1 2 6

1 6 24 35 2 1 4 4 2 5 3 4 78 1 2 6

1 6 24 35 2 1 4 2 5 3 4 78 1 2 6

1 6 24 35 2 1 4 2 5 3 4 78 1 2 6

1 6 24 35 2 1 2 5 3 4 78 1 2

1 6 24 35 2 1 2 5 3 4 78 1 2

1 6 24 35 2 1 2 3 78 1 2 MST weight = 15 4

Topological Sorting Algorithm while (the graph has a node with no successor) do remove one of those nodes from the graph and add it to the end of a list if (the graph is empty) then the list contains the reverse of some topological order else the graph contains a cycle

A B C D E F G HI J L K M

A B C D E F G HI J L K M D

A B C E F G HI J L K M D E

A B C F G HI J L K M DE F

A B C G HI J L K M DEF C

A B G HI J L K M DEFC B

A G H I J L K M DEFCB I

A G H J L K M DEFCBI H

A G J L K M DEFCBIH G

A J L K M DEFCBIHG A

J L K M DEFCBIHGA K

J L M DEFCBIHGAK M

J L DEFCBIHGAKM L

J DEFCBIHGAKML J

DEFCBIHGAKMLJ J L M K A G H I B C F E D

Download ppt "Spanning Trees. Prims MST Algorithm Algorithm ( this is also greedy) Select an arbitrary vertex to start the tree, while there are fringe vertices: 1)select."

Similar presentations