Amenable 2023. 3. 1. 19:28

  ์ตœ์†Œ์‹ ์žฅํŠธ๋ฆฌ๋ฅผ ๋งŒ๋“œ๋Š” ๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์ธ PRIM ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž. (์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” KRUSKAL ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์žˆ์—ˆ๋‹ค.)

 

1. PRIM ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๐Ÿ‘“

1. ๊ธฐ๋ณธ ๊ฐœ๋…

๊ทธ๋ฆฌ๋”” ๋ฐฉ์‹์„ ์ด์šฉํ•˜์—ฌ ์ตœ์†Œ ์‹ ์žฅ ํŠธ๋ฆฌ๋ฅผ ์ฐพ๋Š” ๋ฐฉ์‹
์‹œ์ž‘ ์ •์ ์—์„œ๋ถ€ํ„ฐ ์ถœ๋ฐœํ•˜์—ฌ ์‹ ์žฅํŠธ๋ฆฌ ์ง‘ํ•ฉ์„ ๋‹จ๊ณ„์ ์œผ๋กœ ๋งŒ๋“ค์–ด ๋‚˜๊ฐ€๋Š” ๋ฐฉ์‹

2. ๋™์ž‘ ๋ฐฉ์‹

  1. ๊ทธ๋ž˜ํ”„์—์„œ ํ•˜๋‚˜์˜ ์ •์ ์„ ์„ ํƒํ•˜์—ฌ ํŠธ๋ฆฌ๋ฅผ ๋งŒ๋“ ๋‹ค.
  2. ํŠธ๋ฆฌ์™€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์ง€๋งŒ ์•„์ง ์„ ํƒ๋˜์ง€ ์•Š์€ ์ •์ ๋“ค ์ค‘์—์„œ ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ๊ฐ€์žฅ ๋‚ฎ์€ ์ •์ ์„ ํŠธ๋ฆฌ์— ํฌํ•จ์‹œํ‚จ๋‹ค.
  3. ํŠธ๋ฆฌ๊ฐ€ N-1์˜ ๊ฐ„์„ ์„ ๊ฐ€์งˆ ๋•Œ๊นŒ์ง€ 2๋ฒˆ์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

 

2. ๊ตฌํ˜„ (JAVA) ๐Ÿคฟ

  ๊ตฌํ˜„ ๋ฐฉ์‹์œผ๋กœ๋Š” ์•„๋ž˜์˜ 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

  1. ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ๋‹ค์Œ ์ •์ ์„ ์ฐพ๋Š” ๋ฐฉ์‹
  2. PriorityQueue๋ฅผ ์ด์šฉํ•ด ๋‹ค์Œ ์ •์ ์„ ์ฐพ๋Š” ๋ฐฉ์‹

  ์œ„์˜ ์˜ˆ์‹œ๋ฅผ ํ†ตํ•ด PRIM ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์•Œ์•„๋ณด์ž.('a, b, c, d, e, f, g'๋Š” '0, 1, 2, 3, 4, 5, 6'์œผ๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค.)
  PRIM ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์จ์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋Š” ๋Œ€์ฒด์ ์œผ๋กœ ๊ฐ„์„ ์ด ๋งŽ์€ ๊ฒฝ์šฐ์ด๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” ํ–‰๋ ฌ์˜ ํ˜•์‹์œผ๋กœ ์ž…๋ ฅ์ด ๋งŽ์ด ์ฃผ์–ด์ง€๋ฏ€๋กœ ํ–‰๋ ฌ์˜ ํ˜•์‹์œผ๋กœ ์ž…๋ ฅ์„ ๋ฐ›์•„๋ณด์ž. ๋จผ์ € ์ •์ ์˜ ๊ฐœ์ˆ˜(vertexNum)๋ฅผ ๋ฐ›๊ณ , ๊ทธ๋ฆฌ๊ณ  ๊ฐ€์ค‘์น˜์— ํ•ด๋‹นํ•˜๋Š” 2์ฐจ์› ํ–‰๋ ฌ์„ ๋ฐ›๋Š”๋‹ค.

  ์•„๋ž˜๋Š” ์ž…๋ ฅ ์ •๋ณด์™€ ๊ตฌํ˜„ ์ฝ”๋“œ์ด๋‹ค.

7
0 29 0 0 0 10 0
29 0 16 0 0 0 15
0 16 0 12 0 0 0
0 0 12 0 22 0 18
0 0 0 22 0 27 25
10 0 0 0 27 0 0
0 15 0 18 25 0 0

