Docstoc

Minimum Spanning Trees (2)

Document Sample
Minimum Spanning Trees (2) Powered By Docstoc
					               CS 332: Algorithms

                    Topological Sort
                Minimum Spanning Trees


David Luebke              1              1/26/2011
           Review: Breadth-First Search
BFS(G, s) {
    initialize vertices;
    Q = {s};        // Q is a queue (duh); initialize to s
    while (Q not empty) {
        u = RemoveTop(Q);
        for each v  u->adj {
            if (v->color == WHITE)
                v->color = GREY;
                v->d = u->d + 1;    v->d represents depth in tree
                v->p = u;           v->p represents parent in tree
                Enqueue(Q, v);
        }
        u->color = BLACK;
    }
}
David Luebke                   2                              1/26/2011
               Review: DFS Code
DFS(G)                              DFS_Visit(u)
{                                   {
   for each vertex u  G->V            u->color = YELLOW;
   {                                   time = time+1;
       u->color = WHITE;               u->d = time;
   }                                   for each v  u->Adj[]
   time = 0;                           {
   for each vertex u  G->V               if (v->color == WHITE)
   {                                         DFS_Visit(v);
       if (u->color == WHITE)          }
          DFS_Visit(u);                u->color = BLACK;
   }                                   time = time+1;
}                                      u->f = time;
                                    }


David Luebke                    3                            1/26/2011
                  Review: DFS Example
source
vertex




   David Luebke            4            1/26/2011
                      Review: DFS Example
source
vertex
                  d       f
                  1 |              |        |


    |                                  |


                      |            |        |




   David Luebke                5                1/26/2011
                      Review: DFS Example
source
vertex
                  d       f
                  1 |              |        |


 2 |                                   |


                      |            |        |




   David Luebke                6                1/26/2011
                      Review: DFS Example
source
vertex
                  d     f
                  1 |              |        |


 2 |                                   |


                  3 |              |        |




   David Luebke                7                1/26/2011
                      Review: DFS Example
source
vertex
                  d     f
                  1 |              |        |


 2 |                                   |


                  3 | 4            |        |




   David Luebke                8                1/26/2011
                      Review: DFS Example
source
vertex
                  d     f
                  1 |              |        |


 2 |                                   |


                  3 | 4       5 |           |




   David Luebke                9                1/26/2011
                      Review: DFS Example
source
vertex
                  d     f
                  1 |               |       |


 2 |                                    |


                  3 | 4       5 | 6         |




   David Luebke                10               1/26/2011
                      Review: DFS Example
source
vertex
                  d     f
                  1 |         8 |           |


 2 | 7                                |


                  3 | 4       5 | 6         |




   David Luebke                11               1/26/2011
                      Review: DFS Example
source
vertex
                  d     f
                  1 |         8 |           |


 2 | 7                                |


                  3 | 4       5 | 6         |




   David Luebke                12               1/26/2011
                      Review: DFS Example
source
vertex
                  d     f
                  1 |         8 |           |


 2 | 7                                9 |


                  3 | 4       5 | 6         |




   David Luebke                13               1/26/2011
                      Review: DFS Example
source
vertex
                  d     f
                  1 |         8 |             |


 2 | 7                                9 |10


                  3 | 4       5 | 6           |




   David Luebke                14                 1/26/2011
                      Review: DFS Example
source
vertex
                  d     f
                  1 |         8 |11           |


 2 | 7                                9 |10


                  3 | 4       5 | 6           |




   David Luebke                15                 1/26/2011
                      Review: DFS Example
source
vertex
                  d    f
                  1 |12       8 |11           |


 2 | 7                                9 |10


                  3 | 4       5 | 6           |




   David Luebke                16                 1/26/2011
                      Review: DFS Example
source
vertex
                  d    f
                  1 |12       8 |11           13|


 2 | 7                                9 |10


                  3 | 4       5 | 6             |




   David Luebke                17                   1/26/2011
                      Review: DFS Example
