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.
In plain Python you can store a directed weighted graph as a dict of dicts: graph[u][v] = weight means an edge from u to v. Building that structure from an edge list is enough for many course exercises; specialised libraries exist, but the idea does not depend on them.
graph = {}
for u, v, w in edges:
graph.setdefault(u, {})[v] = w
Shortest path on non-negative weights is Dijkstra: keep the best known distance to each node, and always expand the closest unsettled node next. Centrality and connected components are the same idea at larger scale: walk the adjacency data instead of joining tables.
Related cards
Tasks
Card Info
- Topic: Python for Data Science
- Difficulty: Advanced
- Completed: 0 users