Connected Component in Undirected Graph

本文介绍了一种在无向图中寻找所有连通分量的方法,使用广度优先搜索(BFS)遍历图的每个节点,搜集并返回所有连通节点的集合。通过实例展示了算法的运行过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

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

Clarification

Learn more about representation of graphs

Notice

Nodes in a connected component should sort by label in ascending order. Different connected components can be in any order.

思路:找connected component,跟island一样,用bfs去找neighbor,然后visited去记录是否访问过,这样每次都可以搜集一个component。注意list是在每次search的时候新生成的一个list;

/**
 * Definition for Undirected graph.
 * class UndirectedGraphNode {
 *     int label;
 *     ArrayList<UndirectedGraphNode> neighbors;
 *     UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
 * };
 */


public class Solution {
    /*
     * @param nodes: a array of Undirected graph node
     * @return: a connected set of a Undirected graph
     */
    public List<List<Integer>> connectedSet(List<UndirectedGraphNode> nodes) {
        List<List<Integer>> lists = new ArrayList<List<Integer>>();
        if(nodes == null || nodes.size() == 0) return lists;
        
        HashSet<UndirectedGraphNode> visited = new HashSet<>();
        for(UndirectedGraphNode node: nodes) {
            if(!visited.contains(node)){
                bfsSearch(node, visited, lists);
            }
        }
        return lists;
    }
    
    private void bfsSearch(UndirectedGraphNode node, 
                            HashSet<UndirectedGraphNode> visited,
                            List<List<Integer>> lists) {
        List<Integer> list = new ArrayList<Integer>();
        Queue<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>();
        queue.offer(node);
        visited.add(node);
        list.add(node.label);
        
        while(!queue.isEmpty()){
            UndirectedGraphNode n = queue.poll();
            for(UndirectedGraphNode neighbor: n.neighbors) {
                if(!visited.contains(neighbor)){
                    visited.add(neighbor);
                    queue.offer(neighbor);
                    list.add(neighbor.label);
                }
            }
        }
        Collections.sort(list);
        lists.add(list);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值