
간선을 하나씩 선택해서 MST를 찾는 알고리즘

구현
import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;
public class MST1_KruskalTest {
static class Edge implements Comparable<Edge>{
int start;
int end;
int weight;
public Edge(int start, int end, int weight) {
this.start = start;
this.end = end;
this.weight = weight;
}
@Override
public int compareTo(Edge o) {
return this.weight - o.weight;
}
}
static int N;
static int[] parents;
static Edge[] edgeList;
// 단위 집합 생성
public static void makeSet() {
parents = new int[N];
// 자신의 부모노드를 자신의 값으로 세팅
for(int i=0; i<N; i++) {
parents[i] = i;
}
}
// a의 집합 찾기 : a의 대표자 찾기
public static int findSet(int a) {
if(a==parents[a]) {
return a;
}
return parents[a] = findSet(parents[a]);
}
// a,b 두 집합 합치기
public static boolean union(int a, int b) {
int aRoot = findSet(a);
int bRoot = findSet(b);
if(aRoot == bRoot)
return false;
parents[bRoot] = aRoot;
return true;
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
int E = Integer.parseInt(st.nextToken());
edgeList = new Edge[E];
for(int i=0; i<E; i++) {
st = new StringTokenizer(br.readLine());
int from = Integer.parseInt(st.nextToken());
int to = Integer.parseInt(st.nextToken());
int weight = Integer.parseInt(st.nextToken());
edgeList[i] = new Edge(from, to, weight);
}
Arrays.sort(edgeList);
makeSet();
// 결과 비용 변수
int result = 0;
int cnt = 0;
for(Edge edge : edgeList) {
if(union(edge.start, edge.end)) {
result += edge.weight;
if(++cnt == N-1) {
break;
}
}
}
System.out.println(result);
}
}

import java.io.*;
import java.util.StringTokenizer;
public class MST2_PrimTest {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int N = Integer.parseInt(br.readLine());
int[][] adjMatrix = new int[N][N];
int [] minEdge = new int[N]; // 타 정점에서 자신으로의 간선 비용중 최소 비용을 저장
boolean[] isVisited = new boolean[N];
for(int r=0;r<N;r++) {
st = new StringTokenizer(br.readLine());
for(int c=0;c<N;c++) {
adjMatrix[r][c] = Integer.parseInt(st.nextToken());
}
minEdge[r] = Integer.MAX_VALUE; // 충분이 큰 값으로 최소값 초기화
}
int result = 0; // MST 비용
minEdge[0] = 0; // 임의로 0번부터 시작
// N개의 정점을 모두 연결
for(int c=0;c<N;c++) {
// 신장트리에 연결되지 않은 정점 중 가장 유리한 비용의 정점을 선택
int min = Integer.MAX_VALUE;
int minVertex = -1;
for(int i=0; i<N; i++) {
if(!isVisited[i] && min > minEdge[i]) {
min = minEdge[i];
minVertex = i;
}
}
// 선택된 정점을 신장트리에 포함시킴
isVisited[minVertex] = true;
result += min;
// 선택된 정점 기준으로 신장트리에 포함되지 않은 다른 정점으로의 간선 비용을 따져보고 최소값 갱신
for(int i=0;i<N;i++) {
if(!isVisited[i] && adjMatrix[minVertex][i] != 0 && minEdge[i] > adjMatrix[minVertex][i]) {
minEdge[i] = adjMatrix[minVertex][i];
}
}
}
System.out.println(result);
}
}