Given an unweighted directed graph and Q queries consisting of sequences of traversal between two nodes of the graph, the task is to find out if the sequences represent one of the shortest paths between the two nodes.
Input: 1 2 3 4 Output: NO Explanation: The first and last node of the input sequence is 1 and 4 respectively. The shortest path between 1 and 4 is 1 -> 3 -> 4 hence, the output is NO for the 1st example. Input: 1 3 4 Output: YES
The idea is to use Floyd Warshall Algorithm to store the length of all pairs of vertices. If the length of the shortest path between the starting and ending node of the sequence is one less than the length of the sequence, then the given sequence represents one of the shortest paths between the nodes.
Below is the implementation of above approach:
Time complexity: O(V^3)
- Printing Paths in Dijkstra's Shortest Path Algorithm
- Number of shortest paths in an unweighted and directed graph
- Check if the given graph represents a Bus Topology
- Multistage Graph (Shortest Path)
- Shortest path in an unweighted graph
- Check if the given graph represents a Ring Topology
- Check if the given graph represents a Star Topology
- Shortest Path in Directed Acyclic Graph
- 0-1 BFS (Shortest Path in a Binary Weight Graph)
- Multi Source Shortest Path in Unweighted Graph
- Shortest Path in a weighted Graph where weight of an edge is 1 or 2
- Shortest path with exactly k edges in a directed and weighted graph | Set 2
- Shortest path with exactly k edges in a directed and weighted graph
- Shortest path in a graph from a source S to destination D with exactly K edges for multiple Queries
- Check if two nodes are on same path in a tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.