Oh My Algorithm
Algorithm Guidecomplexity: O(E log V)

프림 (Prim MST)

한 노드에서 시작해 트리에 연결된 가장 싼 간선을 하나씩 더해 가며 최소 신장 트리를 키웁니다. 우선순위 큐로 후보 간선을 관리하며, 크루스칼과 다른 순서로 자라지만 동일한 MST에 도달합니다.

01 Explore How It Works

Interactive Step-by-Step
Prim MST
1234567Akey=0Bkey=∞Ckey=∞Dkey=∞Ekey=∞

Prim 시작. 시작 노드 A의 key를 0, 나머지는 ∞로 둡니다. 트리를 A부터 한 노드씩 키워 갑니다.

Logic Node1 / 7

02 Understand It Simply

For Everyone
🔑Analogy

한 마을에서 시작해 가장 가까운 마을을 하나씩 이어 붙이는 것.

💡In Plain Words

트리에 연결된 가장 싼 간선을 하나씩 더하며 최소 신장 트리를 키웁니다.

크루스칼과 다른 순서지만 같은 결과예요.

📍Where It's Used
  • 최소 비용 연결망
  • 밀집 그래프의 MST

03 Python Implementation

A clean, readable reference implementation of the core logic of 프림 (Prim MST).

core_implementation.py
import heapq

def prim(graph, start):
    visited = {start}
    pq = [(w, start, v) for v, w in graph[start]]
    heapq.heapify(pq)
    mst, total = [], 0
    while pq:
        w, u, v = heapq.heappop(pq)
        if v in visited:
            continue
        visited.add(v)
        mst.append((u, v))
        total += w
        for nxt, w2 in graph[v]:
            if nxt not in visited:
                heapq.heappush(pq, (w2, v, nxt))
    return mst, total

04 Frequently Asked Questions

FAQ
What is 프림 (Prim MST)?+

한 노드에서 시작해 트리에 연결된 가장 싼 간선을 하나씩 더해 가며 최소 신장 트리를 키웁니다. 우선순위 큐로 후보 간선을 관리하며, 크루스칼과 다른 순서로 자라지만 동일한 MST에 도달합니다.

What is the time complexity of 프림 (Prim MST)?+

The time complexity of 프림 (Prim MST) is O(E log V). Follow the step-by-step visualization to see exactly why.

Where is 프림 (Prim MST) used?+

최소 비용 연결망, 밀집 그래프의 MST.

What's a simple analogy for 프림 (Prim MST)?+

한 마을에서 시작해 가장 가까운 마을을 하나씩 이어 붙이는 것.

Guide Progress0%