retagged by
33,032 views
85 85 votes

Let $G = (V,E)$ be a directed, weighted graph with weight function $w: E \rightarrow \mathbb{R}$. For some function $f: V \rightarrow \mathbb{R}$, for each edge$(u,v)\in E$, define ${w}'(u,v)$ as $w(u,v)+f(u)-f(v)$.

Which one of the options completes the following sentence so that it is TRUE?

“The shortest paths in $G$ under $w$ are shortest paths under ${w}'$ too,_____________”.

  1. for every $f: V \rightarrow \mathbb{R}$
  2. if and only if $\forall u \in V, \: f(u)$ is positive
  3. if and only if $\forall u \in V, \: f(u)$ is negative
  4. if and only if $f(u)$ is the distance from $s$ to $u$ in the graph obtained by adding a new vertex $s$ to $G$ and edges of zero weight from $s$ to every vertex of $G$

9 Answers

Best answer
55 55 votes
Correct Answer: A

For any mapping of vertices to real values, the shortest paths won't change. All intermediate nodes values get canceled on any path you take and what you're left with is only the source and destination node values which would add up to cost on any path. Hence, the shortest paths would still be the same.

PS: Option D is wrong because of the "if and only if" clause in it. If it were "if", it would be correct. The condition given is sufficient but not necessary. Hence, "only if" is incorrect in the option. Basically it is saying $f(u)$ would be 0 for all vertices since they're connected to a new vertex s with zero weighted edge. Similarly options B and C are also wrong for the same reason.
selected by
52 52 votes

Some important concepts - 

(Note that the question is asking for shortest paths and not single source shortest path)
Consider this directed weighted graph - 

What is the shortest Path from A to C and its cost?

A->B->C , cost = 2.

But, as there is a negative edge weight(B to C), running Dijkshtra Algorithm may give wrong result. Thus, we have to use Bellman Ford algorithm with is asymptotically costlier than Dijkshtra.

So, now can we think of a solution so that we can apply Dijskhtra on this graph and get the correct Shortest path?

One solution is to add 3 to all edges, so negative edge weight becomes non negative.

The edge weights would become- 

Notice here that the shortest path has been changed. The new shortest path is A->C with cost of 6.

So this solution changes the shortest path itself.

Another solution - Reweighting.

Assign weights to the vertices (assigned in green)

Now, after reweighting the edges as  -> w(u,v) + f(u) - f(v)  we get -

Notice that shortest path from A to C remains unchanged but COST of the shortest path has changed from 2 to 8 . 

More Analysis- 
If we observe carefully, the shortest path from A to C consists of 2 edges, A to B(e1) and B to C(e2)

Thus, cost of total path is e1 + e2 . 
Now, before reweighting, e1 + e2 = 2.

After reweigthing, 

e1  = 5 + f(A)  - f(B)

e2 = (-3) + (B) - f(C)

Thus, e1+ e2 = 5 + f(A) - f(B) + (-3) + f(B) - f(C).

Therefore, e1 + e2 = 5-3+f(A) -f(C)

e1 + e2 = 2 + 10 - 4

e1 + e2 = 8.

Here, the intermediate vertex B's weight got cancelled out. Thus, what matters is only the weights of source and destination vertices  in the  shortest path.
Thus, whatever weights we assign to the vertices(positive/negative), the shortest PATHS wont change, but shortest path COST will change.

Hence option A is correct.


This Question is based on the concepts used in  JOHNSON'S ALGORITHM which is  used to find the shortest paths between all pairs of vertices in a weighted, directed graph. It is particularly useful for sparse graphs that contain negative-weight edges but do not have negative-weight cycles.

ref - https://www.geeksforgeeks.org/johnsons-algorithm/

 

39 39 votes

The way NEW Edge Weights are defined, for any path $Y$ from source $s$ to destination $d$ in $G$, the new length will become:

$\text{Old Length} + f(s) - f(d).$ So, the NEW length of any path ONLY depends on the source and the destination node of the path, NOT on any intermediate vertices on the path(intermediate vertices $f(v)$ values will cancel out, Only source’s $f(s)$ and destination’s $f(d)$ will remain).

For any two vertices $s,d;$ assume that we have two paths $P,Q$ from $s$ to $d.$ 

Assume in the Old Graph, Length of $P = P_{OLD}$ and Length of $Q = Q_{OLD} $ and assume that $P_{OLD} \leq Q_{OLD}.$

In the NEW Graph, Length of $P = P_{NEW} = P_{OLD} + f(s) – f(d)$ and

Length of $Q = Q_{NEW} = Q_{OLD} + f(s) – f(d).$

NOW, Since $P_{OLD} \leq Q_{OLD},$ So, WHATEVER real values $f(s),f(d) $ have, we have $P_{NEW} \leq Q_{NEW}.$

So, this statement is Correct: For any mapping of vertices to real values, the shortest paths won't change BUT length of Shortest path will definitely change(by a value $f(s) – f(d)$).


Regarding Option $D:$

It doesn’t matter what is $f(u)$ for any $u.$ So, WHATEVER option is created regarding $f$ value of vertices, it will be only sufficient condition. Only option $A$ is necessary & sufficient condition.

Just a small “Irrelevant to the question” note that Option $D$ is Not necessarily making all $f(u) = 0.$ In case of negative weights, for some vertices $f$ can be negative in option D. BUT again WHATEVER $f$ we have, it doesn’t matter.

edited by
11 11 votes

Ans-(a)

1. Given, We have a Graph G(V,E) and weight has real value.
2. Now, suppose we compute weight of edges in a new way such that between vertex u and v weight will be defiened as  wi +      f(u) - f(v). 'f' represent effective cost value at vertex u or v. 
3. Now if we want to find minimum cost path between vertex g and h. Then earlier it would have been all weights of edge in

    between g and h path summed. And then choose minimum among them. Assume those weights are w1 , w2  and w3. 

                                               
4. Now in new definition. We will get all edge weight computed. But we notice that all for computing minimum weight path

     we always have = $\sum$wi + f(g) - f(h). Rest all terms of 'f(x)' gets cancelled. If we choose another path then earlier we get

     value at part $\sum$wi different . But part f(g) - f(h) remains same always.

    So we have no change in shortest path under the new definition of edge weight too.

edited by
5 5 votes
Hi,

w(u,v) = (u, v) edge weight

w' (u,v) = w(u,v) + f(u) - f(v) = w( u,v)

Here f(u) - f(v) should be 0 .

So, option d is correct.
1 flag:
✌ Low quality (Vraj884)
Answer:
Position:
Show:

Related questions

59 59 votes
4 answers 4 answers
24.8k
24.8k views
Arjun asked Feb 12, 2020
24,809 views
Consider a double hashing scheme in which the primary hash function is $h_1(k)= k \text{ mod } 23$, and the secondary hash function is $h_2(k)=1+(k \text{ mod } 19)$. Ass...
81 81 votes
13 answers 13 answers
38.3k
38.3k views
Arjun asked Feb 12, 2020
38,308 views
Let $G = (V, E)$ be a weighted undirected graph and let $T$ be a Minimum Spanning Tree (MST) of $G$ maintained using adjacency lists. Suppose a new weighed edge $(u, v) ...
27 27 votes
7 answers 7 answers
18.3k
18.3k views
Arjun asked Feb 12, 2020
18,271 views
Consider a graph $G = (V,E)$, where $V = \{v_1,v_2, \dots ,v_{100}\}$, $E = \{(v_i,v_j) \mid 1\leq i < j \leq 100\}$, and weight of the edge $(v_i,v_j)$ is $\mid i – j \m...
26 26 votes
2 answers 2 answers
8.2k
8.2k views
Arjun asked Feb 12, 2020
8,228 views
There are multiple routes to reach from node $1$ to node $2$, as shown in the network.The cost of travel on an edge between two nodes is given in rupees. Nodes $\text{‘}a...