=========================================================================== CSC 263H Tutorial Outline for Week 12 Winter 2004 =========================================================================== --- BFS --- For any two vertices u,v in a graph, let delta(u,v) represent the number of edges on a shortest path from u to v (i.e., a path with the smallest number of edges). Prove that at the end of BFS (started from vertex s), d[v] = delta(s,v), the number of edges on a shortest path from s to v. First, we show that d[v] >= delta(s,v), by induction on the number of vertices that have been dequeued during the algorithm's execution: - Initially, d[s] = 0 = delta(s,s), and for v =/= s, d[v] = oo >= delta(s,v). - d[v] only changes once: when v is "discovered" from some other vertex u. Since u was placed in the queue before v, d[u] >= delta(s,u) by the induction hypothesis, so d[v] = d[u] + 1 >= delta(s,u) + 1 >= delta(s,v) (since delta(s,v) <= delta(s,u) + 1 for every edge (u,v) in E. The above claim can be easily shown. If u is reachable from s, then v must also be reachable from s. Thus the shortest path from s to v cannot be longer than the shortest path from s to u followed by the edge (u,v). If u is not reachable from s, delta(s,u) = infinity and the inequality holds.) Next, we have to show that d[v] <= delta(s,v). First, we show that if u_1, u_2, ..., u_k are the vertices of G in the order in which they were enqueued (so s = u_1), then d[u_1] <= d[u_2] <= ... <= d[u_k]. - For a contradiction, assume that this is not the case and let j be the first index such that d[u_{j-1}] > d[u_j]. Also, let u_i = p[u_j] (so i < j) and u_i' = p[u_{j-1}] (so i' < j-1). - Then, d[u_i] = d[u_j] - 1 < d[u_{j-1}] - 1 = d[u_i'], by definition of j, so it must be the case that i < i' (by definition of j again). But then, u_i was enqueued before u_i', which means that u_i was dequeued before u_i', which means that u_j must have been enqueued before u_{j-1}, which is a contradiction. This implies that if u is enqueued before v, d[u] <= d[v]. Now, it is possible to show that for every vertex v, d[v] <= delta(s,v). For a contradiction, assume that d[v] > delta(s,v) for some vertex v. Let v' be the vertex with the minimum value of delta(s,v) such that d[v'] > delta(s,v'). The vertex v' must be reachable from s. (If this were not the case, delta(s,v') = infinity >= d[v'].) Consider the shortest path from s to v', and let u be the vertex on this path that is directly before v'. delta(s,v') = delta(s,u) + 1 Thus delta(s,u) < delta(s,v'), and by our choice of v', delta(s,u) <= d[u]. From the first part, we know that this implies that delta(s,u) = d[u]. Thus we have, d[v'] > delta(s,v') = delta(s,u) + 1 = d[u] + 1. (*) Now consider the time when u was dequeued. Assume that v' was white when u was dequeued. Since there exists an edge (u,v'), we update d[v'] = d[u] + 1. This contradicts (*). So v' could not be white. Now assume that v' was black when u was dequeued. Then v' was previously dequeued from the queue. In other words, v' was enqueued before u. Thus d[v] <= d[u]. (We showed this earlier in this proof.) This contradicts (*). So v' could not be balck. Now assume that v' was gray when u was dequeued. Then there was another vertex, say w, that was responsible for colouring v' gray. We know that (w,v') is an edge in the graph, and w was dequeued before u. Thus d[w] <= d[u], and d[v'] = d[w] + 1 This implies that d[v'] <= d[u] + 1. This contradicts (*). So v' could not be gray. This provides a contradiction, since v' must be either white, black, or gray. Thus for every vertex v, d[v] <= delta(s,v). Now we know that delta(s,v) <= d[v] and d[v] <= delta(s,v) for every vertex v, i.e., delta(s,v) = d[v] for every vertex v.