SlideShare uma empresa Scribd logo
1 de 8
Baixar para ler offline
Computer Science 385
                                       Analysis of Algorithms
                                       Siena College
                                       Spring 2011


                       Topic Notes: Graph Algorithms

Our next few topics involve algorithms that operate on graph structures.


Reachability
As a simple example of something we can do with a graph, we determine the subset of the vertices
of a graph G = (V, E) which are reachable from a given vertex s by traversing existing edges.
A possible application of this is to answer the question “where can we fly to from ALB?”. Given
a directed graph where vertices represent airports and edges connect cities which have a regularly-
scheduled flight from one to the next, we compute which other airports you can fly to from the
starting airport. To make it a little more realistic, perhaps we restrict to flights on a specific airline.
For this, we will make use of a “visited” field that is included in the “structure” implementation of
vertices and edges.
We start with all vertices markes as unvisited, and when the procedure completes, all reachable
vertices are marked as visited.
See Example:
˜jteresco/shared/cs385/examples/Reachability
This will visit the vertices starting from s in a breadth-first order.
If we replace toVisit by a stack, we will visit vertices in a depth-first order.
A recursive version implementation of depth-first reachability, would have a stack implicit in the
recursion.
The cost of this procedure will involve at most Θ(|V |+|E|) operations if all vertices are reachable,
which is around Θ(|V |2 ) if the graph is dense.
We can think about how to extend this to find reasonable flight plans, perhaps requiring that all
travel takes place in the same day and that there is a minimum of 30 minutes to transfer.


Transitive Closure
Taking the transitive closure of a graph involves adding an edge from each vertex to all reachable
vertices. We could do this by computing the reachability for each vertex, in turn, with the algorithm
above. This would cost a total of Θ(|V |3 ).
A more direct approach is due to Warshall.
CS 385                               Analysis of Algorithms                            Spring 2011



We modify the graph so that when we’re done, for every pair of vertices u and v such that v is
reachable from u, there is a direct edge from u to v.
Note that this is a destructive process! We modify our starting graph.
The idea is that we build the transitive closure iteratively. When we start, we know that edges exist
between any vertices that are connected by a path of length 1.
We can find all pairs of vertices which are connected by a path of length 2 (2 edges) by looking at
each pair of vertices u and v and checking, for each other vertex, whether there is another vertex
w such that u is connected to w and w is connected to v. If so, we add a direct edge u to v.
If we repeat this, we will then find pairs of vertices that were connected by paths of length 3 in the
original graph. If we do this |V | times, we will have all possible paths added.
This is still an Θ(|V |3 ) algorithm, though efficiency improvements are possible.
The text presents this algorithm as an operation directly on an adjacency matrix representation of
a graph, where each entry is a boolean value indicating whether an edge exists.

warshall(A[1..n][1..n])
  // Each R{i}[1..n][1..n] is an iteration toward the closure
  R{0} = A
  for k=1 to n
    for i=1 to n
      for j=1 to n
        R{k}[i][j] = R{k-1}[i][j] OR
                    (R{k-1}[i][k] AND R{k-1}[k][j])
  return R{n}



All Pairs Minimum Distance
We can expand this idea to get Floyd’s Algorithm for computing minimum distances between all
pairs of (reachable) vertices.
For the example graph:




                                                 2
