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 ...
随机推荐
- python面试题整理
1.谈谈你对csrf的理解和django中CSRF防护机制. 什么是 CSRF CSRF, Cross Site Request Forgery, 跨站点伪造请求.举例来讲,某个恶意的网站上有一个指向 ...
- Hibernate 双向一对多映射
附代码: public class Order { private Integer id; private String OrderName; private Customer customer; p ...
- vue学习:vue+webpack的快速使用指南(新手向)
一.vue有两种使用方式: 1.下载vue.js <script src="vue.js"></script> 2.使用npm npm install vu ...
- Restful概念的理解和践行
在实习是leader有让我们实习生看restful相关的知识,奈何当初根基很浅,看了一篇博文,但是还是很难用自己的话来描述.现在又接触了Restful的内容,就补上一篇文章. 在Spring Fram ...
- linux学习:归档,备份及进程相关命令用法整理
指令:tar.zip.gzip.unzip.rsync.scp.ps.kill.nohup 压缩 tar 归档命令,不具备压缩功能 tar -zcvf test.tar.gz test/ # ...
- 黑盒测试实践-day02
一.任务进展情况 了解测试环境,分析测试步骤. 二.存在的问题 对测试软件还不是很了解 三.解决方法 主要查看网上资料,请求同学帮助 四.下一步计划 先熟悉测试软件,然后进行下一步.
- 删除U8中单据已经审核完成但工作流未完成的任务
问题描述: U8工作流终审结点后面还有节点时,当终审终点完成后,系统会通知后面的节点进行审核,但单据显示已经审核完成,后面的节点无法审核,工作任务会一直挂着,无法删除. 例如下图中,节点"终 ...
- Java自学笔记
1.标识符:字母,数字,下划线,美元符组成,不可以已数字开头,并且严格区分大小写 2.变量 有三个元素描述变量,变量类型,变量名,变量值 如何命名变量:首字母为 字母:下划线:“$“符号,其余部分为 ...
- 微信小程序调用高德地图
index.wxml: longitude:经度 latitude:维度 地图所定位的区域 index.js 地图所定位的点
- iOS 如何查看APP的jetsamEvent日志
1.如何在iPhone上查看 设置-通用-分析-分析数据- JetsamEvent+日志 打头的系统日志. 2.如何在Mac 上查看此类分析日志 1.手机链接MAC 2.打开iTunes,点开手机图标 ...