Connected Component in Undirected Graph
2024-09-02 10:27:19
Description
Find connected component in undirected graph.
Each node in the graph contains a label and a list of its neighbors.
(A connected component of an undirected graph is a subgraph in which any two vertices are connected to each other by paths, and which is connected to no additional vertices in the supergraph.)
You need return a list of label set.
Nodes in a connected component should sort by label in ascending order. Different connected components can be in any order.
Clarification
Example
Example 1:
Input: {1,2,4#2,1,4#3,5#4,1,2#5,3}
Output: [[1,2,4],[3,5]]
Explanation:
1------2 3
\ | |
\ | |
\ | |
\ | |
4 5
Example 2:
Input: {1,2#2,1}
Output: [[1,2]]
Explanation: 1--2
思路:无向图连通块, 可以使用BFS或者并查集union find求解 .
/**
* Definition for Undirected graph.
* class UndirectedGraphNode {
* int label;
* ArrayList<UndirectedGraphNode> neighbors;
* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
* };
*/ 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 next;
while (x != father.get(x)) {
next = father.get(x);
father.put(x, parent);
x = next;
}
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, int n) {
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> > connectedSet(List<UndirectedGraphNode> nodes)
{
// Write your code here HashSet<Integer> hashSet = new HashSet<Integer>();
for (UndirectedGraphNode now : nodes) {
hashSet.add(now.label);
for (UndirectedGraphNode neighbour : now.neighbors) {
hashSet.add(neighbour.label);
}
}
UnionFind uf = new UnionFind(hashSet); for (UndirectedGraphNode now : nodes) { for (UndirectedGraphNode 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, nodes.size());
}
}
最新文章
- iOS-多线程 ,整理集锦,多种线程的创建
- EL表达式与JSTL(C)标签
- Linux mkisofs 创建光盘镜像文件(Linux指令学习笔记)
- poj2823Sliding Window(线段树求最值)
- 玩转Android之数据库框架greenDAO3.0使用指南
- Linux内核:sk_buff解析
- Android 有用的快捷键
- Bootstrap 简洁、直观、强悍、移动设备优先的前端开发框架,让web开发更迅速、简单。
- Matrix(多线程dp)
- PDF解决方案(2)--文件转PDF
- PL/SQL连接数据库时报错12154
- Linux 下源码编译FFMEG
- Strem_01
- JS 单体内置对象
- oracle备份恢复之recover database的四条语句区别
- python中的struct模块
- C++ 输入cin 和输出cout
- sql添加字段说明
- 【xsy1144】选物品 主席树
- 第六章 函数、谓词、CASE表达式 6-1 各种各样的函数
热门文章
- Kafka session.timeout.ms heartbeat.interval.ms参数的区别以及对数据存储的一些思考
- python实现Huffman编码
- Java中Integer和ThreadLocal
- webUI框架miniUI,easyUI,extJS,Bootstrap简介及简单部署
- C#实现服务器间文件同步
- Java的Annnotation (注解)
- 关于Mysql datetime类型存储范围测试
- 女性长期没有";恩爱";,会出现这4个后果?提醒:频率最好能在这个数
- grid网格布局——色子布局
- 封装axios,带请求头和响应头