CS 385                                   Analysis of Algorithms                                   Spring 2011


                                                            Lowell
                                                            11
                                                            00
                    1 11
                    0 00           111111111111111111111
                                   000000000000000000000
           Williamstown 000000000000
                        111111111111                         00000
                                                             11111
                                                            11
                                                            00
                                 Greenfield
                  000
                  1111
                  0000
                  111
                 1 11
                 0 00   1
                        0   37       1
                                     0
                                   111111111111111111111
                                   000000000000000000000
                                     1
                                     0      50         1
                                                       0
                                               Fitchburg 33
                                                                  11111
                                                                  00000
                111
                000
                 1 11
                 0 00
                111
                000
                     1
                     0
                    50
                     1             111111111111111111111
                                   000000000000000000000
                                     1
                                     0
                                     1
                                     0                 1
                                                       0          11111
                                                                  00000
                111  1
                     0
                000 North Adams      1
                                     0                 1111111111
                                                       0000000000
                                                       1
                                                       0          11111
                                                                  00000
                                                                  11111
                                                                  00000
                111
                000  1
                     0               1
                                     0                 1
                                                       0
                                                       1
                                                       0          00000
                                                                  11111
                000
                111  1
                     0               1
                                     0                 1
                                                       0          11111
                                                                  00000
                111
                111
                000
                     1
                     0
                000 21               0
                                     1                 1
                                                       0          11111
                                                                  00000
                                                                     30
                 21 0
                111
                000
                     1
                     1
                     0               1
                                     0
                                     1
                                     0                 0
                                                       1          11111
                                                                  00000
                                                                  11111
                                                                  00000
                111
                000  1
                     0               1
                                     0                 1
                                                       0
                                                       1
                                                       0
                                                        32
                                                                  00000
                                                                  11111
                000
                111  1
                     0               1
                                     0 39              1
                                                       0          11111
                                                                  00000 Boston
                111
                111
                000
                     1
                     0
                000Pittsfield
                     1
                     0               0
                                     1
                                     1
                                     0                 1
                                                       0          11111
                                                                  00000
                111
                000 1
                    01
                     0               1
                                     0                 0
                                                       1          11111
                                                                  00000
                                                       11111111111111
                                                       0000000000000011
                                                                     00
                                                                 11111
                                                                 000001111
                                                                      0000
                    1
                    01
                     0               1
                                     0                 1
                                                       0     47      11
                                                                     00
                                                                 11111
                                                       11111111111111
                                                       00000000000000
                                                                 000001111
                                                                      0000
                     1
                     0                          Auburn 0
                                                    11
                                                    00 1
                                                       111111111
                                                       000000000
                                                       11111111111111
                                                       00000000000000
                                     111111111111111111111111
                                     00000000001
                                     1
                                     0                           11111
                                                                 000001111
                                                                      0000              Provincetown
                     1
                     011             00000000000
                                     0
                                     1              11
                                                    00 111111111
                                                       000000000
                                     111111111100000000000000    11111
                                                                 000001111
                                                                      0000                      1
                                                                                                0
                     1
                     0
                     1
                     0              1
                                    01111111111111111111
                                     0000000000000000000
                                     1
                                     0       47     11
                                                    00
                                     1111111111000000000
                                                       111111111
                                     0000000000000000000
                                                                 11111
                                                                 00000
                                                                 11111
                                                                 000001111
                                                                      0000
                                                                      1111
                                                                      0000
                                                                            1111111111
                                                                            0000000000
                                                                            1111111111
                                                                            0000000000          1
                                                                                                0
                   111111111111
                   000000000000
                     1
                     0              1
                                    01
                                     0                 111111111
                                     1111111111111111111
                                     0000000000000000000
                                     1
                                     0                           11111
                                                                 000000000
                                                                      1111  1111111111
                                                                            0000000000
                   111111111111
                   000000000000
                     1
                     0
                              44
                   111111111111
                   000000000000     1
                                    0                            11111
                                                                 000001111
                                                                      0000  0000000000
                                                                            1111111111
                     1
                     0
                     1
                     0
                     1
                     0                                 111111111
                                                       000000000 11111
                                                                 000001111
                                                                      0000  1111111111
                                                                            0000000000
                                 Springfield
                                                       111111111
                                                       000000000
                                                       111111111 11111
                                                                 000001111
                                                                      0000  1111111111
                                                                            0000000000
                                                       000000000
                                                       111111111
                                                       000000000 11111
                                                                 000001111
                                                                      0000  1111111111
                                                                            0000000000
                                                                           40
                    Lee
                                                       111111111
                                                       000000000 11111
                                                                 000000000
                                                                      1111  1111111111
                                                                            0000000000
                                                                            0000000000
                                                       111111111
                                                       000000000 11111
                                                                 000001111
                                                                      0000  1111111111
                                                                            1111111111
                                                                            0000000000
                                                       111111111
                                                       000000000 11111
                                                                 00000
                                                                 111111111
                                                                      0000           76
                                                                            1111111111
                                                                            0000000000
                                                       111111111
                                                       000000000 00000
                                                                 11111
                                                                 000001111
                                                                      0000
                                                                      1111
                                                                      0000  1111111111
                                                                            0000000000
                                                       111111111
                                                       000000000
                                                       111111111 11111
                                                                 00000
                                                       000000000 58   0000
                                                                      1111  1111111111
                                                                            0000000000
                                                       111111111
                                                       000000000 11111
                                                                 000001111
                                                                      0000  0000000000
                                                                            1111111111
                                                            71
                                                       111111111
                                                       000000000 11111 11
                                                                 00000 00
                                                                      1111
                                                                      0000  1111111111
                                                                            0000000000
                                                                            1111111111
                                                                            0000000000
                                                       111111111
                                                       000000000 11111111
                                                                 00000000
                                                                 11111 11
                                                                 00000 00
                                                                      1111
                                                                      0000
                                                                 11111111
                                                                 00000000   1111111111
                                                                            0000000000
                                                       111111111
                                                       000000000 11111 11
                                                                 00000 00
                                                                 11111111
                                                                 00000000
                                                       111111111
                                                       000000000 11111
                                                                 00000
                                                                 11111111
                                                                 00000000
                                                                 11111
                                                                 00000      Plymouth
                                                       111111111
                                                       000000000
                                                       111111111
                                                       000000000 11111111
                                                                 00000000
                                                                 11111
                                                                 00000
                                                       111111111
                                                       000000000 11111111
                                                                 00000000
                                                                 11111
                                                                 00000
                                                                 11111111
                                                                 00000000
                                                       111111111
                                                       000000000 11111
                                                                 00000
                                                                 11111111
                                                                 00000000
                                                       111111111
                                                       000000000 11111
                                                                 00000
                                                                 11111111
                                                                 00000000
                                                                 11111
                                                                 0000032
                                                       111111111
                                                       000000000
                                                       111111111
                                                       000000000 11111111
                                                                 00000000
                                                                 11111
                                                                 00000
                                                       111111111
                                                       000000000 11111111
                                                                 00000000
                                                                 11111
                                                                 00000
                                                                 11111111
                                                                 00000000
                                                       111111111
                                                       000000000 11111
                                                                 00000
                                                                 11111111
                                                                 00000000
                                                       111111111
                                                       000000000 11111
                                                                 00000
                                                                11
                                                                00
                                                                11
                                                                00
                                                                  New Bedford




We can use the same procedure (three nested loops over vertices) as we did for Warshall’s Algo-
rithm, but instead of just adding edges where they may not have existed, we will add or modify
edges to have the minimum cost path (we know of) between each pair.
The text’s algorithm again works directly on the adjacency matrix, now with weights representing
the edges in the matrix.

floyd(W[1..n][1..n])
  D=W // matrix copy
  for k=1 to n
    for i=1 to n
      for j=1 to n
        D[i][j] = min{D[i][j],D[i][k]+D[k][j]}
  return D

Like Warshall’s Algorithm, this algorithm’s efficiency class is Θ(|V |3 ).
Notice that at each iteration, we are overwriting the adjacency matrix.
And we can see Floyd’s Algorithm in action using the above simple graph.
See Example:
˜jteresco/shared/cs385/examples/MassFloyd


Minimum Spanning Tree (MST) Algorithms
Before we look at the graph algorithm, we think about a general class of algorithms to which they
belong: greedy algorithms.
The idea of a greedy technique is one which constructs a solution to an optimization problem one
piece at a time by a sequence of choices which must be:

   • feasible

                                                       3
CS 385                               Analysis of Algorithms                          Spring 2011



   • locally optimal

   • irrevocable

In some cases, a greedy approach can be used to find an optimal solution, but in many cases it does
not. Even in those cases, it often leads to a reasonably good solution in much less time than would
be required to find the optimal.
In our first example, a greedy approach does find an optimal solution.
The problem is to find the minimum spanning tree of a weighted graph.
The spanning tree of a connected graph G is a connected acyclic subgraph of G that includes all
of G’s vertices.
The minimum spanning tree of a weighted, connected graph G is the spanning tree of G (it may
have many) of minimum total weight.
We can construct examples of spanning trees and find the minimum using the graph:

a ---6---- c
|         /|
|       / |
2     4    1
|   /      |
| /        |
b ---3---- d

The following greedy approach, known as Prim’s Algorithm, will compute the optimal answer as
follows:

   • Start with a tree T1 , consisting of any one vertex.

   • We “grow” the tree one vertex at a time to produce the MST through a series of expanding
     subtrees T1 , T2 , ..., Tn .

         – On each iteration, we construct Ti+1 from Ti by adding the vertex not in Ti that is
           “closest” to those already in Ti (this is a “greedy” step).
         – The algorithm will use a priority queue to help find the nearest neighbor vertices to be
           added at each step.

   • Stop when all vertices are included in the tree.

The text proves by induction that this construction always yields the MST.
Efficiency:


                                                 4
CS 385                               Analysis of Algorithms                         Spring 2011



   • Θ(|V |2 ) for the adjacency matrix representation of graph and an array implementation of the
     priority queue.

   • Θ(|E| log |V |) for an adjacency list representation and a min-heap implementation of the
     priority queue.

A second optimal, greedy algorithm for finding MST’s, Kruskal’s Algorithm, is in the text and you
will consider it as part of the next problem set.