๋ฐฉ๋ฒ• 1 - ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ๋‹ค์Œ ์ •์ ์„ ์ฐพ๋Š” ๋ฐฉ์‹

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(in.readLine(), " ");
		int vertexNum = Integer.parseInt(st.nextToken());
		int edgeNum = Integer.parseInt(st.nextToken());
		
		// ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด
		int[][] adjMatrix = new int[vertexNum][vertexNum];
		// ์ •์ ์— ๋ฐฉ๋ฌธํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฐ„์„ ์˜ ์ตœ์†Œ ๊ฐ€์ค‘์น˜
		int[] minWeightForVertex = new int[vertexNum];
		boolean[] visited = new boolean[vertexNum];
		
		for (int from = 0; from < vertexNum; from++) {
			st = new StringTokenizer(in.readLine(), " ");
			for (int to = 0; to < vertexNum; to++) {
				adjMatrix[from][to] = Integer.parseInt(st.nextToken());
			}
		}
		
		// ์•„์ง ํƒ์ƒ‰์„ ํ•˜๋ฉด์„œ ์ตœ์†Œ ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธ ํ•˜๊ธฐ ์ „์ด๋ฏ€๋กœ,
		// ์•„๋ž˜์™€ ๊ฐ™์ด ์ดˆ๊ธฐํ™”
		for(int i = 0; i < vertexNum; i++) {
			minWeightForVertex[i] = Integer.MAX_VALUE;
		}

		// ๋ฐฉ๋ฌธํ•œ ์ •์ ์˜ ๊ฐœ์ˆ˜
		int vertexCount = 0;
		int mstWeight = 0;
		
		// ์‹œ์ž‘ ์ •์ ์€ ์•„๋ฌด๊ฑฐ๋‚˜ ํ•ด๋„ ๋จ
		int startVertex = 0;
		// ์‹œ์ž‘ ์ •์ ์— ๋ฐฉ๋ฌธํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•œ ๊ฐ„์„ ์€ ์—†์œผ๋ฏ€๋กœ 
		// ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๋Š” 0์œผ๋กœ ์„ธํŒ…
		minWeightForVertex[startVertex] = 0;
		
		// 
		for(int i = 0; i < vertexNum; i++) {
			
			// ํŠธ๋ฆฌ์™€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์ง€๋งŒ ์•„์ง ์„ ํƒ๋˜์ง€ ์•Š์€ ์ •์ ๋“ค ์ค‘์—์„œ 
			// ๊ฐ€์žฅ ๋‚ฎ์€ ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜(minWeightForNextVertex)์™€ 
			// ํ•ด๋‹น ์ •์ (minVertex)์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜
			int minWeightForNextVertex = Integer.MAX_VALUE;
			int minVertex = startVertex;
			
			for(int j = 0; j < vertexNum; j++) {
				if(!visited[j] && minWeightForNextVertex > minWeightForVertex[j]) {
					minWeightForNextVertex = minWeightForVertex[j];
					minVertex = j;
				}
			}
			
			// ํŠธ๋ฆฌ์— ์ •์ ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ณผ์ •
			visited[minVertex] = true;
			mstWeight += minWeightForNextVertex;
			
			// (์ •์  - 1)๊ฐœ์˜ ๊ฐ„์„ ์ด ์ƒ๊ธด ๊ฒฝ์šฐ
			if(++vertexCount == vertexNum) {
				break;
			}
			
			// ์ •์ ์ด ์•„์ง ํŠธ๋ฆฌ์— ํฌํ•จ๋˜์ง€ ์•Š์•˜๊ณ 
			// ๊ฐ„์„ ์ด ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ
			// ํ•ด๋‹น ์ •์ ์œผ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ตœ์†Œ ๊ฐ€์ค‘์น˜์˜ ๊ฐ„์„ ์„ ์„ ํƒํ•˜๋Š” ๊ณผ์ •
			for(int j = 0; j < vertexNum; j++) {
				if(!visited[j] && adjMatrix[minVertex][j] != 0
						&& minWeightForVertex[j] > adjMatrix[minVertex][j]) {
					minWeightForVertex[j] = adjMatrix[minVertex][j];
				}
			}
		}
		
		System.out.println(mstWeight);
	}
}

 

๋ฐฉ๋ฒ• 2 - PriorityQueue๋ฅผ ์ด์šฉํ•ด ๋‹ค์Œ ์ •์ ์„ ์ฐพ๋Š” ๋ฐฉ์‹

public class Main {
	
	private static class Vertex implements Comparable<Vertex> {
		public int idx;
		public int weight;
		
		public Vertex(int idx, int weight) {
			super();
			this.idx = idx;
			this.weight = weight;
		}

		@Override
		public int compareTo(Vertex o) {
			return Integer.compare(this.weight, o.weight);
		}
		
		
	}
	

	public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(in.readLine());
		int vertexNum = Integer.parseInt(st.nextToken());
		
		// ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด
		int[][] adjMatrix = new int[vertexNum][vertexNum];
		// ์ •์ ์— ๋ฐฉ๋ฌธํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฐ„์„ ์˜ ์ตœ์†Œ ๊ฐ€์ค‘์น˜
		int[] minWeightForVertex = new int[vertexNum];
		boolean[] visited = new boolean[vertexNum];
		
		for (int from = 0; from < vertexNum; from++) {
			st = new StringTokenizer(in.readLine(), " ");
			for (int to = 0; to < vertexNum; to++) {
				adjMatrix[from][to] = Integer.parseInt(st.nextToken());
			}
		}
		
