难度:中等
给你一个有 n 个节点的有向无环图(DAG),请你找出所有从节点 0 到节点 n-1 的路径并输出(不要求按特定顺序)
graph[i] 是一个从节点 i 可以访问的所有节点的列表(即从节点 i 到节点 graph[i][j]存在一条有向边)。
示例1:
输入:graph = [[1,2],[3],[3],[]]
输出:[[0,1,3],[0,2,3]]
解释:有两条路径 0 -> 1 -> 3 和 0 -> 2 -> 3
示例2:
输入:graph = [[4,3,1],[3,2,4],[3],[4],[]]
输出:[[0,4],[0,3,4],[0,1,3,4],[0,1,2,3,4],[0,1,4]]
问题分析
这题让在有向无环图中输出从顶点0到顶点n-1的所有路径,可以使用dfs,从顶点0开始搜索,搜索所有路径memcpymemcpy,因为是无环的,所以搜索的时候不会出现死循环。到顶点n-1的时候就把这条路径上所有的点都保存下来。因为是dfs搜索,往下走的时候选择节点,往回走的时候要记得撤销选择。
JAVA:
public List<List> allPathsSourceTarget(int[][] graph) {
List<List> ans = new ArrayList();
ArrayList path = new ArrayList();
path.add(0);// 把起始节点0加进来
dfs(graph, 0, ans, path);
return ans;
}
private void dfs(int[][] graph, int index, List<List> ans, List path) {
// 到最后一个节点的时候,说明找了一个一条有效路径
if (index == graph.length - 1) {
ans.add(new ArrayList(path));
return;
}
// 当前节点指向哪些节点(可以看做是n叉树的子节点,然后遍历他的子节点)
int[] directs = graph[index];
for (int i = 0; i < directs.length; i++) {
path.add(directs[i]);// 把当前节点加入到路径中
dfs(graph, directs[i], ans, path);// 递归
path.remove(path.size() - 1); // 撤销选择
}
}
C++:
public:
vector<vector<int>> allPathsSourceTarget(vector<vector<int>> &graph) {
vector<vector<int>> ans;
vector<int> path;
path.push_back(0);// 把起始节点0加进来
dfs(graph, 0, ans, path);
return ans;
}
void dfs(vector<vector<int>> &graph, int index, vector<vector<int>> &ans, vector<int> &path) {
// 到最后一个节点的时候,说明找了一个一条有效路径
if (index == graph.size() - 1) {
ans.emplace_back(path);
return;
}
// 当前节点指向哪些节点(可以看做是n叉树的子节点,然后遍历他的子节点)
for (int g: graph[index]) {
path.emplace_back(g);// 把当前节点加入到路径中
dfs(graph, g, ans, path);// 递归
path.pop_back(); // 撤销选择
}
}
C:
void dfs(int **graph, int graphSize, int *graphColSize, int *returnSize,
int **returnColumnSizes, int **ans, int *path, int v, int count) {
// 到最后一个节点的时候,说明找了一个一条有效路径
if (v == graphSize - 1) {
ans[*returnSize] = malloc(count * sizeof(int));
memcpy(ans[*returnSize], path, count * sizeof(int));
(*returnColumnSizes)[(*returnSize)++] = count;
return;
}
// 当前节点指向哪些节点(可以看做是n叉树的子节点,然后遍历他的子节点)
for (int i = 0; i < graphColSize[v]; ++i) {
path[count++] = graph[v][i];// 把当前节点加入到路径中
dfs(graph, graphSize, graphColSize, returnSize, returnColumnSizes, ans, path, graph[v][i], count);// 递归
count--;// 撤销选择
}
}
int **allPathsSourceTarget(int **graph, int graphSize, int *graphColSize, int *returnSize, int **returnColumnSizes) {
int **ans = malloc(20000 * sizeof(int *));
int *path = malloc(15 * sizeof(int));
int v = 0;
int count = 0;
*returnSize = 0;
*returnColumnSizes = malloc(20000 * sizeof(int));
path[count++] = v;// 把起始节点0加进来
dfs(graph, graphSize, graphColSize, returnSize, returnColumnSizes, ans, path, v, count);
return ans;
}
:
def allPathsSourceTarget(self, graph: List[List[int]]) -> List[List[int]]:
def dfs(index):
# 到最后一个节点的时候,说明找了一个一条有效路径
if index == len(graph) - 1:
ans.append(path[:])
return
# 当前节点指向哪些节点(可以看做是n叉树的子节点,然后遍历他的子节点)
for direct in graph[index]:
path.append(direct) # 把当前节点加入到路径中
dfs(direct) # 递归
path.pop() # 撤销选择
ans = []
path = [0]
dfs(0)
return ans
1、
2、
3、
4、
5、
限时特惠:本站持续每日更新海量各大内部创业课程,一年会员仅需要98元,全站资源免费下载
点击查看详情
站长微信:Jiucxh
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。