input friends relations{{1,2}, {2,3}, {3,4}}
把人分成两拨,每拨人互相不认识,
所以应该是group1{1,3}, group2{2,4}

这道题应该是how to bipartite a graph

Taken from GeeksforGeeks

Following is a simple algorithm to find out whether a given graph is Birpartite or not using Breadth First Search (BFS) :-

  1. Assign RED color to the source vertex (putting into set U).
  2. Color all the neighbors with BLUE color (putting into set V).
  3. Color all neighbor’s neighbor with RED color (putting into set U).
  4. This way, assign color to all vertices such that it satisfies all the constraints of m way coloring problem where m = 2.
  5. While assigning colors, if we find a neighbor which is colored with same color as current vertex, then the graph cannot be colored with 2 vertices (or graph is not Bipartite).

Also, NOTE :-

-> It is possible to color a cycle graph with even cycle using two colors.

-> It is not possible to color a cycle graph with odd cycle using two colors.

EDIT :-

If a graph is not connected, it may have more than one bipartition. You need to check all those components separately with the algorithm as mentioned above.

So, for various disconnected sub-graph of the same graph, you need to perform this bipartition check on all of them separately using the same algorithm discussed above. All of those various disconnected sub-graph of the same graph will account for its own set of bipartition.

And, the graph will be termed bipartite, IF AND ONLY IF, each of its connected components are proved to be bipartite .

 package fbOnsite;

 import java.util.*;

 public class Bipartite {
HashSet<Integer> list1 = new HashSet<Integer>();
HashSet<Integer> list2 = new HashSet<Integer>(); public void bfs(int[][] relations) {
HashMap<Integer, HashSet<Integer>> graph = new HashMap<Integer, HashSet<Integer>>();
for (int[] each : relations) {
if (!graph.containsKey(each[0]))
graph.put(each[0], new HashSet<Integer>());
if (!graph.containsKey(each[1]))
graph.put(each[1], new HashSet<Integer>());
graph.get(each[0]).add(each[1]);
graph.get(each[1]).add(each[0]);
} Queue<Integer> queue = new LinkedList<Integer>();
queue.offer(relations[0][0]);
list1.add(relations[0][0]);
HashSet<Integer> visited = new HashSet<Integer>();
visited.add(relations[0][0]);
int count = 1;
while (!queue.isEmpty()) {
int size = queue.size();
for (int i=0; i<size; i++) {
int person = queue.poll();
HashSet<Integer> friends = graph.get(person);
for (int each : friends) {
if (list1.contains(each)&&list1.contains(person) || list2.contains(each)&&list2.contains(person)) {
list1.clear();
list2.clear();
return;
} if (!visited.contains(each)) {
if (count%2 == 1) list2.add(each);
else list1.add(each);
queue.offer(each);
visited.add(each);
}
}
}
count++;
}
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Bipartite sol = new Bipartite();
int[][] relations1 = new int[][]{{1,2},{2,3},{3,4}};
int[][] relations2 = new int[][]{{1,2},{1,4},{1,6},{1,8},{2,3},{3,4},{3,6},{2,5},{4,5},{5,6},{5,8}};
int[][] relations3 = new int[][]{{1,2},{2,3},{3,1}};
sol.bfs(relations2);
System.out.println(sol.list1);
System.out.println(sol.list2);
} }

FB面经Prepare: Bipartite a graph的更多相关文章

  1. FB面经 Prepare: All Palindromic Substrings

    Given a string, calculate how many substring is palindrome. Ignore non-char characters. Ignore case; ...

  2. FB面经 Prepare: Task Schedule

    tasks has cooldown time, give an input task id array, output finish time input: AABCA A--ABCA output ...

  3. FB面经 Prepare: Make Parentheses valid

    给一组括号,remove最少的括号使得它valid 从左从右各scan一次 package fb; public class removeParen { public static String fi ...

  4. FB面经Prepare: Friends Recommendation

    有个getFriend() API, 让你推荐你的朋友的朋友做你的朋友,当然这个新朋友不能是你原来的老朋友 package fb; import java.util.*; public class R ...

  5. FB面经Prepare: Dot Product

    Conduct Dot Product of two large Vectors 1. two pointers 2. hashmap 3. 如果没有额外空间,如果一个很大,一个很小,适合scan小的 ...

  6. FB面经prepare: Count the number of Vector

    给一个超级大的排好序的vector [abbcccdddeeee]比如,要求返回[{,a}, {,b}, {,c}, {,d}, {,e}......]复杂度要优于O(N) 分析: 如果是binary ...

  7. FB面经 Prepare: Even Tree

    You are given a tree (a simple connected graph with no cycles). The tree has nodes numbered from to ...

  8. FB面经 Prepare: Largest Island

    Find largest island in a board package fb; public class LargestIsland { public int findLargestIsland ...

  9. FB面经prepare: task schedule II

    followup是tasks是无序的. 一开始是有序的,比如说1, 1, 2, 1,一定要先执行第一个task1,然后等task1恢复,再执行第2个task1,再执行task2..... follow ...

随机推荐

  1. 分布式版本控制系统-git

    Git是目前世界上最先进的分布式版本控制系统 SVN是集中式的版本控制系统,而Git是分布式版本控制系统,集中式和分布式版本控制系统有什么区别呢?这个可以找度娘...... 1.安装Git yum i ...

  2. python运算符——比较运算符

    比较运算符的运算结果会得到一个bool类型,也就是逻辑判定,要么是真True,要不就是False 大于“>”  小于“<”  不说了,看看不等于,用“!=”表示.大于等于“>=”和小 ...

  3. win10下如何解决U盘连接上电脑但不显示的问题

    问题:U盘插上电脑之后,任务栏上有U盘连接上的显示,但是在磁盘符和U盘管理器上没有它的显示. 方法: 1.在任务栏上点击win图标,再点击“设置”(或直接使用快捷键win+i)进入到win10下的“设 ...

  4. 2018-2019-1 20189201《Linux内核原理与分析》第三周作业

    写作业之前,写了时光博物馆参观感受.1978-2018 40年的改革开放历程. 一.C语言中内嵌汇编语言的写法 内嵌汇编的语法如下: asm volatile ( 汇编语句模版: 输出部分: 输入部分 ...

  5. PostgreSQL自学笔记:与python交互

    与python交互教程 原文地址:https://www.yiibai.com/html/postgresql/2013/080998.html 1. Python psycopg2 模块APIs 连 ...

  6. vue中,对象数组多层嵌套时,更新数据更新页面

    vue中的对象和数组的元素直接赋值修改时,是不能响应到view中去的 1.对象更新 this.a={title:'列表1’}; this.a.title='列表2’; <h1>{{a.ti ...

  7. web学习路线

  8. jdbc的入门学习

    一.JDBC相关概念介绍 1.1.数据库驱动 这里的驱动的概念和平时听到的那种驱动的概念是一样的,比如平时购买的声卡,网卡直接插到计算机上面是不能用的,必须要安装相应的驱动程序之后才能够使用声卡和网卡 ...

  9. jquery easyui datagrid 如何第一次点击列标题时是降序排列

    使用 EasyUI的onBeforeLoad事件,在发回到服务器查询之前,修改排序和对应的图标样式. 1.配置回调函数 data-options='onBeforeLoad:fnOnBeforeLoa ...

  10. CAD数据导入Arcgis10.1的依赖关系

    这段时间在做基于Arcgis10.1API处理AutoCAD图纸数据并将处理后的数据坐标转换为xml文件,以便于在开发的项目中使用.通过这段时间的开发总结以下问题希望能对童鞋有所帮助: 1.遇到CAD ...