Upload
others
View
7
Download
0
Embed Size (px)
Citation preview
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
Kruskal segments are ordered low#, high#ex. (5,7), (0,3), (3,7), (4,5), etc..
Before After
4
merging another vertex to an established tree
Union(1,5)… find(1) = 6… find(5) = 5, different, ok to union
Notice this minimizes the height of the tree!(5 now being the root of 16 would have added a level)
Merging Trees, one smaller in height than another
Which one would become the root? Why?? (Draw it out)
5
Merging Trees, tie in number of nodes
Union(1,5)… find(1) = 6… find(5) = 7, different, ok to union
so we have a tie!! Goes to higher numbered vertex
YOU ARE ONLY MERGING ROOTS!!Using the disjointed set above, what would be the result of Union(3,5)? Answerb:
6
Union (3, 6) Merging… oh wait! A CycleUnion(3,6)… find(3) = 7… find(6) = 7, same, STOP!!
Misnomer – Disjoint set data/results does not tell you HOW the Tree is linked together the disjoint set is ONLY to help build the tree
o only checks to see if the two edges are already in a seto the edges will be stored, THAT tells you how the Tree is built
Disjoint set do not tell HOW the tree is constructedExample 1 Example 2
Sorting approach to Kruskal’s7
uses a disjointed set to determine what segments have been establisho and to fend of cycles
let’s sort all edges by weight pick the smallest ones first
o keeping track of who we connect so not to repeat or create a cycle watch the full example
resulting Disjointed set for Example 1
8
Gathering all edges
9
Sorting the edges, then begin selection
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.
Sourceshttp://en.wikipedia.org/wiki/Kruskal's_algorithm http://www.youtube.com/watch?v=wR6JTtAmSWI http://img13.imageshack.us/img13/3689/nlogn.jpg http://en.wikipedia.org/wiki/File:Tree_graph.svg
16
AnswersUnion(3,5)
Before After
Kruskal's algorithm Exercise 1
https://www.youtube.com/watch?v=S9U1-MlVdP4 (with sound)
17
Kruskal's algorithm Exercise 2
segment 1 – 2 (of a weight of 5) was removed since it would create a cycle. But if listed above, ok.
https://www.youtube.com/watch?v=EQVWOgSl9Cc (no sound)
18
Sources: http://www.cs.usfca.edu/~galles/visualization/Kruskal.html
19