24
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

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

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 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

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

Page 2: 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

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

Page 3: 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

3

Page 4: 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

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

Page 5: 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

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

Page 6: 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

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

Page 7: 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

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

Page 8: 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

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

Page 9: 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

Gathering all edges

9

Page 10: 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

Sorting the edges, then begin selection

The circled “6” will haunt us laterNotice that EACH segment is ordered low to high # vertex

10

Page 11: 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

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

Page 12: 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

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

Page 13: 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

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

Page 14: 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

Time Complexity Kruskal’s Algorithm runs in O (E log E) time where E is the number of edges in the graph

14

Page 15: 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

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

Page 16: 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

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

Page 17: 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

AnswersUnion(3,5)

Before After

Kruskal's algorithm Exercise 1

https://www.youtube.com/watch?v=S9U1-MlVdP4 (with sound)

17

Page 18: 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

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

Page 19: 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

Sources: http://www.cs.usfca.edu/~galles/visualization/Kruskal.html

19