FB面经Prepare: Bipartite a graph
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) :-
- Assign RED color to the source vertex (putting into set U).
- Color all the neighbors with BLUE color (putting into set V).
- Color all neighbor’s neighbor with RED color (putting into set U).
- This way, assign color to all vertices such that it satisfies all the constraints of m way coloring problem where m = 2.
- 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的更多相关文章
- FB面经 Prepare: All Palindromic Substrings
Given a string, calculate how many substring is palindrome. Ignore non-char characters. Ignore case; ...
- FB面经 Prepare: Task Schedule
tasks has cooldown time, give an input task id array, output finish time input: AABCA A--ABCA output ...
- FB面经 Prepare: Make Parentheses valid
给一组括号,remove最少的括号使得它valid 从左从右各scan一次 package fb; public class removeParen { public static String fi ...
- FB面经Prepare: Friends Recommendation
有个getFriend() API, 让你推荐你的朋友的朋友做你的朋友,当然这个新朋友不能是你原来的老朋友 package fb; import java.util.*; public class R ...
- FB面经Prepare: Dot Product
Conduct Dot Product of two large Vectors 1. two pointers 2. hashmap 3. 如果没有额外空间,如果一个很大,一个很小,适合scan小的 ...
- FB面经prepare: Count the number of Vector
给一个超级大的排好序的vector [abbcccdddeeee]比如,要求返回[{,a}, {,b}, {,c}, {,d}, {,e}......]复杂度要优于O(N) 分析: 如果是binary ...
- FB面经 Prepare: Even Tree
You are given a tree (a simple connected graph with no cycles). The tree has nodes numbered from to ...
- FB面经 Prepare: Largest Island
Find largest island in a board package fb; public class LargestIsland { public int findLargestIsland ...
- FB面经prepare: task schedule II
followup是tasks是无序的. 一开始是有序的,比如说1, 1, 2, 1,一定要先执行第一个task1,然后等task1恢复,再执行第2个task1,再执行task2..... follow ...
随机推荐
- selenium执行JavaScript语句:控制滚动条 聚焦元素 改变下拉选项
1. 执行js脚本 控制滚动条 # http://www.cnblogs.com/yoyoketang/p/6128655.html In [347]: js = "window.scrol ...
- 使用kubeadm创建kubernets集群
参考: http://docs.kubernetes.org.cn/459.html https://blog.csdn.net/gui951753/article/details/833169 ...
- flash上传头像,截取图像 组件演示
效果图如下: HTML页面代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http:// ...
- URL 链接中 井号#、问号?、连接符& 分别有什么作用?
在一个 URL 中可以包含很多的内容,其中不仅仅是包含 26 个英文字母,10 个罗马数字,中文汉字,还可以拥有井号“#”.问号“?”.连接符“&”等三种最常见的符号,那么这些符号在网站中都有 ...
- python学习中遇到的问题
问题1: ‘unicodeescape’ codec can’t decode bytes in position XXX: trun错误解决方案 http://blog.csdn.net/u0112 ...
- Linux——模拟实现一个简单的shell(带重定向)
进程的相关知识是操作系统一个重要的模块.在理解进程概念同时,还需了解如何控制进程.对于进程控制,通常分成1.进程创建 (fork函数) 2.进程等待(wait系列) 3.进程替换(exec系列) 4 ...
- jQuery (01) 浏览器的事件模型
浏览器的事件模型 由网景公司引入的 DOM0 级事件模型 把事件处理程序绑定到 DOM 元素的属性上: ele.onclick(); ele.onDOMContentLoad(); ele.onloa ...
- JavaScript开发者应懂的33个概念
简介 这个项目是为了帮助开发者掌握 JavaScript 概念而创立的.它不是必备,但在未来学习(JavaScript)中,可以作为一篇指南. 本篇文章是参照 @leonardomso 创立,英文版项 ...
- python hashlib模块 md5加密 sha256加密 sha1加密 sha512加密 sha384加密 MD5加盐
python hashlib模块 hashlib hashlib主要提供字符加密功能,将md5和sha模块整合到了一起,支持md5,sha1, sha224, sha256, sha384, ...
- [dev] Go的协程切换问题
子标题:runtime.Gosched() 是干嘛用的? 1. go程序都有一个环境变量,做线程数设置 GOMAXPROCS 2. 当协程数小于等于线程数的时候,程序行为上与多线程没有区别. 3. 当 ...