python 图
class Graph(object):
def __init__(self,*args,**kwargs):
self.node_neighbors = {}
self.visited = {}
def add_nodes(self,nodelist):
for node in nodelist:
self.add_node(node)
def add_node(self,node):
if not node in self.nodes():
self.node_neighbors[node] = []
def add_edge(self,edge):
u,v = edge
if(v not in self.node_neighbors[u]) and ( u not in self.node_neighbors[v]):
self.node_neighbors[u].append(v)
if(u!=v):
self.node_neighbors[v].append(u)
def nodes(self):
return self.node_neighbors.keys()
# 深度优先
def depth_first_search(self,root=None):
order = []
if root:
self.dfs(root, order)
for node in self.nodes():
if not node in self.visited:
self.dfs(node, order)
print(order)
return order
def dfs(self, node, order):
self.visited[node] = True
order.append(node)
for n in self.node_neighbors[node]:
if not n in self.visited:
self.dfs(n)
# 广度优先
def breadth_first_search(self, root=None):
queue = []
order = []
if root:
queue.append(root)
order.append(root)
self.bfs(queue, order)
for node in self.nodes():
if not node in self.visited:
queue.append(node)
order.append(node)
self.bfs(queue, order)
print(order)
return order
def bfs(self, queue, order):
while len(queue)> 0:
node = queue.pop(0)
self.visited[node] = True
for n in self.node_neighbors[node]:
if (not n in self.visited) and (not n in queue):
queue.append(n)
order.append(n)
# 找一条路径
def find_path(graph, start, end, path=[]):
path = path + [start]
if start == end:
return path
if not graph.has_key(start):
return None
for node in graph[start]:
if node not in path:
newpath = find_path(graph, node, end, path)
if newpath: return newpath
return None
# 找所有路径
def find_all_paths(graph, start, end, path=[]):
path = path + [start]
if start == end:
return [path]
if not graph.has_key(start):
return []
paths = []
for node in graph[start]:
if node not in path:
newpaths = find_all_paths(graph, node, end, path)
for newpath in newpaths:
paths.append(newpath)
return paths
# 找最短路径
def find_shortest_path(graph, start, end, path=[]):
path = path + [start]
if start == end:
return path
if not graph.has_key(start):
return None
shortest = None
for node in graph[start]:
if node not in path:
newpath = find_shortest_path(graph, node, end, path)
if newpath:
if not shortest or len(newpath) < len(shortest):
shortest = newpath
return shortest
if __name__ == '__main__':
g = Graph()
g.add_nodes([i+1 for i in range(8)])
g.add_edge((1, 2))
g.add_edge((1, 3))
g.add_edge((2, 4))
g.add_edge((2, 5))
g.add_edge((4, 8))
g.add_edge((5, 8))
g.add_edge((3, 6))
g.add_edge((3, 7))
g.add_edge((6, 7))
print("nodes:", g.nodes())
order = g.breadth_first_search(1)
order = g.depth_first_search(1)
python 图的更多相关文章
- PySide——Python图形化界面
PySide——Python图形化界面 PySide——Python图形化界面入门教程(四) PySide——Python图形化界面入门教程(四) ——创建自己的信号槽 ——Creating Your ...
- PySide——Python图形化界面入门教程(四)
PySide——Python图形化界面入门教程(四) ——创建自己的信号槽 ——Creating Your Own Signals and Slots 翻译自:http://pythoncentral ...
- PySide——Python图形化界面入门教程(六)
PySide——Python图形化界面入门教程(六) ——QListView和QStandardItemModel 翻译自:http://pythoncentral.io/pyside-pyqt-tu ...
- PySide——Python图形化界面入门教程(五)
PySide——Python图形化界面入门教程(五) ——QListWidget 翻译自:http://pythoncentral.io/pyside-pyqt-tutorial-the-qlistw ...
- PySide——Python图形化界面入门教程(三)
PySide——Python图形化界面入门教程(三) ——使用内建新号和槽 ——Using Built-In Signals and Slots 上一个教程中,我们学习了如何创建和建立交互widget ...
- PySide——Python图形化界面入门教程(二)
PySide——Python图形化界面入门教程(二) ——交互Widget和布局容器 ——Interactive Widgets and Layout Containers 翻译自:http://py ...
- PySide——Python图形化界面入门教程(一)
PySide——Python图形化界面入门教程(一) ——基本部件和HelloWorld 翻译自:http://pythoncentral.io/intro-to-pysidepyqt-basic-w ...
- Python 图_系列之基于<链接表>实现无向图最短路径搜索
图的常用存储方式有 2 种: 邻接炬阵 链接表 邻接炬阵的优点和缺点都很明显.优点是简单.易理解,对于大部分图结构而言,都是稀疏的,使用炬阵存储空间浪费就较大. 链接表的存储相比较邻接炬阵,使用起来更 ...
- 安装Python图型处理库Python Imaging Library(PIL)
方法1: 在Debian/Ubuntu Linux下直接通过apt安装: $sudo apt-get install python-imaging Mac和其他版本的Linux可以直接使用easy_i ...
- python 图实现
#coding:utf-8 __author__ = 'similarface' class Graph: def __init__(self,label,extra=None): #节点是类实例 s ...
随机推荐
- Win8&Win2012R2如何支持DOTA2输入法
微软自带的拼音和五笔就不用看了,没研究过,下面的方法应该不支持. 其实方法很简单运行下ctfmon.exe就可以了,这个原来旧输入模式的基础,测试可以支持QQ五笔. PS:使用拼音输入法的用户可以直接 ...
- Web API在OWIN下实现OAuth
OAuth(Open Authorization) 为用户资源的授权提供了一个安全的.开放而又简易的标准.与以往的授权方式不同之处是OAuth的授权不会使第三方触及到用户的帐号信息(如用户名与密码), ...
- linux 学习随笔-文件目录管理
1:绝对路径:写法一般由'/'根目录引起的 '/'表示根目录 2:相对路径:不是由'/'引起的 需要先进入/ 在进入其它文件 3:mkdir -p +目录+文件夹 可以用于创建不存在的级联目录 不 ...
- 面试题整理:SQL(二)
1. 现有广告合同表Orders,表示广告在哪个广告位的哪几天需要播出 OrderID Positioncode Startdate Enddate 1 A 2015-11-01 2015-11-03 ...
- WPF学习之路(二) XAML(续)
属性 简单属性 前面用到的Width/Height都是简单属性,其赋值一定要放到双引号里 XAML解析器会根据属性的类型执行隐式转换 与C#的区别 SolidBrush.Color = Colors. ...
- python 读写文件
#! -*- encoding:utf-8 -*- import os from config import * from function import GetCsspToken, RegistIn ...
- MySQL入门(四)
我好久没有写这个系列了,也确实不知道写什么.回首我学习数据库的路,最开始搞Oracle的时候其实没有搞懂Oracle,也不知道学了什么,后来学习MySQL尤其是InnoDB才大概入了门了.我最开始学习 ...
- JBOSS目录结构详细说明
一.下载与安装. 如何下载以及安装配置,请参考:Windows下JBOSS安装配置图文教程 二.现在主要了解一下JBOSS目录结构. 1. 主目录: E:\jboos\jboss-6.1.0.Fina ...
- linux shell 之 crontab(定时任务)详解
1.定义: crontab命令常见于Unix和类Unix的操作系统之中,用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和执行.该词来源于希 ...
- scala 第一课
val msg="Hello,World" Scala 可以根据赋值的内容推算出变量的类型.这在Scala语言中成为"type inference". Scal ...