Single-Source Shortest Path
Dijkstra’s Algorithm is a procedure to find shortest paths from a given vertex s in a graph G to
all other vertices. The algorithm incrementally builds a sub-graph of G which is a tree contain-
ing shortest paths from s to every other vertex in the tree. A step of the algorithm consists of
determining which vertex to add to the tree next.
Basic structures needed:

  1. The graph G = (V, E) to be analyzed.

  2. The tree, actually stored as a map, T . Each time a shortest path to a new vertex is found,
     an entry is added to T associating that vertex name with a pair indicating the total minimum
     distance to that vertex and the last edge traversed to get there.

  3. A priority queue in which each element is an edge (u, v) to be considered as a path from
     a located vertex u and a vertex v which we have not yet located. The priority is the total
     distance from the starting vertex s to v using the known shortest path from s to u plus the
     length of (u, v).

The algorithm proceeds as follows:

T is an empty map;
PQ is an empty priority queue;
All vertices in V are marked unvisited;
Add s to T with a total distance of 0 and a null previous edge;
mark s as visited in G;
Add each edge (s,v) of G to PQ with appropriate value
while (T.size() < G.size() and PQ not empty)
       do
           nextEdge = PQ.remove();
       until(one vertex of nextEdge is visited and the other is unvisited)
          or until there are no more edges in PQ

         // assume nextEdge = (v,u) where v is visited (in T) and u is
            unvisited (not in T)


                                                5
CS 385                               Analysis of Algorithms                          Spring 2011




         Add u to T; mark u as visited in G;
         Add (u,v) to T;
         for each unvisited neighbor w of u
            add (u,w) to PQ with appropriate weight


When the procedure finishes, T should contain all vertices reachable from s, along with the last
edge traverest along the shortest path from s to each such vertex.
Disclaimer: Many details still need to be considered, but this is the essential information needed
to implement the algorithm.
Consider the following graph:

                                                    Lowell
                                           1111111111
                                           0000000000
                                           1111111111
                                           0000000000
                                                     11111
                                                     00000
                                                      1
                                                      0
                                                     11111
                                                     00000
         1 11
         0 00111
             0001   37   Greenfield
   Williamstown 000000000000
             11
             00 11111111111111111111111
                           000000000000
                             1
                             01
                              0     50 Fitchburg      1
                                                      0
                                                 1111111111
                                                 0000000000
                                                 33
                                                            11111
                                                            00000
          11
          00
         1 11
         0 001
             0              111111111111
                            000000000000
                              1
                              0               11
                                              00 1111111111
                                                 0000000000 11111
                                                            00000
          11
          00 North Adams
            50
             1              111111111111
                            000000000000
                              1
                              0               11
                                              00 1111111111
                                                 0000000000 11111
                                                            00000
          11
          00
          11
             1
             0                1
                              0               11
                                              00            11111
                                                            00000
          00
          11
          00
             1
             0
             1
             0
                              1
                              0
                              1
                              0               11
                                              00            11111
                                                            00000
          11 1
             0
          00 21               1
                              0               11
                                              00            11111
                                                            00000
                                                            11111
                                                            00000
                                              11
                                              00                30
         21 0
          11
          00
          11
          00
             1
             1
             0
                              1
                              0
                              1
                              0               11
                                              00            11111
                                                            00000
                                                            11111
                                                            00000
          11
          00 1
             0                1
                              0               11
                                              00
                                              11
                                              00
                                                 32
                                                            11111
                                                            00000
          11
          00 1
             0                1
                              0 39            11
                                              00            11111
                                                            00000 Boston
          11
          00 1
             0
             1
             0                1
                              0
                              1
                              0               11
                                              00            11111
                                                            00000
                                              111111111111111
                                              000000000000000
                                                           000000
                                                           1111111111
                                                                 0000
          11
          00Pittsfield
             1
             0                1
                              0               11
                                              00            11111
                                                            00000
                                              111111111111111
                                              000000000000000
                                                           000000
                                                           111111  1
                                                                   0
                                                                 1111
                                                                 0000
          11
          00 1
             0                1
                              0               11
                                              00
                                              111111111111111
                                              000000000000000
                                                       47
                                                           000000
                                                           111111  1
                                                                   0
                                                                 1111
                                                                 0000
             1
             0                11111111111
                              00000000000
                              1
                              0               11
                                              00
                                              111111111111111
                                              000000000000000
                                         Auburn            000000
                                                           1111111111
                                                                 0000               11
                                                                                    00
                                                                              Provincetown
             1
             011              11111111111
                              00000000000
                              1
                              0               11
                                              001
                                                0
                                              0000000000
                                              1111111111
                                              111111111111111 11111111111
                                              000000000000000 00000000000
                                                           000000
                                                           1111111111
                                                                 0000               11
                                                                                    00
             1
             0                11111111111
                              00000000000
                              1
                              0       47        1
                                                0
                                              0000000000
                                              1111111111         1111
                                                                 0000
                                                           000000 00000000000
                                                           111111 11111111111       11
                                                                                    00
             1
             0
           111111111111
           000000000000       11111111111
                              00000000000
                              1
                              0               0000000000
                                              1111111111
                                              0000000000         0000
                                                           000000 00000000000
                                                                 1111
                                                           111111 11111111111
             1
             0
           111111111111
           000000000000
             1
             0               1
                             011111111111
                              00000000000     1111111111
                                              0000000000         1111
                                                                 0000
                                                           000000 00000000000
                                                           111111 11111111111
             1
             0
           111111111111
           000000000000
             1
             0
                       44
                             1
                             0                1111111111
                                              0000000000
                                              1111111111   000000 00000000000
                                                                 1111
                                                                 0000
                                                           111111 11111111111
             1
             0
             1
             0            Springfield
                                              0000000000
                                              1111111111         0000
                                                           000000 00000000000
                                                                 1111
                                                           111111 11111111111
                                              0000000000
                                              1111111111   000000 00000000000
                                                                 1111
                                                                 0000
                                                           111111 11111111111
            Lee                               0000000000
                                              1111111111   000000 00000000000
                                                                 1111
                                                                 0000
                                                           111111 11111111111
                                                                     40
                                                           000000 00000000000
                                              0000000000
                                              1111111111         1111
                                                                 0000
                                                           111111 11111111111
                                                           000000 00000000000
                                              0000000000
                                              1111111111         1111
                                                                 0000
                                                           111111 11111111111
                                                           000000 00000000000
                                                                 0000
                                                           111111 11111111111
                                                                 1111      76
                                              0000000000
                                              1111111111   000000 00000000000
                                                                 1111
                                                                 0000
                                                           111111 11111111111
                                              0000000000
                                              1111111111
                                              0000000000   000000 00000000000
                                                           111111 11111111111
                                                                 1111
                                                                 0000
                                              1111111111
                                              0000000000 58000000 00000000000
                                                                 1111
                                                                 0000
                                                           111111 11111111111
                                              1111111111
                                              0000000000   000000 00000000000
                                                           111111 11111111111
                                                                 1111
                                                                 0000
                                              1111111111
                                                      71
                                              0000000000
                                              1111111111   000000 00000000000
                                                                 0000
                                                           111111 11111111111
                                                                 1111
                                                           11111111
                                                           00000000
                                                           000000 00000000000
                                                                 1111
                                                                 0000
                                                           111111 11111111111
                                              0000000000
                                              1111111111   000000 0
                                                           111111 1
                                                           11111111
                                                           00000000
                                              0000000000
                                              1111111111
                                              0000000000
                                              1111111111   000000 0
                                                           111111 1
                                                           11111111
                                                           00000000
                                                           11111111
                                                           00000000
                                              0000000000
                                              1111111111   000000 Plymouth
                                                           111111
                                                           11111111
                                                           00000000
                                                           000000
                                              0000000000
                                              1111111111   111111
                                                           11111111
                                                           00000000
                                                           000000
                                              0000000000
                                              1111111111   111111
                                                           11111111
                                                           00000000
                                                           000000
                                                           111111
                                              0000000000
                                              1111111111   11111111
                                                           00000000
                                                           000000
                                                           111111
                                              0000000000
                                              1111111111   11111111
                                                           00000000
                                                           000000
                                                           11111132
                                              0000000000
                                              1111111111
                                              0000000000   11111111
                                                           00000000
                                                           000000
                                                           111111
                                              1111111111
                                              0000000000   11111111
                                                           00000000
                                                           000000
                                                           111111
                                              1111111111
                                              0000000000   11111111
                                                           00000000
                                                           000000
                                                           111111
                                              1111111111
                                              0000000000
                                              1111111111   11111111
                                                           00000000
                                                          11
                                                          00
                                                           000000
                                                           111111
                                                          11
                                                          00
                                                           New Bedford