source
vertex
                  d    f
                  1 |12       8 |11           13|


 2 | 7                                9 |10


                  3 | 4       5 | 6           14|




   David Luebke                18                   1/26/2011
                      Review: DFS Example
source
vertex
                  d    f
                  1 |12       8 |11           13|


 2 | 7                                9 |10


                  3 | 4       5 | 6           14|15




   David Luebke                19                     1/26/2011
                      Review: DFS Example
source
vertex
                  d    f
                  1 |12       8 |11           13|16


 2 | 7                                9 |10


                  3 | 4       5 | 6           14|15




   David Luebke                20                     1/26/2011
               Review: Kinds Of Edges

● Thm: If G is undirected, a DFS produces only
  tree and back edges
● Thm: An undirected graph is acyclic iff a DFS
  yields no back edges
● Thus, can run DFS to find cycles




David Luebke             21                  1/26/2011
                      Review: Kinds of Edges
source
vertex
                  d    f
                  1 |12        8 |11           13|16


 2 | 7                                 9 |10


                  3 | 4        5 | 6           14|15


 Tree edges Back edges Forward edges Cross edges

   David Luebke                 22                     1/26/2011
                   DFS And Cycles

● Running time: O(V+E)
● We can actually determine if cycles exist in
     O(V) time:
       ■ In an undirected acyclic forest, |E|  |V| - 1
       ■ So count the edges: if ever see |V| distinct edges,
         must have seen a back edge along the way
       ■ Why not just test if |E| <|V| and answer the
         question in constant time?


David Luebke                     23                            1/26/2011
               Directed Acyclic Graphs

● A directed acyclic graph or DAG is a directed
     graph with no directed cycles:




David Luebke               24                 1/26/2011
                       DFS and DAGs

● Argue that a directed graph G is acyclic iff a
     DFS of G yields no back edges:
       ■ Forward: if G is acyclic, will be no back edges
          ○ Trivial: a back edge implies a cycle
       ■ Backward: if no back edges, G is acyclic
          ○ Argue contrapositive: G has a cycle   a back edge
                Let v be the vertex on the cycle first discovered, and u be the
                 predecessor of v on the cycle
                When v discovered, whole cycle is white

                Must visit everything reachable from v before returning from
                 DFS-Visit()
                So path from uv is yellowyellow, thus (u, v) is a back edge
David Luebke                           25                                   1/26/2011
                         Topological Sort

● Topological sort of a DAG:
       ■ Linear ordering of all vertices in graph G such that
               vertex u comes before vertex v if edge (u, v)  G
● Real-world example: getting dressed




David Luebke                         26                        1/26/2011
                 Getting Dressed
    Underwear                   Socks
                                        Watch
         Pants                  Shoes
                  Shirt

          Belt
                   Tie


                  Jacket




David Luebke               27                   1/26/2011
                         Getting Dressed
        Underwear                                Socks
                                                                   Watch
           Pants                                 Shoes
                               Shirt

            Belt
                                Tie


                               Jacket




Socks      Underwear   Pants     Shoes   Watch    Shirt   Belt   Tie   Jacket

  David Luebke                             28                                   1/26/2011
               Topological Sort Algorithm
Topological-Sort()
{
   Run DFS
   When a vertex is finished, output it
   Vertices are output in reverse
     topological order
}
● Time: O(V+E)
● Correctness: Want to prove that
               (u,v)  G  uf > vf
David Luebke                  29            1/26/2011
       Correctness of Topological Sort

● Claim: (u,v)  G  uf > vf
       ■ When (u,v) is explored, u is yellow
          ○ v = yellow  (u,v) is back edge. Contradiction (Why?)
          ○ v = white  v becomes descendent of u  vf < uf
            (since must finish v before backtracking and finishing u)
          ○ v = black  v already finished  vf < uf




David Luebke                       30                             1/26/2011
                Minimum Spanning Tree

● Problem: given a connected, undirected,
     weighted graph:

                    6            4
                             5                9


               14                         2
                        10
                                              15

                    3            8
