Spanning Tree (015) Lecture1: Graph III ohyung Han S, POSTH bhhan@postech.ac.kr efinition and property Subgraph that contains all vertices of the original graph and is a tree Often, a graph has many different spanning trees. 1 15 Graph 1 ost of spanning tree: the sum of the edge weights in the spanning tree: + + 1 + 15 + = 5 Useful in constructing networks: a minimum spanning tree gives the way to connect a set of points with the smallest total amount of wire 1 15 Spanning tree Minimum Spanning Tree (MSP) spanning tree with the minimum cost 1 ost: + + + + = inding the minimum spanning tree Graph Prim s algorithm Kruskal s algorithms 15 1 Minimum spanning tree Prim s lgorithm Prim s algorithm builds the MST one vertex at a time. Prim s algorithm keeps track of:, the set of vertices included in the MST so far, and, the set of vertices not in the MST yet and, for each such vertex, the cheapest edge connecting to any vertex in Procedure egin with t each step Look at all the vertices in hoose the vertex with the cheapest edge,, and dd to unless creates a cycle fter steps (where is the number of vertices), the chosen vertices and their cheapest edges form a MST for the graph.
Key procedure in each step Prim s lgorithm ind a vertex in with the cheapest edge connecting it to any vertex in Keep the vertices in in a priority queue. The priority of a vertex in the priority queue is the cost of the cheapest edge connecting and some vertex in. If there is no such edge, then s priority is infinity. Record the edge that gives a vertex its priority, because when a vertex is removed from the priority queue, its recorded edge becomes a spanning tree edge. Goal cheapest edge: the lowest cost is the highest priority Use a min heap. n xample ind a minimum spanning tree of the following weighted graph using Prim s algorithm. 1 5 n xample n xample Starting from dd the first priority vertex,. 1 1 Vertex Priority 1 inf inf inf dge (,) (,) Vertex Priority inf 1 inf dge (,) (,)
n xample n xample 1 1 Vertex Vertex Priority 1 inf Priority 1 dge (,) (,) dge (,) (,) n xample n xample 1 1 Vertex Priority 1 dge (,) 1
Kruskal s lgorithm Kruskal s algorithm builds the MST one vertex at a time. reate a forest (a set of trees), where each vertex in the graph is a separate tree reate a set containing all the edges in the graph while is nonempty and is not yet spanning Remove an edge with minimum weight from If that edge connects two different trees, then add it to the forest, combining two trees into a single tree Otherwise discard that edge. t the termination of the algorithm, the forest forms a minimum spanning forest of the graph. n xample ind a minimum spanning tree of the following weighted graph using Kruskal s algorithm. 1 dge (,) (,) (,) (,) (,) (,) (,) (,) (,) Priority 1 5 5 1 1 n xample n xample Start with the edge with minimum weight. dd the first priority edge as long as it does not create a cycle. 1 1 dge (,) (,) (,) (,) (,) (,) (,) (,) Priority 1 dge (,) (,) (,) (,) (,) (,) (,) Priority 1 5 15 1
n xample n xample 1 1 dge (,) (,) (,) (,) (,) (,) Priority 1 5 dge (,) (,) (,) (,) (,) Priority inf inf 1 1 1 n xample nalysis of MST lgorithms 1 Greedy algorithm: Making the locally optimal choice at each stage No backtracking However, the solution is optimal. Time complexity Prim s algorithm: log or with adjacency matrix log with binary heap and adjacency list log with ibonacci heap and adjacency list Kruskal s algorithm: log dge (,) (,) (,) (,) Priority inf inf inf inf 1 0
Hamiltonian ycle cycle that goes through every vertex exactly once efinition Traveling Salesperson Problem (TSP) The problem of finding the minimum cost Hamiltonian circuit in a weighted graph Properties Intractable problem NP: There is no polynomial time algorithm. Many other problems are in this class. Time complexity of TSP where is the number of vertices Time complexity of naïve algorithm:! Time complexity of the best known algorithm: Greedy algorithm Time complexity: Not necessarily optimal 1