		// ์•„์ง ํƒ์ƒ‰์„ ํ•˜๋ฉด์„œ ์ตœ์†Œ ๊ฐ€์ค‘์น˜๋ฅผ ์—…๋ฐ์ดํŠธ ํ•˜๊ธฐ ์ „์ด๋ฏ€๋กœ,
		// ์•„๋ž˜์™€ ๊ฐ™์ด ์ดˆ๊ธฐํ™”
		for(int i = 0; i < vertexNum; i++) {
			minWeightForVertex[i] = Integer.MAX_VALUE;
		}

		// PriorityQueue
		Queue<Vertex> queue = new PriorityQueue<>();
		// ์‹œ์ž‘ ์ •์ ์€ ์•„๋ฌด๊ฑฐ๋‚˜ ํ•ด๋„ ๋จ
		int startVertex = 0;
		// ์‹œ์ž‘ ์ •์ ์— ๋ฐฉ๋ฌธํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉํ•œ ๊ฐ„์„ ์€ ์—†์œผ๋ฏ€๋กœ 
		// ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๋Š” 0์œผ๋กœ ์„ธํŒ…
		queue.offer(new Vertex(startVertex, 0));
		
		// ๋ฐฉ๋ฌธํ•œ ์ •์ ์˜ ๊ฐœ์ˆ˜
		int vertexCount = 0;
		int mstWeight = 0;
		
		while(!queue.isEmpty()) {
			
			// ๋‹ค์Œ ์ •์ ์˜ ํ›„๋ณด๋“ค ์ค‘ ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ์ •์ ์„ ์„ ํƒ
			Vertex minVertex = queue.poll();
			
			// ์ด๋ฏธ ํŠธ๋ฆฌ์— ํฌํ•จ๋œ ๊ฒฝ์šฐ
			if(visited[minVertex.idx]) {
				continue;
			}

			// ํŠธ๋ฆฌ์— ์ •์ ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ณผ์ •
			visited[minVertex.idx] = true;
			mstWeight += minVertex.weight;
			
			// (์ •์  - 1)๊ฐœ์˜ ๊ฐ„์„ ์ด ์ƒ๊ธด ๊ฒฝ์šฐ
			if(++vertexCount == vertexNum) {
				break;
			}
			
			// ์ •์ ์ด ์•„์ง ํŠธ๋ฆฌ์— ํฌํ•จ๋˜์ง€ ์•Š์•˜๊ณ 
			// ๊ฐ„์„ ์ด ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ
			// ํ•ด๋‹น ์ •์ ์œผ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ์ตœ์†Œ ๊ฐ€์ค‘์น˜์˜ ๊ฐ„์„ ์„ ์„ ํƒํ•˜๋Š” ๊ณผ์ •
			for(int i = 0; i < vertexNum; i++) {
				if(!visited[i] && adjMatrix[minVertex.idx][i] != 0
						&& minWeightForVertex[i] > adjMatrix[minVertex.idx][i]) {
					minWeightForVertex[i] = adjMatrix[minVertex.idx][i];
					queue.offer(new Vertex(i, adjMatrix[minVertex.idx][i]));
				}
			}
		}
		
		System.out.println(mstWeight);
	}
}

  ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ์•Œ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ KRUSKAL์— ๋น„ํ•ด ์ƒ๋‹นํžˆ ๋ณต์žกํ•˜๋‹ค. ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ž‘์œผ๋ฉด KRUSKAL ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜๋„๋ก ํ•˜์ž.

 

3. ์‹œ๊ฐ„๋ณต์žก๋„ ๐Ÿฅฝ

  '๋ฐฉ๋ฒ• 1 - ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ๋‹ค์Œ ์ •์ ์„ ์ฐพ๋Š” ๋ฐฉ์‹'์—์„œ๋Š” O(V ^ 2)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ง„๋‹ค.

  '๋ฐฉ๋ฒ• 2 - PriorityQueue๋ฅผ ์ด์šฉํ•ด ๋‹ค์Œ ์ •์ ์„ ์ฐพ๋Š” ๋ฐฉ์‹'์—์„œ๋Š” O((E + V) log V)๋งŒํผ์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ง„๋‹ค. (์ž๋ฐ”์—์„œ๋Š” ์šฐ์„ ์ˆœ์œ„ ํ๊ฐ€ ํ”ผ๋ณด๋‚˜์น˜ ํž™์ด ์•„๋‹Œ ์ด์ง„ ํž™ ํ˜•ํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.) (๋งŒ์•ฝ ํ”ผ๋ณด๋‚˜์น˜ ํž™์ด๋ฉด O(E + VlogV)๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋œ๋‹ค.)

(์‹œ๊ฐ„๋ณต์žก๋„ ๊ด€๋ จํ•ด์„œ๋Š” ํ•ด๋‹น ๊ธ€์„ ์ฐธ๊ณ ํ•˜๊ธฐ ๋ฐ”๋ž€๋‹ค.)

  ์•ž์—์„œ ๊ณ„์†ํ•ด์„œ ์–ธ๊ธ‰ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜์— ๋”ฐ๋ผ์„œ KRUSKAL ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ PRIM ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ ์ ˆํ•˜๊ฒŒ ์„ ํƒํ•ด์•ผ ํ•œ๋‹ค.