๐Ÿ“ Coding Test Study

[C++][๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜] ๊ตฌํ˜„ ๋ฐฉ๋ฒ•

ibelieveinme 2021. 7. 29. 22:42
728x90

โ–ถ ๋‹ค์ต์ŠคํŠธ๋ผ(Dijkstra) ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ž€?

๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ(DP)๋ฅผ ํ™œ์šฉํ•œ ์ตœ๋‹จ ๊ฒฝ๋กœ(Shortest Path) ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜.

ํ•˜๋‚˜์˜ ์ •์ ์—์„œ ๋‹ค๋ฅธ ๋ชจ๋“  ์ •์ ์œผ๋กœ ๊ฐ€๋Š” ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์•Œ๋ ค์ค€๋‹ค.

 

๊ตฌํ˜„์€ Greedy ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‚˜ DP ์•Œ๊ณ ๋ฆฌ์ฆ˜์„  ์‚ฌ์šฉํ•˜๋Š”๋ฐ,

 

Greedy ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ด์œ ๋Š” ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋“ค ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ๋น„์šฉ์„ ๊ฐ–๊ณ  ์žˆ๋Š” ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•˜์—ฌ ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

DP ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ด์œ ๋Š” "์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ตœ๋‹จ ๊ฑฐ๋ฆฌ๋“ค"๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

(์ตœ๋‹จ ๊ฑฐ๋ฆฌ = ์ตœ๋‹จ ๊ฑฐ๋ฆฌ ์ •๋ณด๋“ค์˜ ํ•ฉ)

์ถœ์ฒ˜: Wikipedia Dikstra Algorithm 

์œ„์˜ ๊ทธ๋ฆผ์€ a(1)์™€ b(5)์‚ฌ์ด์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํ๋ฆ„์„ ๋ณด์—ฌ์ค€๋‹ค.

 

๊ฐ€์žฅ ๋‚ฎ์€ ๊ฐ’์„ ๊ฐ€์ง„ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ผญ์ง“์ ์„ ์„ ํƒํ•˜๊ณ , ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ฐ ์ธ์ ‘ ๋…ธ๋“œ์™€์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•˜๊ณ , ์ž‘์„ ๊ฒฝ์šฐ ์ธ์ ‘ ๊ฑฐ๋ฆฌ๋ฅผ ์—…๋ฐ์ดํŠธํ•œ๋‹ค. (์ฐธ๊ณ : ์ธ์ ‘ํ•œ ๋…ธ๋“œ์— ๋ชจ๋‘ ๋ฐฉ๋ฌธํ•˜๋ฉด ๋นจ๊ฐ„์ƒ‰์œผ๋กœ ํ‘œ์‹œ๋˜๋ฉฐ out ์ด๋ผ๊ณ  ๋œฌ๋‹ค.)

 

 

โ–ถ ๊ตฌํ˜„ ๋ฐฉ๋ฒ•๊ณผ ์›๋ฆฌ

1) ๋น„์šฉ์„ ๋‹ด์„ ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ , ๋ฌดํ•œ๋Œ€(INF)์˜ ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™” ํ•œ๋‹ค.

    ๋ฐฉ๋ฌธ์ฒดํฌ ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ , false๋กœ ์ดˆ๊ธฐํ™” ํ•œ๋‹ค.

for (int i = 0; i < V; i++)
	dist[i] = INT_MAX, visited[i] = false;

 

2) ์‹œ์ž‘๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธ๋…ธ๋“œ๋กœ ์ฒดํฌํ•œ๋‹ค. ์‹œ์ž‘๋…ธ๋“œ์™€ ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋“ค์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๋น„๊ตํ•ด์„œ ๋น„์šฉ์„ ์—…๋ฐ์ดํŠธ ํ•œ๋‹ค.

visited[current_node] = true;

for (int v = 0; v < V; v++)
	if (!visited[v] && graph[current_node][v] && dist[current_node] != INT_MAX 
    	&& dist[current_node] + graph[current_node][v] < dist[v]){
			dist[v] = dist[current_node] + graph[current_node][v];
    }

3) ์‹œ์ž‘๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋“ค ์ค‘ ๋น„์šฉ์ด ๊ฐ€์žฅ ์ ์€ ๋…ธ๋“œ๋ฅผ ์„ ํƒํ•˜์—ฌ ์ด๋™ํ•œ๋‹ค.

int u = minDistance(dist, visited);

4) ์ด๋™ํ•œ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธ๋…ธ๋“œ๋กœ ์ฒดํฌํ•œ๋‹ค. ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋“ค์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๋น„๊ตํ•ด์„œ ๋น„์šฉ์„ ์—…๋ฐ์ดํŠธ ํ•œ๋‹ค.

visited[u] = true;

for (int v = 0; v < V; v++)
	if (!visited[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]){
			dist[v] = dist[u] + graph[u][v];
    }

4) ๋„์ฐฉ์ง€์ ์— ๋‹ค๋‹ค๋ฅผ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.

 

 

โ–ถ ์ „์ฒด์ฝ”๋“œ

// A C++ program for Dijkstra's single source shortest path algorithm.
// The program is for adjacency matrix representation of the graph

#include <limits.h>
#include <stdio.h>

#define V 9

int minDistance(int dist[], bool visited[]){
	int min = INT_MAX, min_index;

	for (int v = 0; v < V; v++)
		if (visited[v] == false && dist[v] <= min)
			min = dist[v], min_index = v;

	return min_index;
}

int printSolution(int dist[], int n){
	printf("Vertex   Distance from Source\n");
	for (int i = 0; i < V; i++)
		printf("%d \t\t %d\n", i, dist[i]);
}

void dijkstra(int graph[V][V], int src){
	int dist[V]; 
	bool visited[V];

	for (int i = 0; i < V; i++)
		dist[i] = INT_MAX, visited[i] = false;

	dist[src] = 0;

	for (int count = 0; count < V - 1; count++) {
		int u = minDistance(dist, visited);
		visited[u] = true;
		for (int v = 0; v < V; v++)
			if (!visited[v] && graph[u][v] && dist[u] != INT_MAX
				&& dist[u] + graph[u][v] < dist[v])
				dist[v] = dist[u] + graph[u][v];
	}
	printSolution(dist, V);
}

int main(){
	int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
						{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
						{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
						{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
						{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
						{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
						{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
						{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
						{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };

	dijkstra(graph, 0);

	return 0;
}

โ–ถ ์ถœ๋ ฅํ™”๋ฉด

Vertex   Distance from Source
0          0
1          4
2          12
3          19
4          21
5          11
6          9
7          8
8          14

์ถœ์ฒ˜: https://www.geeksforgeeks.org/c-program-for-dijkstras-shortest-path-algorithm-greedy-algo-7/

 

 

โ–ถ ๊ด€๋ จ๋ฌธ์ œ

 

1261๋ฒˆ: ์•Œ๊ณ ์ŠคํŒŸ

์ฒซ์งธ ์ค„์— ๋ฏธ๋กœ์˜ ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ€๋กœ ํฌ๊ธฐ M, ์„ธ๋กœ ํฌ๊ธฐ N (1 ≤ N, M ≤ 100)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” ๋ฏธ๋กœ์˜ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ˆซ์ž 0๊ณผ 1์ด ์ฃผ์–ด์ง„๋‹ค. 0์€ ๋นˆ ๋ฐฉ์„ ์˜๋ฏธํ•˜๊ณ , 1์€ ๋ฒฝ์„ ์˜๋ฏธ

www.acmicpc.net

 

728x90