Maximum independent set, minimum dominating set, minimum coverage subset (greedy practice) on tree

Source: Internet
Author: User
Tags printf

Meaning: Minimum dominating set: for Figure g= (V,e), take as few points from V as possible, compose V '; for any vertex u in the original, it belongs to the Set V ' or to the point in V ';

Minimum coverage point set: For Figure g= (V,e), take as few points from V as possible to compose V '; for any Edge (U,V) in the original, you belong to V ' or V to V ';

Maximum independent subset: for Figure g= (v,e), the maximum independent set refers to a collection of as many points as possible from V, so that there are no edges connected between the points.

First, the adjacency table is stored below, with any one point as the root, deep traversal, record the parent node of each node and get the depth traversal sequence;

#include <iostream>
#include <string.h>
using namespace std;
const int MAXN=1E5+10;
struct Point {
	int next;
	int to;
};
Point Pt[maxn*2];
int q,k;
int HEAD[MAXN*2],FA[MAXN],STO[MAXN];
BOOL  DIS[MAXN],RE[MAXN],VIS[MAXN];
void Add (int u,int v)
{
         pt[q].next=head[u];
	 Pt[q].to=v;
	 head[u]=q++;	
} 
void Dfs (int st)
{for
	(int i=head[st];i!=-1;i=pt[i].next)
	{
		int v=pt[i].to;
		if (!vis[v])
		{
			vis[v]=true;
			DFS (v);
			fa[v]=st;
			Sto[k++]=v;}}}

① minimum dominating set: reverse checking according to depth first, if the current point is not in the dominant set and is not connected with the point of the dominating set, if his father does not belong to the dominating sets, the parent node is added to the dominating set, the number of the dominant points plus one, the current node, the Father node of the current node, These points either belong to the dominant set, or are connected with the points of the dominant concentration;

int Min_zhi ()
{
	memset (re,false,sizeof (re));
	memset (dis,false,sizeof (dis));
	int ans=0;
	for (int i=0;i<k;i++)
	{
		int va=sto[i];
		if (!dis[va])
		{
			if (!re[fa[va]))
			{
			 re[fa[va]]=true;
			 ans++;
		    }
		    dis[va]=dis[fa[va]]=dis[fa[fa[va]]]=true;
		}
	}
	return ans;
}

② minimum overlay from subset: If both the parent node of the current point and the current point are not part of the vertex overlay collection, add their parents to the vertex overlay collection and mark the current node and the parent node of the current node;

int  Min_fu ()
{
	memset (re,false,sizeof (re));
	memset (dis,false,sizeof (dis));
	int ans=0;
	for (int i=0;i<k;i++)
	{
		int va=sto[i];
		if (!dis[va]&&!dis[fa[va]])
		{
			re[fa[va]]=true;
			ans++;
			dis[va]=dis[fa[va]]=true;
		}
	}
	return ans;
}

③ Maximum independent set: If the current point is not overwritten, the current point is added to the stand-alone set, and the node and its parent node are marked as overwritten;

int  Max_du ()
{
	memset (re,false,sizeof (re));
	memset (dis,false,sizeof (dis));
	int ans=0;
	for (int i=0;i<k;i++)
	{
		int va=sto[i];
		if (!dis[va])
		{
			re[fa[va]]=true;
			ans++;
			dis[va]=dis[fa[va]]=true;
		}
	}
	return ans;
}

Main function:

int main ()
{
	int n,m,u,v;
     while (scanf ("%d%d", &n,&m))
     {
     	memset (head,-1,sizeof (head));
     	q=1,k=0;
     	for (int i=0;i<m;i++)
     	{
     		scanf ("%d%d", &u,&v);
     		Add (u,v);
     		Add (v,u);
		}
		memset (vis,false,sizeof (Vis));
		Vis[1]=true;
		Fa[1]=1; (set 1 as the root node)
		DFS (1);
		for (int i=0;i<k;i++)
		cout<<sto[i]<< "* * *";
		cout<<endl;
		for (int i=1;i<=10;i++)
		cout<<fa[i]<< "";
		cout<<endl;
		printf ("%d\n", Min_zhi ());
		for (int i=1;i<=10;i++)
		cout<<re[i]<< "";
		cout<<endl;
		printf ("%d\n", Min_fu ());
		for (int i=1;i<=10;i++)
		cout<<re[i]<< "";
		cout<<endl;
		printf ("%d\n", Max_du ());
		for (int i=1;i<=10;i++)
		cout<<re[i]<< "";
		cout<<endl;
	 }
	 return 0;





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.