






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Single Source Shortest Paths Introduction: In a shortest- paths problem , we are given a weighted, directed graphs G = (V, E), with weight function w: E → R mapping edges to real-valued weights. The weight of path p = (v 0 ,v 1 ,..... vk) is the total of the weights of its constituent edges: We define the shortest - path weight from u to v by δ(u,v) = min (w (p): u→v), if there is a path from u to v, and δ(u,v)= ∞, otherwise. The shortest path from vertex s to vertex t is then defined as any path p with weight w (p) = δ(s,t). The breadth-first- search algorithm is the shortest path algorithm that works on unweighted graphs, that is, graphs in which each edge can be considered to have unit weight. In a Single Source Shortest Paths Problem , we are given a Graph G = (V, E), we want to find the shortest path from a given source vertex s ∈ V to every vertex v ∈ V. Variants: There are some variants of the shortest path problem.
Example:
3. Upper-bound property: We always have d[v] ≥ δ(s, v) for all vertices v ∈ V, and once d[v] conclude the value δ(s, v), it never changes. 4. No-path property: If there is no path from s to v, then we regularly have d[v] = δ(s, v) = ∞. 5. Convergence property: If s->u->v is a shortest path in G for some u, v ∈ V, and if d[u] = δ(s, u) at any time prior to relaxing edge (u, v), then d[v] = δ(s, v) at all times thereafter. Relaxation The single - source shortest paths are based on a technique known as relaxation , a method that repeatedly decreases an upper bound on the actual shortest path weight of each vertex until the upper bound equivalent the shortest - path weight. For each vertex v ∈ V, we maintain an attribute d [v], which is an upper bound on the weight of the shortest path from source s to v. We call d [v] the shortest path estimate. INITIALIZE - SINGLE - SOURCE (G, s) 1. for each vertex v ∈ V [G] 2. do d [v] ← ∞ 3. π [v] ← NIL 4. d [s] ← 0 After initialization, π [v] = NIL for all v ∈ V, d [v] = 0 for v = s, and d [v] = ∞ for v ∈ V - {s}. The development of relaxing an edge (u, v) consists of testing whether we can improve the shortest path to v found so far by going through u and if so, updating d [v] and π [v]. A relaxation step may decrease the value of the shortest
(b) Here, v. d < u. d + w (u, v) before relaxing the edge, and so the relaxation step leaves v. d unchanged. The subsequent code performs a relaxation step on edge (u, v) RELAX (u, v, w)
Step 3: Now find the adjacent of y that is t, x, z.
Then d [x] ← 13 π [x] ← z Case - (ii) z → s d [v] > d [u] + w [u, v] d [s] > d [z] + w [z, s] 0 > 7 + 7 0 > 14 ∴ This condition does not satisfy so it will be discarded. Now we have x = 13. Step 5: Now we will find Adj [t] Adj [t] → [x, y] [Here t is u and x and y are v] Case - (i) t → x d [v] > d [u] + w [u, v] d [x] > d [t] + w [t, x] 13 > 8 + 1 13 > 9 Then d [x] ← 9 π [x] ← t Case - (ii) t → y d [v] > d [u] + w [u, v] d [y] > d [t] + w [t, y] 5 > 10 ∴ This condition does not satisfy so it will be discarded. Thus we get all shortest path vertex as Weight from s to y is 5 Weight from s to z is 7 Weight from s to t is 8 Weight from s to x is 9 These are the shortest distance from the source's' in the given graph.
Solution: distk^ [u] = [min[distk-^1 [u],min[distk-^1 [i]+cost [i,u]]] as i ≠ u. dist^2 [2]=min[dist^1 [2],min[dist^1 [1]+cost[1,2],dist^1 [3]+cost[3,2],dist^1 [4]+cost[4,2],dist^1 [5]+cost[5,2]]] Min = [6, 0 + 6, 5 + (-2), ∞ + ∞ , ∞ +∞] = 3 dist^2 [3]=min[dist^1 [3],min[dist^1 [1]+cost[1,3],dist^1 [2]+cost[2,3],dist^1 [4]+cost[4,3],dist^1 [5]+cost[5,3]]] Min = [5, 0 +∞, 6 +∞, ∞ + ∞ , ∞ + ∞] = 5 dist^2 [4]=min[dist^1 [4],min[dist^1 [1]+cost[1,4],dist^1 [2]+cost[2,4],dist^1 [3]+cost[3,4],dist^1 [5]+cost[5,4]]] Min = [∞, 0 +∞, 6 + (-1), 5 + 4, ∞ +∞] = 5 dist^2 [5]=min[dist^1 [5],min[dist^1 [1]+cost[1,5],dist^1 [2]+cost[2,5],dist^1 [3]+cost[3,5],dist^1 [4]+cost[4,5]]] Min = [∞, 0 + ∞,6 + ∞,5 + 3, ∞ + 3] = 8 dist^3 [2]=min[dist^2 [2],min[dist^2 [1]+cost[1,2],dist^2 [3]+cost[3,2],dist^2 [4]+cost[4,2],dist^2 [5]+cost[5,2]]] Min = [3, 0 + 6, 5 + (-2), 5 + ∞ , 8 + ∞ ] = 3 dist^3 [3]=min[dist^2 [3],min[dist^2 [1]+cost[1,3],dist^2 [2]+cost[2,3],dist^2 [4]+cost[4,3],dist^2 [5]+cost[5,3]]] Min = [5, 0 + ∞, 3 + ∞, 5 + ∞,8 + ∞ ] = 5 dist^3 [4]=min[dist^2 [4],min[dist^2 [1]+cost[1,4],dist^2 [2]+cost[2,4],dist^2 [3]+cost[3,4],dist^2 [5]+cost[5,4]]] Min = [5, 0 + ∞, 3 + (-1), 5 + 4, 8 + ∞ ] = 2 dist^3 [5]=min[dist^2 [5],min[dist^2 [1]+cost[1,5],dist^2 [2]+cost[2,5],dist^2 [3]+cost[3,5],dist^2 [4]+cost[4,5]]] Min = [8, 0 + ∞, 3 + ∞, 5 + 3, 5 + 3] = 8
dist^4 [2]=min[dist^3 [2],min[dist^3 [1]+cost[1,2],dist^3 [3]+cost[3,2],dist^3 [4]+cost[4,2],dist^3 [5]+cost[5,2]]] Min = [3, 0 + 6, 5 + (-2), 2 + ∞, 8 + ∞ ] = dist^4 [3]=min[dist^3 [3],min[dist^3 [1]+cost[1,3],dist^3 [2]+cost[2,3],dist^3 [4]+cost[4,3],dist^3 [5]+cost[5,3]]] Min = 5, 0 + ∞, 3 + ∞, 2 + ∞, 8 + ∞ ] = dist^4 [4]=min[dist^3 [4],min[dist^3 [1]+cost[1,4],dist^3 [2]+cost[2,4],dist^3 [3]+cost[3,4],dist^3 [5]+cost[5,4]]] Min = [2, 0 + ∞, 3 + (-1), 5 + 4, 8 + ∞ ] = 2 dist^4 [5]=min[dist^3 [5],min[dist^3 [1]+cost[1,5],dist^3 [2]+cost[2,5],dist^3 [3]+cost[3,5],dist^3 [5]+cost[4,5]]] Min = [8, 0 +∞, 3 + ∞, 8, 5] = 5