--- id: wiki-2026-0508-graph-theory title: Graph Theory category: 10_Wiki/Topics status: verified canonical_id: self aliases: [그래프 이론, Graphs] duplicate_of: none source_trust_level: A confidence_score: 0.9 verification_status: applied tags: [algorithms, graphs, cs] raw_sources: [] last_reinforced: 2026-05-10 github_commit: pending tech_stack: language: python framework: networkx --- # Graph Theory ## 매 한 줄 > **"매 vertex + edge 의 abstraction 으로 매 거의 모든 관계를 model"**. 매 Euler (1736 Königsberg) 부터 매 modern Graph Neural Network (2026 GraphSAGE, GAT, Graph Transformer) 까지 — 매 search engine PageRank, social network, dependency graph, GNN, knowledge graph 의 foundation. ## 매 핵심 ### 매 representations - **Adjacency matrix**: O(V²) space, O(1) edge query — 매 dense graph. - **Adjacency list**: O(V+E) space — 매 sparse 일반. - **Edge list**: 매 streaming / disk-based. - **CSR (Compressed Sparse Row)**: 매 cache-friendly, GNN framework 의 표준. ### 매 핵심 algorithm - **BFS** O(V+E) — 매 unweighted shortest path, level order. - **DFS** O(V+E) — 매 cycle detection, topo sort, SCC. - **Dijkstra** O((V+E) log V) — 매 non-negative weighted shortest path. - **Bellman-Ford** O(VE) — 매 negative weight allowed. - **Floyd-Warshall** O(V³) — 매 all-pairs. - **Union-Find** ~ O(α(V)) — 매 connectivity, Kruskal MST. - **Topological sort** — 매 DAG ordering. - **PageRank** — 매 power iteration on stochastic matrix. ### 매 응용 1. Routing / map — 매 OSRM, Valhalla, Google Maps. 2. Dependency resolution — npm, cargo, Bazel. 3. GNN — 매 fraud detection, drug discovery, recommendation (Pinterest PinSage). 4. Knowledge graph — 매 RAG / LLM grounding. ## 💻 패턴 ### Adjacency list (Python) ```python from collections import defaultdict g: dict[int, list[int]] = defaultdict(list) for u, v in edges: g[u].append(v); g[v].append(u) ``` ### BFS shortest path ```python from collections import deque def bfs(g, s, t): q = deque([(s, 0)]); seen = {s} while q: u, d = q.popleft() if u == t: return d for v in g[u]: if v not in seen: seen.add(v); q.append((v, d+1)) return -1 ``` ### Dijkstra (heap) ```python import heapq def dijkstra(g, s): dist = {s: 0}; pq = [(0, s)] while pq: d, u = heapq.heappop(pq) if d > dist[u]: continue for v, w in g[u]: nd = d + w if nd < dist.get(v, float("inf")): dist[v] = nd; heapq.heappush(pq, (nd, v)) return dist ``` ### Topological sort (Kahn) ```python from collections import deque def topo(n, edges): g = [[] for _ in range(n)]; indeg = [0]*n for u, v in edges: g[u].append(v); indeg[v] += 1 q = deque(i for i in range(n) if indeg[i] == 0); out = [] while q: u = q.popleft(); out.append(u) for v in g[u]: indeg[v] -= 1 if indeg[v] == 0: q.append(v) return out if len(out) == n else None # cycle ``` ### Union-Find (path compression + rank) ```python class DSU: def __init__(self, n): self.p = list(range(n)); self.r = [0]*n def find(self, x): while self.p[x] != x: self.p[x] = self.p[self.p[x]]; x = self.p[x] return x def union(self, a, b): ra, rb = self.find(a), self.find(b) if ra == rb: return False if self.r[ra] < self.r[rb]: ra, rb = rb, ra self.p[rb] = ra if self.r[ra] == self.r[rb]: self.r[ra] += 1 return True ``` ### NetworkX (Python prototyping) ```python import networkx as nx G = nx.DiGraph(); G.add_weighted_edges_from([("a","b",1), ("b","c",2)]) print(nx.shortest_path(G, "a", "c", weight="weight")) print(nx.pagerank(G, alpha=0.85)) ``` ### GNN (PyTorch Geometric) ```python import torch from torch_geometric.nn import GCNConv class GCN(torch.nn.Module): def __init__(self, d_in, d_h, d_out): super().__init__() self.c1 = GCNConv(d_in, d_h); self.c2 = GCNConv(d_h, d_out) def forward(self, x, edge_index): x = self.c1(x, edge_index).relu() return self.c2(x, edge_index) ``` ## 매 결정 기준 | 상황 | Approach | |---|---| | Unweighted shortest | BFS | | Non-negative weighted | Dijkstra | | Negative weight | Bellman-Ford / Johnson | | All-pairs, dense, V<500 | Floyd-Warshall | | MST | Kruskal (DSU) / Prim (heap) | | DAG scheduling | Topological sort | | Cycle / SCC | DFS + Tarjan / Kosaraju | | Massive web graph | Pregel-style (GraphX, Giraph) | | Learning on graph | GNN (PyG, DGL) | **기본값**: prototype 은 NetworkX, production 은 graph-tool / igraph / 직접 CSR. ## 🔗 Graph - 변형: [[DAG]] - 응용: [[Shortest Path]] · [[GNN]] · [[Knowledge Graph]] ## 🤖 LLM 활용 **언제**: 매 problem 을 graph 로 model 가능한지 reasoning, 매 algorithm 선택. **언제 X**: 매 V, E 가 매우 작은 brute-force 충분 case — 매 over-engineering. ## ❌ 안티패턴 - **Adjacency matrix on sparse**: 매 V=10⁶ 에 V² 메모리 폭발. - **Recursive DFS on deep graph (Python)**: 매 stack overflow — iterative. - **Dijkstra with negative edges**: 매 wrong answer — Bellman-Ford. - **Re-running BFS from each node for diameter**: 매 BFS×V — multi-source / approx 사용. - **Forgetting visited set**: 매 BFS/DFS 무한 loop. ## 🧪 검증 / 중복 - Verified (CLRS *Introduction to Algorithms* 4th ed., Sedgewick *Algorithms* 4ed, NetworkX docs, PyG 2.5). - 신뢰도 A. ## 🕓 Changelog | 날짜 | 변경 | |---|---| | 2026-05-08 | Phase 1 | | 2026-05-10 | Manual cleanup — graph algorithms + GNN 응용 정리 |