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
Algorithm: 1. Do dfs traversal on the neighbour nodes of the source node. 2. The color between source node and neighbour nodes is known, if the DFS traversal also has the same color, proceed, else stop going on that path. 3. After reaching the destination node, increment count by 1.
NOTE: Number of Colors will always be less than a number of edges.
Implementation:
C++
// C++ code to find unicolored paths #include <bits/stdc++.h> using namespace std; const int MAX_V = 100; int color[MAX_V]; bool vis[MAX_V]; // Graph class represents an undirected graph // using adjacency list representation class Graph { // vertices, edges, adjacency list int V; int E; vector<pair< int , int > > adj[MAX_V]; // function used by UniColorPaths // DFS traversal o from x to y void dfs( int x, int y, int z); // Constructor public : Graph( int V, int E); // function to add an edge to graph void addEdge( int v, int w, int z); // finds paths between a and b having // same color edges int UniColorPaths( int a, int b); }; Graph::Graph( int V, int E) { this -> V = V; this -> E = E; } void Graph::addEdge( int a, int b, int c) { adj[a].push_back({b, c}); // Add b to a’s list. adj[b].push_back({a, c}); // Add c to b’s list. } void Graph::dfs( int x, int y, int col) { if (vis[x]) return ; vis[x] = 1; // mark this as a possible color to reach s to d if (x == y) { color[col] = 1; return ; } // if the next edge is also of same color for ( int i = 0; i < int (adj[x].size()); i++) if (adj[x][i].second == col) dfs(adj[x][i].first, y, col); } // function that finds paths between a and b // such that all edges are same colored // It uses recursive dfs() int Graph::UniColorPaths( int a, int b) { // dfs on nodes directly connected to source for ( int i = 0; i < int (adj[a].size()); i++) { dfs(a, b, adj[a][i].second); // to visit again visited nodes memset (vis, 0, sizeof (vis)); } int cur = 0; for ( int i = 0; i <= E; i++) cur += color[i]; return (cur); } // driver code int main() { // Create a graph given in the above diagram Graph g(5, 7); g.addEdge(1, 2, 1); g.addEdge(1, 3, 2); g.addEdge(2, 3, 3); g.addEdge(2, 4, 2); g.addEdge(2, 5, 4); g.addEdge(3, 5, 3); g.addEdge(4, 5, 2); int s = 2; // source int d = 5; // destination cout << "Number of unicolored paths : " ; cout << g.UniColorPaths(s, d) << endl; return 0; } |
Java
import java.util.ArrayList; import java.util.Arrays; class Graph { int MAX_V = 100 ; int [] color = new int [MAX_V]; int [] vis = new int [MAX_V]; int V, E; ArrayList<ArrayList<Pair>> adj = new ArrayList<ArrayList<Pair>>(); public Graph( int V, int E) { this .V = V; this .E = E; for ( int i = 0 ; i <= MAX_V; i++) { adj.add( new ArrayList<Pair>()); } } // Function used by UniColorPaths // DFS traversal o from x to y void dfs( int x, int y, int col) { if (vis[x] == 1 ) { return ; } vis[x] = 1 ; // mark this as a possible color to reach s to d if (x == y) { color[col] = 1 ; return ; } // if the next edge is also of same color for ( int i = 0 ; i < adj.get(x).size(); i++) { if (adj.get(x).get(i).second == col) { dfs(adj.get(x).get(i).first, y, col); } } } void addEdge( int a, int b, int c) { adj.get(a).add( new Pair(b, c)); // Add b to a’s list. adj.get(b).add( new Pair(a, c)); // Add c to b’s list. } // Function that finds paths between a // and b such that all edges are same // colored. It uses recursive dfs() int UniColorPaths( int a, int b) { Arrays.fill(vis, 0 ); Arrays.fill(color, 0 ); // dfs on nodes directly connected to source for ( int i = 0 ; i < adj.get(a).size(); i++) { dfs(a, b, adj.get(a).get(i).second); // to visit again visited nodes Arrays.fill(vis, 0 ); } int cur = 0 ; for ( int i = 0 ; i <= E; i++) { cur += color[i]; } return cur; } class Pair { int first, second; Pair( int first, int second) { this .first = first; this .second = second; } } } public class Main { public static void main(String[] args) { // Create a graph given in the above diagram Graph g = new Graph( 5 , 7 ); g.addEdge( 1 , 2 , 1 ); g.addEdge( 1 , 3 , 2 ); g.addEdge( 2 , 3 , 3 ); g.addEdge( 2 , 4 , 2 ); g.addEdge( 2 , 5 , 4 ); g.addEdge( 3 , 5 , 3 ); g.addEdge( 4 , 5 , 2 ); int s = 2 ; // source int d = 5 ; // destination System.out.print( "Number of unicolored paths : " ); System.out.println(g.UniColorPaths(s, d)); } } |
Python3
# Python3 code to find unicolored paths MAX_V = 100 color = [ 0 ] * MAX_V vis = [ 0 ] * MAX_V # Graph class represents a undirected graph # using adjacency list representation class Graph: def __init__( self , V, E): self .V = V self .E = E self .adj = [[] for i in range (MAX_V)] # Function used by UniColorPaths # DFS traversal o from x to y def dfs( self , x, y, col): if vis[x]: return vis[x] = 1 # mark this as a possible color to reach s to d if x = = y: color[col] = 1 return # if the next edge is also of same color for i in range ( 0 , len ( self .adj[x])): if self .adj[x][i][ 1 ] = = col: self .dfs( self .adj[x][i][ 0 ], y, col) def addEdge( self , a, b, c): self .adj[a].append((b, c)) # Add b to a’s list. self .adj[b].append((a, c)) # Add c to b’s list. # Function that finds paths between a # and b such that all edges are same # colored. It uses recursive dfs() def UniColorPaths( self , a, b): global vis # dfs on nodes directly connected to source for i in range ( 0 , len ( self .adj[a])): self .dfs(a, b, self .adj[a][i][ 1 ]) # to visit again visited nodes vis = [ 0 ] * len (vis) cur = 0 for i in range ( 0 , self .E + 1 ): cur + = color[i] return cur # Driver code if __name__ = = "__main__" : # Create a graph given in the above diagram g = Graph( 5 , 7 ) g.addEdge( 1 , 2 , 1 ) g.addEdge( 1 , 3 , 2 ) g.addEdge( 2 , 3 , 3 ) g.addEdge( 2 , 4 , 2 ) g.addEdge( 2 , 5 , 4 ) g.addEdge( 3 , 5 , 3 ) g.addEdge( 4 , 5 , 2 ) s = 2 # source d = 5 # destination print ( "Number of unicolored paths : " , end = "") print (g.UniColorPaths(s, d)) # This code is contributed by Rituraj Jain |
C#
// C# code to find unicolored paths using System; using System.Collections.Generic; using System.Collections; using System.Linq; static class Globals { // global int public static int MAX_V = 100; public static int [] color = new int [MAX_V]; public static int [] vis = new int [MAX_V]; } // Graph class represents an undirected graph // using adjacency list representation class Graph { // vertices, edges, adjacency list public int V; public int E; public List<KeyValuePair< int , int >>[] adj; public Graph( int v, int e) { V = v; E = e; adj = new List<KeyValuePair< int , int >>[Globals.MAX_V]; for ( int i = 0; i < adj.Length; ++i) { adj[i] = new List<KeyValuePair< int , int >>(); } } // Appends a new Edge to the linked list public void addEdge( int a, int b, int c) { adj[a].Add( new KeyValuePair< int , int >(b, c)); adj[b].Add( new KeyValuePair< int , int > (a, c)); } public void dfs( int x, int y, int col) { if (Globals.vis[x] == 1) return ; Globals.vis[x] = 1; // mark this as a possible color to reach s to d if (x == y) { Globals.color[col] = 1; return ; } // if the next edge is also of same color for ( int i = 0; i < adj[x].Count; i++) if (adj[x][i].Value == col) dfs(adj[x][i].Key, y, col); } // function that finds paths between a and b // such that all edges are same colored // It uses recursive dfs() public int UniColorPaths( int a, int b) { // dfs on nodes directly connected to source for ( int i = 0; i < adj[a].Count; i++) { dfs(a, b, adj[a][i].Value); // to visit again visited nodes for ( int indx = 0; indx < Globals.MAX_V; indx++){ Globals.vis[indx] = 0; } } int cur = 0; for ( int i = 0; i <= E; i++) cur += Globals.color[i]; return (cur); } } class HelloWorld { static void Main() { // Create a graph given in the above diagram Graph g = new Graph(5, 7); g.addEdge(1, 2, 1); g.addEdge(1, 3, 2); g.addEdge(2, 3, 3); g.addEdge(2, 4, 2); g.addEdge(2, 5, 4); g.addEdge(3, 5, 3); g.addEdge(4, 5, 2); int s = 2; // source int d = 5; // destination Console.Write( "Number of unicolored paths : " ); Console.WriteLine(g.UniColorPaths(s, d)); } } // The code is contributed by Arushi Jindal. |
Javascript
// Javascript code to find unicolored paths const MAX_V = 100; let color = new Array(MAX_V).fill(0); let vis = new Array(MAX_V).fill(0); // Graph class represents an undirected graph // using adjacency list representation class Graph { // Constructor constructor(V, E) { // vertices, edges, adjacency list this .V = V; this .E = E; this .adj = Array.from({ length: MAX_V }, () => []); } // function used by UniColorPaths // DFS traversal o from x to y dfs(x, y, col) { if (vis[x]) { return ; } vis[x] = 1; if (x === y) { color[col] = 1; return ; } for (let i = 0; i < this .adj[x].length; i++) { if ( this .adj[x][i][1] === col) { this .dfs( this .adj[x][i][0], y, col); } } } addEdge(a, b, c) { this .adj[a].push([b, c]); // Add b to a’s list. this .adj[b].push([a, c]); // Add c to b’s list. } // function that finds paths between a and b // such that all edges are same colored // It uses recursive dfs() UniColorPaths(a, b) { for (let i = 0; i < this .adj[a].length; i++) { this .dfs(a, b, this .adj[a][i][1]); vis = new Array(MAX_V).fill(0); } let cur = 0; for (let i = 0; i <= this .E; i++) { cur += color[i]; } return cur; } } // driver code // Create a graph given in the above diagram let g = new Graph(5, 7); g.addEdge(1, 2, 1); g.addEdge(1, 3, 2); g.addEdge(2, 3, 3); g.addEdge(2, 4, 2); g.addEdge(2, 5, 4); g.addEdge(3, 5, 3); g.addEdge(4, 5, 2); let s = 2; let d = 5; console.log( "Number of unicolored paths: " , g.UniColorPaths(s, d)); |
Number of unicolored paths : 3
Time Complexity : O(E * (E + V))
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!