Given a graph, a source vertex in the graph and a number k, find if there is a simple path (without any cycle) starting from given source and ending at any other vertex.
Example: Input : Source s = 0, k = 58 Output : True There exists a simple path 0 -> 7 -> 1 -> 2 -> 8 -> 6 -> 5 -> 3 -> 4 Which has a total distance of 60 km which is more than 58. Input : Source s = 0, k = 62 Output : False In the above graph, the longest simple path has distance 61 (0 -> 7 -> 1-> 2 -> 3 -> 4 -> 5-> 6 -> 8, so output should be false for any input greater than 61.
We strongly recommend you to minimize your browser and try this yourself first.
One important thing to note is, simply doing BFS or DFS and picking the longest edge at every step would not work. The reason is, a shorter edge can produce longer path due to higher weight edges connected through it.
The idea is to use Backtracking. We start from given source, explore all paths from current vertex. We keep track of current distance from source. If distance becomes more than k, we return true. If a path doesn’t produces more than k distance, we backtrack.
How do we make sure that the path is simple and we don’t loop in a cycle? The idea is to keep track of current path vertices in an array. Whenever we add a vertex to path, we check if it already exists or not in current path. If it exists, we ignore the edge.
Below is implementation of above idea.
Modify the above solution to find weight of longest path from a given source.
Time Complexity: O(n!)
From the source node, we one-by-one visit all the paths and check if the total weight is greater than k for each path. So, the worst case will be when the number of possible paths is maximum. This is the case when every node is connected to every other node.
Beginning from the source node we have n-1 adjacent nodes. The time needed for a path to connect any two nodes is 2. One for joining the source and the next adjacent vertex. One for breaking the connection between the source and the old adjacent vertex.
After selecting a node out of n-1 adjacent nodes, we are left with n-2 adjacent nodes (as the source node is already included in the path) and so on at every step of selecting a node our problem reduces by 1 node.
We can write this in the form of a recurrence relation as: F(n) = n*(2+F(n-1))
This expands to: 2n + 2n*(n-1) + 2n*(n-1)*(n-2) + ……. + 2n(n-1)(n-2)(n-3)…..1
As n times 2n(n-1)(n-2)(n-3)….1 is greater than the given expression so we can safely say time complexity is: n*2*n!
Here in the question the first node is defined so time complexity becomes
F(n-1) = 2(n-1)*(n-1)! = 2*n*(n-1)! – 2*1*(n-1)! = 2*n!-2*(n-1)! = O(n!)
This article is contributed by Shivam Gupta. The explanation for time complexity is contributed by Pranav Nambiar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Find maximum path length in a binary matrix
- Multi Source Shortest Path in Unweighted Graph
- Minimum edges to reverse to make path from a source to a destination
- Minimum cost path from source node to destination node via an intermediate node
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Union-Find Algorithm | (Union By Rank and Find by Optimized Path Compression)
- Find whether there is path between two cells in matrix
- Find if there is a path between two vertices in a directed graph
- Find shortest safe route in a path with landmines
- Find length of the largest region in Boolean Matrix
- Union-Find Algorithm | Set 2 (Union By Rank and Path Compression)
- Print all paths from a given source to a destination
- Print all paths from a given source to a destination using BFS
- Number of Walks from source to destination
Improved By : PranchalKatiyar