Graph optimisation
도입됨
5:37
알고리즘 설계 및 분석
This course teaches you to price an algorithm's design before you trust it with real data. The first two units cover analysis tools and divide and conquer. The…
영어 내레이션 · 영어와 중국어 자막 전환 가능
이 수업의 내용
| English | 의미 |
|---|---|
| cut/kʌt/ | a split of a graph's vertices into two groups |
| minimum spanning tree/ˈmɪnɪməm ˈspænɪŋ triː/ | a set of edges that connects all vertices with the smallest total weight and no cycle |
| cut property/kʌt ˈprɒpəti/ | the lightest edge crossing any cut belongs to some minimum spanning tree |
| Kruskal's algorithm/ˈkrʌsklz ˈælɡərɪθəm/ | builds a minimum spanning tree by adding the lightest edges that do not form a cycle |
| Prim's algorithm/prɪmz ˈælɡərɪθəm/ | grows a minimum spanning tree from one vertex, always adding the lightest edge leaving the tree |
| union–find/ˈjuːnɪən faɪnd/ | a structure that tracks which items are in the same set and joins sets quickly |
| topological order/ˌtɒpəˈlɒdʒɪkl ˈɔːdə/ | an order of a directed acyclic graph's vertices in which every edge points forwards |
Light edges across cuts
- A cut 割 splits the vertices into two groups. A lightest edge crossing it belongs to some minimum spanning tree 最小生成树 (MST). It is safe to add to a partial spanning forest when the cut respects that forest, so no selected edge crosses the cut.
- This cut property 割性质 is behind Kruskal's algorithm 克鲁斯卡尔算法 (sort the edges, accept each one unless it makes a cycle) and Prim's algorithm (below). Dijkstra's settle step uses a similar "the closest one is safe" argument, but for distances, not tree weight.

The light edge crossing a cut is —
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
Tree + light edge - cycle other edge keeps or lowers the weight.
Kruskal on the course graph
- Sorted: C–B 1, A–C 2, D–E 2, E–F 3, A–B 4, B–D 5, D–F 6, C–D 8, C–E 10. Accept C–B, A–C, D–E, E–F and B–D; reject the rest, because each would close a cycle.
- The MST weight is $1+2+2+3+5 = 13$. The proof: take any tree, add a light crossing edge, and remove the other crossing edge on the cycle; the weight stays the same or falls.
Kruskal on the course graph yields MST weight —
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
CB 1 + AC 2 + DE 2 + EF 3 + BD 5 = 13.
Negative edges: fine for trees, fatal for Dijkstra
- Dijkstra settles a node and never looks at it again. On A→B (2), A→C (5), C→B (−4), it settles B at 2 before C, and misses the path A→C→B of length 1. Settling is safe only if no later edge can make a path shorter, which needs non-negative weights.
- For shortest paths with negative edges, use Bellman–Ford: $V - 1$ rounds of updating every edge, $\Theta(VE)$, which also detects negative cycles. Minimum spanning trees are different: Kruskal and Prim only compare weights, so they work with negative weights too.
Kruskal's algorithm still finds a minimum spanning tree when some edge weights are negative.
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
Kruskal only sorts and compares weights, and the cut property holds for any weights. It is Dijkstra's shortest-path settling that breaks with negative edges.
With directed edges A→B 2, A→C 5, C→B −4, Dijkstra from A settles B at —
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
B (2) is nearer than C (5), so B is settled first, at 2. The path A→C→B of length 1 is found too late; use Bellman-Ford.
Dijkstra's algorithm finds correct shortest paths whenever the graph has no negative cycle, even if some edges are negative.
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
Dijkstra settles a node for good, so a negative edge found later cannot correct it: on A→B (2), A→C (5), C→B (−4) it settles B at $2$ and misses the path of length $1$ through C. Bellman-Ford handles negative edges.
Prim, and fast cycle checks
- Prim's algorithm 普里姆算法 grows one tree from a start vertex, always adding the lightest edge that leaves the tree. From A on the course graph: A–C $2$, C–B $1$, B–D $5$, D–E $2$, E–F $3$. The total is $13$, the same tree as Kruskal's, as the cut property promises.
- Kruskal's cycle test uses union–find 并查集: each component has a root, and an edge is rejected when both ends have the same root. With union by rank and path compression each operation is almost constant, so Kruskal costs $O(E \log E)$, set by the sort.
How many edges does a minimum spanning tree of a connected 12-vertex graph have?
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
Every spanning tree on V vertices has V - 1 edges.
Prim starts at A on the course graph (A–B 4, A–C 2, ...). Which edge does it add first?
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
Only edges leaving the tree {A} count, and A–C is the lightest of them.
Topological order first
- A topological order 拓扑序 of a directed acyclic graph lists every vertex before the vertices its edges point to: course prerequisites, build steps. Kahn's algorithm keeps removing a vertex with no incoming edges, in $\Theta(V + E)$.
- If vertices remain but none of them is free, the graph has a cycle and no order exists.
A directed graph containing a cycle has a topological order.
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
Every vertex on the cycle would have to come before itself.
Worked example: shortest paths in a DAG
- Edges A→B 3, A→C 6, B→C −2, B→D 4, C→D 1. Update the distances in topological order A, B, C, D: B = 3, C = $\min(6, 3 - 2) = 1$, D = $\min(3 + 4, 1 + 1) = \mathbf{2}$.
- One pass, $\Theta(V + E)$, and negative edges are fine, because each vertex is final before its edges are used. For all pairs in a general graph, Floyd–Warshall takes $\Theta(V^3)$: $10^6$ updates for 100 vertices.
DAG A→B 3, A→C 6, B→C −2, B→D 4, C→D 1: shortest distance A to D?
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
A→B→C→D: $3 - 2 + 1 = 2$.
Floyd–Warshall on 100 vertices: how many distance updates ($V^3$)?
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
$100^3 = 10^6$.
Marks that slip away
- Checking Kruskal's cycles by eye. Use union–find: an edge whose ends are already in one set closes a cycle and is rejected.
- Using the MST as a shortest-path tree. It minimises the total weight, not the distance between two nodes.
- Thinking Prim's start vertex matters. Every start gives a tree of the same total weight, $13$ on the course graph.
Recap
- Cut property: the lightest edge crossing any cut is safe.
- Course MST: C–B, A–C, D–E, E–F, B–D, weight $13$.
- Two different questions on a weighted graph:
| Shortest paths | Minimum spanning tree | |
|---|---|---|
| Finds | cheapest route from one source | cheapest set of edges joining all |
| Algorithms | Dijkstra; Bellman–Ford | Kruskal; Prim |
| Negative weights | Dijkstra fails; use Bellman–Ford | no problem |
어떤 문제가 있었나요? 사이트 팀만 이 내용을 볼 수 있습니다.