今天在图书馆闲逛的时候偶然看见《Think Complexity》(复杂性思考)这本书,下午看了一会儿觉得很有意思。本书第二章讲的是用Python实现的图,特别写篇博客记录。

 

首先,图有两大元素:顶点和边。分别用Vertex和Edge类来描述顶点集和边集。

 

顶点集:Vertex

class Vertex(object):
"""A Vertex is a node in a graph.""" def __init__(self, label=''):
self.label = label def __repr__(self):
"""Returns a string representation of this object that can
be evaluated as a Python expression."""
return 'Vertex(%s)' % repr(self.label) __str__ = __repr__
"""The str and repr forms of this object are the same."""

 

边集:Edge

class Edge(tuple):
"""An Edge is a list of two vertices.""" def __new__(cls, *vs):
"""The Edge constructor takes two vertices."""
if len(vs) != 2:
raise ValueError, 'Edges must connect exactly two vertices.'
return tuple.__new__(cls, vs) def __repr__(self):
"""Return a string representation of this object that can
be evaluated as a Python expression."""
return 'Edge(%s, %s)' % (repr(self[0]), repr(self[1])) __str__ = __repr__
"""The str and repr forms of this object are the same."""

 

可以看出,边集类实现时继承的是元组,是不可变的对象,其初始化方法重写了__new__()方法。在构造类时,Python会调用__new__()方法创建对象,再调用__init__()方法初始化对象属性,对于可变类型来讲,一般做法是使用默认的__new__()方法,而重写__init__()方法来初始化属性;然而对于不可变类型来说(如元组),__init__()方法不能改变其属性值,因为必须重写__new__()方法来进行初始化。

 

如今图的边和点都有了,现在进行图的构造:

class Graph(dict):
"""A Graph is a dictionary of dictionaries. The outer
dictionary maps from a vertex to an inner dictionary.
The inner dictionary maps from other vertices to edges. For vertices a and b, graph[a][b] maps
to the edge that connects a->b, if it exists.""" def __init__(self, vs=[], es=[]): #初始化
"""Creates a new graph.
vs: list of vertices;
es: list of edges.
"""
for v in vs:
self.add_vertex(v) for e in es:
self.add_edge(e) def add_vertex(self, v): #将点添加至图
"""Add a vertex to the graph."""
self[v] = {} def add_edge(self, e): #添加边,不考虑重边
"""Adds and edge to the graph by adding an entry in both directions. If there is already an edge connecting these Vertices, the
new edge replaces it.
"""
v, w = e
self[v][w] = e
self[w][v] = e def get_edge(self, v1, v2): #返回两点之间的边,若无边返回None try:
return self[v1][v2]
except:
return None def remove_edge(self, e): #去掉边 v, w = e
del self[v][w]
del self[w][v] def vertices(self): #返回点集 return [each for each in self] def edges(self): #返回边集
edges_list = []
for each in self.values():
for each_value in each.values():
if each_value not in edges_list:
edges_list.append(each_value) return edges_list def out_vertices(self, v): #返回邻接顶点
nebor = [each for each in self[v]]
return nebor def out_edges(self, v): #返回与顶点连接的边
return [each for each in self[v].values()] def add_all_edges(self): #构造完全图
edges = []
for v in self:
for w in self:
if v != w:
e = Edge(v,w)
self.add_edge(e)

这样,图的结构和基本操作就完成了。

