Lecture #10:0.0.1 Graph Algorithms: Shortest Path (Chapter 24-25)Problem 1 Given a dir ected graph G =[V,E], and weight function w : E −→R mapping e dges to real valued weights. The weight of a path p = hvo,v1, ..., vkiis the sum of the weights of edges along the path and is given by w(p)=Pki=1w(vi−1,vi). A shortest path between v0and vkisapathwhoseweightis minimum among all paths from v0to vkandwedenotethisshortestdistanceas δ(v0,vk).Some variants of this problem: (i)from a single origin to all other nodes;(ii)from all other nodes to a single destination; (iii)between all pairs of nodes.Some algorithms require all weights to be nonnegative and others permit someto be negative. If there is a negative cycle, then we may get into difficulties.Representing Shortest P aths:For each pair of nodes (u, v) we keep track of the node that occurs justbefore the destination v as its predecessor, π(u, v). In the case of a singleorigin, we get a tree using these predecessors in the form a predecessor subgraphGπ=[Vπ,Eπ]. Here, the set of nodes correspond to nodes that can be reachedfrom the origin s.Thus,Vπ= {v ∈ V : π[s, v] 6= NIL}∪{s}; Eπ= {(π[s, v],v) ∈E : v ∈ {Vπ− {s}}. For the single origin case, Gπis a tree rooted at s and isknown as a shortest path tree.Single source shortest path algorithms use a technique called relaxation.INITIALIZE-SINGLE-SOURCE(G, s)for each vertex v ∈ V [G]do d[v] ←− ∞π[v] ←− NILd[s] ←− 0RELAX(u, v, w)if d[v] >d[u]+w(u, v)then d[v] ←− d[u]+w(u, v)π[v] ←− uLet δ(s, v) denote the length of a shortest path from s to v. In all this, d[v]should be thought of as the current estimate of δ(s, v).Lemma 2 Let p = hvo,v1, ..., vki be a shortest path from v0to vkin G =[V, E].For any i and j such that 1 ≤ i ≤ j ≤ k,letpi,j= hvi, ..., vji beasub-pathfromvito vj.Thenpi,jis a shortest path between these vertices.Corollary 3 Let a shortest path p between s and v be broken into ps,uand theedge (u, v).Thenδ(s, v)=δ(s, u)+w(u, v).1Lemma 4 Immediately after relaxing edge (u, v) by executing RELAX(u, v, w),we have d[v] ≤ d[u]+w(u, v).Lemma 5 Assume that we initialize as shown above. Then, the relaxationalgorithm maintains the invariant that d[v] ≥ δ(s, v) for all v ∈ V .Moreover,if equality is achieved at any step, it does not change.Bellman’s Equations:This is for a single source case.δ(s, s)=0δ(s, j)=mink6=j[δ(s, k)+w(k, j)] for j 6= sIf there are no negative cycles reachable from s, then it is clear that δ(s, s)=0,andδ(s, j) are well defined for nodes j reachable from s. For eac h such nodej, there is a last node on the shortest path from s to j other than j.Ifwedenotethis node by k, then the above equations are clear.Directed Acyclic Graphs (DAG):(Chapter 25.4)If G is a driected acyclic graph, then we can do a topological sort of itsvertices in time equal to Θ(|V | + |E|).ThisisdonebyDFS(Depth First Search) on the graph G. This is shown below with an example.Notice the time stamps. The first of these is the time when a node is firstdiscovered and the second is when the node is finished—wehaveconsideredall edges starting from this node. The final topological order is according todecreasing values of finish times. If the graph is acyclic, there will no backedge. Edge classification is as follows:Tree edges: When a node v is first discovered, by using an edge (u, v),thisedge is part of the DFS tree and the node u is the parent of the node v.Forward edges:Ifanedge(u, v) has the node u as an ancestor of the nodev, the this edge is a forward edge.Back edges:Ifanedge(u, v) has the node v as an ancestor of the node u,the this edge is a back edge. Back edges indicate the presence of directed cycles.Cross edges: All other edges are cross edges. These may be between twonodes in distinct trees or between two nodes that do not have an ancestor-descendent relationship.2Intheexamplebelow,wehavethreekindssincetherearenobackedges.S1/B2/I5/6CH7/G4/E3/FDCS1/B2/I5/6CH7/8G4/E3/FDCS1/B2/I5/6CH7/8G4/9E3/FDCS1/B2/I5/6CH7/8G4/9E3/10FDCFS1/B2/I5/6C11/H7/8G4/9E3/10FDCFS1/B2/I5/6C11/H7/8G4/9E3/10FD12/CFS1/B2/I5/6C11/H7/8G4/9E3/10F13/D12/CFS1/B2/I5/6C11/H7/8G4/9E3/10F13/14D12/CFCS1/B2/I5/6C11/H7/8G4/9E3/10F13/14D12/15CFCCS1/B2/I5/6C11/16H7/8G4/9E3/10F13/14D12/15CFCCFS1/B2/17I5/6C11/16H7/8G4/9E3/10F13/14D12/15CFCCFS1/18B2/17I5/6C11/16H7/8G4/9E3/10F13/14D12/15CFCCFF3DAG-SHOR TEST-P A THS(G, w, s)topologically sort the vertices of GINITIALIZE-SINGLE-SOURCE(G, s)for each vertex u taken in the topological sorted orderdo for each vert ex v ∈ Adj[u]do RELAX(u, v, w)An example is shown below: Vertices are in topological order (=alphabeticorder):S=ABIC HGEFD47-38487221-5-496S=A0B4IC8HGEFD47-38487221-5-496S=A0B4IC1HGE12FD47-38487221-5-49687S=A0B4IC1HGE12F2D847-384221-5-49687S=A0B4IC1HGE10F2D847-384221-5-49687S=A0B4IC1H14G17E10F2D847-384221-5-49687S=A0B4IC1H4G17E10F2D847-384221-5-49687S=A0B4I26C1H4G17E10F2D847-384221-5-49687S=A0B4I-1C1H4G17E10F2D847-384221-5-49687S=A0B4I-1C1H4G17E10F2D847-384221-5-49687S=A0B4I-1C1H4G17E10F2D847-384221-5-496Dijkstra’s Algorithm:4Here we assume that we have a single source and that w(u, v) ≥ 0 for alledges. The algorithm maintains a set S of vertices whose final shortest pathweights from the source have already been determined. It does this in increasingorder of δ(s, j) values. The algorithm repeatedly selects a vertex u ∈ V −S withminimum shortest path estimate, inserts u into S,andrelaxesalledgesleavingu. We maintain a priority queue Q that contains all vertices in V − S,keyedbytheir d values.DIJKSTRA(G, w, s)INITIALIZE-SINGLE-SOURCE(G, s)S ←− φQ ←− V [G]while Q 6= φdo u ←− EXTRACT-MIN(Q)S ←− S ∪ {u}for each vertex v ∈ Adj[u]do RELAX(u, v, w)5The following diagram shows the ev olution of this algorithm:SBIC HGEFD47118487221101496SB4IC8HGEFD47118487221101496SB4IC8HGE12FD47118487221101496SB4IC8HGE12F9D1547118487221101496SB4IC8H11GE12F9D1547118487221101496SB4I21C8H11GE12F9D1547118487221101496SB4I21C8H11G19E12F9D1547118487221101496SB4I21C8H11G19E12F9D1547118487221101496SB4I21C8H11G19E12F9D1547118487221101496SB4I21C8H11G19E12F9D1547118487221101496SB4I21C8H11G19E12F9D15478872110Bellman Algorithm:6This algorithm attempts to solve Bellman’s equations as follows:d1[s]=0;d1[j]=w(s, j) for j 6= sdm[j]=min{dm−1[j], mink6=j[dm−1[k]+w(k, j)]} for all jAlgorithm stops when we compute dn[j] or if dm[j]=dm−1[j] for all j forsome m<n.If you think of dm[j] as the minimum weight path
View Full Document