Graphs: relationships as data and algorithms
A graph is relationships as data: nodes connected by edges. Social networks, task dependencies, citations, money moving between accounts, products bought together — all are naturally graphs, and questions like "what's the shortest path?" or "who is most connected?" are awkward on a table but native to a graph.
import networkx as nx
G = nx.DiGraph() # a directed graph
G.add_weighted_edges_from([("a", "b", 1.0),
("b", "c", 2.0)])
You build a graph by adding edges (optionally weighted); recognising when a problem is graph-shaped is itself a valuable skill.
Once a graph is built, you call ready-made algorithms:
nx.shortest_path_length(G, "a", "c", weight="weight") # cheapest route
nx.degree_centrality(G) # how connected each node is
list(nx.connected_components(G.to_undirected())) # the separate clusters
Shortest path finds the cheapest route between two nodes; centrality measures influence by connectedness; connected components find the separate islands in the network. The graph view often turns a hard table query into a one-line call.
dataclasses” and leads into “Reproducibility: environments, containers, seeds”.*
Related cards
Tasks
Card Info
- Topic: Python for Data Science
- Difficulty: Advanced
- Completed: 0 users