<Think Complexity> 用字典实现图的更多相关文章

  1. POJ2513(字典树+图的连通性判断)

    //用map映射TLE,字典树就AC了#include"cstdio" #include"set" using namespace std; ; ;//26个小 ...

  2. python数据结构与算法——图的基本实现及迭代器

    本文参考自<复杂性思考>一书的第二章,并给出这一章节里我的习题解答. (这书不到120页纸,要卖50块!!,一开始以为很厚的样子,拿回来一看,尼玛.....代码很少,给点提示,然后让读者自 ...

  3. 图及其衍生算法(Graphs and graph algorithms)

    1. 图的相关概念 树是一种特殊的图,相比树,图更能用来表示现实世界中的的实体,如路线图,网络节点图,课程体系图等,一旦能用图来描述实体,能模拟和解决一些非常复杂的任务.图的相关概念和词汇如下: 顶点 ...

  4. python 集合(set)和字典(dictionary)的用法解析

    Table of Contents generated with DocToc ditctaionary and set hash 介绍 集合-set 创建 操作和访问集合的元素 子集.超集.相对判断 ...

  5. 阿里面试官:HashMap 熟悉吧?好的,那就来聊聊 Redis 字典吧!

    最近,小黑哥的一个朋友出去面试,回来跟小黑哥抱怨,面试官不按套路出牌,直接打乱了他的节奏. 事情是这样的,前面面试问了几个 Java 的相关问题,我朋友回答还不错,接下来面试官就问了一句:看来 Jav ...

  6. Leetcode: Alien Dictionary && Summary: Topological Sort

    There is a new alien language which uses the latin alphabet. However, the order among letters are un ...

  7. 【DG】Oracle_Data_Guard官方直译

    [DG]Oracle Data Guard官方直译 1 Oracle Data Guard 介绍   Oracle Data Guard概念和管理10g版本2   Oracle Data Guard ...

  8. Redis哈希表总结

    本文及后续文章,Redis版本均是v3.2.8 在文章<Redis 数据结构之dict><Redis 数据结构之dict(2)>中,从代码层面做了简单理解.总感觉思路的不够条理 ...

  9. PHP面试(二):程序设计、框架基础知识、算法与数据结构、高并发解决方案类

    一.程序设计 1.设计功能系统——数据表设计.数据表创建语句.连接数据库的方式.编码能力 二.框架基础知识 1.MVC框架基本原理——原理.常见框架.单一入口的工作原理.模板引擎的理解 2.常见框架的 ...

随机推荐

  1. Unity中的单实例

    static GUIManager myInstance; public static GUIManager Instance { get { if (myInstance == null) myIn ...

  2. homework6-更加简单的题目

    又把时间搞错了 以为这次要写客户端程序的博客 没想到这次是“怎么吃” 言归正传 cnblog上面有很多技术博客 http://perhaps.cnblogs.com/archive/2005/08/0 ...

  3. android使用mount挂载/system/app为读写权限,删除或替换系统应用

    注意:以下代码中#开头的则为需要执行的shell命令,其他的为打印的结果.#代表需要使用ROOT权限(su)执行,所以想要修改您android手机某个目录挂载为读写,首先需要有ROOT权限! 先要得到 ...

  4. 一个简单的Socket通信Demo

    服务器端Demo: Server.java(服务器端运行主程序,直接运行): package cn.wjs; import java.net.InetAddress; import java.net. ...

  5. Unity3D中关于场景销毁时事件调用顺序的一点记录

    先说一下我遇到的问题,我弄了一个对象池管理多个对象,对象池绑定在一个GameObject上,每个对象在OnBecameInvisible时会进行回收(即移出屏幕就回收),但是当场景切换或停止运行程序时 ...

  6. [置顶] 很荣幸被选为2013年度 CSDN博客之星评选,如果觉得我的文章可以,请投我一票!

    亲爱的小伙伴们,很荣幸我被选为<2013年度CSDN博客之星候选人>,希望大家多多支持,geekguy会继续努力,为大家奉献更好的文章. 投票地址:http://vote.blog.csd ...

  7. [置顶] Jquery中DOM操作(详细)

    Jquery中的DOM操作 为了能全面的讲解DOM操作,首先需要构建一个网页. HTML代码: <%@ page language="java" import="j ...

  8. C#中反射的使用(How to use reflect in CSharp)(3)Emit的使用

    Emit意在动态构建一个可以执行(当然也就可以反射)或者只可以反射的动态库. 个人认为在不得不使用反射的情况下,使用Emit会使得效率提升空间很大.亦或者动态插件模式的软件设计中会用到. 依然2%的废 ...

  9. hadoop 关闭进程时报错no 进程 to stop

    前两天和朋友李天王吃饭的时候,聊到了一个hadoop的运维的很简单问题,感觉很有意思,以前也没有注意过,现在加以重现和整理.   感谢李天王的分享....   翻看了yarn-deamon.sh st ...

  10. WinFrom界面框架之WeifenLuo.WinFormsUI.Docking + OutLookBar

    本文转载:http://www.cnblogs.com/luomingui/p/3329763.html WeifenLuo.WinFormsUI.Docking + OutLookBar结合使用的效 ...