Kruskal’s Algorithm Theory/Goal Find the minimum spanning tree of a connected weighted graph o The minimum spanning tree is the smallest graph that connects every vertex of a graph o Or a minimum spanning tree (MST) is a sub-graph that connects each vertex via the paths with the smallest weight notice the vertices have natural order o either alphabetical or numerical o this is important if we have a tie for cost, which do we select first is now a procedure instead of guessing The result is a connected weighed graph and its minimum spanning tree o T = K(G(V, E)) Kruskal results Given Result 1
24
Embed
faculty.cse.tamu.edufaculty.cse.tamu.edu/.../DataStructures/KruskalsNotes.docx · Web viewKruskal’s Algorithm Theory/Goal Find the minimum spanning tree of a connected weighted
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Kruskal’s AlgorithmTheory/Goal
Find the minimum spanning tree of a connected weighted grapho The minimum spanning tree is the smallest graph that connects every
vertex of a grapho Or a minimum spanning tree (MST) is a sub-graph that connects each
vertex via the paths with the smallest weight notice the vertices have natural order
o either alphabetical or numericalo this is important if we have a tie for cost, which do we select first is now
a procedure instead of guessing The result is a connected weighed graph and its minimum spanning tree
o T = K(G(V, E))
Kruskal resultsGiven Result
1
Solving Step by Step big-picture approach must have values to all edges between nodes sort the segments between nodes by length
o how different between Prim’s and Kruskal’s start with the same graph vertices (and locations), but not edges one at a time “re-connect” the segments until every node can be reached If two edges have the same weight, check the lowest vertex value first, check
for cycles, then choose the other
Kruskal’s Step by Step
replace this with video screen capture
2
3
Merging Segments in a Disjoint Set remember, we are using a disjointed set to eliminate the possibility of cycles
within the grapho that would no longer make the result a tree (MST to be exact)
but as we go further along, a forest develops until finally there is one treeo as we begin to merge trees into larger trees, we union by # of nodes!!
Merging 2 simple verticesmerging 2 vertices
Union(1,6)… find(1) = 1… find(6) = 6, different, ok to union
The circled “6” will haunt us laterNotice that EACH segment is ordered low to high # vertex
10
Final result
1. Why are all (except one) vertices in the disjoint set 6?2. Why is the disjointed value for 6 the value -8?
11
Try this one on your own. You might want to draw a Disjointed Set. Answerb:YOU ARE ONLY MERGING ROOTS!!
Try another one. Answerb:
12
Pseudocode the overall code is not hard but the underlying data structure used to store the values can be very different
o why we are doing pseudocode
Kruskal PseudoCode
KRUSKAL(G):A = ∅foreach v ∈ G.V:
MAKE-SET(v)foreach (u, v) ordered by weight(u, v), increasing:
if FIND-SET(u) ≠ FIND-SET(v):A = A ∪ {(u, v)}UNION(u, v)
return A
Kruskal’s vs. Prim’s If the edge weights in your graph are all different from each other
o unique minimum spanning treeo Kruskal's and Prim's algorithms are guaranteed to return the MST
If the edge weights in the graph are not all different o more than on edge has the same weighto neither algorithm is necessarily deterministic since they both have steps
of the form "choose the lowest-weight edge that satisfies some condi-tion" that might yield ambiguous results
remember both are algorithms for finding a minimum spanning tree (MST)o Prim's grows a single tree using a Priority Queue o Kruskal's uses Union/Find to merge several subtrees into a minimum
spanning tree
13
Time Complexity Kruskal’s Algorithm runs in O (E log E) time where E is the number of edges in the graph
14
Real-life Application: Power GridMinimum-cost spanning trees are used to model electrical networks in order to minimize the total amount of wiring required, thus reducing the total cost of a power grid. In a given model, each node represents a power station on the grid. Using Kruskal's Algorithm, we can connect all of the nodes in the optimum layout that minimizes cost and redundancy.
15
Real Life Application: Telephone linesOne real world application of Kruskal's Algorithm to solve minimum spanning tree problems regards when a telephone company must lay down new telephone or cable lines. Using Kruskal's Algorithm the green lines on the right graph represents the minimum spanning tree for this particular situation. By analyzing the weights of each path, which would be affected by how far the cables would have to go, the telephone company is able to save themselves greatly on the cost of the amount of wires while still getting phone or cable service to each house.