1 / 12

Minimum Spanning Trees

Minimum Spanning Trees. Kruskal's and Prim's algorithms. Announcements. USACO is this week. Take it any day between Friday and next Tuesday. Last USACO of the school year! Register for Google Codejam Qualification round on April 12th. Johnny Ho has been impeached.

nam
Download Presentation

Minimum Spanning Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Minimum Spanning Trees Kruskal's and Prim's algorithms

  2. Announcements • USACO is this week. • Take it any day between Friday and next Tuesday. • Last USACO of the school year! • Register for Google Codejam • Qualification round on April 12th. • Johnny Ho has been impeached. • We will have a presidential election now. • Candidates are: • Wenkai Lei

  3. (April Fools)

  4. Definition • A minimum spanning tree of an undirected, connected graph is a spanning tree of minimum weight. • A spanning tree is a tree composed of all the vertices of a graph, and some or all of its edges. • Note that there may be multiple minimum spanning trees (but only one if edge weights are unique).

  5. Optimality conditions • Cycle property: Any edge in the graph not in the MST forms a cycle with the MST, and this edge must be the maximum edge on the cycle. • Cut property: Any edge in the MST crossing a cut C of the graph will be the minimum edge crossing the cut.

  6. Prim's algorithm • Start the tree with a single source node. • Consider all edges adjacent to one tree node and one non-tree node. • Pick the minimum edge and add it to the tree. • Repeat until the tree covers all vertices. • Use a simple array for O(n^2) time or a priority queue for O(mlogn) time. • Similar to Dijkstra's algorithm.

  7. Prim's algorithm pseudocode typedef pair<int, int> pii; priority_queue<pii> pq; pq.push(pii(0, start)); while (!pq.empty()) { pii p = pq.top(); pq.pop(); int cur = p.second; if (vis[cur]) continue; //add edge to MST vis[cur]=true; for pii p2 in adj[cur]: pq.push(p2); }

  8. Kruskal's algorithm • Start with each vertex as a separate tree. • Sort edges by nondecreasing weight. • For each edge, if the two vertices are part of the same tree, ignore it. Otherwise, use this edge to combine the two trees. • Repeat until there is only a single connected tree. • Runs in O(mlogn) time, or O(m a(n)) time if edges are pre-sorted. • Use Union-Find data structure.

  9. Kruskal's algorithm pseudocode for(int i=0;i<n;i++) MakeSet(i); sort(edges); for(int i=0;i<length(edges);i++) { int a=edges[i].src, b=edges[i].dest; if(find(a) != find(b)) { union(find(a), find(b)); //add edge to MST } }

  10. Potw • Farmer John is irrigating his fields and wants to bring water to his N pastures. • He needs all of his pastures to be connected by some sequence of pipes to pasture 1, the water tank. • FJ has M possible pipes he can use, each connecting two pastures, and pipe has a cost c_i. • Pipes connected to pastures disturb the cows there, so there is an additional cost of p_i * d for each pasture, where d is the number of pipes going through/connected to that pasture. • Help FJ find the minimum possible cost.

  11. Potw Input format: Line 1: N M, # pastures and # pipes Line 2...M+1: a_i b_i c_i, the pastures the pipe is connected to and the cost of the pipe Line M+2...M+N+1: p_i, additional cost for this pasture for each pipe going through it Output format: Line 1: C, the minimum total cost (might not fit within a 32 bit integer)

  12. Potw Sample input: 4 5 1 2 2 1 3 4 1 4 1 2 3 6 3 4 3 10 2 1 5 Sample output: 32 Constraints: 1<= c_i, p_i <= 500,000,000 10 points: 1<=N, M<=100 15 points: 1<=N, M<=1,000 25 points: 1<=N, M<=100,000

More Related