NetworkX: Network Analysis in Python#

networkx logo

NetworkX is an open-source Python library designed for creating, manipulating, and analyzing networks made up of nodes and edges. It provides an extensive array of tools and algorithms for general graph operations, catering to both directed and undirected graphs. Beyond basic graph creation and visualization, NetworkX supports a multitude of graph algorithms, ranging from shortest path computations to more complex structural evaluations, making it a versatile toolkit for researchers, developers, and data scientists working in the domain of network analysis.

On the other hand, CORNETO’s graph class has a specialized focus. While NetworkX is geared towards using standard graph methods, CORNETO is all about building optimization problems using graphs. The Graph class implemented in CORNETO is more flexible, and supports directed, undirected edges, parallel edges, hyperedges, and all mixed in the same graph.

Despite the differences in their primary uses, CORNETO and NetworkX can work together. Here, we will see an example of this interoperability between CORNETO and NetworkX.

# Create a networkx graph
import networkx as nx

# Create a graph object
G = nx.Graph()

# Add nodes
G.add_nodes_from([1, 2, 3, 4, 5])

# Add edges with weights
G.add_edge(1, 2, weight=1)
G.add_edge(1, 3, weight=4)
G.add_edge(2, 3, weight=2)
G.add_edge(3, 4, weight=1)
G.add_edge(4, 5, weight=3)
G.add_edge(1, 5, weight=8)
G.add_edge(2, 4, weight=5)

# Compute shortest paths from node 1 to all other nodes
shortest_paths = nx.single_source_dijkstra_path(G, source=1)
shortest_path_lengths = nx.single_source_dijkstra_path_length(G, source=1)

# Print the paths and their lengths
print("Shortest paths from node 1:")
for target, path in shortest_paths.items():
    print(
        f"Node 1 to node {target}: Path: {path} with total weight: {shortest_path_lengths[target]}"
    )
Shortest paths from node 1:
Node 1 to node 1: Path: [1] with total weight: 0
Node 1 to node 2: Path: [1, 2] with total weight: 1
Node 1 to node 3: Path: [1, 2, 3] with total weight: 3
Node 1 to node 5: Path: [1, 2, 3, 4, 5] with total weight: 7
Node 1 to node 4: Path: [1, 2, 3, 4] with total weight: 4
# Create a corneto graph
import corneto as cn

Gc = cn.Graph()
Gc.add_edge(1, 2, weight=1)
Gc.add_edge(1, 3, weight=4)
Gc.add_edge(2, 3, weight=2)
Gc.add_edge(3, 4, weight=1)
Gc.add_edge(4, 5, weight=3)
Gc.add_edge(1, 5, weight=8)
Gc.add_edge(2, 4, weight=5)
Gc.shape
(5, 7)
from corneto.contrib.networkx import corneto_graph_to_networkx

Gcn = corneto_graph_to_networkx(Gc)
Gcn.nodes()
NodeView((1, 2, 3, 4, 5))

Corneto automatically transforms the graph to a networkx graph when using the networkx API

from corneto.contrib.networkx import networkx as nxc

# NOTE: everytime a corneto graph is passed to a networkx function, it is converted to a networkx graph
# Think about converting first the graph to a networkx graph and then using networkx functions for better performance
shortest_paths = nxc.single_source_dijkstra_path(Gc, source=1)
shortest_path_lengths = nxc.single_source_dijkstra_path_length(Gc, source=1)

# Print the paths and their lengths
print("Shortest paths from node 1:")
for target, path in shortest_paths.items():
    print(
        f"Node 1 to node {target}: Path: {path} with total weight: {shortest_path_lengths[target]}"
    )
Shortest paths from node 1:
Node 1 to node 1: Path: [1] with total weight: 0
Node 1 to node 2: Path: [1, 2] with total weight: 1
Node 1 to node 3: Path: [1, 2, 3] with total weight: 3
Node 1 to node 5: Path: [1, 2, 3, 4, 5] with total weight: 7
Node 1 to node 4: Path: [1, 2, 3, 4] with total weight: 4
G = cn.Graph()
G.add_edge((), "A")
G.add_edge("A", "B")
G.add_edge("B", ())
G.plot()
../../_images/25dad3b2c5be1f1cff7e8ab23198a801ebaa30df366d4d32ff39bd56236ab5af.svg
G_nx = corneto_graph_to_networkx(G, skip_unsupported_edges=True)
G_nx.nodes()
NodeView(('A', 'B'))