in Interview Questions retagged by
817 views
1 vote
1 vote
Question was about Single Source shortest path -
We know that Dijkstra may/ may not handle negative edge weights in graph but Bellman Ford can. Also, the time complexity of bellman ford is higher than that of Dijkstra's. Now what if we add a large positive integer to all the weights of the graph, and then simply apply Dijkstra? What is the need of Bellman Ford then?
in Interview Questions retagged by
817 views

2 Comments

@mahima,

please make corrections in ur statement that  Dijkstra cannot handle negative edge weights cycle in the graph but Bellman-Ford can...
1
1
Yes Dijkstra algorithm may or may not handle negative edge weight but can never handle negative edge cycle correct if I am wrong
0
0

1 Answer

3 votes
3 votes
Best answer

Generally adding a weight changes the length of different paths by different amount. Say one path contains 3 edges and another contains 1 edge, and you increment weight by 1, then length of first path is increased by 3 whereas length of second path is increased by only 1. Below is an example of case where adding a weight approach fails. Shortest path from leftmost node to rightmost node is shown in red.

Before:

After:

selected by

Related questions

1 vote
1 vote
1 answer
1
0 votes
0 votes
0 answers
2
Churchill Khangar asked in Algorithms Nov 23, 2018
153 views
Churchill Khangar asked in Algorithms Nov 23, 2018
153 views
0 votes
0 votes
0 answers
3
Churchill Khangar asked in Algorithms Nov 22, 2018
316 views
Churchill Khangar asked in Algorithms Nov 22, 2018
316 views
0 votes
0 votes
0 answers
4
Churchill Khangar asked in Algorithms Nov 22, 2018
238 views
Churchill Khangar asked in Algorithms Nov 22, 2018
238 views
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true