David Luebke                         31            1/26/2011
                Minimum Spanning Tree

● Problem: given a connected, undirected,
     weighted graph, find a spanning tree using
     edges that minimize the total weight
                    6            4
                             5                9


               14                         2
                        10
                                              15

                    3            8
David Luebke                         32            1/26/2011
                Minimum Spanning Tree

● Which edges form the minimum spanning tree
     (MST) of the below graph?

                             A
                    6            4
                             5                    9
                H                         B            C

               14                             2
                        10
                                                  15
                G                         E            D
                    3            8
                             F
David Luebke                         33                    1/26/2011
                Minimum Spanning Tree

● Answer:



                             A
                    6            4
                             5                    9
                H                         B            C

               14                             2
                        10
                                                  15
                G                         E            D
                    3            8
                             F
David Luebke                         34                    1/26/2011
               Minimum Spanning Tree
● MSTs satisfy the optimal substructure property: an
     optimal tree is composed of optimal subtrees
       ■ Let T be an MST of G with an edge (u,v) in the middle
       ■ Removing (u,v) partitions T into two trees T1 and T2
       ■ Claim: T1 is an MST of G1 = (V1,E1), and T2 is an MST of
         G2 = (V2,E2)        (Do V1 and V2 share vertices? Why?)
       ■ Proof: w(T) = w(u,v) + w(T1) + w(T2)
         (There can’t be a better tree than T1 or T2, or T would be
         suboptimal)




David Luebke                       35                             1/26/2011
               Minimum Spanning Tree

● Thm:
       ■ Let T be MST of G, and let A  T be subtree of T
       ■ Let (u,v) be min-weight edge connecting A to V-A
       ■ Then (u,v)  T




David Luebke                  36                       1/26/2011
               Minimum Spanning Tree

● Thm:
       ■ Let T be MST of G, and let A  T be subtree of T
       ■ Let (u,v) be min-weight edge connecting A to V-A
       ■ Then (u,v)  T
● Proof: in book (see Thm 24.1)




David Luebke                  37                       1/26/2011
               Prim’s Algorithm
MST-Prim(G, w, r)
    Q = V[G];
    for each u  Q
        key[u] = ;
    key[r] = 0;
    p[r] = NULL;
    while (Q not empty)
        u = ExtractMin(Q);
        for each v  Adj[u]
            if (v  Q and w(u,v) < key[v])
                 p[v] = u;
                 key[v] = w(u,v);


David Luebke             38                  1/26/2011
               Prim’s Algorithm
MST-Prim(G, w, r)            6         4
    Q = V[G];                     5                9
    for each u  Q
        key[u] = ;     14
                               10          2
    key[r] = 0;                                   15
    p[r] = NULL;
                             3        8
    while (Q not empty)
        u = ExtractMin(Q);     Run on example graph
        for each v  Adj[u]
            if (v  Q and w(u,v) < key[v])
                 p[v] = u;
                 key[v] = w(u,v);


David Luebke                39                         1/26/2011
               Prim’s Algorithm
MST-Prim(G, w, r)            6        4
    Q = V[G];                     5                9
                                                       
    for each u  Q
        key[u] = ;     14
                               10          2
    key[r] = 0;                                   15
                                                       
    p[r] = NULL;
    while (Q not empty)
                             3       8

        u = ExtractMin(Q);     Run on example graph
        for each v  Adj[u]
            if (v  Q and w(u,v) < key[v])
                 p[v] = u;
                 key[v] = w(u,v);


David Luebke                40                         1/26/2011
                 Prim’s Algorithm
MST-Prim(G, w, r)            6          4
    Q = V[G];                      5                    9
                                                             
    for each u  Q
        key[u] = ;     14
                               10               2
    key[r] = 0;                                         15
                     r     0                                  
    p[r] = NULL;
    while (Q not empty)
                             3         8

        u = ExtractMin(Q);        Pick a start vertex r
        for each v  Adj[u]
            if (v  Q and w(u,v) < key[v])
                 p[v] = u;
                 key[v] = w(u,v);


