【Lintcode】431. Connected Component in Undirected Graph

题目地址:

https://www.lintcode.com/problem/connected-component-in-undirected-graph/description

给定一个无向图,求其所有连通分量,每个连通分量里的数要升序排列。

法1:BFS。注意要先开一个哈希表记录已经访问过的顶点,然后用一个for循环,对图中所有未访问过的顶点一一进行BFS。代码如下:

import java.util.*;

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) {
        // write your code here
        List<List<Integer>> res = new ArrayList<>();
        if (nodes == null || nodes.isEmpty()) {
            return res;
        }
    
        Set<UndirectedGraphNode> visited = new HashSet<>();
        for (UndirectedGraphNode node : nodes) {
        	// 如果该node未被访问过,则对其进行BFS并将此连通分量所有点存入一个list并加入res
        	// 同时标记上这个连通分量里的所有点为已经访问过
            if (!visited.contains(node)) {
                res.add(bfs(nodes, node, visited));
            }
        }
        
        return res;
    }
    
    private List<Integer> bfs(List<UndirectedGraphNode> nodes, UndirectedGraphNode node, Set<UndirectedGraphNode> visited) {
        List<Integer> comp = new ArrayList<>();
        comp.add(node.label);
        
        Queue<UndirectedGraphNode> queue = new LinkedList<>();
        queue.offer(node);
        visited.add(node);
        
        while (!queue.isEmpty()) {
            UndirectedGraphNode cur = queue.poll();
            for (UndirectedGraphNode neighbor : cur.neighbors) {
                if (!visited.contains(neighbor)) {
                    comp.add(neighbor.label);
                    queue.offer(neighbor);
                    visited.add(neighbor);
                }
            }
        }
        // 升序排序
        Collections.sort(comp);
        return comp;
    }
}

class UndirectedGraphNode {
    int label;
    ArrayList<UndirectedGraphNode> neighbors;
    UndirectedGraphNode(int x) {
        label = x;
        neighbors = new ArrayList<>();
    }
}

时间复杂度 O ( N + E + ∑ n log ⁡ n ) O(N+E+\sum n\log n) O(N+E+nlogn),后面还需要加上排序的时间。空间 O ( N + E ) O(N+E) O(N+E)

法2:DFS。思路和BFS是一样的。代码如下:

import java.util.*;

public class Solution2 {
    public List<List<Integer>> connectedSet(List<UndirectedGraphNode> nodes) {
        // write your code here
        List<List<Integer>> res = new ArrayList<>();
        if (nodes == null || nodes.isEmpty()) {
            return res;
        }
       
        Set<UndirectedGraphNode> visited = new HashSet<>();
        for (UndirectedGraphNode node : nodes) {
            if (!visited.contains(node)) {
                List<Integer> comp = new ArrayList<>();
                dfs(node, comp, visited);
                Collections.sort(comp);
                res.add(comp);
            }
        }
        
        return res;
    }
    
    private void dfs(UndirectedGraphNode node, List<Integer> cur, Set<UndirectedGraphNode> visited) {
        cur.add(node.label);
        visited.add(node);
    
        for (UndirectedGraphNode neighbor : node.neighbors) {
            if (!visited.contains(neighbor)) {
                dfs(neighbor, cur, visited);
            }
        }
    }
}

时空复杂度不变。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值