47
Minimum Spanning Trees Based on Mahesh Viswanathan’s notes

Minimum Spanning Trees - Oregon State Universityclasses.engr.oregonstate.edu/eecs/winter2014/cs325-001/MST.pdf• Prim’s algorithm – Pick edge with minimum attachment cost to current

Embed Size (px)

Citation preview

Minimum Spanning Trees

Based on Mahesh Viswanathan’s notes

MST: the problem

• Input: Connected graph  with edge costs• Goal: find  such that  is connected and the total cost of all edges in  is the smallest– T is called the Minimum spanning tree of G

Greedy Template

is empty (* will store edges of a MST*)

While is not spanning tree yet (* 1 ∗select ∈ to add to according to a greedy criterion

Return

How should we choose the edge to add to  ?

Kruskal’s algorithm

Kruskal’s algorithm

Kruskal’s algorithm

Kruskal’s algorithm

Kruskal’s algorithm

Kruskal’s algorithm

Prim’s algorithm

Prim’s algorithm

Prim’s algorithm

Prim’s algorithm

Prim’s algorithm

Prim’s algorithm

Prim’s algorithm

Correctness

• For now, we will assume that all edge costs are distinct

Cut property

Lemma• Let  and  . Let  be the 

minimum cost edge with one end in  and the other end in  . Then every MST contains e.

Let’s try this proof:• Suppose (for contradiction) that  is not in MST • Since  is connected (being a tree), there must be some 

edge  with one end in  and the other in • Since  is cheaper than  ,  is a 

cheaper spanning tree′might not be a spanning tree

Error in proof:

Proof of cut property

Proof of cut property

Proof of cut property

Proof of cut property

Show that  is a spanning tree

• is connected– If path uses , then go from  to  , use edge  , then go from  to 

• is acyclic– has only one cycle, one that involves both 

and – Removing  removes the cycle

Correctness of the greedy algorithms

• Prim’s algorithm– Pick edge with minimum attachment cost to current tree, and add to current tree.

• Proof of correctness– If  is added to  , it belongs to every MST

• Let  be the vertices connected by edges in T when  is added

• e is the lowest cost edge with one end in  and the other in ∖ .

– Set of edges forms a spanning tree• No cycles are created because we always add an edge from  an unattached vertex

Prim’s algorithm

Prim’s algorithm

Prim’s algorithm

Prim’s algorithm

Prim’s algorithm

Prim’s algorithm

Prim’s algorithm

Correctness of the greedy algorithms

• Kruskal’s– Pick edge of lowest cost and add if it does not form a cycle with existing edges

• Proof of correctness: – When an edge  is added, let  be the connected components in  that contains 

– is the minimum cost edge with one end in  and one end in 

Kruskal’s algorithm

Kruskal’s algorithm

Kruskal’s algorithm

Kruskal’s algorithm

When edge costs are not distinct

• Order edges lexicographically to break ties• Both Prim’s, kruscal’s are optimal with respect to the lexicographical ordering

Implementation & Data structure

Prim’s algorithm• Idea: Pick edge with minimum attachment cost to current tree, and add to 

current tree

Implementing Prim’sis the set of all edges in

randomly pick a node u and is empty (* stores edge of a MST*)

while pick , ∈ such that

∈ , ∉has minimum cost

∪ ; ∪return set

Analysis: • Number of iterations:  | |• Picking  in each iteration is  | |• Total time 

More efficient implementation

∪ ,  ∪ ,

Maintaining the costs for attaching  to 

Analysis:  ( ‐ same as Dijkstra’s• each node is inserted and deleted once from the priority queue ( log | |• Each edge is checked once, leading one possible decreasekey ( log

Kruskal’s algorithm• Idea: pick edge of lowest cost and add if it does not form a cycle with 

existing edgesImplementing Kruskal’s

sort the edges in in increasing order based on costis empty (* stores edge of a MST*)

for each edge in sorted orderif ∪ does not creates cycle

∪return set

Analysis: • Sorting the edges: | |log| |• For loop executes O times• Each time, deciding if adding an edge  , leads to cycle: 

– Run DFS or BFS on  to see if  and  are connected O(|V|+|E|)• Total time  log

Efficient Implementation of Kruskal’s• Use Union‐by‐rank data structure to maintain a set of disjoint sets• Each set contains the nodes of a particular connected component• Initially each node is in a component by itself

Every node is connected to only itself

If  and  are not in the same connected component

Merge the two connected components of  and

Union‐by‐rank data structure

• Maintain the nodes in a component as a tree• The root of the tree represents the component• creates a single node tree rooted at 

• traverse the tree from  to its root, and return the root

• merges two trees into a single one– Keep track of the rank of each node (the height of subtree rooted at that node)

– When merging two trees, pick the root that has higher rank values to be the root

• Property: any root node of rank  has at least nodes in the tree

– Try to work out the proof (by induction on k) by yourself

• There are maximum of  nodes in the tree– The maximum rank is thus – runs in  time

• Total running time:

Every node is connected to only itself

If  and  are not in the same connected component

Merge the two connected components of  and