David Luebke                    41                           1/26/2011
                Prim’s Algorithm
MST-Prim(G, w, r)            6          4
    Q = V[G];                     5                  9
                                                           
    for each u  Q
        key[u] = ;     14
                               10             2
    key[r] = 0;                                      15
                     u     0                                
    p[r] = NULL;
    while (Q not empty)
                             3         8

        u = ExtractMin(Q); Red vertices have been removed from Q
        for each v  Adj[u]
            if (v  Q and w(u,v) < key[v])
                 p[v] = u;
                 key[v] = w(u,v);


David Luebke                  42                           1/26/2011
                Prim’s Algorithm
MST-Prim(G, w, r)            6      4
    Q = V[G];                     5                9
                                                          
    for each u  Q
        key[u] = ;     14
                               10          2
    key[r] = 0;                                   15
                     u     0                               
    p[r] = NULL;
                             3    3 8
    while (Q not empty)
        u = ExtractMin(Q); Red arrows indicate parent pointers
        for each v  Adj[u]
            if (v  Q and w(u,v) < key[v])
                 p[v] = u;
                 key[v] = w(u,v);


David Luebke                  43                           1/26/2011
               Prim’s Algorithm
MST-Prim(G, w, r)            6      4
    Q = V[G];                     5          9
                          14                       
    for each u  Q
        key[u] = ;     14
                               10       2
    key[r] = 0;                              15
                     u     0                       
    p[r] = NULL;
                             3    3 8
    while (Q not empty)
        u = ExtractMin(Q);
        for each v  Adj[u]
            if (v  Q and w(u,v) < key[v])
                 p[v] = u;
                 key[v] = w(u,v);


David Luebke             44                       1/26/2011
               Prim’s Algorithm
MST-Prim(G, w, r)            6      4
    Q = V[G];                     5          9
                          14                       
    for each u  Q
        key[u] = ;     14
                               10       2
    key[r] = 0;                              15
                           0                       
    p[r] = NULL;
                             3    3 8
    while (Q not empty)      u
        u = ExtractMin(Q);
        for each v  Adj[u]
            if (v  Q and w(u,v) < key[v])
                 p[v] = u;
                 key[v] = w(u,v);


David Luebke             45                       1/26/2011
               Prim’s Algorithm
MST-Prim(G, w, r)            6      4
    Q = V[G];                     5          9
                          14                       
    for each u  Q
        key[u] = ;     14
                               10        2
    key[r] = 0;                              15
                           0           8            
    p[r] = NULL;
                             3    3 8
    while (Q not empty)      u
        u = ExtractMin(Q);
        for each v  Adj[u]
            if (v  Q and w(u,v) < key[v])
                 p[v] = u;
                 key[v] = w(u,v);


David Luebke             46                       1/26/2011
               Prim’s Algorithm
MST-Prim(G, w, r)            6      4
    Q = V[G];                     5          9
                          10                       
    for each u  Q
        key[u] = ;     14
                               10        2
    key[r] = 0;                              15
                           0           8            
    p[r] = NULL;
                             3    3 8
    while (Q not empty)      u
        u = ExtractMin(Q);
        for each v  Adj[u]
            if (v  Q and w(u,v) < key[v])
                 p[v] = u;
                 key[v] = w(u,v);


David Luebke             47                       1/26/2011
               Prim’s Algorithm
MST-Prim(G, w, r)            6      4
    Q = V[G];                     5          9
                          10                       
    for each u  Q
        key[u] = ;     14
                               10        2
    key[r] = 0;                              15
                           0           8            
    p[r] = NULL;
                             3    3 8
    while (Q not empty)                 u
        u = ExtractMin(Q);
        for each v  Adj[u]
            if (v  Q and w(u,v) < key[v])
                 p[v] = u;
                 key[v] = w(u,v);


David Luebke             48                       1/26/2011
               Prim’s Algorithm
