-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(graph/topo.md): 将伪代码改为中文 #5458
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,23 +95,21 @@ $$ | |
|
||
不断重复以上过程,直到集合 $S$ 为空。检查图中是否存在任何边,如果有,那么这个图一定有环路,否则返回 $L$,$L$ 中顶点的顺序就是构造拓扑序列的结果。 | ||
|
||
首先看来自 [Wikipedia](https://en.wikipedia.org/wiki/Topological_sorting#Kahn's_algorithm) 的伪代码 | ||
首先看来自 [Wikipedia](https://zh.wikipedia.org/wiki/%E6%8B%93%E6%92%B2%E6%8E%92%E5%BA%8F) 的伪代码 | ||
|
||
???+ note "实现" | ||
```text | ||
L ← Empty list that will contain the sorted elements | ||
S ← Set of all nodes with no incoming edges | ||
while S is not empty do | ||
remove a node n from S | ||
insert n into L | ||
for each node m with an edge e from n to m do | ||
remove edge e from the graph | ||
if m has no other incoming edges then | ||
insert m into S | ||
Comment on lines
-104
to
-110
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if graph has edges then | ||
return error (graph has at least one cycle) | ||
else | ||
return L (a topologically sorted order) | ||
L ← 包含已排序的元素的列表,目前为空 | ||
S ← 入度为零的节点的集合 | ||
当 S 非空时: | ||
将节点n从S移走 | ||
将n加到L尾部 | ||
选出任意起点为n的边e = (n,m),移除e。如m没有其它入边,则将m加入S。 | ||
重复上一步。 | ||
如图中有剩余的边则: | ||
return error (图中至少有一个环) | ||
否则: | ||
return L (L为图的拓扑排序) | ||
``` | ||
|
||
代码的核心是维持一个入度为 0 的顶点的集合。 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
链接不要换,因为 Wikipedia 那边同一个页面不同语言的版本可能有较大差异。