6. Graph

这里收录的是真正的关于Graph data structure或者Graph Theory的题目.隐式图是一种算法/思想,不是真正关于Graph的,所以就收录在其它章节了BFS/DFS

Graph Valid Tree

  • 这道题很好的考察了对Tree和Graph的区别的理解

  • 还是BFS的思路更intuitive, 面试更好解释

Tree vs. Graph

  • 树和图的区别在于是否有环

  • tree是parent-child relationship

  • Graph 是 neighbor-neighbor relation

  • 如何判断是否是Graph: 图中存在环,同一个节点可能重复进入队列

class Solution {
    /*
    1.valid tree, N nodes can only have N - 1 edges
    2.BFS to traversal all neighbors from a random node, check whether all nodes can be visited
    3.need to build graph, Map<Integer, Set<Integer>> graph, key is node, value is set of neighbors
    */
    public boolean validTree(int n, int[][] edges) {
        if (edges == null || edges.length != n - 1) return false;
        if (n == 1) return true;
        Map<Integer, Set<Integer>> graph = buildGraph(edges);//key is node, value is set of neighbors
        Set<Integer> visited = new HashSet<>();
        Queue<Integer> queue = new LinkedList<>();
        queue.offer(edges[0][0]);
        while (!queue.isEmpty()) {
            int node = queue.poll();
            if (!visited.add(node)) continue;
            for (int neighbor : graph.get(node)) {
                queue.offer(neighbor);
            }
        }
        return visited.size() == n;
    }
    private Map<Integer, Set<Integer>> buildGraph(int[][] edges) {
        Map<Integer, Set<Integer>> graph = new HashMap<>();
        for (int[] edge : edges) {
            Set<Integer> neighbors = graph.getOrDefault(edge[0], new HashSet<>());
            neighbors.add(edge[1]);
            graph.put(edge[0], neighbors);
            neighbors = graph.getOrDefault(edge[1], new HashSet<>());
            neighbors.add(edge[0]);
            graph.put(edge[1], neighbors);
        }
        return graph;
    }
}

Course Schedule

  • 给定一堆课程和 prerequisisites, 判断是否能完成所有课程

  • 判断DAG是否有环的经典题型

  • topo sort模板题

  • 这题除了用Set来记录visited node之外,还可以用list来记录visited node

  • 这样如果需要返回任意一条路径的话就很方便

Course Schedule II

  • course schedule 的follow up, 要求返回任意一条路径的结果

  • 用一个list来记录visited node

Is Graph Bipartite?

graph coloring

BFS

Best Meeting point

Mahattan distance

Last updated

Was this helpful?