# 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 vertexV1is the dominator of another vertexV2if all the paths from the source vertex (in this case the vertex ‘0’) to the vertexV2passes throughV1. 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 –> 0Dominating set of vertex: 1 –> 0 1Dominating set of vertex: 2 –> 0 2Dominating set of vertex: 3 –> 0 3Dominating set of vertex: 4 –> 0 3 4Explanation:0/ \1 2\ /3|4Here 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 –> 0Dominating set of vertex: 1 –> 0 1Dominating set of vertex: 2 –> 0 2Dominating 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
**b[i][i]**to**1**because each node is a dominator of itself. - Recursively, call
**DFS**for children nodes of**i**.

- If the node is not yet visited, set all the bits of
- 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 <bits/stdc++.h>` `using` `namespace` `std;` `// Declare bitsets for each` `// vertex to store the set of` `// dominant vertices` `vector<bitset<100> > 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<vector<` `int` `> > 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<pair<` `int` `, ` `int` `> > adj, ` `int` `E, ` `int` `V)` `{` ` ` `// Vector of vector to store` ` ` `// the adjacency matrix` ` ` `vector<vector<` `int` `> > 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<pair<` `int` `, ` `int` `> > 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<pair<` `int` `, ` `int` `> > adj = {` ` ` `{ 0, 1 }, { 0, 2 }, { 1, 3 }, { 2, 3 }, { 3, 4 }` ` ` `};` ` ` `// Function Call` ` ` `dominantVertices(V, E, adj);` ` ` `return` `0;` `}` |

**Output**

0 -> 0 1 -> 0 1 2 -> 0 2 3 -> 0 3 4 -> 0 3 4

**Time Complexity:** O(V^{3})**Auxiliary Space:** O(V^{2})