Count paths between two vertices in un-directed graph
Count paths for a a given graph G has simple path from source s to destination d. Assume that graph is represented using adjacent matrix.
Solution:
- Use DFS
- When we explored all neighbours of a unvisited vertex then we set visited of that vertex to false so, that we ca n use for next iteration
Algorithm:
- Set count = 0
- Create boolean visited matrix for each vertex of graph G
- Call Count_Path function
- In count_path function
- Set visited of source_vertex to true
- If source_vertex == destination_vertex then
- Increment vertext counter
- Set visited of destination_vertex to false
- Explore all adjacent_vertices of sources_vertex
- If adjacent_vertex is not visited then
- Call count_path recursively with adjacent_vertex as sources vertex
- After exploring all neighbours of adjacent_vertex set visited of this vertex to false
Latest Source Code:
Github: GraphPathFinder.java
Output:
- T T T - T - - T - T - - T - T T T - T - - - T - Path Count between 0 to 5 are: 3