One algorithm question per day --- 6.27 --- Bipartite Graph

Source: Internet
Author: User

Thanks to the platform-one day algorithm question-more progress every day ---

Ah... last...

Very late. It's almost half past two.

Cristiano Ronaldo also said goodbye to this World Cup, mainly because he lost too many balls to Germany and the United States did not win.

Or from ----> waiting for a letter

 

Original question

As we all know, Facebook users are both friends. If a is a friend of B, B must be a friend of A. Now, a user list is given, some of which are friends and some are not, determine whether these users can be divided into two groups, and the users in each group are not friends of each other. If yes, provide this division.

Example 1: User: {1, 2, 3} friend relationship: 1-2, 2-3 Division: {1, 3} {2}

Example 2: User Friend relationship: 1-2, 2-3, 3--1: {1, 3} {2, 4}

Analysis

There are a lot of interview questions that are relatively direct. When you see the question, the interviewer naturally thinks about the method. At this time, the interviewer often wants the bug-free code. There are still some questions, which are not so direct, it is to examine the examinee's ability to analyze and resolve problems. It is often a relatively complex problem, which is broken down into simple problems we have seen. There is also a type of problem. In practice, this requires the ability to build models, it can also be said that the ability to abstract the problem is then broken down into small problems. Such a question can fully examine the examinee's abilities. For example, today's interview questions.

Today's interview questions come from Facebook's actual questions. Like in China, many of our colleagues are doing Sina Weibo data mining, friend recommendations, relationship prediction, circle discovery, forwarding analysis, and influence analysis, these are very practical and useful questions. Every question can be used as an interview question worthy of further discussion.

Today's interview questions are relatively simple. Because the modeling part has already been given in the question. Think about the problem of graph splitting. Facebook's friend relationship is bidirectional, meaning it is an undirected graph (Sina Weibo and Twitter are directed graphs ). Then, divide the graph into two groups. What are the conditions for the two groups? There is no edge in the group and there is an edge between the groups. This is obviously a bipartite graph.

The problem is obvious. Is it a binary chart composed of Facebook friends? If yes, find this division. It is just a bipartite graph judgment + finding a division.

According to the characteristics of the bipartite graph, the two points on one side must belong to different groups. If they appear in the same group, it is definitely not a bipartite graph. How can we determine that two points on one side belong to different groups? We need to traverse the graph. If we find an edge and both nodes are in the same group, it is not a bipartite graph. If such an edge is not found after the graph traversal, It is a bipartite graph. During the traversal process, we need to differentiate that the two nodes of an edge belong to different groups. Here we use the staining method. The core idea is as follows:

Starting from a certain point, the node is colored in white, and the breadth is first traversed to find the adjacent node. If it is a bipartite graph, the color of the adjacent node should be different. If it is black, it will not change; if it is colorless, it will be dyed black; if it is white, that is, the same color, the program will return. When the graph traversal is complete, there are no adjacent nodes in the same color, it is a bipartite graph. The two groups marked as white and black are a division.

Let's look at two examples. Example 2 in the first figure:

Procedure Traverse nodes Adjacent nodes Queue
1 1: white 2: Black, 4: Black 2, 4
2 2: Black 1: white, 3: white 4, 3
3 4: Black 1: white, 3: white 3
4 3: white 2: Black, 4: Black Null

The queue is empty and the traversal ends. Find {1, 3} White {2, 4} black.

Let's look at another example that is not a bipartite graph: User's {1, 2, 3} relationship: 1--3, 1--2-3-4.

The procedure is as follows:

Procedure Traverse nodes Adjacent nodes Queue
1 1: white 2: Black, 3: Black, 4: Black 2, 3, 4
2 2: Black 1: white, 3: white 3, 4

If we find that 3 is in conflict, the relationship above is not a binary graph.

[Analysis completed]

 

 

 

 

 

 

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.