MST-Prim(G, w, r)            6      4
    Q = V[G];                     5          9
                          10           2            
    for each u  Q
        key[u] = ;     14
                               10        2
    key[r] = 0;                              15
                           0           8            
    p[r] = NULL;
                             3    3 8
    while (Q not empty)                 u
        u = ExtractMin(Q);
        for each v  Adj[u]
            if (v  Q and w(u,v) < key[v])
                 p[v] = u;
                 key[v] = w(u,v);


David Luebke             49                       1/26/2011
               Prim’s Algorithm
MST-Prim(G, w, r)            6      4
    Q = V[G];                     5          9
                          10           2            
    for each u  Q
        key[u] = ;     14
                               10        2
    key[r] = 0;                              15
                           0           8           15
    p[r] = NULL;
                             3    3 8
    while (Q not empty)                 u
        u = ExtractMin(Q);
        for each v  Adj[u]
            if (v  Q and w(u,v) < key[v])
                 p[v] = u;
                 key[v] = w(u,v);


David Luebke             50                       1/26/2011
               Prim’s Algorithm
MST-Prim(G, w, r)                         u
                             6       4
    Q = V[G];                     5          9
                          10           2            
    for each u  Q
        key[u] = ;     14
                               10        2
    key[r] = 0;                              15
                           0           8           15
    p[r] = NULL;
                             3    3 8
    while (Q not empty)
        u = ExtractMin(Q);
        for each v  Adj[u]
            if (v  Q and w(u,v) < key[v])
                 p[v] = u;
                 key[v] = w(u,v);


David Luebke              51                      1/26/2011
               Prim’s Algorithm
MST-Prim(G, w, r)                         u
                             6       4
    Q = V[G];                     5          9
                          10           2            9
    for each u  Q
        key[u] = ;     14
                               10        2
    key[r] = 0;                              15
                           0           8           15
    p[r] = NULL;
                             3    3 8
    while (Q not empty)
        u = ExtractMin(Q);
        for each v  Adj[u]
            if (v  Q and w(u,v) < key[v])
                 p[v] = u;
                 key[v] = w(u,v);


David Luebke              52                      1/26/2011
               Prim’s Algorithm
MST-Prim(G, w, r)                 4        u
                             6       4
    Q = V[G];                     5          9
                          10           2            9
    for each u  Q
        key[u] = ;     14
                               10        2
    key[r] = 0;                              15
                           0           8           15
    p[r] = NULL;
                             3    3 8
    while (Q not empty)
        u = ExtractMin(Q);
        for each v  Adj[u]
            if (v  Q and w(u,v) < key[v])
                 p[v] = u;
                 key[v] = w(u,v);


David Luebke              53                      1/26/2011
               Prim’s Algorithm
MST-Prim(G, w, r)                 4        u
                             6       4
    Q = V[G];                     5          9
                           5           2            9
    for each u  Q
        key[u] = ;     14
                               10        2
    key[r] = 0;                              15
                           0           8           15
    p[r] = NULL;
                             3    3 8
    while (Q not empty)
        u = ExtractMin(Q);
        for each v  Adj[u]
            if (v  Q and w(u,v) < key[v])
                 p[v] = u;
                 key[v] = w(u,v);


David Luebke              54                      1/26/2011
               Prim’s Algorithm
                                     u
MST-Prim(G, w, r)            6    4  4
    Q = V[G];                     5          9
                           5           2            9
    for each u  Q
        key[u] = ;     14
                               10        2
    key[r] = 0;                              15
                           0           8           15
    p[r] = NULL;
                             3    3 8
    while (Q not empty)
        u = ExtractMin(Q);
        for each v  Adj[u]
            if (v  Q and w(u,v) < key[v])
                 p[v] = u;
                 key[v] = w(u,v);


David Luebke             55                       1/26/2011
               Prim’s Algorithm