From that graph, the algorithm would construct the following tree for a start node of Williamstown.
Costs on edges indicate total cost from the root.




                                                6
CS 385                             Analysis of Algorithms                        Spring 2011


                                                     1
                                                     0
                                                   Lowell
                                         1111111111
                                         0000000000  1
                                                     0
                                                    11111
                                                    00000
            000
  Williamstown
         1 11
         0 00     42   Greenfield
            11 11111111111
            111
            00 00000000000
                         111111111111
                         000000000000
                                1
                                0              125   1
                                                     0
                                                  1111111111
                                                  0000000000
                                                           11111
                                                           00000
          11
          00
         1 11
         0 00                   1
                                0
                                  92 Fitchburg
                              111111111111
                              000000000000    11
                                              00  1111111111
                                                  0000000000
                                                           11111
                                                           00000
          11
          00 North Adams      111111111111
                              000000000000    11
                                              00  1111111111
                                                  0000000000
                                                           11111
                                                           00000
          11
          00
            5
                                              11
                                              00           11111
                                                           00000
          11
          00
          11                                               11111
                                                           00000
          00
          11
          00                                               11111
                                                           00000
          11
          00                                               11111
                                                           00000
                                                               155
          11
          00                                               11111
                                                           00000
         21
          11
          00                                               11111
                                                           00000
                                                           11111
          11
          00                                               00000
                                                           11111
                                                           00000 Boston
          11
          00Pittsfield                                     11111
                                                           00000 1
                                                                 0
          11
          00 1
             0                                             11111
                                                           00000 1
                                                                 0
                                                                1111
                                                                0000
          11
          00 1
             0                                                   1
                                                                 0
                                                                1111
                                                                0000
             1
             0                                                  1111
                                                                0000       Provincetown
                                                                   11111111111
                                                                   00000000000
             1
             032                                                1111
                                                                0000
                                                                   11111111111
                                                                   00000000000   11
                                                                                 00
             1
             0                          123     1
                                                0               1111
                                                                0000
                                                                   11111111111
                                                                   00000000000   11
                                                                                 00
             1
             0
             1
             0                 1
                               0                1
                                                0               1111
                                                                0000
                                                                   11111111111
                                                                   00000000000
             1
             0
                       76
                               1
                               0                                1111
                                                                0000
                                                                   11111111111
                                                                   00000000000
                                                                1111
                                                                0000
             1
             01
              0                             Auburn                 11111111111
                                                                   00000000000
                                                                1111
                                                                0000
              1
              0             Springfield
                                                                   11111111111
                                                                   00000000000
                                                                1111
                                                                0000
                                                                   11111111111
                                                                   00000000000
                                                                1111
                                                                0000195
           Lee                                                     11111111111
                                                                   00000000000
                                                                1111
                                                                0000
                                                                   11111111111
                                                                   00000000000
                                                                0000
                                                                1111
                                                                   11111111111
                                                                   00000000000
                                                                1111
                                                                0000     271
                                                                   11111111111
                                                                   00000000000
                                                                1111
                                                                0000
                                                                   11111111111
                                                                   00000000000
                                                                1111
                                                                0000
                                                                   11111111111
                                                                   00000000000
                                                                1111
                                                                0000
                                                                   11111111111
                                                                   00000000000
                                                                0000
                                                      194       1111
                                                                   11111111111
                                                                   00000000000
                                                                1111
                                                                0000
                                                                   11111111111
                                                                   00000000000
                                                                1111
                                                                0000 1
                                                                     0
                                                                   11111111111
                                                                   00000000000
                                                                     1
                                                                     0
                                                                      Plymouth




                                                         11
                                                         00
                                                         11
                                                         00
                                                       New Bedford




                                              7
CS 385                                 Analysis of Algorithms                         Spring 2011



We obtain this by filling in the following table, a      The table below shows the evolution of the priority
map which has place names as keys and pairs in-         queue. To make it easier to see how we arrived at the
dicating the distance from Williamstown and the         solution, entries are not erased when removed from
last edge traversed on that shortest route as val-      the queue, just marked with a number in the “Seq”
ues.                                                    column of the table entry to indicate the sequence
It is easiest to specify edges by the labels of their   in which the values were removed from the queue.
endpoints rather than the edge label itself.            Those which indicate the first (and thereby, shortest)
                                                        paths to a city are shown in bold.
       Place               (distance,last-edge)
                                                                     (distance,last-edge)             Seq
     W’town                     (0, null)
  North Adams         (5, W’town-North Adams)                 (5, Williamstown-North Adams)             1
    Pittsfield        (21, Williamstown-Pittsfield)               (21, Williamstown-Pittsfield)            2
       Lee                 (32, Pittsfield-Lee)                    (26, North Adams-Pittsfield)           3
   Greenfield        (42, North Adams-Greenfield)                (42, North Adams-Greenfield)              5
   Springfield             (76, Lee-Springfield)                         (32, Pittsfield-Lee)              4
    Fitchburg         (92, Greenfield-Fitchburg)                       (76, Lee-Springfield)              6
     Auburn            (123, Springfield-Auburn)                   (81, Greenfield-Springfield)            7
     Lowell             (125, Fitchburg-Lowell)                   (92, Greenfield-Fitchburg)             8
     Boston              (155, Lowell-Boston)                      (123, Springfield-Auburn)             9
  New Bedford        (194, Auburn-New Bedford)                      (124, Fitchburg-Auburn)            10
    Plymouth            (195, Boston-Plymouth)                      (125, Fitchburg-Lowell)            11
  Provincetown      (271, Plymouth-Provincetown)                 (194, Auburn-New Bedford)             14
                                                                      (170, Auburn-Boston)             13
                                                                      (155, Lowell-Boston)             12
                                                                  (213, Boston-New Bedford)            16
                                                                    (195, Boston-Plymouth)             15
                                                                (226, New Bedford-Plymouth)            17
                                                               (271, Plymouth-Provincetown)            18



From the table, we can find the shortest path by tracing back from the desired destination until we
work our way back to the source.




                                                    8

Mais conteúdo relacionado

Semelhante a Graph Algorithms Notes

Andrew Goldberg. An Efficient Point-to–Point Shortest Path Algorithm
Andrew Goldberg. An Efficient Point-to–Point  Shortest Path AlgorithmAndrew Goldberg. An Efficient Point-to–Point  Shortest Path Algorithm
Andrew Goldberg. An Efficient Point-to–Point Shortest Path AlgorithmComputer Science Club
 
Graphic Storytelling in New Media
Graphic Storytelling in New MediaGraphic Storytelling in New Media
Graphic Storytelling in New MediaTyler Sticka
 
'Abacus' - future of personal finance project
'Abacus' - future of personal finance project'Abacus' - future of personal finance project
'Abacus' - future of personal finance projectFraser
 
Sketch sort ochadai20101015-public
Sketch sort ochadai20101015-publicSketch sort ochadai20101015-public
Sketch sort ochadai20101015-publicYasuo Tabei
 
Binary numbersystem1
Binary numbersystem1Binary numbersystem1
Binary numbersystem1Neha Satwani
 
Microsoft Word Hw#2
Microsoft Word   Hw#2Microsoft Word   Hw#2
Microsoft Word Hw#2kkkseld
 

Semelhante a Graph Algorithms Notes (10)

Andrew Goldberg. An Efficient Point-to–Point Shortest Path Algorithm
Andrew Goldberg. An Efficient Point-to–Point  Shortest Path AlgorithmAndrew Goldberg. An Efficient Point-to–Point  Shortest Path Algorithm
Andrew Goldberg. An Efficient Point-to–Point Shortest Path Algorithm
 
Binary addition
Binary additionBinary addition
Binary addition
 
Informatico
InformaticoInformatico
Informatico
 
Graphic Storytelling in New Media
Graphic Storytelling in New MediaGraphic Storytelling in New Media
Graphic Storytelling in New Media
 
'Abacus' - future of personal finance project
'Abacus' - future of personal finance project'Abacus' - future of personal finance project
'Abacus' - future of personal finance project
 
Sketch sort ochadai20101015-public
Sketch sort ochadai20101015-publicSketch sort ochadai20101015-public
Sketch sort ochadai20101015-public
 
Binary numbersystem1
Binary numbersystem1Binary numbersystem1
Binary numbersystem1
 
Microsoft Word Hw#2
Microsoft Word   Hw#2Microsoft Word   Hw#2
Microsoft Word Hw#2
 
16%20 lecture
16%20 lecture16%20 lecture
16%20 lecture
 
01 - Software
01 - Software01 - Software
01 - Software
 

Mais de chidabdu

Sienna 10 dynamic
Sienna 10 dynamicSienna 10 dynamic
Sienna 10 dynamicchidabdu
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashingchidabdu
 
Sienna 8 countingsorts
Sienna 8 countingsortsSienna 8 countingsorts
Sienna 8 countingsortschidabdu
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heapschidabdu
 
Sienna 6 bst
Sienna 6 bstSienna 6 bst
Sienna 6 bstchidabdu
 
Sienna 5 decreaseandconquer
Sienna 5 decreaseandconquerSienna 5 decreaseandconquer
Sienna 5 decreaseandconquerchidabdu
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquerchidabdu
 
Sienna 3 bruteforce
Sienna 3 bruteforceSienna 3 bruteforce
Sienna 3 bruteforcechidabdu
 
Sienna 2 analysis
Sienna 2 analysisSienna 2 analysis
Sienna 2 analysischidabdu
 
Sienna 1 intro
Sienna 1 introSienna 1 intro
Sienna 1 introchidabdu
 
Sienna 13 limitations
Sienna 13 limitationsSienna 13 limitations
Sienna 13 limitationschidabdu
 
Unit 3 basic processing unit
Unit 3   basic processing unitUnit 3   basic processing unit
Unit 3 basic processing unitchidabdu
 
Unit 5 I/O organization
Unit 5   I/O organizationUnit 5   I/O organization
Unit 5 I/O organizationchidabdu
 
Algorithm chapter 1
Algorithm chapter 1Algorithm chapter 1
Algorithm chapter 1chidabdu
 
Algorithm chapter 11
Algorithm chapter 11Algorithm chapter 11
Algorithm chapter 11chidabdu
 
Algorithm chapter 10
Algorithm chapter 10Algorithm chapter 10
Algorithm chapter 10chidabdu
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9chidabdu
 
Algorithm chapter 8
Algorithm chapter 8Algorithm chapter 8
Algorithm chapter 8chidabdu
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7chidabdu
 
Algorithm chapter 6
Algorithm chapter 6Algorithm chapter 6
Algorithm chapter 6chidabdu
 

Mais de chidabdu (20)

Sienna 10 dynamic
Sienna 10 dynamicSienna 10 dynamic
Sienna 10 dynamic
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashing
 
Sienna 8 countingsorts
Sienna 8 countingsortsSienna 8 countingsorts
Sienna 8 countingsorts
 
Sienna 7 heaps
Sienna 7 heapsSienna 7 heaps
Sienna 7 heaps
 
Sienna 6 bst
Sienna 6 bstSienna 6 bst
Sienna 6 bst
 
Sienna 5 decreaseandconquer
Sienna 5 decreaseandconquerSienna 5 decreaseandconquer
Sienna 5 decreaseandconquer
 
Sienna 4 divideandconquer
Sienna 4 divideandconquerSienna 4 divideandconquer
Sienna 4 divideandconquer
 
Sienna 3 bruteforce
Sienna 3 bruteforceSienna 3 bruteforce
Sienna 3 bruteforce
 
Sienna 2 analysis
Sienna 2 analysisSienna 2 analysis
Sienna 2 analysis
 
Sienna 1 intro
Sienna 1 introSienna 1 intro
Sienna 1 intro
 
Sienna 13 limitations
Sienna 13 limitationsSienna 13 limitations
Sienna 13 limitations
 
Unit 3 basic processing unit
Unit 3   basic processing unitUnit 3   basic processing unit
Unit 3 basic processing unit
 
