forked from itcharge/AlgoNote
-
Notifications
You must be signed in to change notification settings - Fork 0
[pull] main from itcharge:main #87
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
069aee5
Update 2246. 相邻字符不同的最长路径.md
itcharge 3b50afc
Update Graph-Topological-Sorting-Kahn.py
itcharge f221d53
Update 0210. 课程表 II.md
itcharge 2f43b6d
Update 0802. 找到最终的安全状态.md
itcharge 4688815
更新拓扑排序代码模版
itcharge 46c2a10
更新分类题解列表
itcharge 29f0aef
Update 05.Graph-Topological-Sorting.md
itcharge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
310 changes: 303 additions & 7 deletions
Contents/08.Graph/02.Graph-Traversal/05.Graph-Topological-Sorting.md
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -98,4 +98,4 @@ class Solution: | |
### 思路 1:复杂度分析 | ||
|
||
- **时间复杂度**:$O(n),ドル其中 $n$ 是树的节点数目。 | ||
- **空间复杂度**:$O(n)$ | ||
- **空间复杂度**:$O(n)$。 |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import collections | ||
|
||
class Solution: | ||
# 拓扑排序,graph 中包含所有顶点的有向边关系(包括无边顶点) | ||
def topologicalSortingDFS(self, graph: dict): | ||
visited = set() # 记录当前顶点是否被访问过 | ||
onStack = set() # 记录同一次深搜时,当前顶点是否被访问过 | ||
order = [] # 用于存储拓扑序列 | ||
hasCycle = False # 用于判断是否存在环 | ||
|
||
def dfs(u): | ||
nonlocal hasCycle | ||
if u in onStack: # 同一次深度优先搜索时,当前顶点被访问过,说明存在环 | ||
hasCycle = True | ||
if u in visited or hasCycle: # 当前节点被访问或者有环时直接返回 | ||
return | ||
|
||
visited.add(u) # 标记节点被访问 | ||
onStack.add(u) # 标记本次深搜时,当前顶点被访问 | ||
|
||
for v in graph[u]: # 遍历顶点 u 的邻接顶点 v | ||
dfs(v) # 递归访问节点 v | ||
|
||
order.append(u) # 后序遍历顺序访问节点 u | ||
onStack.remove(u) # 取消本次深搜时的 顶点访问标记 | ||
|
||
for u in graph: | ||
if u not in visited: | ||
dfs(u) # 递归遍历未访问节点 u | ||
|
||
if hasCycle: # 判断是否存在环 | ||
return [] # 存在环,无法构成拓扑序列 | ||
order.reverse() # 将后序遍历转为拓扑排序顺序 | ||
return order # 返回拓扑序列 | ||
|
||
def findOrder(self, n: int, edges): | ||
# 构建图 | ||
graph = dict() | ||
for i in range(n): | ||
graph[i] = [] | ||
for v, u in edges: | ||
graph[u].append(v) | ||
|
||
return self.topologicalSortingDFS(graph) | ||
|
||
print(Solution().findOrder(2, [[1,0]])) | ||
print(Solution().findOrder(4, [[1,0],[2,0],[3,1],[3,2]])) | ||
print(Solution().findOrder(1, [])) |
45 changes: 41 additions & 4 deletions
Templates/08.Graph/Graph-Topological-Sorting-Kahn.py
This file contains hidden or 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 |
---|---|---|
@@ -1,4 +1,41 @@ | ||
class solution: | ||
def topologicalSorting(graph): | ||
indegrees = [] | ||
for | ||
import collections | ||
|
||
class Solution: | ||
# 拓扑排序,graph 中包含所有顶点的有向边关系(包括无边顶点) | ||
def topologicalSortingKahn(self, graph: dict): | ||
indegrees = {u: 0 for u in graph} # indegrees 用于记录所有节点入度 | ||
for u in graph: | ||
for v in graph[u]: | ||
indegrees[v] += 1 # 统计所有节点入度 | ||
|
||
# 将入度为 0 的顶点存入集合 S 中 | ||
S = collections.deque([u for u in indegrees if indegrees[u] == 0]) | ||
order = [] # order 用于存储拓扑序列 | ||
|
||
while S: | ||
u = S.pop() # 从集合中选择一个没有前驱的顶点 0 | ||
order.append(u) # 将其输出到拓扑序列 order 中 | ||
for v in graph[u]: # 遍历顶点 u 的邻接顶点 v | ||
indegrees[v] -= 1 # 删除从顶点 u 出发的有向边 | ||
if indegrees[v] == 0: # 如果删除该边后顶点 v 的入度变为 0 | ||
S.append(v) # 将其放入集合 S 中 | ||
|
||
if len(indegrees) != len(order): # 还有顶点未遍历(存在环),无法构成拓扑序列 | ||
return [] | ||
return order # 返回拓扑序列 | ||
|
||
|
||
def findOrder(self, n: int, edges): | ||
# 构建图 | ||
graph = dict() | ||
for i in range(n): | ||
graph[i] = [] | ||
|
||
for u, v in edges: | ||
graph[u].append(v) | ||
|
||
return self.topologicalSortingKahn(graph) | ||
|
||
print(Solution().findOrder(2, [[1,0]])) | ||
print(Solution().findOrder(4, [[1,0],[2,0],[3,1],[3,2]])) | ||
print(Solution().findOrder(1, [])) |
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.