Distance in Graphs While the preios lectre stdied jst the connectiity properties of a graph, now we are going to inestigate how long (short, actally) a connection in a graph is. This natrally leads to the concept of graph distance, which has two ariants: the simple one considering only the nmber of edges, while the weighted one haing a length for each edge. a b c Brief otline of this lectre d x Distance in a graph, basic properties, triangle ineqality. Graph metrics: all-pairs shortest distances. Dijkstra s algorithm for the shortest weighted distance in a graph. Rote planning: a sketch of some adanced ideas. Petr Hliněný, FI MU Brno FI: MA: Distance in Graphs
. Graph distance (nweighted) Recall that a walk of length n in a graph G is an alternating seqence of ertices and edges, e,, e,,...,e n, n sch that each e i has the ends i, i. Definition.. Distance d G (, ) between two ertices, of a graph G is defined as the length of the shortest walk between and in G. If there is now walk between,, then we declare d G (, ) =. Informally and natrally, the distance between, eqals the least possible nmber of edges traersed from to. Specially d G(, ) =. Recall, moreoer, that the shortest walk is always a path Theorem.. Fact: The distance in an ndirected graph is symmetric, i.e. d G (, ) = d G (, ). Lemma.. The graph distance satisfies the triangle ineqality:,, w V (G) : d G (, ) + d G (, w) d G (, w). Proof. Easily; starting with a walk of length d G (, ) from to, and appending a walk of length d G (, w) from to w, reslts in a walk of length d G (, ) + d G (, w) from to w. This is an pper bond on the real distance from to w. Petr Hliněný, FI MU Brno FI: MA: Distance in Graphs
How to find the distance Theorem.. Let,, w be ertices of a connected graph G sch that d G (, ) < d G (, w). Then the breadth-first search algorithm on G, starting from, finds the ertex before w. Proof. We apply indction on the distance d G (, ): If d G (, ) =, i.e. =, then it is triial that is fond first. So let d G (, ) = d > and be a neighbor of closer to, which means d G (, ) = d. Analogosly choose w a neighbor of w closer to. Then d G (, w ) d G (, w) > d G (, ) = d G (, ), and so has been fond before w by the indctie assmption. Hence has been stored into U before w, and (cf. FIFO) the neighbors of ( among them, bt not w) are fond before the neighbors of w (sch as w). Corollary.. The breadth-first search algorithm on G correctly determines graph distances from the starting ertex. Petr Hliněný, FI MU Brno FI: MA: Distance in Graphs
Other related terms Definition. Let G be a graph. We define, with respect to G, the following notions: The excentricity of a ertex exc() is the largest distance from to another ertex; exc() = max x V (G) d G (, x). The diameter diam(g) of G is the largest excentricity oer its ertices, and the radis rad(g) of G is the smallest excentricity oer its ertices. The center of G is the sbset U V (G) of ertices sch that their excentricity eqals rad(g). Petr Hliněný, FI MU Brno FI: MA: Distance in Graphs
. All-pairs shortest distances Definition: The metrics of a graph is the collection of distances between all pairs of its ertices. In other words, the metrics is a matrix d[,] sch that d[i,j] is the distance from i to j. Method.. Dynamic programming for all-pairs distances in a graph G on the ertex set V (G) = {,,..., N }. Initially, let d[i,j] be (alternatiely, the edge length of { i, j }), or if i, j are not adjacent. After step t let it hold that d[i,j] is the shortest length of a walk between i, j sch that its internal ert. are from {,,..., t } (empty for t = ). Moing from step t to t +, we pdate all the distances as: Either d[i,j] from the preios step is still optimal (the ertex t does not help to obtain a shorter walk from i to j ), or there is a shorter i to j walk sing (also) the ertex t which is, by the assmption at step t, of length d[i,t]+d[t,j] d[i,j]. Theorem.6. Method. correctly comptes the distance d[i,j] between each pair of ertices i, j in N = V (G) steps. Petr Hliněný, FI MU Brno FI: MA: Distance in Graphs
Remark: In a practical implementation we may se, say, MAX INT/ in place of. Algorithm.7. Floyd Warshall algorithm (cf..) inpt < the adjacency matrix G[,] of an N-ertex graph, sch that the ertices of G are indexed as...n-, and G[i,j]= if i, j adjacent and G[i,j]= otherwise; for (i=; i<n; i++) for (j=; j<n; j++) d[i,j] = (i==j?: (G[i,j]? : MAX INT/)); for (t=; t<n; t++) { for (i=; i<n; i++) for (j=; j<n; j++) d[i,j] = min(d[i,j], d[i,t]+d[t,j]); } retrn The distance matrix d[,] ; Notice that this Algorithm.7 is extremely simple and relatiely fast it needs abot N steps to get the whole distance matrix. Its only problem is that all-pairs distances mst be compted at the same time, een if we need to know jst one distance... Petr Hliněný, FI MU Brno 6 FI: MA: Distance in Graphs
. Weighted distance in graphs Definition: A weighted graph is a graph G together with a weighting w of the edges by real nmbers w : E(G) R (edge lengths in this case). A positiely weighted graph G, w is sch that w(e) > for all edges e. Definition.8. (Weighted distance) Consider a positiely weighted graph G, w. The length of the weighted walk S =, e,, e,,..., e n, n in G is the sm d w G(S) = w(e ) + w(e ) + + w(e n ). The weighted distance in G, w between a pair of ertices, is d w G (, ) = min{dw G (S) : S is a walk from to }. All these terms natrally extend from graphs to directed graphs. Analogosly to Section. we get: Fact: The shortest walk in a positiely weighted (di)graph is always a path. Lemma.9. The weighted distance in a positiely weighted (di)graph satisfies the triangle ineqality. Petr Hliněný, FI MU Brno 7 FI: MA: Distance in Graphs
See an example... a b c The distances between a c and between b c are. What abot the a b distance? Is it 6? No, the distance from a to b in the graph is (traerse the pper path ). Petr Hliněný, FI MU Brno 8 FI: MA: Distance in Graphs
Negatie edge-lengths? What is the reason we are aoiding negatie edge lengths? x y Hence, what is the x y distance this graph? Say, or? No, it is, precisely by Definition.8, and this answer does not sond nice... Hence we hae got a good reason not to consider negatie edges in general. Petr Hliněný, FI MU Brno 9 FI: MA: Distance in Graphs
. Single-sorce shortest paths problem This section deals with the more specific problem of finding the shortest distance between one pair of terminals in a graph (or, from a single sorce to all other ertices). Remark: The coming Dijkstra s algorithm is, on one hand, slightly more inoled than Algorithm.7, bt it is significantly faster in the comptation of single-sorce shortest distances, on the other hand. Dijkstra s algorithm: Is a ariant of graph searching (related to BFS), in which eery discoered ertex carries a ariable keeping its temporary distance the length of the shortest so far discoered walk reaching this ertex from the starting ertex. We always pick from the depository the ertex with the shortest temporary distance. This is becase no shorter walk may reach this ertex (assming nonnegatie edge lengths). At the end of processing, the temporary distances become final shortest distances from the starting ertex (cf. Theorem.). Petr Hliněný, FI MU Brno FI: MA: Distance in Graphs
Algorithm.. Compting the single-sorce shortest paths (Dijkstra), i.e. finding the shortest walk from to, or from to all other ertices. inpt < N-ertex graph gien by adjacency mat. G[,] and cor. lengths len[,]; inpt <,, where is the starting ertex and the destination; // state[i] records the ertex processing state, dist[i] is the temporary distance for (i=; i<n; i++) { dist[i] = MAX INT; state[i] = init ; } dist[] = ; depository D = {}; while (state[]!= processed ) { if (D== ) retrn No path ; select m D with minimal dist[m]; // now pdating all neighbors of m and their temporary distances for (i=; i<n; i++) if (G[m,i]) { D = D {i}; if (dist[m]+len[m,i]<dist[i]) { income[i] = m; dist[i] = dist[m]+len[m,i]; } } state[m] = processed ; D = D \ {m}; } retrn A - path of length dist[], stored in incm[] reersely ; Petr Hliněný, FI MU Brno FI: MA: Distance in Graphs
Petr Hliněný, FI MU Brno FI: MA: Distance in Graphs Remark: Notice that Algorithm. works as-is also in directed graphs. Example.. An illstration rn of Dijkstra s Algorithm. from to in the following graph.
Petr Hliněný, FI MU Brno FI: MA: Distance in Graphs 7 7 7 6
Fact: The nmber of steps performed by Algorithm. to find the shortest path from to is abot N when N is the nmber of ertices (not so good...). On the other hand, with a better implementation of the depository, one can achiee on sparse graphs rntime almost linear in the nmber of edges. Theorem.. Eery iteration of Algorithm. (since jst after finishing the first while() loop) maintains an inariant that dist[i] is the length of a shortest path from to i sing only those internal ertices x of state[x]== processed. Proof: Briefly sing mathematical indction: In the first iteration, the first ertex m= is picked and processed, and its neighbors receie the correct straight distances (edge lengths). In eery next iteration, the picked ertex m is the nearest nprocessed one to the starting ertex. Assming nonnegatie costs len[,], this certifies that no shorter walk from to m may exist in the graph. On the other hand, any improed path from to an nfinished ertex i passing throgh m has mi as the last edge (since the distance of m is not smaller than of the other finished ertices). Hence dist[i] is pdated correctly in the algorithm. Petr Hliněný, FI MU Brno FI: MA: Distance in Graphs
. Adanced rote planning In some sitations, there is a better alternatie to ordinary Dijkstra s algorithm the Algorithm A which ses a sitable potential fnction to direct the search towards the destination. Wheneer we hae a good sense of direction (e.g. in a topo-map naigation), A can perform mch better! Algorithm A It re-implements Dijkstra with sitably modified edge costs. Let p (x) be a potential fnction giing an arbitrary lower bond on the distance from x to the destination. E.g., in a map naigation, p (x) may be the Eclidean distance from x to. Each directed(!) edge xy of the weighted graph G, w gets a new cost w (xy) = w(xy) + p (y) p (x). The potential p is admissible when all w (xy), i.e. w(xy) p (x) p (y). The aboe Eclidean potential is always admissible. The modified length of any - walk S then is d w G (S) = dw G (S)+p () p (), which is a constant difference from d w G (S)! Hence some S is optimal for the weighting w iff S is optimal for w. Here the Eclidean potential strongly prefers edges in the dest. direction. Petr Hliněný, FI MU Brno FI: MA: Distance in Graphs