Algorithm/Theory

ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (Kruskal Algorithm)

๐Ÿ‘‰ ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ MST(์ตœ์†Œ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ) ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜๋“ค ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. (๋‚˜๋จธ์ง€ ํ•˜๋‚˜๋Š” Prim ์•Œ๊ณ ๋ฆฌ์ฆ˜)

 

๐Ÿ’ก MST๋ž€?

๊ทธ๋ž˜ํ”„์˜ ์—ฐ๊ฒฐ์„ฑ์„ ๊ทธ๋Œ€๋กœ ์œ ์ง€ํ•˜๋Š” ๊ฐ€์žฅ '์ €๋ ดํ•œ' ๊ทธ๋ž˜ํ”„๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ

 

ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๊ทธ๋ž˜ํ”„์˜ ๋ชจ๋“  ๊ฐ„์„ ์„ ๊ฐ€์ค‘์น˜์˜ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๋’ค,

์ŠคํŒจ๋‹ ํŠธ๋ฆฌ์— ํ•˜๋‚˜์”ฉ ์ถ”๊ฐ€ํ•ด ๊ฐ€๋Š” ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ, ๊ฐ€์ค‘์น˜๊ฐ€ ์ž‘๋‹ค๊ณ  ํ•ด์„œ ๋ฌด์กฐ๊ฑด ๊ฐ„์„ ์„ ํŠธ๋ฆฌ์— ๋”ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ, ์‚ฌ์ดํด์ด ์ƒ๊ธฐ๋Š” ๊ฐ„์„ ์„ ๊ณ ๋ คํ•ด ์ œ์™ธ ํ•ด์ค๋‹ˆ๋‹ค.

 

๐Ÿ‘‰ ๋™์ž‘ํ•˜๋Š” ๊ณผ์ •

 

์œ„ ๊ทธ๋ž˜ํ”„๋ฅผ ๋ณด๋ฉด ๊ฐ€์ค‘์น˜๊ฐ€ 1์ธ ๊ฐ„์„ , 2์ธ ๊ฐ„์„ , 3.. ์ˆœ์„œ๋Œ€๋กœ ์„ ํƒํ•ด ๊ฐ€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ด๋•Œ 4๋ฒˆ์งธ์—์„œ 5๋ฒˆ์งธ๋ฅผ ๋ณด๋ฉด ๊ฐ€์ค‘์น˜๊ฐ€ 3์ธ ๊ฐ„์„ ๋“ค์ด ๋‚จ์•„ ์žˆ๋Š”๋ฐ๋„ ๋ถˆ๊ตฌํ•˜๊ณ ,

4์ธ (c,d) ๊ฐ„์„ ์ด ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” 3์ธ ๊ฐ„์„ ๋“ค์ด ์ถ”๊ฐ€๋˜๋ฉด ํŠธ๋ฆฌ์— ์‚ฌ์ดํด์ด ์ƒ๊ธฐ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

ํŠธ๋ฆฌ์— ์‚ฌ์ดํด์ด ์ƒ๊ธด๋‹ค๋ฉด, MST๋ฅผ ๋งŒ์กฑํ•˜์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค.

 

๐Ÿ“„ ๊ตฌํ˜„

ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ๊ฐ„์„ ์˜ ๋ชฉ๋ก์„ ์–ป์–ด์„œ ๊ฐ€์ค‘์น˜ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•œ ๋’ค, ์ˆœํšŒํ•˜๋ฉฐ ์ด๋“ค์„ ์ŠคํŒจ๋‹ ํŠธ๋ฆฌ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

์ด๋•Œ ๊ฐ„์„ ์„ ์ถ”๊ฐ€ํ–ˆ์„ ๋•Œ ์‚ฌ์ดํด์ด ์ด๋ฃจ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.

 

์ด๋ฅผ ์œ„ํ•ด ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰์„ ํ•˜๋ฉฐ ์—ญ๋ฐ•ํ–ฅ ๊ฐ„์„ ์„ ์ฐพ๋Š” ํ•ด๊ฒฐ์ฑ…๋„ ์žˆ์ง€๋งŒ, ๋ณต์žก๋„๊ฐ€ ๋†’๊ฒŒ ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์—,

๊ฐ„์„ ์˜ ์–‘ ๋ ์ ์ด ๊ฐ™์€ ์ปดํฌ๋„ŒํŠธ์— ์†ํ•ด ์žˆ๋Š” ๊ฒƒ์„ ๊ณ ๋ คํ•ด ์ƒํ˜ธ ๋ฐฐํƒ€์  ์ง‘ํ•ฉ ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ’ก ์ƒํ˜ธ๋ฐฐํƒ€์  ์ง‘ํ•ฉ ์ž๋ฃŒ ๊ตฌ์กฐ๋ž€?

์ฐธ๊ณ  : https://withseungryu.tistory.com/40?category=753127

 

Union-Find, Disjoint Set (์œ ๋‹ˆ์˜จ-ํŒŒ์ธ๋“œ, ์ƒํ˜ธ ๋ฐฐํƒ€์  ์ง‘ํ•ฉ)

โ˜ž Union-Find ๋ž€?  ์ƒํ˜ธ ๋ฐฐํƒ€์ ์ธ ๋ถ€๋ถ„ ์ง‘ํ•ฉ(Disjoint Set)๋“ค๋กœ ๋‚˜๋ˆ ์ง„ ์›์†Œ๋“ค์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ณ  ์กฐ์ž‘ํ•˜๋Š” ์ž๋ฃŒ ๊ตฌ์กฐ Union-Find์—๋Š” ์ž๋ฃŒ ๊ตฌ์กฐ์— ๋ช…์นญ์— ๋งž๊ฒŒ ์—ฐ์‚ฐ์ด ์ดˆ๊ธฐํ™”(init), ํ•ฉ์น˜๊ธฐ(union) ๊ทธ๏ฟฝ๏ฟฝ

withseungryu.tistory.com

 

๐Ÿ‘‰ ๊ตฌํ˜„ ์ฝ”๋“œ

const int MAX_V = 100; //๋ฌธ์ œ์— ๋”ฐ๋ผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.

int V; //์ •์ ์˜ ๊ฐœ์ˆ˜

vector<pair<int, int>> adj[MAX_V]; // ๊ทธ๋ž˜ํ”„์˜ ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ (์—ฐ๊ฒฐ๋œ ์ •์  ๋ฒˆํ˜ธ, ๊ฐ„์„  ๊ฐ€์ค‘์น˜)

struct OptimizedDisjointSet {
	vector<int> parent, rank; 
	OptimizedDisjointSet(int n) : parent(n), rank(n, 1) {
		for (int i = 0; i < n; ++i)
			parent[i] = i;
	}

	int find(int u) {
		if (u == parent[u]) return u;
		return parent[u] = find(parent[u]); 
	}

	void merge(int u, int v) {
		u = find(u); v = find(v);
		if (u == v) return;
		if (rank[u] > rank[v]) swap(u, v);
		parent[u] = v;
		if (rank[u] == rank[v]) ++rank[v];
	}
};


int kruskal(vector<pair<int, int>> selected) {
	int ret = 0;
	selected.clear();

	vector<pair<int, pair<int, int>>> edges; //(๊ฐ€์ค‘์น˜, (์ •์ 1, ์ •์ 2))
	for(int u=0; u<V; ++u)
		for (int i = 0; i < adj[u].size(); ++i) {
			int v = adj[u][i].first, cost = adj[u][i].second;
			edges.push_back(make_pair(cost, make_pair(u, v)));
		}

	sort(edges.begin(), edges.end()); // ๊ฐ€์ค‘์น˜ ์ˆœ์œผ๋กœ ์ •๋ ฌ
	
	OptimizedDisjointSet sets(V);

	for (int i = 0; i < edges.size(); ++i) {
		int cost = edges[i].first;
		int u = edges[i].second.first, v = edges[i].second.second;
		
		if (sets.find(u) == sets.find(v)) continue; //์ด๋ฏธ u์™€ v๊ฐ€ ์—ฐ๊ฒฐ๋˜์–ด์žˆ์„๊ฒฝ์šฐ

		sets.merge(u, v);
		selected.push_back(make_pair(u, v));
		ret += cost;
		
	}
	return ret;
}

 

๐Ÿ’ก ์‹œ๊ฐ„๋ณต์žก๋„ : O(E* logE)

 

๊ถ๊ธˆํ•˜์‹  ๊ฒƒ์ด ์žˆ์œผ์‹œ๋ฉด ์–ธ์ œ๋“ ์ง€ ๋Œ“๊ธ€ ๋‹ฌ์•„์ฃผ์„ธ์š”!

 

๋ฐ˜์‘ํ˜•