最后一章,坚持!!!

# coding = utf-8

class Vertex:
    def __init__(self, key):
        self.id = key
        self.connected_to = {}

    def add_neighbor(self, nbr, weight=0):
        self.connected_to[nbr] = weight

    def __str__(self):
        return str(self.id) + ' connected to: ' + str([x.id for x in self.connected_to])

    def get_connections(self):
        return self.connected_to.keys()

    def get_id(self):
        return self.id

    def get_weight(self, nbr):
        return self.connected_to[nbr]

class Graph:
    def __init__(self):
        self.vertex_list = {}
        self.num_vertices = 0

    def add_vertex(self, key):
        self.num_vertices = self.num_vertices + 1
        new_vertex = Vertex(key)
        self.vertex_list[key] = new_vertex
        return new_vertex

    def get_vertex(self, n):
        if n in self.vertex_list:
            return self.vertex_list[n]
        else:
            return None

    def __contains__(self, item):
        return item in self.vertex_list

    def add_edge(self, f, t, cost=0):
        if f not  in self.vertex_list:
            nv = self.add_vertex(f)
        if t not in self.vertex_list:
            nv = self.add_vertex(t)
        self.vertex_list[f].add_neighbor(self.vertex_list[t], cost)

    def get_vertices(self):
        return self.vertex_list.keys()

    def __iter__(self):
        return iter(self.vertex_list.values())

g = Graph()
for i in range(6):
    g.add_vertex(i)
print(g.vertex_list)
g.add_edge(0, 1, 5)
g.add_edge(0, 5, 2)
g.add_edge(1, 2, 4)
g.add_edge(2, 3, 9)
g.add_edge(3, 4, 7)
g.add_edge(3, 5, 3)
g.add_edge(4, 0, 1)
g.add_edge(5, 4, 8)
g.add_edge(5, 2, 1)

for v in g:
    for w in v.get_connections():
        print("( %s, %s )" % (v.get_id(), w.get_id()))
C:\Users\Sahara\.virtualenvs\test\Scripts\python.exe C:/Users/Sahara/PycharmProjects/test/python_search.py
{0: <__main__.Vertex object at 0x0000000001E83DA0>, 1: <__main__.Vertex object at 0x0000000001E83DD8>, 2: <__main__.Vertex object at 0x0000000001E83E10>, 3: <__main__.Vertex object at 0x0000000001E83E48>, 4: <__main__.Vertex object at 0x0000000001E83E80>, 5: <__main__.Vertex object at 0x0000000001E83EB8>}
( 0, 1 )
( 0, 5 )
( 1, 2 )
( 2, 3 )
( 3, 4 )
( 3, 5 )
( 4, 0 )
( 5, 4 )
( 5, 2 )

Process finished with exit code 0

  

python---使用字典来实现链接表图的更多相关文章

  1. Python中字典和集合

    Python中字典和集合 映射类型: 表示一个任意对象的集合,且可以通过另一个几乎是任意键值的集合进行索引 与序列不同,映射是无序的,通过键进行索引 任何不可变对象都可用作字典的键,如字符串.数字.元 ...

  2. Python的字典

    1.  Python的字典 1.1.  字典的定义 在Python中,字典是一种key-value的数据类型,也是唯一的映射类型:字典还是另一种可变容器类型,且可存储任意类型对象,其中也可包括其他容器 ...

  3. Python的字典和JSON

    Python的字典和JSON在表现形式上非常相似 #这是Python中的一个字典 dic = { 'str': 'this is a string', 'list': [1, 2, 'a', 'b'] ...

  4. python基础--字典

    Python基础--字典 字典的常用函数: dict.clear( )--->无任何返回值 说明: 清除字典内的所有的元素 语法: In [5]: dict.clear? Type: metho ...

  5. Python dictionary 字典 常用法

    Python dictionary 字典 常用法 d = {} d.has_key(key_in)       # if has the key of key_in d.keys()          ...

  6. python有序字典OrderedDict()

    转python创建有序字典OrderedDict # -*- coding:utf-8 -*- """ python有序字典 需导入模块collections " ...

  7. python:字典嵌套列表

    Python的字典{ }以键值对的形式保存数据,可以以键来访问字典中保存的值而不能用下标访问.字典中几乎可以包含任意的变量,字典,数列,元组.数列也一样. python的列表[ ]与字典不同,列表通过 ...

  8. Python的网络编程--思维导图

    Python的网络编程--思维导图

  9. Python的字典类型

    Python的字典类型为dict,用{}来表示,字典存放键值对数据,每个键值对用:号分隔,每个键值对之间用,号分隔,其基本格式如下: d = {key1 : value1, key2 : value2 ...

随机推荐

  1. 【转】python 内置函数总结(大部分)

    [转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...

  2. Linux动态调频系统CPUFreq之一:概述【转】

    转自:https://blog.csdn.net/zhangyongfeiyong/article/details/53506362 随着技术的发展,我们对CPU的处理能力提出了越来越高的需求,芯片厂 ...

  3. python性能分析之cProfile模块

    cProfile是标准库内建的分析工具的其中一个,另外两个是hotshot和profile -s cumulative -s cumulative开关告诉cProfile对每个函数累计花费的时间进行排 ...

  4. 前端清除缓存方法(微信缓存引起的bug)

    bug1:在新版微信中,部门安卓机子(华为)出现window.location.href/window.location.reload....等方法来刷新本页面链接,发现页面没有被刷新,经过排查,发现 ...

  5. 64位程序,利用ADO连接Oracle数据库

        刚好手头项目解决了ADO连接Oracle数据库的问题,记录下来,防止忘记. 项目情况:用32位环境完成算法动态库,结果后来需要升级到64位环境,由64位的软件来调用,则在64位设置下生成算法动 ...

  6. Mysql支持哪几种索引

    从数据结构角度 1.B+树索引(O(log(n))):关于B+树索引,可以参考 MySQL索引背后的数据结构及算法原理 2.hash索引:a 仅仅能满足"=","IN&q ...

  7. python操作三大主流数据库(2)python操作mysql②python对mysql进行简单的增删改查

    python操作mysql②python对mysql进行简单的增删改查 1.设计mysql的数据库和表 id:新闻的唯一标示 title:新闻的标题 content:新闻的内容 created_at: ...

  8. CANopen--Copley驱动器 ACJ-055-18 过程数据PDO的断电保存方法

    Copley CANopen 系列驱动器中,均保存有默认的 PDO 设置, 如下图所示 实际使用中,往往需要修改默认的 PDO 配置,以映射不同模式或功能下的 PDO 对象,但由于 PDO 配置无法存 ...

  9. PYTHON深拷贝与浅拷贝

    浅拷贝就是对引用的拷贝,深拷贝就是对对象的资源的拷贝 浅拷贝 浅拷贝仅仅复制了容器中元素的地址 赋值的原则 1.赋值是将一个对象的地址赋值给一个变量,让变量指向该地址( 旧瓶装旧酒 ). 2.修改不可 ...

  10. 前端 --- 关于DOM的介绍

    111 什么是DOM DOM:文档对象模型.DOM 为文档提供了结构化表示,并定义了如何通过脚本来访问文档结构.目的其实就是为了能让js操作html元素而制定的一个规范. DOM就是由节点组成的. 解 ...