中级农民
- 积分
- 100
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2018-3-16
- 最后登录
- 1970-1-1
|
• 图的表示法
o Adjacency Matrix(easy to implement, update/queryo(1), space o(n^2)), sparse graph
o Adjacency List, Saves space O(|V|+|E|), query O(V).
• BFS: time: o(b^m), space: o(b^m), 或者理解空间为边的个数
• DFS: time: o(b^m), space: o(bm)
• Detect Cycle, valid tree,
o 有向图: dfs(用black gray 方法); bfs/dfs(将indegree=0的点一次入queue, 如果存在环,则无法访问的所有节点)
o 无向图:dfs(用black gray 方法, 确保不是prev节点); 删除degree<=1的点,看最后是否还有未访问节点; union-find
• topological sort, course schedule, Alien Dictionary
o dfs,
♣ 类似找欧拉回路,dfs走到底后,加入subList中,得到几段排好序的subList,然后汇总
♣ 用建HashMap<DirectedGraphNode, Integer> indegree 的方法找,遍历顺序不同而已
o bfs,建HashMap<DirectedGraphNode, Integer> indegree
• BFS: 最短路径, wallsAndGates, best meeting points, shortest distance to all building
o 矩阵上,直接入pair坐标
• DAG中的一条最长路径: topological sort, then update point one after one
• 欧拉:经过所有的“边”仅一次,不走重复路
o 有向 In degree and out degree of every vertex is same.
o 无向 All vertices have even degree
o eulier path, dfs同时删除访问过节点,最后添加至result,result是反向的
• Hamiltonian path: 经过其他“点”仅一次
|
|