python 实现Graph class
来源:互联网 发布:云交易软件 编辑:程序博客网 时间:2024/06/05 17:45
Today, I wrote a class Graph in python, for the purpose of implementing the following dfs and bfs:
Can use the tinyG.txt for input.
class Graph(): def __init__(self, filepath): self.V = 0 #-- num of vertices self.E = 0 #-- num of edges self.adj = {} self.filepath = filepath fhand = open(self.filepath) self.V = int(fhand.readline()) self.E = int(fhand.readline()) for i in range(0, self.E): string = fhand.readline() string = string.split(' ') ''' the two vertices was seperated by ' '. ''' v = int(string[0]) w = int(string[1]) '''if vertices was not created yet,''' '''have to create a empty list for that vertice''' if v not in self.adj: self.adj[v] = [] if w not in self.adj: self.adj[w] = [] self.addEdge(v, w) def __str__(self): s = '' for item in self.adj: s = s + str(item) + ':' + str(self.adj[item]) + '\n' return s #return str(self.adj) def addEdge(self, v , w): self.adj[v].append(w) self.adj[w].append(v) self.E += 1 def get_Vertivces(): return self.Vdef main(): filename = raw_input('please input file name: ') g = Graph(filename) print gmain()
0 0
- python 实现Graph class
- multi-class logestic regression的python实现
- python uml code graph
- 【leetcode】Clone Graph(python)
- C#实现图(Graph)
- python class
- Python Class
- Python Class
- Python - class
- Python class
- python Class
- python-class
- Python Class
- <python>class
- Python-Class
- [从头学数学] 第252节 Python实现数据结构:图(Graph)
- 133. Clone Graph Leetcode Python
- Python 使用list实现堆栈 (基于class, 包含迭代器)
- linux下C编程之文件系统编程
- GET和POST请求的区别
- 第13周项目5-完整复制字符串
- hdu 1025 Constructing Roads In JGShining's Kingdom(二分法+最长上升子序列)
- 偏最小二乘 PLS erro 误差部分 < 三>
- python 实现Graph class
- PHP产生不重复随机数的方法
- BZOJ 1103 POI 2007 大都市meg 树状数组
- 第十三周 项目五--字符串操作(2.1)字符串复制功能
- hadoop 2.5.2 安装时遇到的异常
- 可以对 SSI 说,我叫 SI !
- PHP网站开发常用8个小技巧
- 神经网络概述
- linux之LVM