Lecture19: Graph III Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Spanning Tree Definition and property Subgraph that contains all vertices of the original graph and is a tree Often, a graph has many different spanning trees. Cost of spanning tree: the sum of the edge weights in the spanning tree: = 57 Useful in constructing networks: a minimum spanning tree gives the way to connect a set of points with the smallest total amount of wire 2 CB A E D F 10 8 Graph CB A E D F 10 8 Spanning tree
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Minimum Spanning Tree (MSP) A spanning tree with the minimum cost Cost: = 28 Finding the minimum spanning tree Prim’s algorithm Kruskal’s algorithms 3 CB A E D F 10 8 Graph CB A E D F 8 Minimum spanning tree
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Prim’s Algorithm 4
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Prim’s Algorithm 5
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example Find a minimum spanning tree of the following weighted graph using Prim’s algorithm. 6 C B A D E F
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example Starting from A 7 C B A D E F VertexBCDEF Priority12inf Edge(A,B)(A,C)---
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example Add the first priority vertex, B. 8 C B A D E F VertexCDEF Priority2inf1 Edge(A,C)-(B,E)-
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example 9 C B A D E F VertexCDF Priority1inf2 Edge(A,C)-(E,F)
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example 10 C B A D E F VertexDF Priority21 Edge(C,D)(E,F)
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example 11 C B A D E F VertexD Priority1 Edge(C,D)
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example 12 C B A D E F
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Kruskal’s Algorithm 13
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example Find a minimum spanning tree of the following weighted graph using Kruskal’s algorithm. 14 C B A D E F Edge(A,B)(A,C)(B,C)(B,E)(C,E)(C,D)(C,F)(E,F)(D,F) Priority
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example Start with the edge with minimum weight. 15 C B A D E F Edge(A,B)(A,C)(B,C)(C,E)(C,D)(C,F)(E,F)(D,F) Priority
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example Add the first priority edge as long as it does not create a cycle. 16 C B A D E F Edge(A,B)(A,C)(B,C)(C,E)(C,D)(C,F)(E,F) Priority
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example 17 C B A D E F Edge(A,C)(B,C)(C,E)(C,D)(C,F)(E,F) Priority142562
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example 18 C B A D E F Edge(B,C)(C,E)(C,D)(C,F)(E,F) Priorityinf 231
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 An Example 19 C B A D E F Edge(B,C)(C,E)(C,D)(C,F) Priorityinf
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Analysis of MST Algorithms 20
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Hamiltonian Cycle A cycle that goes through every vertex exactly once 21 C B A D E F C B A D E F
CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Traveling Salesperson Problem (TSP) 22
23