MST-Prim(G, w, r)        u        4
                             6       4
    Q = V[G];                     5          9
                           5           2            9
    for each u  Q
        key[u] = ;     14
                               10        2
    key[r] = 0;                              15
                           0           8           15
    p[r] = NULL;
                             3    3 8
    while (Q not empty)
        u = ExtractMin(Q);
        for each v  Adj[u]
            if (v  Q and w(u,v) < key[v])
                 p[v] = u;
                 key[v] = w(u,v);


David Luebke             56                       1/26/2011
               Prim’s Algorithm
                                                   u
MST-Prim(G, w, r)            6    4  4
    Q = V[G];                     5          9
                           5           2            9
    for each u  Q
        key[u] = ;     14
                               10        2
    key[r] = 0;                              15
                           0           8           15
    p[r] = NULL;
                             3    3 8
    while (Q not empty)
        u = ExtractMin(Q);
        for each v  Adj[u]
            if (v  Q and w(u,v) < key[v])
                 p[v] = u;
                 key[v] = w(u,v);


David Luebke             57                       1/26/2011
               Prim’s Algorithm
MST-Prim(G, w, r)            6    4  4
    Q = V[G];                     5          9
                           5           2            9
    for each u  Q
        key[u] = ;     14
                               10        2         u
    key[r] = 0;                              15
                           0           8           15
    p[r] = NULL;
                             3    3 8
    while (Q not empty)
        u = ExtractMin(Q);
        for each v  Adj[u]
            if (v  Q and w(u,v) < key[v])
                 p[v] = u;
                 key[v] = w(u,v);


David Luebke             58                       1/26/2011
               Review: Prim’s Algorithm
MST-Prim(G, w, r)
    Q = V[G];
    for each u  Q
        key[u] = ;
    key[r] = 0;
                     What is the hidden cost   in this code?
    p[r] = NULL;
    while (Q not empty)
        u = ExtractMin(Q);
        for each v  Adj[u]
            if (v  Q and w(u,v) < key[v])
                 p[v] = u;
                 key[v] = w(u,v);


David Luebke              59                           1/26/2011
               Review: Prim’s Algorithm
MST-Prim(G, w, r)
    Q = V[G];
    for each u  Q
        key[u] = ;
    key[r] = 0;
    p[r] = NULL;
    while (Q not empty)
        u = ExtractMin(Q);
        for each v  Adj[u]
            if (v  Q and w(u,v) < key[v])
                 p[v] = u;
                 DecreaseKey(v, w(u,v));


David Luebke              60                 1/26/2011
               Review: Prim’s Algorithm
MST-Prim(G, w, r)
    Q = V[G];
    for each u  Q
        key[u] = ; How often is ExtractMin() called?
    key[r] = 0;
    p[r] = NULL;
                     How often is DecreaseKey() called?
    while (Q not empty)
        u = ExtractMin(Q);
        for each v  Adj[u]
            if (v  Q and w(u,v) < key[v])
                 p[v] = u;
                 DecreaseKey(v, w(u,v));


David Luebke              61                      1/26/2011
               Review: Prim’s Algorithm
MST-Prim(G, w, r)
    Q = V[G];
    for each u  Q      What will be the running time?
        key[u] = ;     A: Depends on queue
    key[r] = 0;           binary heap: O(E lg V)
    p[r] = NULL;
                          Fibonacci heap: O(V lg V + E)
    while (Q not empty)
        u = ExtractMin(Q);
        for each v  Adj[u]
            if (v  Q and w(u,v) < key[v])
                 p[v] = u;
                 key[v] = w(u,v);


David Luebke              62                      1/26/2011
               Single-Source Shortest Path

● Problem: given a weighted directed graph G,
     find the minimum-weight path from a given
     source vertex s to another vertex v
       ■ “Shortest-path” = minimum weight
       ■ Weight of path is sum of edges
       ■ E.g., a road map: what is the shortest path from
               Chapel Hill to Charlottesville?




David Luebke                         63                     1/26/2011

				
About if any file u wil find copyright contact me it will be remove in 3 to 4 buisnees days. add me on sanjaydudeja007@gmail.com or visit http://www.ohotech.com/