Find the Dominators for every vertex in a given DAG (Directed Acyclic Graph)
Last Updated : 23 Jul, 2025
Given a Directed Acyclic Graph with V vertices and E edges, the task is to find the set of dominant vertices for each vertex of the graph.
What are Dominators in Graph Theory: In control flow graphs a vertex V1 is the dominator of another vertex V2 if all the paths from the source vertex (in this case the vertex ‘0’) to the vertex V2 passes through V1. By definition, every vertex is one of its own dominators.
Examples:
Input: V = 5, E = 5, adj[][] = {{0, 1}, {0, 2}, {1, 3}, {2, 3}, {3, 4}} Output: Dominating set of vertex: 0 –> 0 Dominating set of vertex: 1 –> 0 1 Dominating set of vertex: 2 –> 0 2 Dominating set of vertex: 3 –> 0 3 Dominating set of vertex: 4 –> 0 3 4 Explanation:
Here 0 is the entry node, so its dominator is 0 itself. Only one path exists between (0, 1) so the dominators of 1 are 0, 1. Only one path exists between (0, 2) so the dominators of 2 are 0, 2. There are 2 paths between(0, 3) having only 0, 3 in common. From (0, 4) there are 2 paths (0 1 3 4) and (0 2 3 4) with 0, 3 and 4 common.
Input: V = 4, E = 3, adj[][] = {{0, 1}, {0, 2}, {3, 2}} Output: Dominating set of vertex: 0 –> 0 Dominating set of vertex: 1 –> 0 1 Dominating set of vertex: 2 –> 0 2 Dominating set of vertex: 3 –> 0 2 3
Approach:
The idea is to perform DFS and maintain a set of all the dominators of each vertex. Follow the steps below to solve the problem:
Graph Traversal and Postorder: Perform a DFS from the source node (usually node 0) to obtain a postorder of the vertices. This will help process the vertices in reverse order.
Initialize Dominators: Initialize the dominators for each vertex. The entry vertex (source vertex) should dominate only itself. For all other vertices, their initial dominators should include all vertices in the graph.
Iterative Process: Repeat the following process until there is no change in the dominators for any vertex:
For each vertex v in postorder (starting from the entry point), calculate its dominators as the intersection of its predecessors' dominators, adding itself to the intersection result.
Update the dominator set for each vertex.
Calculate Intersection: Calculate the intersection of dominators of each vertex's predecessors and add the current vertex to its dominator set.
Repeat Until No Changes: Keep iterating until no changes are made to any vertex's dominator set.
Output: Once the process completes, output the set of dominators for each vertex.
Below is the implementation of the above approach:
C++14
#include<iostream>#include<vector>#include<queue>#include<unordered_map>#include<set>#include<algorithm>usingnamespacestd;voidfindDominators(intV,constvector<pair<int,int>>&edges){// Initialize graphunordered_map<int,vector<int>>graph;for(constauto&edge:edges){graph[edge.first].push_back(edge.second);}// Initialize the list of dominatorsvector<set<int>>dominators(V);// Initialize a list of incoming edges count for each vertexvector<int>inDegrees(V,0);for(constauto&edge:edges){inDegrees[edge.second]++;}// Initialize the dominator set for the entry node (source node 0)dominators[0].insert(0);// Initialize a queue for BFS traversalqueue<int>q;q.push(0);// Perform BFSwhile(!q.empty()){intu=q.front();q.pop();// Traverse neighbors of the current vertexfor(intv:graph[u]){// Calculate the intersection of dominators from all predecessors of vif(dominators[v].empty()){dominators[v]=dominators[u];dominators[v].insert(v);}else{set<int>intersection;set_intersection(dominators[v].
Java
importjava.util.*;publicclassDominators{publicstaticvoidfindDominators(intV,int[][]edges){// Initialize graphMap<Integer,List<Integer>>graph=newHashMap<>();for(int[]edge:edges){graph.computeIfAbsent(edge[0],k->newArrayList<>()).add(edge[1]);}// Initialize the list of dominatorsList<Set<Integer>>dominators=newArrayList<>(V);for(inti=0;i<V;i++){dominators.add(newHashSet<>());}// Initialize a list of incoming edges count for each vertexint[]inDegrees=newint[V];for(int[]edge:edges){inDegrees[edge[1]]++;}// Initialize the dominator set for the entry node (source node 0)dominators.get(0).add(0);// Initialize a queue for BFS traversalQueue<Integer>queue=newLinkedList<>();queue.add(0);// Perform BFSwhile(!queue.isEmpty()){intu=queue.poll();// Traverse neighbors of the current vertexfor(intv:graph.getOrDefault(u,newArrayList<>())){// Calculate the intersection of dominators from all predecessors of vif(dominators.get(v).isEmpty()){dominators.get(v).addAll(dominators.get(u));dominators.get(v).add(v);}else{dominators.get(v).retainAll(dominators.get(u));}// Reduce the in-degree count for vertex vinDegrees[v]--;// If all predecessors of v have been processed, add v to the queueif(inDegrees[v]==0){queue.add(v);}}}// Print the dominator sets for each vertexfor(inti=0;i<V;i++){System.out.print("Dominating set of vertex "+i+": ");List<Integer>sortedDominators=newArrayList<>(dominators.get(i));Collections.sort(sortedDominators);for(intdominator:sortedDominators){System.out.print(dominator+" ");}System.out.println();}}publicstaticvoidmain(String[]args){intV=5;int[][]edges={{0,1},{0,2},{1,3},{2,3},{3,4}};findDominators(V,edges);}}
Python
fromcollectionsimportdefaultdict,dequedeffind_dominators(V,edges):# Initialize graphgraph=defaultdict(list)foru,vinedges:graph[u].append(v)# Initialize the list of dominatorsdominators=[set()for_inrange(V)]# Initialize a list of incoming edges count for each vertexin_degrees=[0]*Vforu,vinedges:in_degrees[v]+=1# Initialize the dominator set for the entry node (source node 0)dominators[0]={0}# Initialize a queue for BFS traversalqueue=deque([0])# Perform BFSwhilequeue:u=queue.popleft()# Traverse neighbors of current vertexforvingraph[u]:# Calculate the intersection of dominators from all predecessors of vifnotdominators[v]:dominators[v]=dominators[u].copy()dominators[v].add(v)else:dominators[v].intersection_update(dominators[u])# Reduce the in-degree count for vertex vin_degrees[v]-=1# If all predecessors of v have been processed, add v to the queueifin_degrees[v]==0:queue.append(v)# Print the dominator sets for each vertexforiinrange(V):print(f"Dominating set of vertex {i}: {sorted(dominators[i])}")# Example usage:V=5edges=[(0,1),(0,2),(1,3),(2,3),(3,4)]find_dominators(V,edges)
C#
usingSystem;usingSystem.Collections.Generic;publicclassDominators{publicstaticvoidFindDominators(intV,int[][]edges){// Initialize graphDictionary<int,List<int>>graph=newDictionary<int,List<int>>();foreach(varedgeinedges){if(!graph.ContainsKey(edge[0]))graph[edge[0]]=newList<int>();graph[edge[0]].Add(edge[1]);}// Initialize the list of dominatorsList<HashSet<int>>dominators=newList<HashSet<int>>(V);for(inti=0;i<V;i++){dominators.Add(newHashSet<int>());}// Initialize a list of incoming edges count for each vertexint[]inDegrees=newint[V];foreach(varedgeinedges){inDegrees[edge[1]]++;}// Initialize the dominator set for the entry node (source node 0)dominators[0].Add(0);// Initialize a queue for BFS traversalQueue<int>queue=newQueue<int>();queue.Enqueue(0);// Perform BFSwhile(queue.Count>0){intu=queue.Dequeue();// Traverse neighbors of the current vertexif(graph.TryGetValue(u,outvarneighbors)){foreach(varvinneighbors){// Calculate the intersection of dominators from all predecessors of vif(dominators[v].Count==0){dominators[v].UnionWith(dominators[u]);dominators[v].Add(v);}else{dominators[v].IntersectWith(dominators[u]);}// Reduce the in-degree count for vertex vinDegrees[v]--;// If all predecessors of v have been processed, add v to the queueif(inDegrees[v]==0){queue.Enqueue(v);}}}}// Print the dominator sets for each vertexfor(inti=0;i<V;i++){Console.Write($"Dominating set of vertex {i}: ");varsortedDominators=newList<int>(dominators[i]);sortedDominators.Sort();foreach(vardominatorinsortedDominators){Console.Write(dominator+" ");}Console.WriteLine();}}publicstaticvoidMain(){intV=5;int[][]edges=newint[][]{newint[]{0,1},newint[]{0,2},newint[]{1,3},newint[]{2,3},newint[]{3,4}};FindDominators(V,edges);}}
JavaScript
functionfindDominators(V,edges){// Initialize graphconstgraph={};for(const[u,v]ofedges){if(!graph[u])graph[u]=[];graph[u].push(v);}// Initialize the list of dominatorsconstdominators=Array.from({length:V},()=>newSet());// Initialize a list of incoming edges count for each vertexconstinDegrees=newArray(V).fill(0);for(const[u,v]ofedges){inDegrees[v]++;}// Initialize the dominator set for the entry node (source node 0)dominators[0].add(0);// Initialize a queue for BFS traversalconstqueue=[0];// Perform BFSwhile(queue.length>0){constu=queue.shift();// Traverse neighbors of the current vertexif(graph[u]){for(constvofgraph[u]){// Calculate the intersection of dominators from all predecessors of vif(dominators[v].size===0){dominators[v]=newSet([...dominators[u]]);dominators[v].add(v);}else{dominators[v]=newSet([...dominators[v]].filter(val=>dominators[u].has(val)));}// Reduce the in-degree count for vertex vinDegrees[v]--;// If all predecessors of v have been processed, add v to the queueif(inDegrees[v]===0){queue.push(v);}}}}// Print the dominator sets for each vertexfor(leti=0;i<V;i++){console.log(`Dominating set of vertex ${i}: ${[...dominators[i]].sort().join(' ')}`);}}// Example usage:constV=5;constedges=[[0,1],[0,2],[1,3],[2,3],[3,4]];findDominators(V,edges);