• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python数据结构之图的实现方法

python数据结构之图的实现方法

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python数据结构,python数据结构与算法,数据结构python版,python数据结构pdf,python有哪些数据结构等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了python数据结构之图的实现方法。分享给大家供大家参考。具体如下:

下面简要的介绍下:

比如有这么一张图:

    A -> B
    A -> C
    B -> C
    B -> D
    C -> D
    D -> C
    E -> F
    F -> C

可以用字典和列表来构建

graph = {'A': ['B', 'C'],
       'B': ['C', 'D'],
       'C': ['D'],
       'D': ['C'],
       'E': ['F'],
       'F': ['C']}
</div>

找到一条路径:

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

</div>

找到所有路径:

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

</div>

找到最短路径:

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

</div>

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • 基于python的七种经典排序算法(推荐)
  • python数据结构之图的实现方法
  • Python常用列表数据结构小结

相关文章

  • Python实现获取操作系统版本信息方法
  • Python实现以时间换空间的缓存替换算法
  • 使用IPython来操作Docker容器的入门指引
  • Python获取文件所在目录和文件名的方法
  • Python Web开发模板引擎优缺点总结
  • python executemany的使用及注意事项
  • 在Python中使用第三方模块的教程
  • Python实现的Kmeans++算法实例
  • Python常用模块用法分析
  • python3中dict(字典)的使用方法示例

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 关于python的bottle框架跨域请求报错问题的处理方法
    • Python中bisect的用法
    • python dict remove数组删除(del,pop)
    • 实例解析Python设计模式编程之桥接模式的运用
    • 在漏洞利用Python代码真的很爽
    • Python获取apk文件URL地址实例
    • 使用Python的Tornado框架实现一个一对一聊天的程序
    • python中的五种异常处理机制介绍
    • Python常见文件操作的函数示例代码
    • Python 数据结构之旋转链表

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有