>>> import networkx as nx
>>> G=nx.Graph()
>>> G.add_node("spam")
>>> G.add_edge(1,2)
>>> print(G.nodes())
[1, 2, 'spam']
>>> print(G.edges())
[(1, 2)]
最短路徑
>>> g=nx.Graph()
>>> g.add_edge(1,2)
>>> g.add_edge(1,4)
>>> g.add_edge(2,3)
>>> g.add_edge(2,4)
>>> g.add_edge(3,4)
>>> length=nx.single_source_shortest_path_length(G,1)
>>> length
{0: 1, 1: 0, 2: 1, 3: 2, 4: 3}
>>> length=nx.all_pairs_shortest_path_length(G)
>>> length
{0: {0: 0, 1: 1, 2: 2, 3: 3, 4: 4}, 1: {0: 1, 1: 0, 2: 1, 3: 2, 4: 3}, 2: {0: 2, 1: 1, 2: 0, 3: 1, 4: 2}, 3: {0: 3, 1: 2, 2: 1, 3: 0, 4: 1}, 4: {0: 4, 1: 3, 2: 2, 3: 1, 4: 0}}
>>>
>>> nx.bidirectional_shortest_path(G,1,2)
[1, 2]
沒有留言:
張貼留言