Trees by pengxiuhui

VIEWS: 3 PAGES: 23

									CS211, Lecture 21 Graphs                                            Readings: Weiss,
shortest path algorithm                                            secs. 14.1--14.3,
"This 'telephone' has too many shortcomings to be seriously considered
                                                                         sec. 14.5.1.
as a means of communications. " - Western Union internal memo - 1876

"I think there is a world market for maybe five computers." - Watson, chair of IBM - 1943

"The problem with television is that the people must sit and keep their eyes glued on a screen; the
average American family hasn't time for it.” - The New York Times - 1949

"Where ... the ENIAC is equipped with 18,000 vacuum tubes and weighs 30 tons, computers in
the future may have only 1,000 vacuum tubes and weigh only 1.5 tons.” - Popular Mechanics -
1949

"There is no reason anyone would want a computer in their home." - Ken Olson, founder DEC -
1977
                                                             did he mean memory or money?
"640K ought to be enough for anybody.” - Bill Gates - 1981

"By the turn of this century, we will live in a paperless society." - Roger Smith, chair GM - 1986

"I predict the Internet... will go spectacularly supernova and in 1996 catastrophically collapse."
                                                                                                      1
 - Bob Metcalfe, inventor and 3Com founder - 1995
                             Graphs
A graph is a set V of nodes (or vertices) together with
a set E of edges between them. Here, graph could represent
roads between cities, or airplane flights between cities.
                                                         london
                      chic

     SF                                                           paris
                              NY

                      Nodes: V = { SF, NY, LA, chic, london, paris }
     LA
                      Edges: { (SF, LA), (SF, NY), (SF, chic), (LA, chic)
                                (LA, chic), (NY, chic), (london, paris) }

       |V| = size of V = number of nodes (here, 6)
       |E| = size of E = number of edges (here, 7)
                                                                            2
            Leonhard Euler (1707--1783) started graph
               theory: Koenigsberg, Prussia, 1736.
                                       land



                  island                         island



                                  land                       land 2
allows for more than
one edge between two                                                       island 1
vertices                    island 2
                                                            land 1
   Koenigsberg bridges. Is it possible to travel over the bridges so that each
   bridge is walked over exactly once?
   Extend to graph theory: For which graphs is it possible to find a path
   that contains each edge exactly once?
                                                                                 3
            Important graph problems

Traveling salesman problem (TSP): Graph is a bunch of cities, edges
have weights that give the cost for traveling from one city to another. Find
the cheapest way to travel to all cities and end up back at the home city.
Four-color problem. Can one color countries of a map using four colors
so that no two adjacent countries have the same color?
Nodes: countries.
Edges: edge from one country to another if they have a common
boundary. Graph is planar --can draw so that no two edges intersect.
Color nodes of a graph using four color so that no two adjacent nodes
have the same color?
About 1870. An incorrect proof published; error detected ~10 years later.
1977: Appel and Haken used computers to solve it (yes, one can).

                                                                               4
                          Directed graph
A directed graph, or digraph, is a set V of vertices together with a
set E of directed edges (arrows) between them. Only one edge
allowed in a particular direction between the nodes.
We concentrate on directed graphs.
Below, “…” stands for“http://www.cs.cornell.edu/courses/cs211/2004sp”

                                             http://java.sun.com/j
        …/index.html                         2se/1.4.2/download.
                                             html



        …/bootcamp.html              …DrJava.html




                                                                        5
   Celebrity problem uses a directed graph

At a party, a celebrity is a person who everyone knows and
who knows no one (except themselves).
How much time does it take to find a celebrity (if present)?
Graph: nodes are people, Edge (p1, p2) if p1 knows p2.
It’s a directed graph.
Originally: thought this required required time O(|V|*|V|).
Can you write an O(|V|) algorithm to find the celebrity?




                                                               6
                         Directed graph
Sometimes, we put positive weights on the edges. If we don’t
assume the weight is one.

For a map of roads between cities, the weight might be the shortest
mileage.

                             4              http://java.sun.com/j
        …/index.html                        2se/1.4.2/download.
                                            html
          8                      3
                  9
                            6
       …/bootcamp.html               …DrJava.html




                                                                    7
                      Directed graph
A path is a sequence of edges       path (V1, v2, v0) has length 2
that connect by successive          path (V1, V2) has length 1
vertices. The length of the path    path (V1) has length 0
is the number of edges.             path (V1, V2, V0, V1, V2) has length 4
Simple path: all vertices (except
possibly first and last) are
different.
Cycle: simple path in which first
                                                             V0
and last vertices are the same.
A graph without cycles is called           V2
                                                                     V1
an acyclic graph.

                                           V3                             8
                  Adjacency matrix:
              representation of a digraph
Use a boolean array b[0..|V|-1][0..|V|–1]
b[r][c] = “there is an edge from Vr to Vc”
                                                    0 1 2 3
Constant time to tell whether there               0 F T F F
is an edge from r to c. Good.                     1 F F T F

Takes |V|*|V| time to construct,                  2 T F F F

|V|*|V| space, and |V|*|V| time to                3 T T T F
process all edges.
                                                       V0
No good if graph is sparse (very
few edges in relation to number of           V2
                                                              V1
nodes). Usually, sparse means that
the number of edges is O(|V|).
                                             V3
                                                              9
                               Adjacency list: representation of a
Use array b[0..|V|–1] of linked lists digraph for sparse graphs
If (h, k) is an edge, k is on linked list b[h].

                                                             V0
                    1
  0
  1                                                   V2
                    2                                             V1
  2
  3                 0
                                                      V3

                    1            0                2

No prescribed order of values in a linked list.
Time to construct: O(|V|) + O(|E|)
Time to tell whether an edge is there: O(|V|) (worst case)
Time to process the edges: O(|E|)
                                                                  10
       Dijkstra’s shortest path algorithm (assuming
               edges have positive weights)

 Given a path from w to v, we can count the weights that are on the
 edges of that path. Call the sum the cost of the path.
 Find the shortest path –the path with minimum cost– from a start
 vertex v to each other vertex.


                                          1               V0
                                                               5
                                     V2       3
Shortest path from V3 to                              9
                                                                   V1
V1 is: (V3, V2, V0, V1)
                                 1
                                                  9
                                     V3

                                                                        11
         Find shortest paths from v to all nodes
Let n = |V| –i.e. the number of vertices
Store values in L[0..n-1] so that:
  R: L[k] the shortest path length from w to k, for 0 ≤ k < n
    (if there is no path from v to k, set L[k] to ∞
      or, can use Integer.MAX_VALUE instead of ∞)

For the graph on this slide, with w = V3,
L = { 2, 7, 1, 0 }                            1               V0
                                                                   5
                                                  3
                                         V2               9
                                                                       V1
                                     1
                                                      9
                                         V3
                                                                            12
               Find shortest paths from v to all nodes
Initially, L[v] = 0      // shortest path from v to v
L[w] = ∞                 // for all other nodes
red set = {}
                                        red set: the set of vertices
                                        (1) whose L-value has
                                           been calculated
                                        (2) whose neighbors have
           3
                                           L-values < ∞
v          4
                                        Frontier: vertices that
       1                                (1) are not in the red set
                                        (2) have L-value < ∞



                                                                       13
                 Find shortest paths from v to all nodes
  Initially, L[v] = 0   // shortest path from v to v
  We put the L-values in the nodes themselves.

                                        red set: the set of vertices
             3
                      3                 (1) whose L-value has
                                           been calculated
  v 0                 4
                                        (2) whose neighbors have
         1        2                        L-values < ∞
                          6
          14                            Frontier: vertices that
                  3                     (1) are not in the red set
                                        (2) have L-value < ∞

For a node w in Frontier, L[w] is the minimum path length
over (v,w) paths using only red nodes (except for w).                  14
                 Find shortest paths from v to all nodes
  Initially, L[v] = 0  // shortest path from v to v
  We put the L=values in the nodes themselves.

                                        red set: the set of vertices
            3
                      3                 (1) whose L-value has
             4
                                           been calculated
  v 0                 3
                                        (2) whose neighbors have
        1         2                        L-values < ∞
                          6       7
         1                              Frontier: vertices that
                  3
                              3
                                        (1) are not in the red set
                                        (2) have L-value < ∞

For a node w in Frontier, L[w] is the minimum path length
over (v,w) paths using only red nodes (except for w).                  15
At each iteration:
                                         Red set. For red w, L[w]
                                         has been calculated.
Let f be the Frontier node               Neighbors have L[w] < ∞
    with smallest L value.                                …
Make f red and
                                         Frontier F. For w in here,
for each node w adjacent to f:
                                         L[w] is min path length
   if (L[f] + weight (f,w) < L[w]) {     over paths from v to w for
       L[w]= L[f] + weight(f,w);         which all nodes are red
       Put w in Frontier (make blue);    except w.
  }                                                      …
         5      1                                rest of nodes
                 2
         7
                4                       Invariant: shows only edges
         3       5
                                        from red to blue, blue to black
                                                                    16
L[w]= ∞ for all nodes w;                 Red set. For red w, L[w]
L[v]= 0; F= {v};                         has been calculated.
while (F not empty) {                    Neighbors have L[w] < ∞
  f= a node in F with min L value;                       …
  Make f red (delete from F);            Frontier F. For w in here,
  for each node w adjacent to f:         L[w] is min path length
     if (L[f] + weight (f,w) < L[w]) {   over paths from v to w for
        L[w]= L[f] + weight(f,w);        which all nodes are red
                                         except w.
        if (w is not in F) put w in F;
                                                         …
     }
}                  5      1
                                                rest of nodes
                        2
                 7
                        4            Invariant: shows only edges
                                     from red to blue, blue to black
                 3      5
                                                                      17
L[w]= ∞ for all nodes w;
                                                  How much time?
L[v]= 0; F= {v};
while (F not empty) {                  // outer loop: |V| iterations
  f= a node in F with min L value;          // done |V| times
  Make f red (delete from F);               // done |V| times
  for each node w adjacent to f:
     if (L[w] = ∞) {                         // done |E| times
        L[w]= L[f] + weight(f,w);           // done < |V| times
        Put w in F;                         // done < |V| times
    } else if (L[f] + weight (f,w) < L[w]) // done < |E| – |V| times
           L[w]= L[f] + weight(f,w);        // done < |E| – |V| times
}
                    5       1
                             2              What data structure do
                    7                       we use for F?
                            4
                    3       5
                                                                        18
L[w]= ∞ for all nodes w;
L[v]= 0; F= {v};                             What data structure do
while (F not empty) {                        we use for F?
  f= a node in F with min L value;           A min-heap --a heap with
  Make f red (delete from F);                minimum at the top.
  for each node w adjacent to f:
     if (L[w] = ∞) {                         f= node with min L value:
        L[w]= L[f] + weight(f,w);            constant time
        Put w in F;
    } else if (L[f] + weight (f,w) < L[w])   Delete f from F:
           L[w]= L[f] + weight(f,w);         log |F| time
}
                                             Put w in F:
                    5       1
                             2
                                             log |F| time
                    7
                            4
                                             Total time: O(|V| log |V|)
                    3        5
                                                                          19
/** An instance is an edge */
public class Edge {
  public int start; // An edge from vertex start
  public int end; // to vertex end
  public int weight; // the weight of the edge (> 0)

    /** Constructor: and edge (start, end) with weight w */
    public Edge(int start, int end, int w) {
       this.start= start;
       this.end= end;
       weight= w;
    }
}




                                                              20
public class AdjacencyList {
  private LNode[] V; // The adjacency list for the graph
  private int Esize; // number of edges

    /** Constructor: a graph of n nodes, with e edges
      given by edge set E[0..e-1].
      Precondition: the edges are all different. */
    public AdjacencyList(int n, int e, Edge[] E) {}

    /** = number of nodes in graph */
    public int Vsize() {}

    /** = number of edges in graph */
    public int Esize() {}

    /** = an enumerator over edges with start vertex w
         next() returns an Edge */
    public Iterator adjacencyEnumerator(int w) {}
}
                                                           21
/** = an array of shortest paths from node v to other nodes in graph g */
public static int[] shortest(AdjacencyList g, int v) {
    int[] L= new int[g.Vsize()]; // The shortest-path lengths from v
    for (int k= 0; k != L.length; k= k+1)
         L[k]= Integer.MAX_VALUE;
    L[v]= 0;

     int[] F= new int[g.Vsize()]; // Set F is in min-heap
     int Fsize= 0;         // F[0..Fsize-1]

     // Add v to heap F and store the new heap size in Fsize
     Fsize= addHeap(F, Fsize, v);

     // Invariant: The red nodes have their L-values calculated and their
                   neighbors have L-values < MAX_VALUE.
                  F is the set of nodes with L-value < MAX_VALUE
                  that are not red. …


                                                                            22
    while (Fsize != 0) {
        int f= F[0]; Fsize= removeMinHeap(F, Fsize);
        Iterator it= g.adjacencyEnumerator(f);
        while (it.hasNext()) {
           Edge e= (Edge)it.next();

            if (L[e.end] == Integer.MAX_VALUE) {
               L[e.end]= L[f] + e.weight;
               Fsize= addHeap(F, Fsize, e.end);
            }
            else { // e.end is already in F
               if (L[f] + e.weight < L[e.end]) {
                   L[e.end]= L[f] + e.weight;
                   BubbleUp(F, Fsize, e.end);
               }
            }
        }
    }
    return L;
}                                                      23

								
To top