Given an undirected graph, a source vertex ‘s’ and a destination vertex ‘d’, the task is to count total paths from given ‘s’ to ‘d’.
Input: s = 1, d = 4
Below are the 2 paths from 1 to 4
1 -> 3 -> 4
1 -> 2 -> 3 -> 4
Input: s = 3, d = 9
Below are the 6 paths from 3 to 9
3 -> 2 -> 1 -> 7 -> 6 -> 5 -> 10 -> 9
3 -> 2 -> 1 -> 7 -> 6 -> 10 -> 9
3 -> 2 -> 1 -> 7 -> 8 -> 9
3 -> 4 -> 2 -> 1 -> 7 -> 6 -> 5 -> 10 -> 9
3 -> 4 -> 2 -> 1 -> 7 -> 6 -> 10 -> 9
3 -> 4 -> 2 -> 1 -> 7 -> 8 -> 9
The idea is to do Depth First Traversal of given undirected graph.
- Start the traversal from the source.
- Keep storing the visited vertices in an array say ‘visited’.
- If we reach the destination vertex, increment the count by ‘1’.
- The important thing is to mark current vertices in visited as visited, so that the traversal doesn’t go in cycles.
Below is the implementation of the above approach:
- Time Complexity: In the above approach, for a given vertex, we check all vertices, So the time complexity is O(N*N) where N is no of vertices.
- Auxiliary Space Complexity: In the above approach, we are using a visited array of size N where N is number of vertices, so Auxiliary space complexity is O(N).
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print all shortest paths between given source and destination in an undirected graph
- Count number of ways to reach destination in a maze
- Shortest path in a graph from a source S to destination D with exactly K edges for multiple Queries
- Count all possible walks from a source to a destination with exactly k edges
- Print all paths from a given source to a destination
- Minimum edges to reverse to make path from a source to a destination
- Print all paths from a given source to a destination using BFS
- Check if a destination is reachable from source with two movements allowed
- Minimum cost path from source node to destination node via an intermediate node
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Sum of shortest distance on source to destination and back having at least a common vertex
- Print path from given Source to Destination in 2-D Plane
- Shortest Path with even number of Edges from Source to Destination
- Number of Walks from source to destination
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Convert undirected connected graph to strongly connected directed graph
- Find the minimum cost to reach destination using a train
- Minimum possible modifications in the matrix to reach destination
- Minimum steps to reach a destination
- Number of decisions to reach destination
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.