minimum spanning tree

16
Minimum Spanning Tree Sarah Brubaker Tuesday 4/22/8

Upload: finnea

Post on 06-Jan-2016

18 views

Category:

Documents


1 download

DESCRIPTION

Minimum Spanning Tree. Sarah Brubaker Tuesday 4/22/8. Minimum Spanning Tree. Input: graph G with weights on the edges Output: connected sub graph G’ of G that includes all the vertices of G, of minimum total weight. Exhaustive Search. List all connected sub-graphs of G - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Minimum Spanning Tree

Minimum Spanning Tree

Sarah BrubakerTuesday 4/22/8

Page 2: Minimum Spanning Tree

Minimum Spanning Tree

• Input: graph G with weights on the edges• Output: connected sub graph G’ of G that

includes all the vertices of G, of minimum total weight

Page 3: Minimum Spanning Tree

Exhaustive Search

• List all connected sub-graphs of G• Return sub-graph with least weight• Number of vertices, N, and number of edges,

M• O(mn-1)

Page 4: Minimum Spanning Tree

Greedy Algorithm

• Start with a graph containing just the vertices, G’={V,Ø}

• Add edges with the least weight until the graph is connected but no cycles are created

• To do this:– Order the edges in increasing weight– While the graph is not connected, add an edge– End when all vertices are connected

Page 5: Minimum Spanning Tree

Greedy Algorithm - Example

Initial Graph: List of Edges:Edge Weight

AB 1

AF 5

BC 8

BD 4

BF 5

CD 2

CF 3

DE 10

DF 1

EF 4

Page 6: Minimum Spanning Tree

Greedy Algorithm - Example

Graph: Sorted List of Edges:Edge Weight

AB 1

DF 1

CD 2

CF 3

BD 4

EF 4

AF 5

BF 5

BC 8

DE 10

Start with G’={V,Ø} and sorted list of edges

Page 7: Minimum Spanning Tree

Greedy Algorithm - Example

Graph: Sorted List of Edges:

Add edge from list to graph s.t. no cycles are createdRemove edge from list

Page 8: Minimum Spanning Tree

Greedy Algorithm - Example

Graph: Sorted List of Edges:

Add edge from list to graph s.t. no cycles are createdRemove edge from list

Page 9: Minimum Spanning Tree

Greedy Algorithm - Example

Graph: Sorted List of Edges:Edge Weight

CF 3

BD 4

EF 4

AF 5

BF 5

BC 8

DE 10

Add edge from list to graph s.t. no cycles are createdRemove edge from list

Check for cycles using Depth First Search starting at a vertex in the added edge:•Start at C •C-> D -> F -> C •C gets visited twice => a cycle exists and CF should not be added to the graph

Page 10: Minimum Spanning Tree

Greedy Algorithm - Example

Graph: Sorted List of Edges:Edge Weight

BD 4

EF 4

AF 5

BF 5

BC 8

DE 10

Add edge from list to graph s.t. no cycles are createdRemove edge from list

End when all vertices can be visited (graph is connected)

Check for cycles using Depth First Search starting at a vertex in the added edge:•Start at B •B ->D ->C -> F -> A•If all vertices are visited by DFS, then the graph is connected and we are done

Page 11: Minimum Spanning Tree

Greedy Algorithm - Example

Graph: Sorted List of Edges:Edge Weight

EF 4

AF 5

BF 5

BC 8

DE 10

Add edge from list to graph s.t. no cycles are createdRemove edge from list

End when all vertices can be visited (graph is connected)

Check for cycles using Depth First Search starting at a vertex in the added edge:•Start at E •E -> F -> D -> B -> A ->C•All vertices were visited and there were no cycles => we have found a minimum spanning tree with weight 12.

Page 12: Minimum Spanning Tree

Greedy Algorithm- Psuedocode

• Given G = (V, E)• G’ <- (V,Ø)• While G’ is not connected– Add e Є E to G’ s.t. G’ is acyclic and e is minimum– Remove e from E

Page 13: Minimum Spanning Tree

Greedy Algorithm – Run Time• Initialization – constant • While loop – O(n-1)

– Connected graph has n-1 edges– Must add n-1 edges to the graph for it to be connected

• Find a minimum e ЄE – O(m)• Make sure G’ is acyclic – O(2n)

– DFS is O(m+n)<O(2n) where m<=n-1• Test connectivity of G’ – O(n)

– Can use DFS; could be done in same step as testing acyclicity• Remove e from E - constant• Total Runtime: O(n*(m+2n+n)) ~ O(nm+ n2) -> POLYNOMIAL

Page 14: Minimum Spanning Tree

Error?

• Let X be any subset of the vertices of G, and let edge e be the smallest edge connecting X to G-X. Then e is part of the minimum spanning tree.

Page 15: Minimum Spanning Tree

Error?1. T does not contain e and is a

spanning tree2. e is smallest edge connecting X =

{C,D} to G-X ={A,B,F,E}3. Adding e creates a cycle in T4. Another spanning tree exists, T2

= T+e-f, where f is another edge that could connect X to G-x

5. Because e < f, T was not the MST and e must be part of the MST

1

2

4

Page 16: Minimum Spanning Tree

Error?

• In the greedy algorithm, the edge added was always the smallest.

• Because the smallest edge to connect two parts of the original graph must be included, each edge added must be a part of the final minimum spanning tree.

• In this case, the greedy algorithm is always correct (but we know this is not the case for ALL greedy algorithms).