Unit 5 I/O organization
Unit 5   I/O organizationUnit 5   I/O organization
Unit 5 I/O organization
 
Algorithm chapter 1
Algorithm chapter 1Algorithm chapter 1
Algorithm chapter 1
 
Algorithm chapter 11
Algorithm chapter 11Algorithm chapter 11
Algorithm chapter 11
 
Algorithm chapter 10
Algorithm chapter 10Algorithm chapter 10
Algorithm chapter 10
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9
 
Algorithm chapter 8
Algorithm chapter 8Algorithm chapter 8
Algorithm chapter 8
 
Algorithm chapter 7
Algorithm chapter 7Algorithm chapter 7
Algorithm chapter 7
 
Algorithm chapter 6
Algorithm chapter 6Algorithm chapter 6
Algorithm chapter 6
 

Último

Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 

Último (20)

Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 

Graph Algorithms Notes

  • 1. Computer Science 385 Analysis of Algorithms Siena College Spring 2011 Topic Notes: Graph Algorithms Our next few topics involve algorithms that operate on graph structures. Reachability As a simple example of something we can do with a graph, we determine the subset of the vertices of a graph G = (V, E) which are reachable from a given vertex s by traversing existing edges. A possible application of this is to answer the question “where can we fly to from ALB?”. Given a directed graph where vertices represent airports and edges connect cities which have a regularly- scheduled flight from one to the next, we compute which other airports you can fly to from the starting airport. To make it a little more realistic, perhaps we restrict to flights on a specific airline. For this, we will make use of a “visited” field that is included in the “structure” implementation of vertices and edges. We start with all vertices markes as unvisited, and when the procedure completes, all reachable vertices are marked as visited. See Example: ˜jteresco/shared/cs385/examples/Reachability This will visit the vertices starting from s in a breadth-first order. If we replace toVisit by a stack, we will visit vertices in a depth-first order. A recursive version implementation of depth-first reachability, would have a stack implicit in the recursion. The cost of this procedure will involve at most Θ(|V |+|E|) operations if all vertices are reachable, which is around Θ(|V |2 ) if the graph is dense. We can think about how to extend this to find reasonable flight plans, perhaps requiring that all travel takes place in the same day and that there is a minimum of 30 minutes to transfer. Transitive Closure Taking the transitive closure of a graph involves adding an edge from each vertex to all reachable vertices. We could do this by computing the reachability for each vertex, in turn, with the algorithm above. This would cost a total of Θ(|V |3 ). A more direct approach is due to Warshall.
  • 2. CS 385 Analysis of Algorithms Spring 2011 We modify the graph so that when we’re done, for every pair of vertices u and v such that v is reachable from u, there is a direct edge from u to v. Note that this is a destructive process! We modify our starting graph. The idea is that we build the transitive closure iteratively. When we start, we know that edges exist between any vertices that are connected by a path of length 1. We can find all pairs of vertices which are connected by a path of length 2 (2 edges) by looking at each pair of vertices u and v and checking, for each other vertex, whether there is another vertex w such that u is connected to w and w is connected to v. If so, we add a direct edge u to v. If we repeat this, we will then find pairs of vertices that were connected by paths of length 3 in the original graph. If we do this |V | times, we will have all possible paths added. This is still an Θ(|V |3 ) algorithm, though efficiency improvements are possible. The text presents this algorithm as an operation directly on an adjacency matrix representation of a graph, where each entry is a boolean value indicating whether an edge exists. warshall(A[1..n][1..n]) // Each R{i}[1..n][1..n] is an iteration toward the closure R{0} = A for k=1 to n for i=1 to n for j=1 to n R{k}[i][j] = R{k-1}[i][j] OR (R{k-1}[i][k] AND R{k-1}[k][j]) return R{n} All Pairs Minimum Distance We can expand this idea to get Floyd’s Algorithm for computing minimum distances between all pairs of (reachable) vertices. For the example graph: 2
  • 3. CS 385 Analysis of Algorithms Spring 2011 Lowell 11 00 1 11 0 00 111111111111111111111 000000000000000000000 Williamstown 000000000000 111111111111 00000 11111 11 00 Greenfield 000 1111 0000 111 1 11 0 00 1 0 37 1 0 111111111111111111111 000000000000000000000 1 0 50 1 0 Fitchburg 33 11111 00000 111 000 1 11 0 00 111 000 1 0 50 1 111111111111111111111 000000000000000000000 1 0 1 0 1 0 11111 00000 111 1 0 000 North Adams 1 0 1111111111 0000000000 1 0 11111 00000 11111 00000 111 000 1 0 1 0 1 0 1 0 00000 11111 000 111 1 0 1 0 1 0 11111 00000 111 111 000 1 0 000 21 0 1 1 0 11111 00000 30 21 0 111 000 1 1 0 1 0 1 0 0 1 11111 00000 11111 00000 111 000 1 0 1 0 1 0 1 0 32 00000 11111 000 111 1 0 1 0 39 1 0 11111 00000 Boston 111 111 000 1 0 000Pittsfield 1 0 0 1 1 0 1 0 11111 00000 111 000 1 01 0 1 0 0 1 11111 00000 11111111111111 0000000000000011 00 11111 000001111 0000 1 01 0 1 0 1 0 47 11 00 11111 11111111111111 00000000000000 000001111 0000 1 0 Auburn 0 11 00 1 111111111 000000000 11111111111111 00000000000000 111111111111111111111111 00000000001 1 0 11111 000001111 0000 Provincetown 1 011 00000000000 0 1 11 00 111111111 000000000 111111111100000000000000 11111 000001111 0000 1 0 1 0 1 0 1 01111111111111111111 0000000000000000000 1 0 47 11 00 1111111111000000000 111111111 0000000000000000000 11111 00000 11111 000001111 0000 1111 0000 1111111111 0000000000 1111111111 0000000000 1 0 111111111111 000000000000 1 0 1 01 0 111111111 1111111111111111111 0000000000000000000 1 0 11111 000000000 1111 1111111111 0000000000 111111111111 000000000000 1 0 44 111111111111 000000000000 1 0 11111 000001111 0000 0000000000 1111111111 1 0 1 0 1 0 111111111 000000000 11111 000001111 0000 1111111111 0000000000 Springfield 111111111 000000000 111111111 11111 000001111 0000 1111111111 0000000000 000000000 111111111 000000000 11111 000001111 0000 1111111111 0000000000 40 Lee 111111111 000000000 11111 000000000 1111 1111111111 0000000000 0000000000 111111111 000000000 11111 000001111 0000 1111111111 1111111111 0000000000 111111111 000000000 11111 00000 111111111 0000 76 1111111111 0000000000 111111111 000000000 00000 11111 000001111 0000 1111 0000 1111111111 0000000000 111111111 000000000 111111111 11111 00000 000000000 58 0000 1111 1111111111 0000000000 111111111 000000000 11111 000001111 0000 0000000000 1111111111 71 111111111 000000000 11111 11 00000 00 1111 0000 1111111111 0000000000 1111111111 0000000000 111111111 000000000 11111111 00000000 11111 11 00000 00 1111 0000 11111111 00000000 1111111111 0000000000 111111111 000000000 11111 11 00000 00 11111111 00000000 111111111 000000000 11111 00000 11111111 00000000 11111 00000 Plymouth 111111111 000000000 111111111 000000000 11111111 00000000 11111 00000 111111111 000000000 11111111 00000000 11111 00000 11111111 00000000 111111111 000000000 11111 00000 11111111 00000000 111111111 000000000 11111 00000 11111111 00000000 11111 0000032 111111111 000000000 111111111 000000000 11111111 00000000 11111 00000 111111111 000000000 11111111 00000000 11111 00000 11111111 00000000 111111111 000000000 11111 00000 11111111 00000000 111111111 000000000 11111 00000 11 00 11 00 New Bedford We can use the same procedure (three nested loops over vertices) as we did for Warshall’s Algo- rithm, but instead of just adding edges where they may not have existed, we will add or modify edges to have the minimum cost path (we know of) between each pair. The text’s algorithm again works directly on the adjacency matrix, now with weights representing the edges in the matrix. floyd(W[1..n][1..n]) D=W // matrix copy for k=1 to n for i=1 to n for j=1 to n D[i][j] = min{D[i][j],D[i][k]+D[k][j]} return D Like Warshall’s Algorithm, this algorithm’s efficiency class is Θ(|V |3 ). Notice that at each iteration, we are overwriting the adjacency matrix. And we can see Floyd’s Algorithm in action using the above simple graph. See Example: ˜jteresco/shared/cs385/examples/MassFloyd Minimum Spanning Tree (MST) Algorithms Before we look at the graph algorithm, we think about a general class of algorithms to which they belong: greedy algorithms. The idea of a greedy technique is one which constructs a solution to an optimization problem one piece at a time by a sequence of choices which must be: • feasible 3
  • 4. CS 385 Analysis of Algorithms Spring 2011 • locally optimal • irrevocable In some cases, a greedy approach can be used to find an optimal solution, but in many cases it does not. Even in those cases, it often leads to a reasonably good solution in much less time than would be required to find the optimal. In our first example, a greedy approach does find an optimal solution. The problem is to find the minimum spanning tree of a weighted graph. The spanning tree of a connected graph G is a connected acyclic subgraph of G that includes all of G’s vertices. The minimum spanning tree of a weighted, connected graph G is the spanning tree of G (it may have many) of minimum total weight. We can construct examples of spanning trees and find the minimum using the graph: a ---6---- c | /| | / | 2 4 1 | / | | / | b ---3---- d The following greedy approach, known as Prim’s Algorithm, will compute the optimal answer as follows: • Start with a tree T1 , consisting of any one vertex. • We “grow” the tree one vertex at a time to produce the MST through a series of expanding subtrees T1 , T2 , ..., Tn . – On each iteration, we construct Ti+1 from Ti by adding the vertex not in Ti that is “closest” to those already in Ti (this is a “greedy” step). – The algorithm will use a priority queue to help find the nearest neighbor vertices to be added at each step. • Stop when all vertices are included in the tree. The text proves by induction that this construction always yields the MST. Efficiency: 4
  • 5. CS 385 Analysis of Algorithms Spring 2011 • Θ(|V |2 ) for the adjacency matrix representation of graph and an array implementation of the priority queue. • Θ(|E| log |V |) for an adjacency list representation and a min-heap implementation of the priority queue. A second optimal, greedy algorithm for finding MST’s, Kruskal’s Algorithm, is in the text and you will consider it as part of the next problem set. Single-Source Shortest Path Dijkstra’s Algorithm is a procedure to find shortest paths from a given vertex s in a graph G to all other vertices. The algorithm incrementally builds a sub-graph of G which is a tree contain- ing shortest paths from s to every other vertex in the tree. A step of the algorithm consists of determining which vertex to add to the tree next. Basic structures needed: 1. The graph G = (V, E) to be analyzed. 2. The tree, actually stored as a map, T . Each time a shortest path to a new vertex is found, an entry is added to T associating that vertex name with a pair indicating the total minimum distance to that vertex and the last edge traversed to get there. 3. A priority queue in which each element is an edge (u, v) to be considered as a path from a located vertex u and a vertex v which we have not yet located. The priority is the total distance from the starting vertex s to v using the known shortest path from s to u plus the length of (u, v). The algorithm proceeds as follows: T is an empty map; PQ is an empty priority queue; All vertices in V are marked unvisited; Add s to T with a total distance of 0 and a null previous edge; mark s as visited in G; Add each edge (s,v) of G to PQ with appropriate value while (T.size() < G.size() and PQ not empty) do nextEdge = PQ.remove(); until(one vertex of nextEdge is visited and the other is unvisited) or until there are no more edges in PQ // assume nextEdge = (v,u) where v is visited (in T) and u is unvisited (not in T) 5
  • 6. CS 385 Analysis of Algorithms Spring 2011 Add u to T; mark u as visited in G; Add (u,v) to T; for each unvisited neighbor w of u add (u,w) to PQ with appropriate weight When the procedure finishes, T should contain all vertices reachable from s, along with the last edge traverest along the shortest path from s to each such vertex. Disclaimer: Many details still need to be considered, but this is the essential information needed to implement the algorithm. Consider the following graph: Lowell 1111111111 0000000000 1111111111 0000000000 11111 00000 1 0 11111 00000 1 11 0 00111 0001 37 Greenfield Williamstown 000000000000 11 00 11111111111111111111111 000000000000 1 01 0 50 Fitchburg 1 0 1111111111 0000000000 33 11111 00000 11 00 1 11 0 001 0 111111111111 000000000000 1 0 11 00 1111111111 0000000000 11111 00000 11 00 North Adams 50 1 111111111111 000000000000 1 0 11 00 1111111111 0000000000 11111 00000 11 00 11 1 0 1 0 11 00 11111 00000 00 11 00 1 0 1 0 1 0 1 0 11 00 11111 00000 11 1 0 00 21 1 0 11 00 11111 00000 11111 00000 11 00 30 21 0 11 00 11 00 1 1 0 1 0 1 0 11 00 11111 00000 11111 00000 11 00 1 0 1 0 11 00 11 00 32 11111 00000 11 00 1 0 1 0 39 11 00 11111 00000 Boston 11 00 1 0 1 0 1 0 1 0 11 00 11111 00000 111111111111111 000000000000000 000000 1111111111 0000 11 00Pittsfield 1 0 1 0 11 00 11111 00000 111111111111111 000000000000000 000000 111111 1 0 1111 0000 11 00 1 0 1 0 11 00 111111111111111 000000000000000 47 000000 111111 1 0 1111 0000 1 0 11111111111 00000000000 1 0 11 00 111111111111111 000000000000000 Auburn 000000 1111111111 0000 11 00 Provincetown 1 011 11111111111 00000000000 1 0 11 001 0 0000000000 1111111111 111111111111111 11111111111 000000000000000 00000000000 000000 1111111111 0000 11 00 1 0 11111111111 00000000000 1 0 47 1 0 0000000000 1111111111 1111 0000 000000 00000000000 111111 11111111111 11 00 1 0 111111111111 000000000000 11111111111 00000000000 1 0 0000000000 1111111111 0000000000 0000 000000 00000000000 1111 111111 11111111111 1 0 111111111111 000000000000 1 0 1 011111111111 00000000000 1111111111 0000000000 1111 0000 000000 00000000000 111111 11111111111 1 0 111111111111 000000000000 1 0 44 1 0 1111111111 0000000000 1111111111 000000 00000000000 1111 0000 111111 11111111111 1 0 1 0 Springfield 0000000000 1111111111 0000 000000 00000000000 1111 111111 11111111111 0000000000 1111111111 000000 00000000000 1111 0000 111111 11111111111 Lee 0000000000 1111111111 000000 00000000000 1111 0000 111111 11111111111 40 000000 00000000000 0000000000 1111111111 1111 0000 111111 11111111111 000000 00000000000 0000000000 1111111111 1111 0000 111111 11111111111 000000 00000000000 0000 111111 11111111111 1111 76 0000000000 1111111111 000000 00000000000 1111 0000 111111 11111111111 0000000000 1111111111 0000000000 000000 00000000000 111111 11111111111 1111 0000 1111111111 0000000000 58000000 00000000000 1111 0000 111111 11111111111 1111111111 0000000000 000000 00000000000 111111 11111111111 1111 0000 1111111111 71 0000000000 1111111111 000000 00000000000 0000 111111 11111111111 1111 11111111 00000000 000000 00000000000 1111 0000 111111 11111111111 0000000000 1111111111 000000 0 111111 1 11111111 00000000 0000000000 1111111111 0000000000 1111111111 000000 0 111111 1 11111111 00000000 11111111 00000000 0000000000 1111111111 000000 Plymouth 111111 11111111 00000000 000000 0000000000 1111111111 111111 11111111 00000000 000000 0000000000 1111111111 111111 11111111 00000000 000000 111111 0000000000 1111111111 11111111 00000000 000000 111111 0000000000 1111111111 11111111 00000000 000000 11111132 0000000000 1111111111 0000000000 11111111 00000000 000000 111111 1111111111 0000000000 11111111 00000000 000000 111111 1111111111 0000000000 11111111 00000000 000000 111111 1111111111 0000000000 1111111111 11111111 00000000 11 00 000000 111111 11 00 New Bedford From that graph, the algorithm would construct the following tree for a start node of Williamstown. Costs on edges indicate total cost from the root. 6
  • 7. CS 385 Analysis of Algorithms Spring 2011 1 0 Lowell 1111111111 0000000000 1 0 11111 00000 000 Williamstown 1 11 0 00 42 Greenfield 11 11111111111 111 00 00000000000 111111111111 000000000000 1 0 125 1 0 1111111111 0000000000 11111 00000 11 00 1 11 0 00 1 0 92 Fitchburg 111111111111 000000000000 11 00 1111111111 0000000000 11111 00000 11 00 North Adams 111111111111 000000000000 11 00 1111111111 0000000000 11111 00000 11 00 5 11 00 11111 00000 11 00 11 11111 00000 00 11 00 11111 00000 11 00 11111 00000 155 11 00 11111 00000 21 11 00 11111 00000 11111 11 00 00000 11111 00000 Boston 11 00Pittsfield 11111 00000 1 0 11 00 1 0 11111 00000 1 0 1111 0000 11 00 1 0 1 0 1111 0000 1 0 1111 0000 Provincetown 11111111111 00000000000 1 032 1111 0000 11111111111 00000000000 11 00 1 0 123 1 0 1111 0000 11111111111 00000000000 11 00 1 0 1 0 1 0 1 0 1111 0000 11111111111 00000000000 1 0 76 1 0 1111 0000 11111111111 00000000000 1111 0000 1 01 0 Auburn 11111111111 00000000000 1111 0000 1 0 Springfield 11111111111 00000000000 1111 0000 11111111111 00000000000 1111 0000195 Lee 11111111111 00000000000 1111 0000 11111111111 00000000000 0000 1111 11111111111 00000000000 1111 0000 271 11111111111 00000000000 1111 0000 11111111111 00000000000 1111 0000 11111111111 00000000000 1111 0000 11111111111 00000000000 0000 194 1111 11111111111 00000000000 1111 0000 11111111111 00000000000 1111 0000 1 0 11111111111 00000000000 1 0 Plymouth 11 00 11 00 New Bedford 7
  • 8. CS 385 Analysis of Algorithms Spring 2011 We obtain this by filling in the following table, a The table below shows the evolution of the priority map which has place names as keys and pairs in- queue. To make it easier to see how we arrived at the dicating the distance from Williamstown and the solution, entries are not erased when removed from last edge traversed on that shortest route as val- the queue, just marked with a number in the “Seq” ues. column of the table entry to indicate the sequence It is easiest to specify edges by the labels of their in which the values were removed from the queue. endpoints rather than the edge label itself. Those which indicate the first (and thereby, shortest) paths to a city are shown in bold. Place (distance,last-edge) (distance,last-edge) Seq W’town (0, null) North Adams (5, W’town-North Adams) (5, Williamstown-North Adams) 1 Pittsfield (21, Williamstown-Pittsfield) (21, Williamstown-Pittsfield) 2 Lee (32, Pittsfield-Lee) (26, North Adams-Pittsfield) 3 Greenfield (42, North Adams-Greenfield) (42, North Adams-Greenfield) 5 Springfield (76, Lee-Springfield) (32, Pittsfield-Lee) 4 Fitchburg (92, Greenfield-Fitchburg) (76, Lee-Springfield) 6 Auburn (123, Springfield-Auburn) (81, Greenfield-Springfield) 7 Lowell (125, Fitchburg-Lowell) (92, Greenfield-Fitchburg) 8 Boston (155, Lowell-Boston) (123, Springfield-Auburn) 9 New Bedford (194, Auburn-New Bedford) (124, Fitchburg-Auburn) 10 Plymouth (195, Boston-Plymouth) (125, Fitchburg-Lowell) 11 Provincetown (271, Plymouth-Provincetown) (194, Auburn-New Bedford) 14 (170, Auburn-Boston) 13 (155, Lowell-Boston) 12 (213, Boston-New Bedford) 16 (195, Boston-Plymouth) 15 (226, New Bedford-Plymouth) 17 (271, Plymouth-Provincetown) 18 From the table, we can find the shortest path by tracing back from the desired destination until we work our way back to the source. 8