Introduction
Imagine trying to understand how people connect and interact in a large community. Social networks are complex, and graphs help us see and analyze these connections clearly.
Think of a neighborhood where each house is a person and the roads between houses are friendships or connections. Some roads are one-way, like a one-way street, while others allow two-way travel. The size or condition of the road shows how strong the connection is.
┌─────────┐ ┌─────────┐
│ PersonA │────▶│ PersonB │
└─────────┘ └─────────┘
▲ │
│ ▼
┌─────────┐ ┌─────────┐
│ PersonC │◀────│ PersonD │
└─────────┘ └─────────┘import networkx as nx # Create a directed graph G = nx.DiGraph() # Add nodes representing people G.add_nodes_from(['Alice', 'Bob', 'Carol', 'Dave']) # Add edges representing connections (Alice follows Bob, Dave follows Carol) G.add_edge('Alice', 'Bob') G.add_edge('Dave', 'Carol') # Add a weighted edge (Carol and Bob communicate frequently) G.add_edge('Carol', 'Bob', weight=5) # Print all edges with weights for u, v, w in G.edges(data='weight'): print(f"{u} -> {v}, weight: {w if w is not None else 1}")