chapter_graph/graph_traversal/ #368
Replies: 31 comments 42 replies
-
发现一个问题,就是在一个图中如果有顶点不与其它顶点相连的话就遍历不到了。 |
Beta Was this translation helpful? Give feedback.
-
这里会讲有向图相关的内容吗 |
Beta Was this translation helpful? Give feedback.
-
你好,K神!有一个想法,可以在代码中添加一些测试用例嘛?这样在调试代码的过程中更好的理解代码。 |
Beta Was this translation helpful? Give feedback.
-
i suggest to add some classic algorithms for Graph ,like the Dijkstra algorithm |
Beta Was this translation helpful? Give feedback.
-
graph.adjList.keySet 不是就可以获取所有的节点了吗,还是图遍历必须是走边才行呢? |
Beta Was this translation helpful? Give feedback.
-
我看了下关于CSharp的代码,遍历方式更像是一笔画方式,如果说图不满足一笔画,也可以实现BFS所有元素吗 |
Beta Was this translation helpful? Give feedback.
-
你好,
/* 深度优先遍历 DFS */
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
vector<Vertex *> graphDFS(GraphAdjList &graph, Vertex *startVet) {
// 顶点遍历序列
vector<Vertex *> res;
// 哈希表,用于记录已被访问过的顶点
unordered_set<Vertex *> visited;
// 栈用于实现 DFS
stack<Vertex *> stk;
stk.push(startVet);
// 以顶点 vet 为起点,循环直至访问完所有顶点
while (!stk.empty()) {
Vertex *vet = stk.top();
stk.pop(); // 栈顶顶点出栈
res.push_back(vet); // 记录访问顶点
// 遍历该顶点的所有邻接顶点
for (auto adjVet : graph.adjList[vet]) {
if (visited.count(adjVet))
continue; // 跳过已被访问过的顶点
stk.push(adjVet); // 只入栈未访问的顶点
visited.emplace(adjVet); // 标记该节点已入栈
}
}
// 返回顶点遍历序列
return res;
} /* 深度优先遍历 BFS */
// 使用邻接表来表示图,以便获取指定顶点的所有邻接顶点
func graphDFS(g *graphAdjList, startVet Vertex) []Vertex {
// 顶点遍历序列
res := make([]Vertex, 0)
// 哈希表,用于记录已被访问过的顶点
visited := make(map[Vertex]struct{})
visited[startVet] = struct{}{}
// 栈用于实现 BFS, 使用切片模拟栈
stack := make([]Vertex, 0)
stack = append(stack, startVet)
// 以顶点 vet 为起点,循环直至访问完所有顶点
for len(stack) > 0 {
// 队首顶点出队
vet := stack[len(stack)-1]
stack = stack[:len(stack)-1]
// 记录访问顶点
res = append(res, vet)
// 遍历该顶点的所有邻接顶点
for _, adjVet := range g.adjList[vet] {
_, isExist := visited[adjVet]
// 只入栈未访问的顶点
if !isExist {
stack = append(stack, adjVet)
visited[adjVet] = struct{}{}
}
}
}
// 返回顶点遍历序列
return res
} |
Beta Was this translation helpful? Give feedback.
-
/**
* 0 1 2
* 3 4 5
* 6 7 8
*/
Vertex vet0 = new Vertex(0);
Vertex vet1 = new Vertex(1);
Vertex vet2 = new Vertex(2);
Vertex vet3 = new Vertex(3);
Vertex vet4 = new Vertex(4);
Vertex vet5 = new Vertex(5);
Vertex vet6 = new Vertex(6);
Vertex vet7 = new Vertex(7);
Vertex vet8 = new Vertex(8);
Vertex[][] vertex = new Vertex[][]{
{vet0, vet1}, {vet0, vet3},
{vet1, vet2}, {vet1, vet0}, {vet1, vet4},
{vet2, vet1}, {vet2, vet5},
{vet3, vet0}, {vet3, vet4}, {vet3, vet6},
{vet4, vet1}, {vet4, vet3}, {vet4, vet5}, {vet4, vet7},
{vet5, vet2}, {vet5, vet4}, {vet5, vet8},
{vet6, vet3}, {vet6, vet7},
{vet7, vet4}, {vet7, vet6}, {vet7, vet8},
{vet8, vet5}, {vet8, vet7}}; 构造出来这个图好麻烦,有没有简洁的方式? |
Beta Was this translation helpful? Give feedback.
-
建议备注 哈希表 |
Beta Was this translation helpful? Give feedback.
-
k神您好,我作为初学者对递归算法理解的一直不是太到位,看完本篇有两点疑问: |
Beta Was this translation helpful? Give feedback.
-
你好,我是初学者,我有一个疑问。(9.3.1→ 2.复杂度分析) 在询问chatgpt时:“在最坏情况下,如果图是连通的(每个节点都能直接或间接到达其他节点),则 |E| 最多为 O(|V|^2),因此 BFS 的时间复杂度可以近似表示为 O(|V| + |V|^2)。” 但是在询问Monica时:“然而,需要注意的是,在实际应用中,图的边数通常不会达到 O(|V|^2),因此广度优先搜索的时间复杂度可以简化为 O(|V|)。” |
Beta Was this translation helpful? Give feedback.
-
求一个 |
Beta Was this translation helpful? Give feedback.
-
对于广度优先遍历bfs,如果有一个节点没有与任何节点连接,那么可能就访问不到该节点,或者该起始节点没有与其他节点连接,那么访问不到除起始节点外的任何节点 |
Beta Was this translation helpful? Give feedback.
-
这里作者没有写使用邻接矩阵实现BFS和DFS遍历,大家可以去做一下 力扣547省份数量
|
Beta Was this translation helpful? Give feedback.
-
# 哈希表,用于记录已被访问过的顶点
visited = set[Vertex]([start_vet])
# 队列用于实现 BFS
que = deque[Vertex]([start_vet]) 你好,请问这里的 |
Beta Was this translation helpful? Give feedback.
-
请问时间复杂度O(|V|)中的绝对值符号表示啥意思? |
Beta Was this translation helpful? Give feedback.
-
前面时间复杂度都是O(n)这样表示,请问这节BFS的时间复杂度怎么突然变成O(|V|)了?有什么特殊意义吗 |
Beta Was this translation helpful? Give feedback.
-
想问下 visited = setVertex是python哪个标准的写法啊? |
Beta Was this translation helpful? Give feedback.
-
用java广度优先遍历时,res和visited两个集合是否可换成一个LinkedHashSet,可以包含两个集合的功能,既能判断是否存在,也可以按顺序保存。 |
Beta Was this translation helpful? Give feedback.
-
没有考虑非连通图呀 |
Beta Was this translation helpful? Give feedback.
-
visited = setVertex 让新手有点晕了,我只了解有两种 |
Beta Was this translation helpful? Give feedback.
-
邻接矩阵的BFS: public static List<Integer> BFS(int A[][], int n) {
int[] visited = new int[n];
//BFS遍历结果
List<Integer> res = new ArrayList<Integer>();
//辅助队列,用于遍历
Queue<Integer> queue = new LinkedList<>();
//找到第一个符合的顶点
for (int i = 0; i < n; i++) {
if (!queue.isEmpty()) {
break;
}
for (int j = 0; j < n; j++) {
if (A[i][j] == 1) {
queue.add(i);
visited[i] = 1;
break;
}
}
}
while (!queue.isEmpty()) {
int cur = queue.poll();
//加入结果
res.add(cur);
for (int j = 0; j < n; j++) {
//不存在边,则跳过
if (A[cur][j] != 1) {
continue;
}
//已经访问过,进行跳过
if (visited[j] == 1) {
continue;
}
//将当前顶点加入队列继续访问
queue.add(j);
//访问当前入队顶点
visited[j] = 1;
}
}
return res;
} 邻接矩阵DFS public static void DFS(int A[][], int cur, int[] visited, List<Integer> res) {
visited[cur] = 1;
res.add(cur);
for (int i = 0; i < A.length; i++) {
if (A[cur][i] != 1)
continue;
if (visited[i] == 1)
continue;
DFS(A, i, visited, res);
}
} |
Beta Was this translation helpful? Give feedback.
-
这个bfs和dfs的图太棒了 看明白了 谢谢作者! |
Beta Was this translation helpful? Give feedback.
-
尝试写了一版本邻接矩阵版本的BFS
|
Beta Was this translation helpful? Give feedback.
-
对邻接顶点有个疑问:图9-10步骤3中的节点3,没有跟节点1相连的边,不属于节点1的邻接顶点吧?后续步骤没有边与顶点相连的也一样。 |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
//作者深度优先遍历有些繁琐,我觉可以用一个列表visited来实现
} |
Beta Was this translation helpful? Give feedback.
-
I can't agree more !!!
izip-aTLa2 ***@***.***>于2024年10月4日 周五10:57写道:
… 打卡
—
Reply to this email directly, view it on GitHub
<#368 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BE623QVDS3UTNI7GTZMI5KDZZX72HAVCNFSM6AAAAAAU37GS7CVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTAOBTHEZDQMQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
https://users.rust-lang.org/t/leetcode1584-in-rust/120833 |
Beta Was this translation helpful? Give feedback.
-
C 版本中,BFS 借助了 |
Beta Was this translation helpful? Give feedback.
-
chapter_graph/graph_traversal/
一本动画图解、能运行、可提问的数据结构与算法入门书
https://www.hello-algo.com/chapter_graph/graph_traversal/
Beta Was this translation helpful? Give feedback.
All reactions