minimum spanning trees

Click here to load reader

Post on 05-Feb-2016

27 views

Category:

Documents

3 download

Embed Size (px)

DESCRIPTION

Minimum Spanning Trees. CS 146 Prof. Sin-Min Lee Regina Wang. Spanning Tree properties:. On a connected graph G=(V, E), a spanning tree: is a connected subgraph acyclic is a tree (|E| = |V| - 1) contains all vertices of G (spanning). A connected, undirected graph. - PowerPoint PPT Presentation

TRANSCRIPT

  • Minimum Spanning TreesCS 146Prof. Sin-Min LeeRegina Wang

  • Spanning Tree properties:On a connected graph G=(V, E), a spanning tree:is a connected subgraphacyclicis a tree (|E| = |V| - 1) contains all vertices of G (spanning)

  • Spanning treesSuppose you have a connected undirected graphConnected: every node is reachable from every other nodeUndirected: edges do not have an associated direction...then a spanning tree of the graph is a connected subgraph in which there are no cyclesTotal of 16 different spanning trees for the graph below

  • Finding a spanning treeBFSDFSGreedy algorithm

  • Minimum weight spanning tree (MST)On a weighted graph, A MST:connects all vertices through edges with least weights.

    has w(T) w(T) for every other spanning tree T in G

    Adding one edge not in MST will create a cycle

  • Finding Minimum Spanning Tree Two greedy algorithmsKruskals and Primsmake best decision at every stagelocally optimal choice globally optimal solution.Greedy algorithms do not always yields optimal solutions.

  • Kruskals algorithm Edge first1. Arrange all edges in a list (L) in non-decreasing orderSelect edges from L, and include that in set T, avoid cycle.Repeat 3 until T becomes a tree that covers all vertices

  • Kruskals Algorithm85142376141515141313161612121414

    {1,2}12{3,4}12{1,8}13{4,5}13{2,7}14{3,6}14{7,8}14{5,6}14{5,8}15{6,7}15{1,4}16{2,3}16

  • Kruskals Algorithm85142376141515141313161612121414

    {1,2}12{3,4}12{1,8}13{4,5}13{2,7}14{3,6}14{7,8}14{5,6}14{5,8}15{6,7}15{1,4}16{2,3}16

  • Kruskals Algorithm85142376141515141313161612121414

    {1,2}12{3,4}12{1,8}13{4,5}13{2,7}14{3,6}14{7,8}14{5,6}14{5,8}15{6,7}15{1,4}16{2,3}16

  • Kruskals Algorithm85142376141515141313161612121414

    {1,2}12{3,4}12{1,8}13{4,5}13{2,7}14{3,6}14{7,8}14{5,6}14{5,8}15{6,7}15{1,4}16{2,3}16

  • Kruskals Algorithm85142376141515141313161612121414

    {1,2}12{3,4}12{1,8}13{4,5}13{2,7}14{3,6}14{7,8}14{5,6}14{5,8}15{6,7}15{1,4}16{2,3}16

  • Kruskals Algorithm85142376141515141313161612121414

    {1,2}12{3,4}12{1,8}13{4,5}13{2,7}14{3,6}14{7,8}14{5,6}14{5,8}15{6,7}15{1,4}16{2,3}16

  • Kruskals Algorithm85142376141515141313161612121414

    {1,2}12{3,4}12{1,8}13{4,5}13{2,7}14{3,6}14{7,8}14{5,6}14{5,8}15{6,7}15{1,4}16{2,3}16

  • Kruskals Algorithm85142376141515141313161612121414SkipSkip {7,8} to avoid cycle

    {1,2}12{3,4}12{1,8}13{4,5}13{2,7}14{3,6}14{7,8}14{5,6}14{5,8}15{6,7}15{1,4}16{2,3}16

  • Kruskals Algorithm85142376141515141313161612121414SkipSkip {5,6} to avoid cycle Skip

    {1,2}12{3,4}12{1,8}13{4,5}13{2,7}14{3,6}14{7,8}14{5,6}14{5,8}15{6,7}15{1,4}16{2,3}16

  • Kruskals Algorithm85142376141515141313161612121414MST is formed

    {1,2}12{3,4}12{1,8}13{4,5}13{2,7}14{3,6}14{7,8}14{5,6}14{5,8}15{6,7}15{1,4}16{2,3}16

  • Prims algorithmStart form any arbitrary vertex Find the edge that has minimum weight form all known verticesStop when the tree covers all vertices

  • Prims algorithm851423761415151413131616121214141Start from any arbitrary vertex

  • Prims algorithm85142376141515141313161612141412Weight:12The best choice is {1,2}12Whats next?

  • Prims algorithm851423761415151413161612141412Weight:12+13After {1,8} we may choose {2,7} or {7,8}There are more than one MST 12Whats next?813

  • Prims algorithm8514237614151514131616121412Lets choose {2, 7} at this moment12We are free to choose {5,8} or {6,7} but not {7,8} because we need to avoid cycle813147

  • Prims algorithm85142376141514131616121412Lets choose {5,8} at this moment12The best choice is now {4,5}813147155

  • Prims algorithm85142376141514161612141212The best choice is now {4,3}813147155134

  • Prims algorithm851423761415141616141212The best choice is now {3,6} or {5,6}813147155134123

  • Prims algorithm8514237614151416161212A MST is formedWeight = 93813147155134123614

  • Prims algorithm more complex 052054344149932810011143119

  • Prims algorithm more complex 052054344149932810011143119

  • Prims algorithm 052054344149932810011143119

  • Prims algorithm 052054344149932810011143119

  • Prims algorithm 052054344149932810011143119

  • Prims algorithm 052054344149932810011143119

  • Prims algorithm 052054344149932810011143119

  • Prims algorithm 052054344149932810011143119

  • Prims algorithm 052054344149932810011143119MST is formed

  • Compare Prim and KruskalBoth have the same output MSTKruskals begins with forest and merge into a treePrims always stays as a treeIf you dont know all the weight on edges use Prims algorithmIf you only need partial solution on the graph use Prims algorithm

  • Compare Prim and KruskalComplexityKruskal: O(NlogN) comparison sort for edgesPrim: O(NlogN) search the least weight edge for every vertice

  • Why do we need MST? a reasonable way for clustering points in space into natural groups can be used to give approximate solutions to hard problems

  • Minimizing costsSuppose you want to provide solution to :electric power, Water, telephone lines, network setup

    To minimize cost, you could connect locations using MST

    However, MST is not necessary the shortest path and it does not apply to cycle

  • MST dont solve TSPTravel salesman problem (TSP) can not be solved by MST : salesman needs to go home (whats the cost going home?) TSP is a cycle Use MST to approximate solve TSP by exhaustive approach try every permutation on cyclic graph

  • More practiceKruskal's Algorithm applet

    Prims Algorithm applet

  • The EndThank you