Introduction to algorithms 22.2-6: good players and bad players

Source: Internet
Author: User

Q: There are two types of professional wrestling players: good players and bad players ". For any wrestling professional, they may or may not have a match. Assume there are n wrestling professionals and there is a list that lists the R vs contestants participating in the competition. Try to give an O (N + r) time algorithm, whether it can determine whether to specify some runners as good players, and set the remaining wrestling fingers as bad players, in this way, each game is played between a good player and a bad player. If this is possible, your algorithm should generate it.

I. It is equivalent to determining whether an image is a bipartite graph (two colors ).

A bipartite graph is a vertex set V that can be divided into two subsets that are mutually exclusive. The two nodes attached to each edge in the graph belong to the two subsets that are mutually exclusive. As shown in:

2. Determine whether the image is a bipartite graph.

Classify good players as one type, and bad players as one type.

If a graph is a bipartite graph, if and only when all the loops in the graph are even vertices, the number of vertices must be at least 2. Therefore, you can determine whether there is a loop with an odd number of vertices in the graph. Judgment Method:

1. In depth (or breadth) search, if two gray nodes have edge connections and the depth (distance to the root node) of the two nodes is an even number, it indicates that there is a loop with an odd number of vertices, that is, the graph is not a bipartite graph.

2. If the figure does not have a loop, it must be divided into two parts. If there is a loop, the number of nodes in the loop must be greater than or equal to 2.

Calculate the degree of each node. Delete the nodes with all degrees <1 (delete the corresponding edge of the node and set the degree of the corresponding vertex to-1 ); then, add all vertices whose vertices degree is 1 to the queue and repeat the previous step for each element in the queue, that is, delete the vertices whose vertices degree is 1. Finally, if there are still nodes that have not been deleted (that is, nodes with a degree> = 2), the circuit exists in the figure. If all entries are deleted, no loop exists in the graph, that is, the graph is divided into two parts. If a loop exists, all the loops (DFS) starting from this node are counted for each remaining node ), record the path length and mark the access status. If an odd edge loop exists, it indicates that it is not a bipartite graph. Otherwise, run until all nodes are accessed.

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.