Download presentation
Presentation is loading. Please wait.
Published byDaniel Richard Modified over 8 years ago
1
Minimum Spanning Trees Kruskal's and Prim's algorithms
2
Announcements USACO is this week. o Take it any day between Friday and next Tuesday. o Last USACO of the school year! Register for Google Codejam o Qualification round on April 12th. Johnny Ho has been impeached. We will have a presidential election now. Candidates are: o 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 pii; priority_queue 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.