This lecture explores how to create, visualize, and filter graphs that store metadata within nodes and edges using Python's networkx library.
A property graph is a directed graph where nodes and edges can have key-value pairs (attributes). This is essential for modeling real-world entities like social networks or organizational structures.
import networkx as nx
import matplotlib.pyplot as plt
def create_and_visualize_properties_graph():
# Initialize a Directed Graph
G = nx.DiGraph()
# Add nodes with 'role' and 'age' attributes
G.add_node("Alice", role="Manager", age=30)
G.add_node("Bob", role="Developer", age=25)
G.add_node("Charlie", role="Designer", age=28)
# Add edges with 'weight' attributes
G.add_edge("Alice", "Bob", weight=5.0)
G.add_edge("Alice", "Charlie", weight=2.0)
G.add_edge("Bob", "Charlie", weight=3.5)
return G
To visualize attributes, we must map internal data to labels and layouts. The spring_layout is commonly used to position nodes based on a force-directed algorithm.
pos = nx.spring_layout(G) to prevent overlap.Filtering allows us to isolate specific subsets of data. Below are the primary methods for querying graph attributes.
Using list comprehensions to find specific data points (e.g., nodes where age > 25):
senior_nodes = [node for node, attrs in G.nodes(data=True) if attrs.get('age', 0) > 25]
A subgraph is a new graph object containing a subset of the original nodes and their existing connections.
G.subgraph() provides a view of the original graph, making it memory efficient.
| Method | Logic | Common Use Case |
|---|---|---|
| Node Filtering | G.nodes(data=True) |
Finding entities by role, age, or status. |
| Edge Filtering | G.edges(data=True) |
Finding "strong" or "weak" connections via weights. |
| Subgraphs | G.subgraph(list) |
Visualizing a specific department or team. |
When working with property graphs, always ensure your attributes are consistent across nodes to avoid KeyError issues during filtering.