Leetcode - Graph Valid Tree

这道题目并没能做出来。
感觉最近睡眠比较少,所以有些问题晚上根本想不清楚,尤其图这种需要许多空间思考的。
这道题目很重要,他介绍了一个基本知识。
如何判断一个图是否有环。
当然,这道题目还需要再多判断一个问题:
如何判断这个图只有一个强连接部分 strong connected component

三种方法:
DFS, BFS, Union Find

我觉得 Union Find 最直接巧妙,也不用构造图,一个数组解决问题。

Union Find:
My code:

public class Solution {
    private int V = 0;
    private int[] id;
    private int[] sz;
    
    public boolean validTree(int n, int[][] edges) {
        this.V = n;
        id = new int[V];
        sz = new int[V];
        
        for (int i = 0; i < V; i++) {
            id[i] = i;
            sz[i] = 1;
        }
        
        for (int i = 0; i < edges.length; i++) {
            int left = find(edges[i][0]);
            int right = find(edges[i][1]);
            if (left == right) {
                return false;
            }
            else {
                union(left, right);
            }
        }
        
        return edges.length == n - 1;
    }
    
    private int find(int index) {
        if (id[index] == index) {
            return index;
        }
        else {
            int ret = find(id[index]);
            id[index] = ret;
            return ret;
        }
    }
    
    private void union(int x, int y) {
        int id1 = find(x);
        int id2 = find(y);
        if (id1 == id2) {
            return;
        }
        else {
            if (sz[id1] > sz[id2]) {
                id[id2] = id1;
                sz[id1] += sz[id2];
            }
            else {
                id[id1] = id2;
                sz[id2] += sz[id1];
            }
        }
    }
}

reference:
https://discuss.leetcode.com/topic/21712/ac-java-union-find-solution

DFS:
My code:

public class Solution {
    private int V = 0;
    private List<List<Integer>> adj = new ArrayList<List<Integer>>();
    
    public boolean validTree(int n, int[][] edges) {
        this.V = n;
        for (int i = 0; i < V; i++) {
            adj.add(new ArrayList<Integer>());
        }
        
        for (int i = 0; i < edges.length; i++) {
            int u = edges[i][0];
            int v = edges[i][1];
            adj.get(v).add(u);
            adj.get(u).add(v);
        }
        
        boolean[] isVisited = new boolean[V];
        if (hasCycle(0, isVisited, -1)) {
            return false;
        }
        
        for (int i = 0; i < isVisited.length; i++) {
            if (!isVisited[i]) {
                return false;
            }
        }
        
        return true;
    }
    
    private boolean hasCycle(int u, boolean[] isVisited, int parent) {
        isVisited[u] = true;
        
        for (Integer temp : adj.get(u)) {
            if (isVisited[temp] && temp != parent) {
                return true;
            }
            else if (!isVisited[temp] && hasCycle(temp, isVisited, u)) {
                return true;
            }
        }
        
        return false;
    }
}

reference:
https://discuss.leetcode.com/topic/21714/ac-java-graph-dfs-solution-with-adjacency-list

BFS:
My code:

public class Solution {
    private int V = 0;
    private List<Set<Integer>> adj = new ArrayList<Set<Integer>>();
    
    public boolean validTree(int n, int[][] edges) {
        this.V = n;
        for (int i = 0; i < V; i++) {
            adj.add(new HashSet<Integer>());
        }
        
        for (int i = 0; i < edges.length; i++) {
            int u = edges[i][0];
            int v = edges[i][1];
            adj.get(v).add(u);
            adj.get(u).add(v);
        }
        
        boolean[] isVisited = new boolean[V];
        Queue<Integer> q = new LinkedList<Integer>();
        q.offer(0);
        isVisited[0] = true;
        while (!q.isEmpty()) {
            int u = q.poll();
            n--;
            for (Integer v : adj.get(u)) {
                if (isVisited[v]) {
                    return false;
                }
                adj.get(v).remove(u);
                q.offer(v);
                isVisited[v] = true;
            }
        }
        
        return n == 0;
    }
}

reference:
https://discuss.leetcode.com/topic/57131/java-straightforward-bfs-solution

全部都自己写了下。如果判断环在graph里面应该算是基本的算法,必须要掌握。

还有就是 topological sort

最重要的还是如果快速地构建一个graph

Anyway, Good luck, Richardo! — 09/10/2016

    原文作者:Richardo92
    原文地址: https://www.jianshu.com/p/5ad6612ddedb#comments
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