Find the Weak Connected Component in the Directed Graph
Description
Clarification
graph model explaination:
https://www.lintcode.com/help/graph
Example
Example 1:
Input: {1,2,4#2,4#3,5#4#5#6,5}
Output: [[1,2,4],[3,5,6]]
Explanation:
1----->2 3-->5
\ | ^
\ | |
\ | 6
\ v
->4
Example 2:
Input: {1,2#2,3#3,1}
Output: [[1,2,3]]
思路:
可以把每条有向边看成无向边, 就等同于在无向图中寻找联通块.
两种做法: 1. BFS/DFS 2. 并查集 (但由于输入数据是有向边, 所以使用并查集更合适, 否则还需要先把有向边转换成无向边)
public class Solution {
class UnionFind {
HashMap<Integer, Integer> father = new HashMap<Integer, Integer>();
UnionFind(HashSet<Integer> hashSet) {
for(Integer now : hashSet) {
father.put(now, now);
}
}
int find(int x) {
int parent = father.get(x);
while(parent != father.get(parent)) {
parent = father.get(parent);
}
return parent;
}
int compressed_find(int x) {
int parent = father.get(x);
while (parent != father.get(parent)) {
parent = father.get(parent);
}
int temp = -1;
int fa = father.get(x);
while (fa != father.get(fa)) {
temp = father.get(fa);
father.put(fa, parent) ;
fa = temp;
}
return parent;
}
void union(int x, int y) {
int fa_x = find(x);
int fa_y = find(y);
if (fa_x != fa_y)
father.put(fa_x, fa_y);
}
}
List<List<Integer>> print(HashSet<Integer> hashSet, UnionFind uf) {
List<List<Integer>> ans = new ArrayList<List<Integer>>();
HashMap<Integer, List <Integer>> hashMap = new HashMap<Integer, List<Integer>>();
for (int i : hashSet) {
int fa = uf.find(i);
if (!hashMap.containsKey(fa)) {
hashMap.put(fa, new ArrayList<Integer>());
}
List<Integer> now = hashMap.get(fa);
now.add(i);
hashMap.put(fa, now);
}
for (List<Integer> now: hashMap.values()) {
Collections.sort(now);
ans.add(now);
}
return ans;
}
public List<List<Integer>> connectedSet2(ArrayList<DirectedGraphNode> nodes) {
HashSet<Integer> hashSet = new HashSet<Integer>();
for (DirectedGraphNode now : nodes) {
hashSet.add(now.label);
for (DirectedGraphNode neighbour : now.neighbors) {
hashSet.add(neighbour.label);
}
}
UnionFind uf = new UnionFind(hashSet);
for(DirectedGraphNode now : nodes) {
for(DirectedGraphNode neighbour : now.neighbors) {
int fnow = uf.find(now.label);
int fneighbour = uf.find(neighbour.label);
if (fnow != fneighbour) {
uf.union(now.label, neighbour.label);
}
}
}
return print(hashSet , uf);
}
}
Find the Weak Connected Component in the Directed Graph的更多相关文章
- [LintCode] Find the Weak Connected Component in the Directed Graph
Find the number Weak Connected Component in the directed graph. Each node in the graph contains a ...
- [LintCode] Find the Connected Component in the Undirected Graph
Find the Connected Component in the Undirected Graph Find the number connected component in the undi ...
- lintcode:Find the Connected Component in the Undirected Graph 找出无向图汇总的相连要素
题目: 找出无向图汇总的相连要素 请找出无向图中相连要素的个数. 图中的每个节点包含其邻居的 1 个标签和 1 个列表.(一个无向图的相连节点(或节点)是一个子图,其中任意两个顶点通过路径相连,且不与 ...
- LeetCode Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- LeetCode 323. Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- Connected Component in Undirected Graph
Description Find connected component in undirected graph. Each node in the graph contains a label an ...
- algorithm@ Strongly Connected Component
Strongly Connected Components A directed graph is strongly connected if there is a path between all ...
- [HDU6271]Master of Connected Component
[HDU6271]Master of Connected Component 题目大意: 给出两棵\(n(n\le10000)\)个结点的以\(1\)为根的树\(T_a,T_b\),和一个拥有\(m( ...
- Codeforces Round #575 (Div. 3) E. Connected Component on a Chessboard(思维,构造)
E. Connected Component on a Chessboard time limit per test2 seconds memory limit per test256 megabyt ...
随机推荐
- idea Autowired 提示红色的解决方式
- ClassPathBeanDefinitionScanner 说明
Spring 工具类 ClassPathBeanDefinitionScanner 组件Bean定义扫描https://blog.csdn.net/andy_zhang2007/article/det ...
- LinkedList、ArrayList、Vector三者的关系与区别?
LinkedList.ArrayList.Vector三者的关系与区别? 区分ArrayList,Vector,LinkedList的区别 ArrayList,Vector的区别: 1.出现版本:Ar ...
- [個人紀錄] windows form , usercontrol design 模式不見
windows form 跟 usercontrol 都變成cs檔 無法點擊進入設計模式 <Compile Include="Form1.cs"/> <Compi ...
- 2019 途牛旅游网java面试笔试题 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.途牛旅游网等公司offer,岗位是Java后端开发,因为发展原因最终选择去了途牛旅游网,入职一年时间了,也成为 ...
- 为什么K8s会成为主流?
容器技术和K8s是云原生概念的核心和基础.云计算诞生已有超过10年,但云计算时代的应用到底该是什么样子,一直没人能说清楚,也没人能确定云计算的基础架构将会如何发展.在K8s出现之前,没人设想过会有一个 ...
- SimHash算法--文章相似度匹配
SimHash原理 1.SimHash背景 SimHash算法来自于 GoogleMoses Charikar发表的一篇论文"detecting near-duplicates for we ...
- 33、vue中的事件修饰符.stop、.prevent、.self、.capture、.once
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- JavaScript 之 定时器
JavaScript 里面有两个定时器:setTimeout() 和 setInterval() . 区别: setTimeout():相当于一个定时炸弹,隔一段时间执行,并且只会执行一次就不在执行了 ...
- Android 使用traceView
Android在做性能优化的时候需要使用traceView进行检测,traceView可以详细的记录下线程执行的时间让我们在做优化的时候可以清楚优化哪些内容.首先我们需要使用这个traceView,在 ...