一、题目描述
节点间通路。给定有向图,设计一个算法,找出两个节点之间是否存在一条路径。
二、理解题意
- 给定一些节点,某些节点间存在单向通路
- 需要查找是否存在从节点a到节点b的通路
- 例如图中存在0→1, 0→3, 0→2, 1→3的通路
- 不存在3→1, 3→2, 3→0的通路
- 额外信息
• 节点数量n在[0, 100000],节点编号大于0小于n
• 可能存在自环和平行边
三、解法一:广度优先遍历
public boolean findWhetherExistsPath(int n, int[][] graph, int start, int target) {
//建立邻接表 adj
ArrayList<ArrayList<Integer> > adj = new ArrayList<ArrayList<Integer>>(n);
for(int i=0; i < n; i++)
adj.add(new ArrayList<Integer>()