简体   繁体   English

邻接表在 python 中的图形表示

[英]Graph Representation in python by adjacency list

I want to represent a directed graph with weights in Python.我想用 Python 中的权重表示一个有向图。

The most important thing for me is the efficiency of time and memory.对我来说最重要的是时间效率和memory。

The aim of the graph is to find the shortest path by dijkstra algorithm.该图的目的是通过dijkstra算法找到最短路径。

I am undecided between existing libaries such as networkx, igraph and graph-tool (or others) or through self-implementation.我在networkx、igraph和graph-tool(或其他)等现有库之间还是通过自我实现犹豫不决。

Which package would best fit and is based on a adjacency list and not on a adjacency matrix?哪个 package 最适合并且基于邻接列表而不是邻接矩阵?

Another question- Is there an option in one of the above packages to save multiple options for edges weights and then run dijkstra according to the preferred weight selection.另一个问题-上述软件包中是否有一个选项可以保存边缘权重的多个选项,然后根据首选权重选择运行 dijkstra。

Thank you!谢谢!

You can use defaultdict, having tuple of edge weight您可以使用 defaultdict,具有边缘权重的元组

graph = { key1: [(neighbour1, weight1), (neighbour12, weight2)] .... }

for multiple weights you can take weight as list and just handle which weights to use对于多个权重,您可以将权重作为列表并仅处理要使用的权重

graph = { key1: [(neighbour1, [weight11, weight12 ... weight1n]), .... }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM