8_binary_trees

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

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

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 A B C D E 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 A B C D E

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

View Full Document
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 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 • 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
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 it to the forest, combining two trees into a single

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern