In [1]:
# 代码来自 https://www.rustworkx.org/dev/install.html

import rustworkx as rx

graph = rx.PyGraph()

# Each time add node is called, it returns a new node index
a = graph.add_node("A")
b = graph.add_node("B")
c = graph.add_node("C")

# add_edges_from takes tuples of node indices and weights,
# and returns edge indices
graph.add_edges_from([(a, b, 1.5), (a, c, 5.0), (b, c, 2.5)])

# Returns the path A -> B -> C
shortest_path = rx.dijkstra_shortest_paths(graph, a, c, weight_fn=float)
In [2]:
type(shortest_path)
Out[2]:
rustworkx.PathMapping
In [3]:
dir(shortest_path)
Out[3]:
['__class__',
 '__contains__',
 '__delattr__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__getstate__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__setstate__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'items',
 'keys',
 'values']
In [4]:
import inspect

inspect.getmro(type(shortest_path))
Out[4]:
(rustworkx.PathMapping, object)
In [5]:
print(shortest_path)
PathMapping{2: [0, 1, 2]}
In [6]:
iter(shortest_path)
Out[6]:
<rustworkx.PathMappingKeys at 0x106727c30>
In [7]:
next(iter(shortest_path))
Out[7]:
2
In [8]:
shortest_path[next(iter(shortest_path))]
Out[8]:
NodeIndices[0, 1, 2]
In [9]:
list(shortest_path[next(iter(shortest_path))])
Out[9]:
[0, 1, 2]
In [10]:
print('shortest path: ')
for node in list(shortest_path[next(iter(shortest_path))]):
    print(graph.get_node_data(node))
shortest path: 
A
B
C
In [11]:
a = {10: 9, 8: 7, 6: 5, 4: 3, 100: 10}
In [12]:
next(iter(a))
Out[12]:
10
In [ ]: