Data Structures and Algorithms Scribe notes for lecture on Sept 20, 2004 By Yaung Zin Hlaing ID:101767 In this lecture, we are going to discuss about one of the shortest path algorithms called Breadth First Search and we will also discuss about the correctness of the algorithm. Moreover, we are also going to study relabeling and shortest path problems. Breadth First Search Given a graph G= (V,E), breadth first search (BFS) explores the edges of G to discover every vertex that is reachable from starting point s. For any vertex v reachable from s, the path in the BFS tree from s to v corresponds to a shortest path from s to v in G. The BFS Algorithm computes the distance from s to all reachable vertices. In BFS we explore the vertices in the order of their distance from the start. We also use FIFO queue to store the vertices that we have visited. The algorithm discovers all vertices at distance k from s before discovering any vertices at distance k+1. We first start at vertex s and enqueue s. Then we visit all other vertices that is reachable from s in one level and enqueue them. Then we dequeue one vertex and visit all other vertices that is reachable in one level from the dequeued vertex and so on, until all vertices have been visited. Here is how the algorithm works. BFS (G) 1 2 3 4 5 6 7 8 9 10 11 12 For each vertex v belongs to G Visited[v] <-false initialize d[v] Enqueue (Q,s) Visited[s] <-true While (Queue!= empty) v<- Dequeue(Q) For each edge (u,v) If visited[v]= false then Visited[v]<- true Enqueue(Q,v) d[u]<- d[v]+1 We can describe the distance we traveled in the graph in the following way. distance 4 3 2 1 time Analysis We can analyze the running time of the algorithm as follows: Each vertex is enqueued and dequeued at most once. This operation takes O(1) time, so the total time is O(V). Since the sum of length of edges is E, total running time is O(V+E). Lemma 1: Let G =(V,E) and s belongs to V, then for any edge (u,v) in E, shortest path distance(s,v) <= shortest path distance(s,u)+1 Lemma 2: Let G =(V,E) and BFS is run on G, then upon termination, for each vertex v in V, distance(v) satisfies the condition distance (v)>= shortest path distance(s,v) Lemma 3: Suppose during execution of BFS on G = (V,E), the queue Q contains vertices (v1, v2 , …., vr ) where v1 is head of Q and vr is tail. Then distance (vr)<=distance(v1)+1 and distance (vi)<=distance(vi+1) for i =1,2,…,r-1 Corollary: Suppose vertices vi and vj are enqueued during execution of BFS and vi enqueued before vj then distance (vi)<=distance(vj) Correctness of Breadth First Search algorithm Let G = (V,E) be a directed or undirected graph and BFS is run on G from vertex s. During execution BFS discovers every vertex v that is reachable from s and upon termination, BFS discovers that distance (v)= shortest path distance from s to v for all vertex v which belongs to V. Moreover, for any vertex v that is not equal to s and reachable from s, one of the shortest paths from s to v is the shortest path from s to predecessor of v which is followed by the edge from predecessor of v to v. Shortest Path Problem Given a directed graph G =(V,E) with edge length l and a source node s, we have to define the shortest path from s to v that is minimum number of edges from s to v. The shortest path means a path from s to v with the property that no other such path has a lower weight than the path from s to v. Negative Cycles: They refer to directed cycles in a graph whose total weight is negative. In these cases, we could use the cycle to construct a path that has a weight lower than any given value. B -3 -1 C A 2 Theorem: G has a shortest path from s to every node if and only if there is no negative cycles reachable from s. Proof: If there is a negative cycle reachable from s, then the algorithm gives arbitrarily shorter paths. If there is no negative cycle any path can be made simple and thus shortest path exists. Shortest Path Tree A shortest path tree T rooted at s is a spanning tree such that every path every path from s is the shortest path. Given T, find distance(v) ie, distance from s to v on T. Lemma: T is the shortest path tree if and only if for every edge (u,v) distance(v)<= distance(u)+length (u,v) distance(u) U V distance(v) length (u,v) S Proof: Consider any path p from s to t in the following figure. v1 v2 s v3 t Length of path>= distance(t) Length of path = length(s,v1)+ length(v1,v2)+ length(v2,v3)+length(v3,t) = (distance (v1)-distance(s)) + (distance (v2)-distance(v1))+ (distance (v3)-distance(v2))+ (distance (t)-distance (v3)) = distance (t) So, the length of path is at most distance of t. Relabeling (Ford Algorithm) distance(s) =0, distance(v) = infinite for all v+s p(v)=null for all v pick edge (u,v) such that distance(v)>distance(u)+length(u,v) distance(v) =distance(u)+length(u,v) p(v)=u repeat The purpose of this algorithm is to find edges that violate the given condition and to fix them. Correctness of the algorithm 1. If distance (v) is finite for some v, there must be some path p from s whose length is distance (v). We can prove this by induction. 2. When the algorithm halts distance (v) is the shortest path distance from s to v. distance (v)<= distance(u)+length(u,v) 3. If there is a negative cycle reachable from s, the algorithm never stops. 4. At any point, the parent pointer p either form a tree or there is some node v and integer k such that pk(v)=v ie, to follow p for k times. 5. If there is some node v and intger k such that pk(v)=v, then there is a negative cycle. 6. If the graph has no negative cycle reachable from v, the algorithm halts. A simple tree has no negative cycles.