1. 程式人生 > >NetworkX的基本用法(轉)

NetworkX的基本用法(轉)

在PythonWin 的Shell裡輸入:

import networkx as nx                            #匯入NetworkX包,為了少打幾個字母,將其重新命名為nx
G = nx.Graph()                                        #建立一個空的無向圖G
G.add_node(1)                                        #新增一個節點1
G.add_edge(2,3)                                     #新增一條邊2-3(隱含著添加了兩個節點2、3)

G.add_edge(3,2)                                     #對於無向圖,邊3-2與邊2-3被認為是一條邊
print G.nodes()                                       #輸出全部的節點: [1, 2, 3]
print G.edges()                                       #輸出全部的邊:[(2, 3)]
print G.number_of_edges()                    #輸出邊的數量:1