Docsity

Spanning Trees - Combinatorial Computing | MAT 3770, Study notes of Mathematics

Partial preview of the text

Download Spanning Trees - Combinatorial Computing | MAT 3770 and more Study notes Mathematics in PDF only on Docsity!

  • Mat
  • Weeks 9 & - Spring

3.3 Spanning Trees

I (^) A spanning tree of a graph G is a subgraph of G that is a tree containing all vertices of G.

I (^) A minimal spanning tree is a spanning tree whose sum of the edge weights (lengths) is as small as possible.

I (^) Problem Statement : Given a graph G = (V, E) with positive edge weights (cost: E → <+), nd the cheapest connected spanning subgraph H of G.

I (^) Note : If H = (V, EH ), then cost(H) =

e∈E (H ) cost(e), i.e., cost of subgraph is sum of costs of edges in subgraph.

Observations

I (^) H must be a tree (if H exists). Why?

  1. must span and be connected
  2. if cycle, then extra edge with positive weight, which could be removed to reduce cost

I (^) If G has n vertices (jV j = n), then any minimal spanning tree of G has N 1 edges.

I (^) A graph with no cycles is called a forest

I (^) A connected forest is called a tree

Prim's Minimal Spanning Tree

Idea: ìGrowî a Tree

  1. Pick an arbitrary vertex in the graph, place in VH
  2. From among the edges going from VH to vertices not in VH , choose a cheapest one, say edge e to vertex x
  3. Add vertex x to VH and e to EH
  4. Repeat process from step 2 until no more vertices remain to be added to VH , which is equivalent to saying jEH j = jVG j 1

Prim's MST Algorithm

Procedure Prim (G): H // PRE: G is connected // POST: H is an MST of G

begin pick an arbitrary vertex x in the vertices of G, and add it to VH, the vertices in H from among the edges incident to x, select the cheapest and add it to EH, the edges in H while |EH| < |VG| - 1 find the cheapest edge <a, b> where a is in VH, and b is in VG - VH add <a, b> to EH, add b to VH end

Kruskal's MST

Idea: connect forests until one tree

  1. Place the vertices of VG into jVG j = n individual subtrees
  2. Find the minimum cost edge, e ∈ EG , which doesn't cause a cycle in the spanning forest
  3. Add e to EH , joining two of the subtrees
  4. repeat process from step 2 until a single spanning tree exists, which is equivalent to saying jEH j = jVG j 1

Kruskal's MST Algorithm

Procedure Kruskal (G): H

// PRE: G is connected

// POST: H is an MST of G

begin

put n vertices into n singleton trees

H = { }

// repeat until tree has n-1 edges

while |EH| < |VG| - 1

a) find the min_cost_edge e in EG

b) if H remains a forest when e is added

add e to VH

c) delete e from EG

end

Implementing Kruskal's Algorithm

I (^) We need to be able to ( quickly ) nd the next cheapest edge

I (^) Using a min-heap vs sorted list

I (^) Heap is better since not every edge may be examined / removed

I (^) But, what's a heap?

Implementation

Heaps can be efciently implemented with arrays, which form an implicit (vs explicit) representation of a tree. For example:

i = 1 2 3 4 5 6 7 8 9 10 11 12 L 20 10 15 8 7 14 3 5 6 4 2 1

Where Children of L[i] are in positions 2i and 2i+1.

20

14

10 15

8 7

5 6 4 2 1

3

The Minñheap Property

An array L[k..n] has the Minñheap property if

∀ i 3 k  i < n 2

; L [i ]  L [ 2 i ] and L [i ]  L [ 2 i + 1 ]

if n is even, then L [ n 2 ]  L [n]

In other words: Parents are smaller than their children , or child in the case n is even.

Example ó Removeñmax

20

14

10 15

8 7

5 6 4 2 1

3

max is deleted, last child is moved up, and trickled down

1

14

10 15

8 7

5 6 4 2

3

Insert Algorithm

I (^) Increment heap size

I (^) Put new value (x) in L[size]

I (^) While x is smaller (bigger) than its parent, swap them (aka percolateñ or bubbleñup)

10 14

8 7

5 6 4 2

3

17

1

15

What if we merely kept an unordered list?

I (^) Delete: nd, delete item, and ll in I (^) array: O (n) + O ( 1 ) + O ( 1 ) = O (n) I (^) linked list: O (n) + O ( 1 ) = O (n)

I (^) Insert: array / linked list: O ( 1 )

An ordered list?

I (^) Delete: nd, delete item, and ll in I (^) array: O ( 1 ) + O ( 1 ) + O (n) = O (n) I (^) linked list: O ( 1 ) + O ( 1 ) = O ( 1 )

I (^) Insert: nd position, insert (move) I (^) array: O (log n) + O (n) = O (n) I (^) linked list: O (n) + O ( 1 ) = O (n)

An Aside: Heapsort

I (^) An array, L, can be sorted as follows:

  1. Turn L[1..n] into a heap ( heapify )
  2. Remove n times, storing the removed (min or max) value at the end of the heap

I (^) How fast is this sort? I (^) Step 2 takes time:

log (n) + log (n 1 ) +    + log ( 1 ) =

∑

i = 1 ::n

log i ∈ O (n log n)

I (^) Step 1? It depends...