Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Last Updated : 15 Jun, 2021

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.

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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 <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 adjancy 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(V3)
Auxiliary Space: O(V2)




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!