# Find the Dominators for every vertex in a given DAG (Directed Acyclic Graph)

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:
0
/     \
1        2
\      /
3
|
4
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:

• Initialize a vector of bitset data structure, say b to store the set of all the dominators of the vertices.
• For every node i, set bits in b[i] will represent the set of dominant vertices of i.
• In order to find the dominators for every vertex, it is important to find all the paths in a Directed Acyclic Graph.
• Traverse the graph using DFS to find all the paths in the graph.
• Start the traversal from the root node i.e 0
• While performing DFS, for each vertex i
• If the node is not yet visited, set all the bits of b[i] and mark the node visited
• Store the set of the dominant vertices in the bitset b[i] as the intersection of the set of dominant vertices of its parent. Update b[i] to b[i] & b[parent].
• Update the value of b[i][i] to 1 because each node is a dominator of itself.
• Recursively, call DFS for children nodes of i.
• After performing the above steps, print the dominant vertices for vertex, i.e, the position of the set bits in b[i].

Below is the implementation of the above approach:

## C++14

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Declare bitsets for each``// vertex to store the set of``// dominant vertices``vector > b(100);` `// Visited array to check if``// a vertex has been visited or not``int` `vis[100] = {};` `// Function to find set of dominator``// vertices for a particular node``void` `findDominator(vector > graph,``                   ``bitset<100> par, ``int` `node)``{``    ``// If node is unvisited``    ``if` `(vis[node] == 0) {``        ``// Set all bits of b[pos]``        ``b[node] = ~b[node];` `        ``// Update vis[node] to 1``        ``vis[node] = 1;``    ``}` `    ``// Update b[node] with bitwise and``    ``// of parent's dominant vertices``    ``b[node] &= par;` `    ``// Node is itself is a``    ``// dominant vertex of node``    ``b[node][node] = 1;` `    ``// Traverse the neighbours of node``    ``for` `(``int` `i = 0; i < (``int``)graph[node].size(); i++) {``      ` `        ``// Recursive function call to``        ``//  children nodes of node``        ``findDominator(graph, b[node], graph[node][i]);``    ``}``}` `// Function to build the graph``void` `buildGraph(vector > adj, ``int` `E, ``int` `V)``{``    ``// Vector of vector to store``    ``// the adjacency matrix``    ``vector > graph(V + 1);` `    ``// Build the adjacency matrix``    ``for` `(``int` `i = 0; i < E; i++) {``        ``graph[adj[i].first].push_back(adj[i].second);``    ``}` `    ``// Bitset for node 0``    ``bitset<100> g;` `    ``// Node 0 itself is a dominant``    ``// vertex of itself``    ``g[0] = 1;` `    ``// Update visited of source``    ``// node as true``    ``vis[0] = 1;` `    ``// DFS from source vertex``    ``findDominator(graph, g, 0);``}` `// Function to find dominant set of vertices``void` `dominantVertices(``int` `V, ``int` `E,``                      ``vector > adj)``{``    ``// Function call to build the graph``    ``// and dominant vertices``    ``buildGraph(adj, E, V);` `    ``// Print set of dominating vertices``    ``for` `(``int` `i = 0; i < V; i++) {``        ``cout << i << ``" -> "``;``        ``for` `(``int` `j = 0; j < V; j++) {``            ``if` `(b[i][j] == 1)``                ``cout << j << ``" "``;``        ``}``        ``cout << endl;``    ``}``}` `// Driver Code``int` `main()``{``    ``// Given Input``    ``int` `V = 5, E = 5;``    ``vector > adj = {``        ``{ 0, 1 }, { 0, 2 }, { 1, 3 }, { 2, 3 }, { 3, 4 }``    ``};` `    ``// Function Call``    ``dominantVertices(V, E, adj);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach ``import` `java.util.*; ` `class` `GFG { ` `    ``// Declare bitsets for each``    ``// vertex to store the set of``    ``// dominant vertices``    ``static` `Vector b[] = ``new` `Vector[``100``]; ` `    ``// Visited array to check if``    ``// a vertex has been visited or not``    ``static` `int` `vis[] = ``new` `int``[``100``]; ` `    ``// Function to find set of dominator``    ``// vertices for a particular node``    ``static` `void` `findDominator(ArrayList > graph, ``                            ``Vector par, ``int` `node) ``    ``{ ``        ``// If node is unvisited ``        ``if` `(vis[node] == ``0``) { ``            ``// Set all bits of b[pos] ``            ``for``(``int` `i=``0``; i > graph, ``int` `V) ``    ``{ ``        ``// Vector of vector to store ``        ``// the adjacency matrix ` `        ``// Bitset for node 0 ``        ``Vector g = ``new` `Vector<>(); ``        ``for``(``int` `i=``0``; i > graph) ``    ``{ ``        ``// Function call to build the graph ``        ``// and dominant vertices ``        ``buildGraph(graph, V); ` `        ``// Print set of dominating vertices ``        ``for` `(``int` `i = ``0``; i < V; i++) { ``            ``System.out.print(i + ``" -> "``); ``            ``for` `(``int` `j = ``0``; j < V; j++) { ``                ``if` `(b[i].get(j) == ``1``) ``                    ``System.out.print(j + ``" "``); ``            ``} ``            ``System.out.println(); ``        ``} ``    ``} ` `    ``// Driver Code ``    ``public` `static` `void` `main(String[] args) ``    ``{ ``        ``// Given Input ``        ``int` `V = ``5``; ``        ``ArrayList > graph = ``new` `ArrayList<>(); ``        ``for``(``int` `i=``0``; i());``            ``b[i] = ``new` `Vector<>();``            ``for``(``int` `j=``0``; j

## Python3

 `# import the bitarray library``from` `bitarray ``import` `bitarray``# Declare bitarrays for each``# vertex to store the set of``# dominant vertices``b ``=` `[bitarray(``100``) ``for` `i ``in` `range``(``100``)]` `# Visited list to check if``# a vertex has been visited or not``vis ``=` `[``0``] ``*` `100` `def` `findDominator(graph, par, node):``    ``# If node is unvisited``    ``if` `vis[node] ``=``=` `0``:``        ``# Set all bits of b[pos]``        ``b[node].setall(``True``)` `        ``# Update vis[node] to 1``        ``vis[node] ``=` `1` `    ``# Update b[node] with bitwise and``    ``# of parent's dominant vertices``    ``b[node] &``=` `par` `    ``# Node is itself is a``    ``# dominant vertex of node``    ``b[node][node] ``=` `True` `    ``# Traverse the neighbours of node``    ``for` `i ``in` `range``(``len``(graph[node])):``        ``# Recursive function call to``        ``#  children nodes of node``        ``findDominator(graph, b[node], graph[node][i])` `def` `buildGraph(adj, E, V):``    ``# List of lists to store``    ``# the adjacency matrix``    ``graph ``=` `[[] ``for` `i ``in` `range``(V ``+` `1``)]` `    ``# Build the adjacency matrix``    ``for` `i ``in` `range``(E):``        ``graph[adj[i][``0``]].append(adj[i][``1``])` `    ``# Bitarray for node 0``    ``g ``=` `bitarray(``100``)``    ``g.setall(``False``)` `    ``# Node 0 itself is a dominant``    ``# vertex of itself``    ``g[``0``] ``=` `True` `    ``# Update visited of source``    ``# node as true``    ``vis[``0``] ``=` `1` `    ``# DFS from source vertex``    ``findDominator(graph, g, ``0``)` `def` `dominantVertices(V, E, adj):``    ``# Function call to build the graph``    ``# and dominant vertices``    ``buildGraph(adj, E, V)` `    ``# Print set of dominating vertices``    ``for` `i ``in` `range``(V):``        ``print``(i, ``" -> "``, end``=``"")``        ``for` `j ``in` `range``(V):``            ``if` `b[i][j]:``                ``print``(j, end``=``" "``)``        ``print``()` `# Driver Code` `if` `__name__ ``=``=` `'__main__'``:``    ``# Given Input``    ``V ``=` `5``    ``E ``=` `5``    ``adj ``=` `[[``0``, ``1``], [``0``, ``2``], [``1``, ``3``], [``2``, ``3``], [``3``, ``4``]]``    ``# Function Call``    ``dominantVertices(V, E, adj)``# this code is contributed by devendrasaluke `

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;` `class` `GFG {``static` `List<``int``>[] b = ``new` `List<``int``>[100];` `// Visited array to check if``// a vertex has been visited or not``static` `int``[] vis = ``new` `int``[100];` `// Function to find set of dominator``// vertices for a particular node``static` `void` `findDominator(List> graph,``                        ``List<``int``> par, ``int` `node)``{``    ``// If node is unvisited``    ``if` `(vis[node] == 0) {``        ``// Set all bits of b[pos]``        ``for``(``int` `i=0; i> graph, ``int` `V)``{``    ``// Vector of vector to store``    ``// the adjacency matrix` `    ``// Bitset for node 0``    ``List<``int``> g = ``new` `List<``int``>();``    ``for``(``int` `i=0; i> graph)``{``    ``// Function call to build the graph``    ``// and dominant vertices``    ``buildGraph(graph, V);` `    ``// Print set of dominating vertices``    ``for` `(``int` `i = 0; i < V; i++) {``        ``Console.Write(i + ``" -> "``);``        ``for` `(``int` `j = 0; j < V; j++) {``            ``if` `(b[i][j] == 1)``                ``Console.Write(j + ``" "``);``        ``}``        ``Console.WriteLine();``    ``}``}` `// Driver Code``static` `void` `Main(``string``[] args)``{``    ``// Given Input``    ``int` `V = 5;``    ``List> graph = ``new` `List>();``    ``for``(``int` `i=0; i());``        ``b[i] = ``new` `List<``int``>();``        ``for``(``int` `j=0; j

## Javascript

 ``

Output
```0 -> 0
1 -> 0 1
2 -> 0 2
3 -> 0 3
4 -> 0 3 4 ```

Time Complexity: O(V3)
Auxiliary Space: O(V2)

Previous
Next