Do you want to represent networks of communication, data organization, computational devices, the flow of computation, etc in your GUI App? This post will get to understand how to use NetworkX Python Library using Python4Delphi in Delphi/C++ application with some basic NetworkX manipulations. NetworkX is a Python package for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks.

The NetworkX package provides classes for graph objects, generators to create standard graphs, IO routines for reading in existing datasets, algorithms to analyze the resulting networks, and some basic drawing tools.

Python for Delphi (P4D) is a set of free components that wrap up the Python DLL into Delphi. They let you easily execute Python scripts, create new Python modules and new Python types. You can use Python4Delphi a number of different ways such as:

- Create a Windows GUI around your existing Python app.
- Add Python scripting to your Delphi Windows apps.
- Add parallel processing to your Python apps through Delphi threads.
- Enhance your speed-sensitive Python apps with functions from Delphi for more speed.

**Prerequisites.**

- If not python and Python4Delphi is not installed on your machine, Check this how to run a simple python script in Delphi application using Python4Delphi sample app
- Open windows open command prompt, and type
**pip install -U networkx**to install NetworkX. For more info for Installing Python Modules check here - Alternatively, you can type
**pip install networkx[all]**to include other optional packages. - First, run the Demo1 project for executing Python script in Python for Delphi. Then load the Texblob sample script in the Memo1 field and press the
**Execute Script**button to see the result. On Clicking**Execute**Button the script strings are executed using the below code. Go to GitHub to download the**Demo1**source.

1 2 3 4 |
procedure TForm1.Button1Click(Sender: TObject); begin PythonEngine1.ExecStrings( Memo1.Lines ); end; |

**Key NetworkX Concepts**

A Graph consists of a finite set of vertices(or **nodes**) and a set of **edges **that connect a pair of nodes. Methods of the graph object are limited to basic manipulation and reporting. The following basic graph types are provided as Python classes:

- Graph – This class implements an undirected graph. It ignores multiple edges between two nodes. It does allow self-loop edges between a node and itself.
- DiGraph – Directed graphs, that is, graphs with directed edges. Provides operations common to directed graphs, (a subclass of Graph).
- MultiGraph – A flexible graph class that allows multiple undirected edges between pairs of nodes. The additional flexibility leads to some degradation in performance, though usually not significant.
- MultiDiGraph – A directed version of a MultiGraph.

**NetworkX Python Library sample script** **details**:

- Creation of undirected graph and Digraph.
- Adding Nodes one at a time, from any iterable container such as a list, adding nodes with node attributes. Also, add a node from another graph.
- Adding Edges one at a time, from any iterable container such as a list.
- Examining the elements of the group by listing a number of nodes and edges in a graph, node list, edge list, finding the neighbor node, degree of the graph, etc.
- How to remove nodes, edges, and clear both from the graph.
- Draw a cubical graph using
**matplotlib**library.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
import networkx as nx import matplotlib.pyplot as plt # No Nodes and No edges G = nx.Graph() # Add Nodes one at a time G.add_node(1) # Add nodes from any iterable container G.add_nodes_from([2,3]) # add nodes along with node attributes G.add_nodes_from([(4,{"color":"red"}),(5,{"color":"green"})]) #Node from one graph H = nx.path_graph(6) G.add_nodes_from(H)# G now contains the nodes of H as nodes of G . You could see the graph H as a node in G # Adding Edges G.add_edge(1, 2) e = (2,3) G.add_edge(*e) #unpack edge tuple # edges from list G.add_edges_from([(3,4),(4,5)]) # Examining elements of a group G.number_of_nodes() G.number_of_edges() print(list(G.nodes)) print(list(G.edges)) #neighbours of node 1 print(list(G.adj[1])) # the number of edges incident to 1 print(G.degree[1]) G.remove_node(2) G.remove_nodes_from([3,4]) print(list(G.nodes)) #remove all nodes and edges G.clear() #Directed Graphs print('*****DirectedGraph******') DG = nx.DiGraph() DG.add_weighted_edges_from([(1, 2, 0.5), (3, 1, 0.75)]) DG.out_degree(1, weight='weight') print(list(DG.successors(1))) print(list(DG.neighbors(1))) #Drawing print('*****Drawing******') G = nx.cubical_graph() plt.subplot(121) nx.draw(G) # default spring_layout plt.subplot(122) nx.draw(G, pos=nx.circular_layout(G), node_color='r', edge_color='b') plt.show() |

Note: Samples used for demonstration were picked from here with only the difference of printing the outputs. You can check the APIs and some more samples from the same place.

You have read the quick overview of NetworkX library, download this library from here, and easily generate graphs, manipulate graph operations, use numerical graph algorithms and draw the graphs in your applications. Check out Python4Delphi and easily build Python GUIs for Windows using Delphi.