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
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.
Number of unicolored paths : 3
Time Complexity : O(E * (E + V))
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Paths to travel each nodes using each edge (Seven Bridges of Königsberg)
- Check if given path between two nodes of a graph represents a shortest paths
- Print levels with odd number of nodes and even number of nodes
- Number of shortest paths in an unweighted and directed graph
- Number of unique paths in tree such that every path has a value greater than K
- Find the number of paths of length K in a directed graph
- Find maximum number of edge disjoint paths between two vertices
- Count the number of non-reachable nodes
- Calculate number of nodes in all subtrees | Using DFS
- Count the nodes whose sum with X is a Fibonacci number
- Number of sink nodes in a graph
- Number of special nodes in an n-ary tree
- Check which player visits more number of Nodes
- Count the number of nodes at a given level in a tree using DFS
- Minimize the number of weakly connected nodes
Improved By : rituraj_jain