简体   繁体   中英

Graph Representation in python by adjacency list

I want to represent a directed graph with weights in Python.

The most important thing for me is the efficiency of time and memory.

The aim of the graph is to find the shortest path by dijkstra algorithm.

I am undecided between existing libaries such as networkx, igraph and graph-tool (or others) or through self-implementation.

Which package would best fit and is based on a adjacency list and not on a adjacency matrix?

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.

Thank you!

You can use defaultdict, having tuple of edge weight

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]), .... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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