A minimum spanning tree (MST) is defined as a spanning tree that has the minimum weight among all the possible spanning trees.
A spanning tree is defined as a tree-like subgraph of a connected, undirected graph that includes all the vertices of the graph.
아이디어의 핵심은 그래프를 두 가지로 나누어 생각하는 것이다.
MST를 만드는 중간 단계에서 A와 B를 연결하는 최선의 방법은 무엇일까?
A와B 사이 edges중 가장 최소를 연결하는 것이다.
Steps of Prim's Algorithm:
1. Initialization: Start with an arbitrary vertex and mark it as part of the MST.
2. Edge Selection: From the set of edges that connect vertices in the MST to vertices outside the MST, select the edge with the minimum weight.
3. Update: Add the selected edge and the connected vertex to the MST.
4. Repeat: Repeat the edge selection and update steps until all vertices are included in the MST.
아이디어의 핵심은 edge의 weight에 집중하는 것이다.
edges를 오름차순으로 정렬한 후에, 짧은 것부터 MST에 필요한 것을 선택한다.
Steps of Kruskal's Algorithm:
1. Initialization: Sort all the edges in the graph by their weight in non-decreasing order.
2. Edge Selection: Starting from the smallest edge, add the edge to the MST if it doesn't form a cycle with the already included edges.
3. Cycle Detection: Use a union-find data structure to detect and prevent cycles.
4. Repeat: Continue adding edges until the MST contains exactly (V-1) edges, where V is the number of vertices.
(공간 복잡도: O(E+V) )