MST Project

Embed Size (px)

Citation preview

  • 8/8/2019 MST Project

    1/42

    Minimum SpanningMinimum Spanning

    TreesTrees

    CS 146CS 146

    Prof. SinProf. Sin--Min LeeMin Lee

    Regina WangRegina Wang

  • 8/8/2019 MST Project

    2/42

    Spanning Tree properties:Spanning Tree properties:

    On a connected graph G=(V, E), aspanning tree:

    is a connected subgraph

    acyclic is a tree (|E| = |V| - 1)

    contains all vertices of G

    (spanning)

  • 8/8/2019 MST Project

    3/42

    Spanning treesSpanning trees

    Suppose you have a connected undirected graph

    Connected: every node is reachable from every other node

    Undirected: edges do not have an associated direction

    ...then a spanning tree of the graph is a connected subgraph in

    which there are no cycles Total of 16 different spanning trees for the graph below

    A connected,

    undirected graph

    Four of the spanning trees of the graph

  • 8/8/2019 MST Project

    4/42

    Finding a spanning treeFinding a spanning tree

    BFS

    DFS

    Greedy algorithm

    An undirected graph Result of a BFS

    starting from top

    Result of a DFS

    starting from top

  • 8/8/2019 MST Project

    5/42

    Minimum weight spanning tree (MST)Minimum weight spanning tree (MST)

    On a weighted graph, A MST:

    connects all vertices through edges withleast weights.

    has w(T) w(T) for every other spanningtree T in G

    Adding one edge not in MST will create acycle

  • 8/8/2019 MST Project

    6/42

    Finding Minimum Spanning TreeFinding Minimum Spanning Tree

    Two greedy algorithms

    Kruskals and Prims

    make best decision at every stage

    locally optimal choice globally optimal

    solution.

    Greedy algorithms do not always yields

    optimal solutions.

  • 8/8/2019 MST Project

    7/42

    Kruskals algorithmKruskals algorithm

    EdgeEdge firstfirst

    1. Arrange all edges in a list (L)1. Arrange all edges in a list (L)

    in nonin non--decreasing orderdecreasing order

    2.2. Select edges from L, and includeSelect edges from L, and includethat in set T, avoid cycle.that in set T, avoid cycle.

    3.3. Repeat 3 until T becomes a treeRepeat 3 until T becomes a tree

    that covers all verticesthat covers all vertices

  • 8/8/2019 MST Project

    8/42

    KruskalKruskals Algorithms Algorithm

    {{1,2}1,2} 1122

    {{3,4}3,4} 1212

    {{1,8}1,8} 1313

    {{4,5}4,5} 1313{{2,7}2,7} 1414

    {{3,6}3,6} 1414

    {{7,8}7,8} 1414

    {{5,6}5,6} 1414

    {5,8}{5,8} 1515

    {6,7}{6,7} 1515

    {1,4}{1,4} 1616

    {2,3}{2,3} 1616

    8 5

    1 4

    2 3

    7 6

    14

    15

    15

    14

    13 13

    16

    16

    1212

    1414

  • 8/8/2019 MST Project

    9/42

    KruskalKruskals Algorithms Algorithm

    {{1,2}1,2} 1122

    {{3,4}3,4} 1212

    {{1,8}1,8} 1313

    {{4,5}4,5} 1313{{2,7}2,7} 1414

    {{3,6}3,6} 1414

    {{7,8}7,8} 1414

    {{5,6}5,6} 1414

    {5,8}{5,8} 1515

    {6,7}{6,7} 1515

    {1,4}{1,4} 1616

    {2,3}{2,3} 1616

    8 5

    1 4

    2 3

    7 6

    14

    15

    15

    14

    13 13

    16

    16

    1212

    1414

  • 8/8/2019 MST Project

    10/42

    KruskalKruskals Algorithms Algorithm

    {{1,2}1,2} 1122

    {{3,4}3,4} 1212

    {{1,8}1,8} 1313

    {{4,5}4,5} 1313{{2,7}2,7} 1414

    {{3,6}3,6} 1414

    {{7,8}7,8} 1414

    {{5,6}5,6} 1414

    {5,8}{5,8} 1515

    {6,7}{6,7} 1515

    {1,4}{1,4} 1616

    {2,3}{2,3} 1616

    8 5

    1 4

    2 3

    7 6

    14

    15

    15

    14

    13 13

    16

    16

    1212

    1414

  • 8/8/2019 MST Project

    11/42

    KruskalKruskals Algorithms Algorithm

    {{1,2}1,2} 1122

    {{3,4}3,4} 1212

    {{1,8}1,8} 1313

    {{4,5}4,5} 1313{{2,7}2,7} 1414

    {{3,6}3,6} 1414

    {{7,8}7,8} 1414

    {{5,6}5,6} 1414

    {5,8}{5,8} 1515

    {6,7}{6,7} 1515

    {1,4}{1,4} 1616

    {2,3}{2,3} 1616

    8 5

    1 4

    2 3

    7 6

    14

    15

    15

    14

    13 13

    16

    16

    1212

    1414

  • 8/8/2019 MST Project

    12/42

    KruskalKruskals Algorithms Algorithm

    {{1,2}1,2} 1122

    {{3,4}3,4} 1212

    {{1,8}1,8} 1313

    {{4,5}4,5} 1313{{2,7}2,7} 1414

    {{3,6}3,6} 1414

    {{7,8}7,8} 1414

    {{5,6}5,6} 1414

    {5,8}{5,8} 1515

    {6,7}{6,7} 1515

    {1,4}{1,4} 1616

    {2,3}{2,3} 1616

    8 5

    1 4

    2 3

    7 6

    14

    15

    15

    14

    13 13

    16

    16

    1212

    1414

  • 8/8/2019 MST Project

    13/42

    KruskalKruskals Algorithms Algorithm

    {{1,2}1,2} 1122

    {{3,4}3,4} 1212

    {{1,8}1,8} 1313

    {{4,5}4,5} 1313{{2,7}2,7} 1414

    {{3,6}3,6} 1414

    {{7,8}7,8} 1414

    {{5,6}5,6} 1414

    {5,8}{5,8} 1515

    {6,7}{6,7} 1515

    {1,4}{1,4} 1616

    {2,3}{2,3} 1616

    8 5

    1 4

    2 3

    7 6

    14

    15

    15

    14

    13 13

    16

    16

    1212

    1414

  • 8/8/2019 MST Project

    14/42

    KruskalKruskals Algorithms Algorithm

    {{1,2}1,2} 1122

    {{3,4}3,4} 1212

    {{1,8}1,8} 1313

    {{4,5}4,5} 1313{{2,7}2,7} 1414

    {{3,6}3,6} 1414

    {{7,8}7,8} 1414

    {{5,6}5,6} 1414

    {5,8}{5,8} 1515

    {6,7}{6,7} 1515

    {1,4}{1,4} 1616

    {2,3}{2,3} 1616

    8 5

    1 4

    2 3

    7 6

    14

    15

    15

    14

    13 13

    16

    16

    1212

    1414

  • 8/8/2019 MST Project

    15/42

    KruskalKruskals Algorithms Algorithm

    {{1,2}1,2} 1122

    {{3,4}3,4} 1212

    {{1,8}1,8} 1313

    {{4,5}4,5} 1313{{2,7}2,7} 1414

    {{3,6}3,6} 1414

    {{7,8}7,8} 1414

    {{5,6}5,6} 1414

    {5,8}{5,8} 1515

    {6,7}{6,7} 1515

    {1,4}{1,4} 1616

    {2,3}{2,3} 1616

    8 5

    1 4

    2 3

    7 6

    14

    15

    15

    14

    13 13

    16

    16

    1212

    1414

    Skip

    Skip {7,8} to avoid cycle

  • 8/8/2019 MST Project

    16/42

    KruskalKruskals Algorithms Algorithm

    {{1,2}1,2} 1122

    {{3,4}3,4} 1212

    {{1,8}1,8} 1313

    {{4,5}4,5} 1313{{2,7}2,7} 1414

    {{3,6}3,6} 1414

    {{7,8}7,8} 1414

    {{5,6}5,6} 1414

    {5,8}{5,8} 1515

    {6,7}{6,7} 1515

    {1,4}{1,4} 1616

    {2,3}{2,3} 1616

    8 5

    1 4

    2 3

    7 6

    14

    15

    15

    14

    13 13

    16

    16

    1212

    1414

    Skip

    Skip {5,6} to avoid cycle

    Skip

  • 8/8/2019 MST Project

    17/42

    KruskalKruskals Algorithms Algorithm

    {{1,2}1,2} 1122

    {{3,4}3,4} 1212

    {{1,8}1,8} 1313

    {{4,5}4,5} 1313{{2,7}2,7} 1414

    {{3,6}3,6} 1414

    {{7,8}7,8} 1414

    {{5,6}5,6} 1414

    {5,8}{5,8} 1515

    {6,7}{6,7} 1515

    {1,4}{1,4} 1616

    {2,3}{2,3} 1616

    8 5

    1 4

    2 3

    7 6

    14

    15

    15

    14

    13 13

    16

    16

    1212

    1414

    MST is formed

  • 8/8/2019 MST Project

    18/42

    Prims algorithmPrims algorithm

    Start form any arbitraryStart form any arbitrary vertexvertex

    Find the edge that has minimumFind the edge that has minimum

    weight formweight form allall known verticesknown vertices

    Stop when the tree covers allStop when the tree covers all

    verticesvertices

  • 8/8/2019 MST Project

    19/42

    8 5

    1 4

    2 3

    7 6

    14

    15

    15

    14

    13 1316

    16

    1212

    1414

    1

    Start from any arbitrary vertex

    Prims algorithmPrims algorithm

  • 8/8/2019 MST Project

    20/42

    8 5

    1 4

    2 3

    7 6

    14

    15

    15

    14

    13 1316

    16

    12

    1414

    1

    2

    Weight:12

    The best

    choice is {1,2}

    Prims algorithmPrims algorithm

    12

    Whats next?

  • 8/8/2019 MST Project

    21/42

    8 5

    1 4

    2 3

    7 6

    14

    15

    15

    14

    1316

    16

    12

    1414

    1

    2

    Weight:12+13

    After {1,8} we may

    choose {2,7} or

    {7,8}

    There are more

    than one MST

    Prims algorithmPrims algorithm

    12

    Whats next?

    8

    13

  • 8/8/2019 MST Project

    22/42

    8 5

    1 4

    2 3

    7 6

    14

    15

    15

    14

    1316

    16

    12

    14

    1

    2

    Lets choose{2, 7} at this

    moment

    Prims algorithmPrims algorithm

    12 We are free to

    choose {5,8} or

    {6,7} but not {7,8}

    because we need

    to avoid cycle

    8

    13

    14

    7

  • 8/8/2019 MST Project

    23/42

    8 5

    1 4

    2 3

    7 6

    14

    15

    14

    1316

    16

    12

    14

    1

    2

    Lets choose

    {5,8} at this

    moment

    Prims algorithmPrims algorithm

    12

    The best choice

    is now {4,5}

    8

    13

    14

    7

    155

  • 8/8/2019 MST Project

    24/42

    8 5

    1 4

    2 3

    7 6

    14

    15

    14

    16

    16

    12

    14

    1

    2

    Prims algorithmPrims algorithm

    12The best choice

    is now {4,3}

    8

    13

    14

    7

    155

    13

    4

  • 8/8/2019 MST Project

    25/42

    8 5

    1 4

    2 3

    7 6

    14

    15

    14

    16

    16

    14

    1

    2

    Prims algorithmPrims algorithm

    12The best choice

    is now {3,6} or

    {5,6}

    8

    13

    14

    7

    155

    13

    4

    12

    3

  • 8/8/2019 MST Project

    26/42

    8 5

    1 4

    2 3

    7 6

    14

    15

    14

    16

    16

    1

    2

    Prims algorithmPrims algorithm

    12A MST is formed

    Weight = 93

    8

    13

    14

    7

    155

    13

    4

    12

    3

    6

    14

  • 8/8/2019 MST Project

    27/42

    Prims algorithmPrims algorithm moremore

    complexcomplex

    0

    5

    20

    54

    3

    4414

    99

    32

    8100

    11

    1431

    19

  • 8/8/2019 MST Project

    28/42

    Prims algorithmPrims algorithm moremore

    complexcomplex

    0

    5

    20

    54

    3

    4414

    99

    32

    8100

    11

    1431

    19

  • 8/8/2019 MST Project

    29/42

    Prims algorithmPrims algorithm

    0

    5

    20

    54

    3

    4414

    99

    32

    8100

    11

    1431

    19

  • 8/8/2019 MST Project

    30/42

    Prims algorithmPrims algorithm

    0

    5

    20

    54

    3

    4414

    99

    32

    8100

    11

    1431

    19

  • 8/8/2019 MST Project

    31/42

    Prims algorithmPrims algorithm

    0

    5

    20

    54

    3

    4414

    99

    32

    8100

    11

    1431

    19

  • 8/8/2019 MST Project

    32/42

    Prims algorithmPrims algorithm

    0

    5

    20

    54

    3

    4414

    99

    32

    8100

    11

    1431

    19

  • 8/8/2019 MST Project

    33/42

    Prims algorithmPrims algorithm

    0

    5

    20

    54

    3

    4414

    99

    32

    8100

    11

    1431

    19

  • 8/8/2019 MST Project

    34/42

    Prims algorithmPrims algorithm

    0

    5

    20

    54

    3

    4414

    99

    32

    8100

    11

    1431

    19

  • 8/8/2019 MST Project

    35/42

    Prims algorithmPrims algorithm

    0

    5

    20

    54

    3

    4414

    99

    32

    8100

    11

    1431

    19

    MST is formed

  • 8/8/2019 MST Project

    36/42

    Compare Prim and KruskalCompare Prim and Kruskal

    Both have the same output MST

    Kruskals begins with forest and merge into a tree

    Prims always stays as a tree

    If you dont know all the weight on edges usePrims algorithm

    If you only need partial solution on the graph usePrims algorithm

  • 8/8/2019 MST Project

    37/42

    Compare Prim and KruskalCompare Prim and Kruskal

    Complexity

    Kruskal: O(NlogN)

    comparison sort for edgesPrim: O(NlogN)

    search the least weight edge for

    every vertice

  • 8/8/2019 MST Project

    38/42

    Why do we need MST?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

  • 8/8/2019 MST Project

    39/42

    Minimizing costsMinimizing costs

    Suppose you want to provide solution to :

    electric power, Water, telephone lines,network setup

    To minimize cost, you could connectlocations using MST

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

  • 8/8/2019 MST Project

    40/42

    MST dont solve TSPMST dont solve TSP

    Travel 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

  • 8/8/2019 MST Project

    41/42

    More practiceMore practice

    Kruskal's AlgorithmKruskal's Algorithm appletapplet

    Prims AlgorithmPrims Algorithm appletapplet

  • 8/8/2019 MST Project

    42/42

    The EndThe End

    Thank youThank you