Shortest Path in Graph Theory: Single Source, Negative Weights, Bellman-Ford, Summaries of Computer Networks

The shortest path problem in graph theory, focusing on the single source shortest paths problem, negative weights, and the Bellman-Ford algorithm. It covers the concepts of weighted graphs, shortest path weights, breadth-first search, and Dijkstra's algorithm. The document also explains the effects of negative weights and negative weight cycles on shortest path weights and provides an overview of the Bellman-Ford algorithm.

Typology: Summaries

2020/2021

Uploaded on 08/03/2021

ur4j
ur4j ๐Ÿ‡ณ๐Ÿ‡ต

4 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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 = (v0,v1,..... 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.
โ€ข Single- destination shortest - paths problem: Find the shortest path to a given destination vertex t from
every vertex v. By shift the direction of each edge in the graph, we can shorten this problem to a single -
source problem.
โ€ข Single - pair shortest - path problem: Find the shortest path from u to v for given vertices u and v. If we
determine the single - source problem with source vertex u, we clarify this problem also. Furthermore, no
algorithms for this problem are known that run asymptotically faster than the best single - source
algorithms in the worst case.
โ€ข All - pairs shortest - paths problem: Find the shortest path from u to v for every pair of vertices u and v.
Running a single - source algorithm once from each vertex can clarify this problem; but it can generally be
solved faster, and its structure is of interest in the own right.
Shortest Path: Existence:
If some path from s to v contains a negative cost cycle then, there does not exist the shortest path. Otherwise, there
exists a shortest s - v that is simple.
Negative Weight Edges
It is a weighted graph in which the total weight of an edge is negative. If a graph has a negative edge, then it
produces a chain. After executing the chain if the output is negative then it will give - โˆž weight and condition get
discarded. If weight is less than negative and - โˆž then we can't have the shortest path in it.
Briefly, if the output is -ve, then both condition get discarded.
1. - โˆž
2. Not less than 0.
And we cannot have the shortest Path.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Shortest Path in Graph Theory: Single Source, Negative Weights, Bellman-Ford and more Summaries Computer Networks in PDF only on Docsity!

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.

  • Single- destination shortest - paths problem: Find the shortest path to a given destination vertex t from every vertex v. By shift the direction of each edge in the graph, we can shorten this problem to a single - source problem.
  • Single - pair shortest - path problem: Find the shortest path from u to v for given vertices u and v. If we determine the single - source problem with source vertex u, we clarify this problem also. Furthermore, no algorithms for this problem are known that run asymptotically faster than the best single - source algorithms in the worst case.
  • All - pairs shortest - paths problem: Find the shortest path from u to v for every pair of vertices u and v. Running a single - source algorithm once from each vertex can clarify this problem; but it can generally be solved faster, and its structure is of interest in the own right. Shortest Path: Existence: If some path from s to v contains a negative cost cycle then, there does not exist the shortest path. Otherwise, there exists a shortest s - v that is simple. Negative Weight Edges It is a weighted graph in which the total weight of an edge is negative. If a graph has a negative edge, then it produces a chain. After executing the chain if the output is negative then it will give - โˆž weight and condition get discarded. If weight is less than negative and - โˆž then we can't have the shortest path in it. Briefly, if the output is - ve, then both condition get discarded.
    • โˆž
  1. Not less than 0. And we cannot have the shortest Path.

Example:

  1. Beginning from s
  2. Adj [s] = [a, c, e]
  3. Weight from s to a is 3 Suppose we want to calculate a path from sโ†’c. So We have 2 paths /weight
  4. s to c = 5 , sโ†’cโ†’dโ†’c= 8
  5. But sโ†’c is minimum
  6. So sโ†’c = 5 Suppose we want to calculate a path from sโ†’e. So we have two paths again
  7. sโ†’e = 2 , sโ†’eโ†’fโ†’e=- 1
  8. As - 1 < 0 โˆด Condition gets discarded. If we execute this chain, we will get - โˆž. So we can't get the shortest pat h โˆด e = โˆž. This figure illustrates the effects of negative weights and negative weight cycle on the shortest path weights. Because there is only one path from "s to a" (the path <s, a>), ฮด (s, a) = w (s, a) = 3. Furthermore, there is only one path from "s to b", so ฮด (s, b) = w (s, a) + w (a, b) = 3 + (-4) = - 1. There are infinite many path from "s to c" : <s, c> : <s, c, d, c>, <s, c, d, c, d, c> and so on. Because the cycle <c, d, c> has weight ฮด (c, d) = w (c, d) + w (d, c) = 6 + (-3) = 3, which is greater than 0, the shortest path from s to c is <s, c> with weight ฮด (s, c) = 5.

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

  • path estimate d [v] and updated v's predecessor field ฯ€ [v]. Fig: Relaxing an edge (u, v) with weight w (u, v) = 2. The shortest-path estimate of each vertex appears within the vertex. (a) Because v. d > u. d + w (u, v) prior to relaxation, the value of v. d decreases

(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)

  1. If d [v] > d [u] + w (u, v)
  2. then d [v] โ† d [u] + w (u, v)
  3. ฯ€ [v] โ† u Dijkstra's Algorithm It is a greedy algorithm that solves the single-source shortest path problem for a directed graph G = (V, E) with nonnegative edge weights, i.e., w (u, v) โ‰ฅ 0 for each edge (u, v) โˆˆ E. Dijkstra's Algorithm maintains a set S of vertices whose final shortest - path weights from the source s have already been determined. That's for all vertices v โˆˆ S; we have d [v] = ฮด (s, v). The algorithm repeatedly selects the vertex u โˆˆ V - S with the minimum shortest - path estimate, insert u into S and relaxes all edges leaving u. Because it always chooses the "lightest" or "closest" vertex in V - S to insert into set S, it is called as the greedy strategy. Dijkstra's Algorithm (G, w, s)
  4. INITIALIZE - SINGLE - SOURCE (G, s)
  5. Sโ†โˆ…
  6. Qโ†V [G]
  7. while Q โ‰  โˆ…
  8. do u โ† EXTRACT - MIN (Q)
  9. S โ† S โˆช {u}
  10. for each vertex v โˆˆ Adj [u]
  11. do RELAX (u, v, w) Analysis: The running time of Dijkstra's algorithm on a graph with edges E and vertices V can be expressed as a function of |E| and |V| using the Big - O notation. The simplest implementation of the Dijkstra's algorithm stores vertices of set Q in an ordinary linked list or array, and operation Extract - Min (Q) is simply a linear search through all vertices in Q. In this case, the running time is O (|V^2 |+|E|=O(V^2 ). Example:

Step 3: Now find the adjacent of y that is t, x, z.

  1. Adj [y] โ†’ t, x, z [Here y is u and t, x, z are v] Case - (i) y โ†’t d [v] > d [u] + w [u, v] d [t] > d [y] + w [y, t] 10 > 5 + 3 10 > 8 Then d [t] โ† 8 ฯ€ [t] โ† y Case - (ii) y โ†’ x d [v] > d [u] + w [u, v] d [x] > d [y] + w [y, x] โˆž > 5 + 9 โˆž > 14 Then d [x] โ† 14 ฯ€ [x] โ† 14 Case - (iii) y โ†’ z d [v] > d [u] + w [u, v] d [z] > d [y] + w [y, z] โˆž > 5 + 2 โˆž > 7 Then d [z] โ† 7 ฯ€ [z] โ† y By comparing case (i), case (ii) and case (iii) Adj [y] โ†’ x = 14, t = 8, z = z is shortest z is assigned in 7 = [s, z] Step - 4 Now we will find adj [z] that are s, x
  2. Adj [z] โ†’ [x, s] [Here z is u and s and x are v] Case - (i) z โ†’ x d [v] > d [u] + w [u, v] d [x] > d [z] + w [z, x] 14 > 7 + 6 14 > 13

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