[算法导论]BFS @ Python
class Graph:
def __init__(self):
self.V = [] class Vertex:
def __init__(self, x):
self.key = x
self.color = 'white'
self.d = 10000
self.pi = None
self.adj = [] class Solution:
def BFS(self, G, s):
for u in G.V:
if u != s:
u.color = 'white'
u.d = 10000
u.pi = None
s.color = 'gray'
s.d = 0
s.pi = None
Q = []
Q.append(s)
while Q != []:
u = Q.pop(0)
for v in u.adj:
if v.color == 'white':
v.color = 'gray'
v.d = u.d + 1
v.pi = u
Q.append(v)
u.color = 'black' if __name__ == '__main__':
G = Graph()
r = Vertex('r')
s = Vertex('s')
t = Vertex('t')
u = Vertex('u')
v = Vertex('v')
w = Vertex('w')
x = Vertex('x')
y = Vertex('y')
r.adj = [s, v]
s.adj = [r, w]
t.adj = [u, w, x]
u.adj = [t, x, y]
v.adj = [r]
w.adj = [s, t, x]
x.adj = [t, u, w, y]
y.adj = [u, x]
G.V = [r, s, t, u, v, w, x, y]
m = Solution()
m.BFS(G, s)
for v in G.V:
if v != s:
print v.key, v.color, v.d, v.pi.key
[算法导论]BFS @ Python的更多相关文章
- [算法导论]DFS @ Python
class Graph: def __init__(self): self.V = [] class Vertex: def __init__(self, x): self.key = x self. ...
- 算法导论之python实现插入排序
插入排序的花费时间 c*n2, c 是常数 伪代码 INSERTION-SORT(A) for i to A.length key = A[j] //Insert A[j] into the sor ...
- [算法导论]插入排序 @ Python
class insertionsort(): def insertion_sort(self,Array): for i in range(1, len(Array)): key = Array[i] ...
- 算法导论 第一章and第二章(python)
算法导论 第一章 算法 输入--(算法)-->输出 解决的问题 识别DNA(排序,最长公共子序列,) # 确定一部分用法 互联网快速访问索引 电子商务(数值算 ...
- [算法导论]二叉查找树的实现 @ Python
<算法导论>第三版的BST(二叉查找树)的实现: class Tree: def __init__(self): self.root = None # Definition for a b ...
- [算法导论]quicksort algorithm @ Python
算法导论上面快速排序的实现. 代码: def partition(array, left, right): i = left-1 for j in range(left, right): if arr ...
- [Algorithm] 如何正确撸<算法导论>CLRS
其实算法本身不难,第一遍可以只看伪代码和算法思路.如果想进一步理解的话,第三章那些标记法是非常重要的,就算要花费大量时间才能理解,也不要马马虎虎略过.因为以后的每一章,讲完算法就是这样的分析,精通的话 ...
- 算法导论第十八章 B树
一.高级数据结构 本章以后到第21章(并查集)隶属于高级数据结构的内容.前面还留了两章:贪心算法和摊还分析,打算后面再来补充.之前的章节讨论的支持动态数据集上的操作,如查找.插入.删除等都是基于简单的 ...
- "《算法导论》之‘图’":深度优先搜索、宽度优先搜索(无向图、有向图)
本文兼参考自<算法导论>及<算法>. 以前一直不能够理解深度优先搜索和广度优先搜索,总是很怕去碰它们,但经过阅读上边提到的两本书,豁然开朗,马上就能理解得更进一步. 下文将会用 ...
随机推荐
- js 判断字符为空
function checkIsNull(value){ if(typeof value=='undefined'){ return true; } if(value==null){ return t ...
- listView获取item的Edit内容,listView中的edit内容在滚动时自动赋值了前面的内容
Today I am going to explain how to create a ListView with EditText and why will we need a TextWatche ...
- 【洛谷P1351】联合权值
我们枚举中间点,当连的点数不小于2时进行处理 最大值好搞 求和:设中间点 i 所连所有点权之和为sum 则对于每个中间点i的联合权值之和为: w[j]*(sum-w[j])之和 #include< ...
- python语法快速入门(1)
http://www.runoob.com/python/python-tutorial.html Python 是一种解释型语言: 这意味着开发过程中没有了编译这个环节.类似于PHP和Perl语言 ...
- java开发中的一些工具软件
1. XJad, 反编译工具,类似于.Net中的Refractor.可以反编译单个jar文件或一个文件夹下的class文件,效果还不错. 2. dirtyJOE, class文件直接修改工具.有时想修 ...
- Search in Rotated Sorted Array II leetcode
原题链接,点我 该题解题参考博客 和Search in Rotated Sorted Array唯一的区别是这道题目中元素会有重复的情况出现.不过正是因为这个条件的出现,出现了比较复杂的case,甚至 ...
- Position和anchorPoint
Main.storyboard ViewController.m // // ViewController.m // 7A12.position和anchorPoint // // Create ...
- android tcp协议主要函数
1 tcp_timers: 处理各种timer超时信息,关键函数tcp_xmit_timer 2 tcp_iutput: 3 tcp_output:接收方的接收窗口struct tcpcb.snd_w ...
- Servlet的几种跳转(转)
源地址:http://hi.baidu.com/awmtaplnaibmnxq/item/29d76f3dafcf00312e20c482 Servlet: 当然,在servlet中,一般跳转都发生在 ...
- iOS中数字的格式化 NSNumberFormatter
NSNumberFormatter 和NSDateFormatter 是NsFormatter的子类. NSNumberFormatter类有个属性numberStyle,它是一个枚举型,设置不同的值 ...