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))
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.
- Check if given path between two nodes of a graph represents a shortest paths
- Find maximum number of edge disjoint paths between two vertices
- Paths to travel each nodes using each edge (Seven Bridges of Königsberg)
- Count of Root to Leaf Paths consisting of at most M consecutive Nodes having value K
- Count all possible paths between two vertices
- Print levels with odd number of nodes and even number of nodes
- Sum of all odd nodes in the path connecting two given nodes
- Maximum product of two non-intersecting paths in a tree
- Print all shortest paths between given source and destination in an undirected graph
- Minimum number of Nodes to be removed such that no subtree has more than K nodes
- Calculate number of nodes between two vertices in an acyclic Graph by Disjoint Union method
- Calculate number of nodes between two vertices in an acyclic Graph by DFS method
- Number of shortest paths in an unweighted and directed graph
- Find the number of paths of length K in a directed graph
- Count number of times each Edge appears in all possible paths of a given Tree
- Number of unique paths in tree such that every path has a value greater than K
- Count nodes within K-distance from all nodes in a set
- Maximize count of nodes disconnected from all other nodes in a Graph
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Find the shortest distance between any pair of two different good nodes
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.
Improved By : rituraj_jain