
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Solve the Dominating Set Problem
This is a C++ program to solve the Dominating Set problem.
Algorithm
Begin Take the number of vertices and edges as input. Also take the edge point of the edges. function dominant(): Declare vector Set. Take any edge e graph connecting the vertices i.e.; X and Y. Add one vertex between X and Y to set s. Delete all the edges connected to X. End
Example
#include<bits/stdc++.h> using namespace std; vector<vector<int> > g; bool visit[10001]; int i,j; vector<int> dominant(int v,int e) { vector<int> Set; //Take any edge e graph connecting the vertices i.e.; X and Y. for(i=0;i<v;i++) { if(!visit[i]) { Set.push_back(i); //Add vertex visit[i]=true; for(j=0;j<(int)g[i].size();j++) { if(!visit[g[i][j]]) { visit[g[i][j]]=true; break; } } } } return Set; } int main() { int v,e,a,b; cout<<"Enter number of vertices:"; cin>>v; cout<<"Enter number of Edges:"; cin>>e; g.resize(v); memset(visit,0,sizeof(visit)); //set all index value of an array as 0 for(i=0;i<e;i++) { cout<<"Enter the end-points of edge "<<i+1<<" : "; cin>>a>>b; a--; b--; g[a].push_back(b); g[b].push_back(a); } vector<int> Set = dominant(v,e); cout<<"The Dominant Set is:\n"; for(i=0;i<(int)Set.size();i++) cout<<Set[i]+1<<" "; return 0; }
Output
Enter number of vertices:7 Enter number of Edges:6 Enter the end-points of edge 1 : 1 2 Enter the end-points of edge 2 : 2 2 Enter the end-points of edge 3 : 3 4 Enter the end-points of edge 4 : 4 5 Enter the end-points of edge 5 : 6 7 Enter the end-points of edge 6 : 4 5 The Dominant Set is: 1 3 5 6
- Related Articles
- C++ Program to Solve the Fractional Knapsack Problem
- C++ Program to Solve N-Queen Problem
- C++ Program to Solve the 0-1 Knapsack Problem
- Write a C# program to solve FizzBuzz problem
- C++ Program to Solve Knapsack Problem Using Dynamic Programming
- C++ Program to Solve Travelling Salesman Problem for Unweighted Graph
- Python Program to solve Maximum Subarray Problem using Kadane’s Algorithm
- C++ program to Solve Tower of Hanoi Problem using Binary Value
- Python Program to solve Maximum Subarray Problem using Divide and Conquer
- C++ Program to Solve a Matching Problem for a Given Specific Case
- 10 ways to solve the problem of computer illiteracy
- Solve the Sherlock and Array problem in JavaScript
- How we Can Solve this Problem
- Largest Independent Set Problem
- How to solve the diamond problem using default methods in Java?

Advertisements