NetworkX is a powerful Python library for creating, manipulating, and studying the structure, dynamics, and functions of complex networks. Its simplicity and versatility make it an indispensable tool for numerous domains, from social network analysis to biological systems modeling. Let's explore some practical examples to illustrate its capabilities.

Before diving into examples, ensure you have NetworkX installed. If not, you can install it using pip: `pip install networkx`.

Creating and Visualizing Networks
NetworkX provides intuitive ways to create networks and visualize them using its built-in functions and external libraries like matplotlib and Graphviz.

Let's create a simple undirected graph with Rock, Paper, and Scissors nodes and visualize it using Graphviz.
Undirected Graph

First, import the necessary libraries and create an undirected graph using `networkx.Graph()`.
```python import networkx as nx import graphviz G = nx.Graph() G.add_edge('Rock', 'Paper') G.add_edge('Paper', 'Scissors') G.add_edge('Rock', 'Scissors') ```
Now, let's create a visualization using graphviz:

```python dot = graphviz.Dot() dot.node_attr.update(color='lightblue2') for node in G.nodes: dot.node(node) for edge in G.edges: dot.edge(edge[0], edge[1]) dot.view() ```
Directed Graph
A directed graph (digraph) is useful when tracking the direction of relationships. Let's create a directed graph to represent word associations.

```python G_directed = nx.DiGraph() G_directed.add_edge('Apple', 'Banana') G_directed.add_edge('Banana', 'Apple') G_directed.add_edge('Apple', 'Tree') nx.draw(G_directed, with_labels=True, node_color='lightblue', edge_color='black') ```
Analyzing Network Properties









NetworkX offers various functions to analyze network properties, such as degree centrality, betweenness centrality, and clustering coefficient.
Degree Centrality
The degree centrality of a node is the number of edges connected to it. Let's calculate the degree centrality of our Rock-Paper-Scissors network.
```python degree_centrality = nx.degree_centrality(G) print(degree_centrality) ```
Betweenness Centrality
Betweenness centrality measures the number of times a node lies on the shortest path between other nodes. It helps identify nodes that play a vital role in connecting other parts of the network.
```python betweenness_centrality = nx.betweenness_centrality(G) print(betweenness_centrality) ```
Exploring NetworkX's capabilities is an ongoing journey filled with discovering new ways to model and analyze networks. Whether you're a researcher, data scientist, or just curious about network analysis, NetworkX provides an excellent starting point. Happy network毫无疑عان!