Dominant Set of a Graph - GeeksforGeeks

04 Feb, 2020
In graph theory, a dominating set for a graph G = (V, E) is a subset D of V such that every vertex not in D is adjacent to at least one member of D. The domination number is the number of vertices in a smallest dominating set for G.Examples:
Input :   A graph with 4 vertex and 4 edges   
Output :  The Dominant Set S= { a, b } or { a, d } or { a, c } and more.

Input : A graph with 6 vertex and 7 edges   
Output :  The Dominant Set S= { a, d, f } or { e, c } and more.

It is believed that there may be no efficient algorithm that finds a smallest dominating set for all graphs, but there are efficient approximation algorithms.Algorithm :
First we have to initialize a set ‘S’ as empty
Take any edge ‘e’ of the graph connecting the vertices ( say A and B )
Add one vertex between A and B ( let say A ) to our set S
Delete all the edges in the graph connected to A
Go back to step 2 and repeat, if some edge is still left in the graph
The final set S is a Dominant Set of the graph
C++
Java
C#
// C++ program to find the Dominant Set of a graph#include <bits/stdc++.h>using namespace std;  vector<vector<int> > g;bool box[100000];  vector<int> Dominant(int ver, int edge){    vector<int> S; // set S    for (int i = 0; i < ver; i++) {        if (!box[i]) {            S.push_back(i);            box[i] = true;            for (int j = 0; j < (int)g[i].size(); j++) {                if (!box[g[i][j]]) {                    box[g[i][j]] = true;                    break;                }            }        }    }    return S;}  // Driver functionint main(){    int ver, edge, x, y;      ver = 5; // Enter number of vertices    edge = 6; // Enter number of Edges    g.resize(ver);      // Setting all index value of an array as 0    memset(box, 0, sizeof(box));       // Enter all the end-points of all the Edges    // g[x--].push_back[y--]      g[y--].push_back[x--]    g[0].push_back(1);    g[1].push_back(0); // x = 1, y = 2 ;    g[1].push_back(2);    g[2].push_back(1); // x = 2, y = 3 ;    g[2].push_back(3);    g[3].push_back(2); // x = 3, y = 4 ;    g[0].push_back(3);    g[3].push_back(0); // x = 1, y = 4 ;    g[3].push_back(4);    g[4].push_back(3); // x = 4, y = 5 ;    g[2].push_back(4);    g[4].push_back(2); // x = 3, y = 5 ;      vector<int> S = Dominant(ver, edge);    cout << "The Dominant Set is : { ";    for (int i = 0; i < (int)S.size(); i++)        cout << S[i] + 1 << " ";    cout << "}";    return 0;}
// Java program to find the Dominant Set of a graphimport java.util.*;  class GFG{  static Vector<Integer> []g;static boolean []box = new boolean[100000];  static Vector<Integer> Dominant(int ver, int edge){    Vector<Integer> S = new Vector<Integer>(); // set S    for (int i = 0; i < ver; i++)     {        if (!box[i])         {            S.add(i);            box[i] = true;            for (int j = 0; j < (int)g[i].size(); j++)             {                if (!box[g[i].get(j)])                {                    box[g[i].get(j)] = true;                    break;                }            }        }    }    return S;}  // Driver codepublic static void main(String[] args){    int ver, edge, x, y;      ver = 5; // Enter number of vertices    edge = 6; // Enter number of Edges    g = new Vector[ver];    for (int i = 0; i < ver; i++)        g[i] = new Vector<Integer>();        // Enter all the end-points of all the Edges    // g[x--].push_back[y--]     g[y--].push_back[x--]    g[0].add(1);    g[1].add(0); // x = 1, y = 2 ;    g[1].add(2);    g[2].add(1); // x = 2, y = 3 ;    g[2].add(3);    g[3].add(2); // x = 3, y = 4 ;    g[0].add(3);    g[3].add(0); // x = 1, y = 4 ;    g[3].add(4);    g[4].add(3); // x = 4, y = 5 ;    g[2].add(4);    g[4].add(2); // x = 3, y = 5 ;      Vector<Integer> S = Dominant(ver, edge);    System.out.print("The Dominant Set is : { ");    for (int i = 0; i < (int)S.size(); i++)        System.out.print(S.get(i) + 1 + " ");    System.out.print("}");}}  // This code is contributed by Rajput-Ji
// C# program to find the Dominant Set of a graphusing System;using System.Collections.Generic;  class GFG{  static List<int> []g;static bool []box = new bool[100000];  static List<int> Dominant(int ver, int edge){    List<int> S = new List<int>(); // set S    for (int i = 0; i < ver; i++)     {        if (!box[i])         {            S.Add(i);            box[i] = true;            for (int j = 0; j < (int)g[i].Count; j++)             {                if (!box[g[i][j]])                {                    box[g[i][j]] = true;                    break;                }            }        }    }    return S;}  // Driver codepublic static void Main(String[] args){    int ver, edge;      ver = 5; // Enter number of vertices    edge = 6; // Enter number of Edges    g = new List<int>[ver];    for (int i = 0; i < ver; i++)        g[i] = new List<int>();      // Enter all the end-points of all the Edges    // g[x--].push_back[y--]     g[y--].push_back[x--]    g[0].Add(1);    g[1].Add(0); // x = 1, y = 2 ;    g[1].Add(2);    g[2].Add(1); // x = 2, y = 3 ;    g[2].Add(3);    g[3].Add(2); // x = 3, y = 4 ;    g[0].Add(3);    g[3].Add(0); // x = 1, y = 4 ;    g[3].Add(4);    g[4].Add(3); // x = 4, y = 5 ;    g[2].Add(4);    g[4].Add(2); // x = 3, y = 5 ;      List<int> S = Dominant(ver, edge);    Console.Write("The Dominant Set is : { ");    for (int i = 0; i < (int)S.Count; i++)        Console.Write(S[i] + 1 + " ");    Console.Write("}");}}  // This code is contributed by PrinciRaj1992
The Dominant Set is : { 1 3 5 }

Reference : wiki
Rajput-Ji
princiraj1992
NP Complete
Graph
Graph
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Topological Sorting
Bellman–Ford Algorithm | DP-23
Detect Cycle in a Directed Graph
Floyd Warshall Algorithm | DP-16
Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph)
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Check whether a given graph is Bipartite or not
Ford-Fulkerson Algorithm for Maximum Flow Problem
Traveling Salesman Problem (TSP) Implementation
Detect cycle in an undirected graph