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.
- 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.
procedure TForm1.Button1Click(Sender: TObject);
PythonEngine1.ExecStrings( Memo1.Lines );
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.
import networkx as nx
import matplotlib.pyplot as plt
# No Nodes and No edges
G = nx.Graph()
# Add Nodes one at a time
# Add nodes from any iterable container
# add nodes along with node attributes
#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
e = (2,3)
G.add_edge(*e) #unpack edge tuple
# edges from list
# Examining elements of a group
#neighbours of node 1
# the number of edges incident to 1
#remove all nodes and edges
DG = nx.DiGraph()
DG.add_weighted_edges_from([(1, 2, 0.5), (3, 1, 0.75)])
G = nx.cubical_graph()
nx.draw(G) # default spring_layout
nx.draw(G, pos=nx.circular_layout(G), node_color='r', edge_color='b')
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.