Find a shortest path for a directed graph - algorithm

There is a directed graph G = [V ; E] with edge weights w(u, v) for (u, v) ∈ E.
Suppose the values for {d[v], π[v]}; v ∈ V and claims
that these are the length of the shortest path and the predecessor node in
it for v ∈ V , how could I verify if this statement is true or false that does not solve the entire shortest path problem from scratch? This is an problem I met with not many ideas in my head ..

The problem is a bit unclear, but to clarify:
There's a node s in your graph, and that for each vertex v:
for v != s, pi[v] is intended to be a node adjacent to v that's on a shortest path from v to s.
d[v] is intended to store the shortest distance from v to s.
The problem is to verify, given a pi, d, that they legitimately contain back-edges and minimal distances.
An easily implemented condition that verifies this is as follows:
For each vertex v
Either:
v = s and d[v] = 0
Or:
d[pi[v]] = d[v] - 1
d[u] >= d[v] - 1 for each u adjacent to v
pi[v] is adjacent to v
This check runs in O(V + E) time.

Related

Design an algorithm for the single source shortest path problem that runs in time O(k(|V|+|E|))

Suppose we are given a directed graph G = (V, E) with potentially positive and negative edge lengths, but no negative cycles. Let s ∈ V be a given source
vertex. How to design an algorithm for the single-source shortest path problem that runs in time O(k(|V | + |E|)) if the shortest paths from s to any other vertex takes at most k edges?
Here`s O(k(|V | + |E|)) approach:
We can use Bellman-Ford algorithm with some modifications
Create array D[] to store shortest path from node s to some node u
initially D[s]=0, and all other D[i]=+oo (infinity)
Now after we iterate throught all edges k times and relax them, D[u] holds shortest path value from node s to u after <=k edges
Because any s-u shortest path is atmost k edges, we can end algorithm after k iterations over edges
Pseudocode:
for each vertex v in vertices:
D[v] := +oo
D[s] = 0
repeat k times:
for each edge (u, v) with weight w in edges:
if D[u] + w < D[v]:
D[v] = D[u] + w

shortest Path in directed graph G

I had an exam yesterday and I would like to check if I was answering correctly on one of the questions.
The question:
G = (V, E, w) is a directed, simple graph (V: set of vertices, E: set of edges, w: non-negative weight function). There is a non-empty subset of G denoted E(red).
A path p in G will be called n-red if there are n red edges on p. d_red(u, v) will be the lightest path from vertex u to vertex v that is at least 1-red. If all paths from u to v are 0-red, d_red(u, v) = Infinity.
The weight of a path p is the sum of all edges that are part of p.
Input:
G = (V, E, w)
s, t that are elements of V
f_red: E -> { true, false }
f_red(red edge) = true
f_red(non-red edge) = false
Output:
d_red(s, t) (the lightest path that includes at least one red edge).
Runtime Constraint: O(V log V + E)
In a few words, my solution was to use Dijkstra's algorithm. A Boolean variable that is initially false is used to keep track of whether at least one red edge has been encountered. This is checked for every iteration with f_red and the variable is set to true if f_red(current edge) = true. If the variable is still false at the end, return d_red(u, v) = Infinity.
What do you think about that?

Algorithm for finding weight of path with lowest weight in weighted directed graph

I am given a G=(V,E) directed graph, and all of its edges have weight of either "0" or "1".
I'm given a vertex named "A" in the graph, and for each v in V, i need to find the weight of the path from A to v which has the lowest weight in time O(V+E).
I have to use only BFS or DFS (although this is probably a BFS problem).
I though about making a new graph where vertices that have an edge of 0 between them are united and then run BFS on it, but that would ruin the graph direction (this would work if the graph was undirected or the weights were {2,1} and for an edge of 2 i would create a new vertex).
I would appreciate any help.
Thanks
I think it can be done with a combination of DFS and BFS.
In the original BFS for an unweighted graph, we have the invariant that the distance of nodes unexplored have a greater or equal distance to those nodes explored.
In our BFS, for each node we first do DFS through all 0 weighted edges, mark down the distance, and mark it as explored. Then we can continue the other nodes in our BFS.
Array Seen[] = false
Empty queue Q
E' = {(a, b) | (a, b) = 0 and (a, b) is of E}
DFS(V, E', u)
for each v is adjacent to u in E' // (u, v) has an edge weighted 0
if Seen[v] = false
v.dist = u.dist
DFS(V, E', v)
Seen[u] = true
Enqueue(Q, u)
BFS(V, E, source)
Enqueue(Q, source)
source.dist = 0
DFS(V, E', source)
while (Q is not empty)
u = Dequeue(Q)
for each v is adjacent to u in E
if Seen[v] = false
v.dist = u.dist + 1
Enqueue(Q, v)
Seen[u] = true
After running the BFS, it can give you all shortest distance from the node source. If you only want a shortest distance to a single node, simply terminate when you see the destination node. And yes, it meets the requirement of O(V+E) time complexity.
This problem can be modified to the problem of Single Source Shortest Path.
You just need to reverse all the edge directions and find the minimum distance of each vertex v from the vertex A.
It could be easily observed that if in the initial graph if we had a minimal path from some vertex v to A, after changing the edge directions we would have the same minimal path from A to v.
This could be simply done either by Dijkstra OR as the edges just have two values {0 and 1}, it could also be done by modified BFS (first go to vertexes with distance 0, then 1, then 2 and so on.).

unable to comprehend prims algorithm

Please help in understanding prims algo pseudocode(as it is in coreman and wiki)
Prim's algorithm.
MST-PRIM (G, w, r) {
for each u ∈ G.V
u.key = ∞
u.parent = NIL
r.key = 0
Q = G.V
while (Q ≠ ø)
//1
u = Extract-Min(Q)
for each v ∈ G.Adj[u]
if (v ∈ Q) and w(u,v) < v.key
v.parent = u
v.key = w(u,v)}
i am able to understand till 1 or while loop that r.key=0 ensure that neighours or adjacents of root are scanned first,
but as u already belongs to Q(queue of nodes till now not included in prims minimum spanning tree) and v also in Q,will not help in generating prims MST.
also both coreman and thus wiki states
1. A = { (v, v.parent) : v ∈ V - {r} - Q }.
2. The vertices already placed into the minimum spanning tree are those in V−Q.
3. For all vertices v ∈ Q, if v.parent ≠ NIL, then v.key < ∞ and v.key is the weight of a light edge
Prior to each iteration of the while loop of lines 6–11,
(v, v.parent) connecting v ::to some vertex already placed into the minimum spanning tree.
as A is our MST then how 1. will help as v is already been included in our MST (as shown by v ∈ V - {r} - Q ) why then it should be included.
For the part that you have doubts:
u = Extract-Min(Q)
for each v ∈ G.Adj[u]
if (v ∈ Q) and w(u,v) < v.key
v.parent = u
v.key = w(u,v)
"For each vertex v, the attribute v:key is the minimum weight of any edge connecting to a vertex in the tree; by convention, key = ∞ if there is no such edge." (http://en.wikipedia.org/wiki/Prim's_algorithm)
Therefore, u = Extract-Min(Q) will get the vertex with the minimum key.
for each v ∈ G.Adj[u] will find all the neighbors of u.
if (v ∈ Q) and w(u,v) < v.key condition to eliminate cycle and check if path should be updated.
Then the following lines of code update the neighbors edges.
v.parent = u
v.key = w(u,v)
"Prior to each iteration of the while loop of lines 6–11,
1. A = { (v, v.parent) : v ∈ V - {r} - Q }. " (http://en.wikipedia.org/wiki/Prim's_algorithm)
Based on the above statement, before the while loop A is empty as Q = G.V! After the while loop you will get A contains all the vertices that form the MST. Each vertex v in A has a parent (v.parent). For root r, its parent is NIL. Root r is excluded due to the statement V - {r} but it exists in A thanks to its children in the form of v.parent.
Therefore in this link http://en.wikipedia.org/wiki/Prim's_algorithm , it states that: "2. The vertices already placed into the minimum spanning tree are those in V−Q."
and "When the algorithm terminates, the min-priority queue Q is empty; the minimum spanning tree A for G is thus A = { (v, v.parent) : v ∈ V - {r} }."

Analysis of BFS

I have the following BFS function from Cormen.
Definition of the shortest-path distance path(s,v) from s to v as the minimum number of edges in any path from vertex s to vertex v, or else if there is no path from s to v. A path of length path(s,v) from s to v is said to be a shortest path from s to v.
Following is lemma given
Let G = (V,E) be a directed or undirected graph, and let s belongs to V be an arbitrary vertex. Then, for any edge (u, v) E,
path(s,v) <= path(s,u) + 1 .
My question is why we have to have <= in above formula, i taught "=" is ok, can any one tell me one scenrio why we require <= ?
Below is BFS algorithm
Leema 2:
Let G = (V,E) be a directed or undirected graph, and suppose that BFS is run on G from a given source vertex s belongs to V. Then upon termination, for each vertex v belongs to V, the value d[v] computed by BFS satisfies d[v] >= path (s, v).
Proof:
We use induction on the number of times a vertex is placed in the queue Q. Our inductive hypothesis is that d[v] >= path(s,v) for all v belongs to V.
The basis of the induction is the situation immediately after s is placed in Q in line 8 of BFS.
The inductive hypothesis holds here, because d[s] = 0 = path(s, s) and d[v] = path (s, v) for all v belongs to V - {s}.
My question is what does author mean by "We use induction on the number of times a vertex is placed in the queue Q" ? and how it is related to inductive hypothesis?
Thanks!
BFS(G,s)
1 for each vertex u V[G] - {s}
2 do color[u] WHITE
3 d[u]
4 [u] NIL
5 color[s] GRAY
6 d[s] 0
7 [s] NIL
8 Q {s}
9 while Q
10 do u head[Q]
11 for each v Adj[u]
12 do if color[v] = WHITE
13 then color[v] GRAY
14 d[v] d[u] + 1
15 [v] u
16 ENQUEUE(Q,v)
17 DEQUEUE(Q)
18 color[u] BLACK
For your first question, consider a complete graph with only three vertices. In this graph is it true that path(s,v) = path(s,u) + 1 ?

Resources