Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
baibaixiaoya
Python
提交
4fa8c9d4
P
Python
项目概览
baibaixiaoya
/
Python
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Python
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
4fa8c9d4
编写于
10月 29, 2020
作者:
L
Lewis Tian
提交者:
GitHub
10月 29, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update graphs/depth_first_search_2.py (#3799)
- update naming style to snake_case - add type hints
上级
aebf9bda
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
28 addition
and
28 deletion
+28
-28
graphs/depth_first_search_2.py
graphs/depth_first_search_2.py
+28
-28
未找到文件。
graphs/depth_first_search_2.py
浏览文件 @
4fa8c9d4
...
...
@@ -8,58 +8,58 @@ class Graph:
self
.
vertex
=
{}
# for printing the Graph vertices
def
print
Graph
(
self
)
:
def
print
_graph
(
self
)
->
None
:
print
(
self
.
vertex
)
for
i
in
self
.
vertex
.
keys
()
:
for
i
in
self
.
vertex
:
print
(
i
,
" -> "
,
" -> "
.
join
([
str
(
j
)
for
j
in
self
.
vertex
[
i
]]))
# for adding the edge between two vertices
def
add
Edge
(
self
,
fromVertex
,
toVertex
)
:
def
add
_edge
(
self
,
from_vertex
:
int
,
to_vertex
:
int
)
->
None
:
# check if vertex is already present,
if
from
Vertex
in
self
.
vertex
.
keys
()
:
self
.
vertex
[
from
Vertex
].
append
(
toV
ertex
)
if
from
_vertex
in
self
.
vertex
:
self
.
vertex
[
from
_vertex
].
append
(
to_v
ertex
)
else
:
# else make a new vertex
self
.
vertex
[
from
Vertex
]
=
[
toV
ertex
]
self
.
vertex
[
from
_vertex
]
=
[
to_v
ertex
]
def
DFS
(
self
)
:
def
dfs
(
self
)
->
None
:
# visited array for storing already visited nodes
visited
=
[
False
]
*
len
(
self
.
vertex
)
# call the recursive helper function
for
i
in
range
(
len
(
self
.
vertex
)):
if
visited
[
i
]
is
False
:
self
.
DFSRec
(
i
,
visited
)
if
not
visited
[
i
]
:
self
.
dfs_recursive
(
i
,
visited
)
def
DFSRec
(
self
,
startVertex
,
visited
)
:
def
dfs_recursive
(
self
,
start_vertex
:
int
,
visited
:
list
)
->
None
:
# mark start vertex as visited
visited
[
start
V
ertex
]
=
True
visited
[
start
_v
ertex
]
=
True
print
(
start
V
ertex
,
end
=
" "
)
print
(
start
_v
ertex
,
end
=
" "
)
# Recur for all the vertices that are adjacent to this node
for
i
in
self
.
vertex
.
keys
()
:
if
visited
[
i
]
is
False
:
self
.
DFSRec
(
i
,
visited
)
for
i
in
self
.
vertex
:
if
not
visited
[
i
]
:
self
.
dfs_recursive
(
i
,
visited
)
if
__name__
==
"__main__"
:
g
=
Graph
()
g
.
add
E
dge
(
0
,
1
)
g
.
add
E
dge
(
0
,
2
)
g
.
add
E
dge
(
1
,
2
)
g
.
add
E
dge
(
2
,
0
)
g
.
add
E
dge
(
2
,
3
)
g
.
add
E
dge
(
3
,
3
)
g
.
add
_e
dge
(
0
,
1
)
g
.
add
_e
dge
(
0
,
2
)
g
.
add
_e
dge
(
1
,
2
)
g
.
add
_e
dge
(
2
,
0
)
g
.
add
_e
dge
(
2
,
3
)
g
.
add
_e
dge
(
3
,
3
)
g
.
print
G
raph
()
g
.
print
_g
raph
()
print
(
"DFS:"
)
g
.
DFS
()
g
.
dfs
()
# OUTPUT:
# 0
-> 1 ->
2
# 1
->
2
# 2
-> 0 ->
3
# 3
->
3
# 0
-> 1 ->
2
# 1
->
2
# 2
-> 0 ->
3
# 3
->
3
# DFS:
#
0 1
2 3
#
0 1
2 3
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录