Number of Unicolored Paths between two nodes

Given an undirected colored graph(edges are colored), with a source vertex ‘s’ and a destination vertex ‘d’, print number of paths which from given ‘s’ to ‘d’ such that the path is UniColored(edges in path having same color).

The edges are colored, here Colors are represented with numbers. At maximum, number of different colors will be number of edges.

Input : Graph
u, v, color
1, 2, 1
1, 3, 2
2, 3, 3
2, 4, 2
2, 5, 4
3, 5, 3
4, 5, 2
source = 2   destination = 5

Output : 3
Explanation : There are three paths from 2 to 5
2 -> 5 with color red
2 -> 3 - > 5 with color sky blue
2 -> 4 - > 5  with color green

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Algorithm :
1. Do dfs traversal on the neighbour nodes of source node.
2. The color between source node and neighbour nodes is known, if the DFS traversal also have same color, proceed, else stop going on that path.
3. After reaching destination node, increment count by 1.

NOTE : Number of Colors will always be less than number of edges.

Python3

Output:

Number of unicolored paths : 3

Time Complexity : O(E * (E + V))

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.

Improved By : rituraj_jain

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.