Example of the largest group problem-Implementation of the tribal guard Problem

Source: Internet
Author: User

First, we will introduce the biggest problem:

Problem description: For an undirected graph G = (V, E), V is the vertex set, and E is the edge set. Then select several vertices in the vertex set.

Any two vertices have edges in E. Find the maximum number of vertices that can be selected. These vertices constitute the largest group.

Note: The largest group may not be unique.

Problem solving idea: the essence of this problem is a subset selection problem, that is, a set contains n elements {1, 2,..., n} to select one of them.

Set. This subset satisfies a certain nature (for the biggest problem, that is, an edge exists between any two vertices) and calculates the maximum number of elements in this subset.

Each element (corresponding to the vertex number) has two options: Add or not. Therefore, the number of subsets is 2 ^ n.

Here we use the backtracking method to solve the problem.

The concept of backtracking is understood as follows: In the solution space tree that contains all solutions to all problems, perform in-depth priority search from the root node and search for the space tree.

When any node of, first determine whether it may contain the optimal solution, if not, skip the search for the subtree that changed the node to the root

Trace back an ancestor node. If the content is included, the Child tree is displayed for in-depth priority search.

Topic description:

Residents in the original tribe often encounter conflicts to compete for resources. Almost every resident has enemies. The chief hopes to organize a tribal guard

Select the largest number of residents from the tribe and ensure that no one in the team is an enemy.

Programming task:

Calculate the optimal solution of the tribal guard program based on the enemy relationship between the specified residents.

Data input:

The number of residents in the tribe is N, an integer of 2 in the row 1st. M indicates the number of residents in the tribe, and M indicates the enemy relationship among the residents. Resident No. 1, 2,..., n. Next m rows, 2 rows

The integer U, V indicates that the residents U and V are enemies.

Data output:

Line 1 is the number of tribal guards in the best solution. Line 2 is the number of guards in the form of Xi, 1 = <I <= N, 0 indicates that the residents are not in the guard, and 1 indicates that the residents are in the guard.

Code implementation:

Coder: huifeng00

# Include <iostream>
Using namespace STD;

Int graph [200] [200]; // an undirected graph is stored in the adjacent matrix.
Int ans [200]; // store the optimal solution. 0 indicates not in the group, and 1 indicates in the group
Int temp [200]; // records the status of each vertex in the search.
Int bestn = 0; // stores the largest vertex count
Int Cn = 0; // record the number of vertices in the search
Int N; // Number of vertices in the graph

Void go (int I) // the search process is actually the deep-first search plus backtracking.
{
If (I = N)
{
If (CN> bestn)
{
Bestn = cn;
Memcpy (ANS, temp, sizeof (INT) * n );
}
Return;
}
Int J, OK = 1;
For (j = 0; j <I; j ++)
{
If (temp [J] = 1 & graph [I] [J] = 0)
{
OK = 0;
Break;
}
}
If (OK)
{
Temp [I] = 1;
CN ++;
Go (I + 1 );
CN --;
Temp [I] = 0;
}

If (CN + n-i-1> bestn)
{
Temp [I] = 0;
Go (I + 1 );
}
}

Int main ()
{
Int m;
Cin> N> m;
Memset (graph, 1, sizeof (graph); // note that byte operations are performed here, so not every value is 1, which is convenient to write.
For (INT I = 0; I <m; I ++)
{
Int A, B;
Cin> A> B;
Graph [A-1] [b-1] = 0;
Graph [b-1] [A-1] = 0;
}
Go (0 );
Cout <bestn <Endl;
For (Int J = 0; j <n; j ++)
{
Cout <ans [J] <'';
}
Cout <Endl;
Return 0;
}

Input instance:

7 10
1 2
1 4
2 4
2 3
2 5
2 6
3 5
3 6
4 5
5 6

Output instance:

3
1 0 1 0 0 0 1

Note: the value of the adjacent matrix of the graph corresponding to the enemy is 0, indicating no EDGE connection.

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.