python实现dict版图遍历示例
时间:2023-11-01 11:43:41
#_*_coding:utf_8_
import sys
import os
class Graph():
def __init__(self, V, E):
self.V = V
self.E = E
self.visited = []
self.dict = {}
self.fd = open("input.txt")
def initGraph(self):
self.visited = [0 for i in range(self.V+1)]
for i in range(self.E):
f, t = map(int, self.fd.readline().split())
#f, t = map(int, sys.stdin.readline().split())
if self.dict.has_key(f)==False:
l = []
l.append(t)
self.dict[f] = l
else:
l = self.dict[f]
l.append(t)
self.dict[f] = l
def dfsGraph(self, src):
self.visited[src] = 1
print src ,
if self.dict.get(src): #self.dict[src]会出现异常
for u in self.dict[src]:
if self.visited[u]==0:
self.dfsGraph(u)
graph = Graph(6, 10)
graph.initGraph()
graph.dfsGraph(1)
nput.txt
1 2
1 3
1 4
3 2
2 6
4 3
3 5
4 5
6 5
3 6
output:
1 2 6 5 3 4
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python之多进程与多线程的使用
python3.x上post发送json数据
![](https://img.aspxhome.com/file/2023/3/92153_0s.jpg)
Springboot使用influxDB时序数据库的实现
客户端数据存储–超越cookies
Git 教程之远程仓库详解
![](https://img.aspxhome.com/file/2023/9/132269_0s.jpg)
python读写ini文件示例(python读写文件)
Vue的v-if和v-show的区别图文介绍
![](https://img.aspxhome.com/file/2023/9/130089_0s.png)
python 打印完整异常问题
Vue3中的极致防抖/节流详解(附常见方式防抖/节流)
Python编程在flask中模拟进行Restful的CRUD操作
![](https://img.aspxhome.com/file/2023/2/134772_0s.png)
python中使用PIL制作并验证图片验证码
Python通用验证码识别OCR库之ddddocr验证码识别
![](https://img.aspxhome.com/file/2023/4/126914_0s.jpg)
python 子类调用父类的构造函数实例
![](https://img.aspxhome.com/file/2023/5/70135_0s.jpg)
MySQL编码不一致可能引起的一些问题
一些与网页密切相关的技术
Django把SQLite数据库转换为Mysql数据库的过程
![](https://img.aspxhome.com/file/2023/6/98816_0s.png)
Python3中在Anaconda环境下安装basemap包
![](https://img.aspxhome.com/file/2023/5/131525_0s.png)
ASP 统计某字符串中“A”出现过的次数
Flask项目中实现短信验证码和邮箱验证码功能
![](https://img.aspxhome.com/file/2023/4/121114_0s.png)