Network Analysis with Python


>>> print nx.shortest_path(g, 'b', 'd')



Yüklə 1,5 Mb.
Pdf görüntüsü
səhifə3/12
tarix22.03.2023
ölçüsü1,5 Mb.
#89129
1   2   3   4   5   6   7   8   9   ...   12
l109-tutorial 2015

>>> print nx.shortest_path(g, 'b', 'd')
['b', 'c', 'd']
>>> print nx.shortest_path(g, 'b', 'd', weight='weight')
['b', 'a', 'c', 'd']


Introduction: drawing and plotting
• It is possible to draw small graphs with NetworkX. You can export network data 
and draw with other programs (GraphViz, Gephi, etc.).
10


Introduction: NetworkX official website
11
http://networkx.github.io/


2. Getting started with Python and NetworkX
12


Getting started: the environment
• Start Python (interactive or script mode) and import NetworkX
• Different classes exist for directed and undirected networks. Let’s create a basic 
undirected Graph:
• The graph g can be grown in several ways. NetworkX provides many generator 
functions and facilities to read and write graphs in many formats.
$ python
>>> import networkx as nx
>>> g = nx.Graph() # empty graph
13


Getting started: adding nodes
# One node at a time
>>> g.add_node(1)
# A list of nodes
>>> g.add_nodes_from([2, 3])
# A container of nodes
>>> h = nx.path_graph(5)
>>> g.add_nodes_from(h)
# You can also remove any node of the graph
>>> g.remove_node(2)
14


Getting started: node objects
• A node can be any hashable object such as a string, a function, a file and more.

Yüklə 1,5 Mb.

Dostları ilə paylaş:
1   2   3   4   5   6   7   8   9   ...   12




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©www.azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin