Lecture 10 0 0 1 Graph Algorithms Shortest Path Chapter 24 25 Problem 1 Given a directed graph G V E and weight function w E R mapping edges to real valued weights The weight of a path p hvo v1 vk i is the sum of the weights of edges along the path and is given by w p Pk i 1 w vi 1 vi A shortest path between v0 and vk is a path whose weight is minimum among all paths from v0 to vk and we denote this shortest distance as 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 some to be negative If there is a negative cycle then we may get into di culties Representing Shortest Paths For each pair of nodes u v we keep track of the node that occurs just before the destination v as its predecessor u v In the case of a single origin we get a tree using these predecessors in the form a predecessor subgraph G V E Here the set of nodes correspond to nodes that can be reached from the origin s Thus V v V s v 6 N IL s E s v v E v V s For the single origin case G is a tree rooted at s and is known 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 N IL d s 0 RELAX u v w if d v d u w u v then d v d u w u v v u Let 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 vk i be a shortest path from v0 to vk in G V E For any i and j such that 1 i j k let pi j hvi vj i be a sub path from vi to vj Then pi j is a shortest path between these vertices Corollary 3 Let a shortest path p between s and v be broken into ps u and the edge u v Then s v s u w u v 1 Lemma 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 relaxation algorithm 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 min s k w k j for j 6 s k6 j If 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 each such node j there is a last node on the shortest path from s to j other than j If we denote this 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 its vertices in time equal to V E This is done by DFS 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 first discovered and the second is when the node is finished we have considered all edges starting from this node The final topological order is according to decreasing values of finish times If the graph is acyclic there will no back edge Edge classification is as follows Tree edges When a node v is first discovered by using an edge u v this edge is part of the DFS tree and the node u is the parent of the node v Forward edges If an edge u v has the node u as an ancestor of the node v the this edge is a forward edge Back edges If an edge 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 two nodes in distinct trees or between two nodes that do not have an ancestordescendent relationship 2 In the example below we have three kinds since there are no back edges B 2 E 3 S 1 B 2 G 4 I 5 6 D D 12 S 1 C C B 2 S 1 F H 7 E 3 G 4 B 2 S 1 F H 7 8 E 3 G 4 9 I 5 6 C F H 7 8 B 2 E 3 10 G 4 9 C F H 7 8 B 2 E 3 10 G 4 9 S 1 C 11 F C H 7 8 C 11 16 F C 11 16 G 4 9 I 5 6 F H 7 8 C E 3 10 F C C G 4 9 C I 5 6 F F 13 14 H 7 8 C C G 4 9 C I 5 6 F F 13 14 F C E 3 10 F H 7 8 C C F 13 14 E 3 10 D 12 15 I 5 6 E 3 10 D 12 15 C F F 13 14 D 12 15 S 1 B 2 17 S 1 18 G 4 9 B 2 D 12 15 C 11 16 E 3 10 S 1 B 2 17 S 1 B 2 C 11 I 5 6 I 5 6 H 7 8 D 12 C F D G 4 9 F 13 C 11 I 5 6 E 3 10 C F B 2 F D H 7 8 C 11 S 1 C I 5 6 F D 12 S 1 C D S 1 C 11 I 5 6 G 4 9 F B 2 D C E 3 10 H 7 8 C G 4 9 C I 5 6 F 3 F 13 14 C H 7 8 C DAG SHORTEST PATHS G w s topologically sort the vertices of G INITIALIZE SINGLE SOURCE G s for each vertex u taken in the topological sorted order do for each vertex v Adj u do RELAX u v w An example is shown below Vertices are in topological order alphabetic order 8 B 4 3 S A 4 S A 0 6 C 8 2 3 8 4 S A 0 F 1 B 4 7 C 8 2 7 8 4 S A 0 …
View Full Document
Unlocking...