8_binary_trees

8_binary_trees - Today More about Trees Spanning trees...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Computers and Programming Prof. I. K. Lundqvist Lecture 8 April 5 2004 Today – More about Trees • Spanning trees – Prim’s algorithm – Kruskal’s algorithm • Generic search algorithm – Depth-first search example – Handling cycles – Breadth-first search example A B C D E B A A A B D D D B E C D C E B A C D E 0 1 0 1 0 1 0 1 1 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 E D C B A AB CDE A B C D E B D D D B C D B A C D E 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 1 1 0 E D C B A
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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 x as the starting vertex •P lace 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 we ight of y to weight of the edge connecting y to x –Set x to be parent of y
Background image of page 2
Prim – Body While number of vertices in MST < vertices in the graph Find 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 Set Add z to the Fringe Set Set parent to y Set weight of z to weight of the edge connecting z to y Else If 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 LAX SFO DEN ORD 957 860 191 1090 760 595 606 722 2534 1855 908 2451 834 349 BOS JFK 191 MIA 595 ATL 606 ORD 722 DEN 908 LAX 834 SFO 349 Minimum spanning tree – Prim Minimum Spanning Tree • Kruskal’s Algorithm – Finds a minimum spanning tree for a connected weighted graph • Create a set of trees, where each vertex in the graph 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 add
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 13

8_binary_trees - Today More about Trees Spanning trees...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online