# Some interesting shortest path questions | Set 1

**Question 1: Given a directed weighted graph. You are also given the shortest path from a source vertex ‘s’ to a destination vertex ‘t’. If weight of every edge is increased by 10 units, does the shortest path remain same in the modified graph?**

The shortest path may change. The reason is, there may be different number of edges in different paths from s to t. For example, let shortest path be of weight 15 and has 5 edges. Let there be another path with 2 edges and total weight 25. The weight of the shortest path is increased by 5*10 and becomes 15 + 50. Weight of the other path is increased by 2*10 and becomes 25 + 20. So the shortest path changes to the other path with weight as 45.

**Question 2: ***This is similar to above question. Does the shortest path change when weights of all edges are multiplied by 10?*

If we multiply all edge weights by 10, the shortest path doesn’t change. The reason is simple, weights of all paths from s to t get multiplied by same amount. The number of edges on a path doesn’t matter. It is like changing unit of weights.

**Question 3: ***Given a directed graph where every edge has weight as either 1 or 2, find the shortest path from a given source vertex ‘s’ to a given destination vertex ‘t’. Expected time complexity is O(V+E).*

If we apply Dijkstra’s shortest path algorithm, we can get a shortest path in O(E + VLogV) time. How to do it in O(V+E) time? The idea is to use BFS . One important observation about BFS is, the path used in BFS always has least number of edges between any two vertices. So if all edges are of same weight, we can use BFS to find the shortest path. For this problem, we can modify the graph and split all edges of weight 2 into two edges of weight 1 each. In the modified graph, we can use BFS to find the shortest path. How is this approach O(V+E)? In worst case, all edges are of weight 2 and we need to do O(E) operations to split all edges, so the time complexity becomes O(E) + O(V+E) which is O(V+E).

**Question 4: **

*Given a directed acyclic weighted graph, how to find the shortest path from a source s to a destination t in O(V+E) time?*

See: Shortest Path in Directed Acyclic Graph

**More Questions** See following links for more questions.

http://algs4.cs.princeton.edu/44sp/

https://www.geeksforgeeks.org/algorithms-gq/graph-shortest-paths-gq/

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Shortest Path using Meet In The Middle
- Dijkstra’s shortest path algorithm using set in STL
- Shortest path in a Binary Maze
- Shortest path in an unweighted graph
- Multistage Graph (Shortest Path)
- 0-1 BFS (Shortest Path in a Binary Weight Graph)
- Dijkstra's Shortest Path Algorithm using priority_queue of STL
- Shortest Path in Directed Acyclic Graph
- Dijkstra's shortest path with minimum edges
- Find shortest safe route in a path with landmines
- Printing Paths in Dijkstra's Shortest Path Algorithm
- Multi Source Shortest Path in Unweighted Graph
- Shortest path with exactly k edges in a directed and weighted graph
- Dijkstra's shortest path algorithm in Java using PriorityQueue