import networkx as nx
Here is a simple initial example:
# Create an empty undirected graph
G = nx.Graph()
# Create some nodes
G.add_node(1) # create a single node
G.add_nodes_from([2,3]) # create nodes from a list
G.add_node("netsci") # node labels can be of different types
# Create some edges
G.add_edge(1,2)
G.add_edges_from([(2,3),(3,"netsci"), ("netsci", 1), (1,3)])
G.add_edges_from([(2,5),(2,6)]) # if a node does not exist, it is created
# Draw the graph
nx.draw(G, with_labels = True)
Graphs can be of different types:
# Here is a simple directed graph
G = nx.DiGraph()
G.add_edges_from([(1,2), (1,3), (2,3)])
nx.draw(G, with_labels=True)
Nodes and edges can have attributes:
G = nx.Graph()
G.add_node(1, Name="Pedro", Number=42)
G.add_edge(1, 2, weight=1.5)
G.add_weighted_edges_from([(2, 3, 3.1), (1, 3, 1.2)])
NetworkX graph objects can be created in one of three ways:
G = nx.complete_graph(5) # a clique of size 5
nx.draw(G, with_labels=True)
G = nx.karate_club_graph()
nx.draw(G, with_labels=True)
# You can get this file at https://github.com/gephi/gephi/wiki/Datasets
G = nx.read_gml("lesmiserables.gml")
nx.draw(G, with_labels=True)
Graphs are essentially a "dictionary of dictionaries"
G = nx.complete_graph(4)
print(G)
print("nodes:", G.nodes)
print("edges:", G.edges)
print("adj:", G.adj)
print("degree:", G.degree)
print("Properties of node 0:")
print(" - neighbors:", list(G.adj[0]));
print(" - degree:", G.degree[0]);
print("Edges of nodes 1 and 2:", G.edges([1,2]))
print("Degrees nodes 1 and 2:", G.degree([1,2]))
print("Neighbors of node 1", G[1]) # same as G.adj[1]
Graph with 4 nodes and 6 edges nodes: [0, 1, 2, 3] edges: [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)] adj: {0: {1: {}, 2: {}, 3: {}}, 1: {0: {}, 2: {}, 3: {}}, 2: {0: {}, 1: {}, 3: {}}, 3: {0: {}, 1: {}, 2: {}}} degree: [(0, 3), (1, 3), (2, 3), (3, 3)] Properties of node 0: - neighbors: [1, 2, 3] - degree: 3 Edges of nodes 1 and 2: [(1, 0), (1, 2), (1, 3), (2, 0), (2, 3)] Degrees nodes 1 and 2: [(1, 3), (2, 3)] Neighbors of node 1 {0: {}, 2: {}, 3: {}}
We can also use graph operations to obtain new graphs:
G1 = nx.cycle_graph(4)
G2 = nx.complement(G1)
G3 = nx.subgraph(G1, [1,2,3])
print("G1:", G1.edges)
print("G2:", G2.edges)
print("G3:", G3.edges)
G1: [(0, 1), (0, 3), (1, 2), (2, 3)] G2: [(0, 2), (1, 3)] G3: [(1, 2), (2, 3)]
There are many algorithms you can choose to analyzing a graph (see Algorithms):
G = nx.barabasi_albert_graph(10,2)
nx.draw(G, with_labels=True)
print("Diameter:", nx.diameter(G))
print("Nr Components:", nx.number_connected_components(G))
print("Clustering coefficient:", nx.clustering(G))
print("Betweenness Centrality:", nx.betweenness_centrality(G))
Diameter: 3 Nr Components: 1 Clustering coefficient: {0: 0.26666666666666666, 1: 0, 2: 0.26666666666666666, 3: 1.0, 4: 0, 5: 1.0, 6: 0.3333333333333333, 7: 0.16666666666666666, 8: 0, 9: 0} Betweenness Centrality: {0: 0.2847222222222222, 1: 0.013888888888888888, 2: 0.287037037037037, 3: 0.0, 4: 0.08101851851851852, 5: 0.0, 6: 0.07870370370370369, 7: 0.21527777777777773, 8: 0.023148148148148143, 9: 0.016203703703703703}