๐Ÿ“ Coding Test Study/C++

[C++][์ž๋ฃŒ๊ตฌ์กฐ] Graph์™€ Tree์˜ ์ฐจ์ด? ๊ตฌํ˜„๋ฒ•์€?

ibelieveinme 2022. 9. 6. 01:13
728x90

1. Graph(๊ทธ๋ž˜ํ”„) ์ž๋ฃŒ๊ตฌ์กฐ

 

*์šฉ์–ด์ •๋ฆฌ
G = (V, E) (์ •์  Node, Vertex / ๊ฐ„์„  Edge)

- ๊ฒฝ๋กœ(์‹œ์ž‘์ ๊ณผ ๋„์ฐฉ์ ์ด ๋‹ค๋ฅผ ๊ฒฝ์šฐ), ์‚ฌ์ดํด(์‹œ์ž‘์ ๊ณผ ๋„์ฐฉ์ ์ด ๊ฐ™์„ ๊ฒฝ์šฐ)
cf) ๋‹จ์ˆœ๊ฒฝ๋กœ/๋‹จ์ˆœ ์‚ฌ์ดํด: ๊ฐ™์€ ์ •์ ์„ ๋‘ ๋ฒˆ ์ด์ƒ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š๋Š” ๊ฒฝ๋กœ/์‚ฌ์ดํด

- ๋ฐฉํ–ฅ์ด ์žˆ๋Š” ๊ทธ๋ž˜ํ”„/๋ฐฉํ–ฅ์ด ์—†๋Š” ๊ทธ๋ž˜ํ”„

- ๊ฐ€์ค‘์น˜: ๊ฐ„์„ ์˜ ๋น„์šฉ.

- ์ฐจ์ˆ˜(Degree): ์ •์ ๊ณผ ์—ฐ๊ฒฐ๋œ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜

cf) ๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์˜ ๊ฒฝ์šฐ, In-degree, Out-degree๋กœ ๋‚˜๋ˆ„์–ด์„œ ์ฐจ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.

 

*๊ทธ๋ž˜ํ”„๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฒ•
; ์–ด๋–ค ์ •์  x์™€ ์—ฐ๊ฒฐ๋œ ๊ฐ„์„ ์„ ์ฐพ๊ธฐ ์œ„ํ•ด ์–ด๋–ป๊ฒŒ ํšจ์œจ์ ์œผ๋กœ ์ €์žฅํ• ์ง€.

 

1) ์ธ์ ‘ ํ–‰๋ ฌ ๋ฐฉ๋ฒ•
A[i][j] = 1, A[i][j] = 0. <- ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์œผ๋ฉด 1, ์—ฐ๊ฒฐ์•ˆ๋˜์–ด ์žˆ์œผ๋ฉด 0

 

2) ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ ๋ฐฉ๋ฒ•
A[i] = i์™€ ์—ฐ๊ฒฐ๋œ ์ •์ ์„ ๋ฆฌ์ŠคํŠธ๋กœ ํฌํ•จํ•จ. ๋™์  ๋ฐฐ์—ด(vector, ArrayList ๋“ฑ)์ด ํ•„์š”ํ•จ.
ex) A[1] = {2, 5}, A[2] = {1, 3, 4, 5}, ...
ex) ๊ฐ€์ค‘์น˜๋„ ๊ฐ™์ด ์ €์žฅํ•  ๋•Œ๋Š” pair๋ฅผ ์ด์šฉํ•ด์„œ A[1] = { (2, 2), (5, 7) }, A[2] = { (1, 2), (3, 2), (4, 3), (5, 1) }๋กœ ํ‘œํ˜„ํ•จ. 1์—์„œ 2๋กœ ๊ฐˆ ๋•Œ ๊ฐ€์ค‘์น˜๊ฐ€ 2, 1์—์„œ 5๋กœ ๊ฐˆ ๋•Œ ๊ฐ€์ค‘์น˜๊ฐ€ 7. ์ด๋ ‡๊ฒŒ

//์ธ์ ‘๋ฆฌ์ŠคํŠธ๋กœ ๊ทธ๋ž˜ํ”„ ํ‘œํ˜„ํ•˜๊ธฐ

#include <iostream>
#include <vector>
using namespace std;

int main() {
	int nodeNum, inputNode1, inputNode2;
	cin >> nodeNum;
	vector<int> *graph = new vector<int>[nodeNum + 1];

	//Input
	for (int i = 0; i < nodeNum - 1; i++) {//edge์˜ ๊ฐœ์ˆ˜๋Š” node์˜ ๊ฐœ์ˆ˜ - 1.
		cin >> inputNode1 >> inputNode2;
		graph[inputNode1].push_back(inputNode2);
		graph[inputNode2].push_back(inputNode1);
	}

	delete[] graph;
	return 0;
}
//์ธ์ ‘๋ฆฌ์ŠคํŠธ์— ๊ฐ€์ค‘์น˜๋„ ์ €์žฅํ•˜๋Š” ๋ฒ•

#include <iostream>
#include <vector>
using namespace std;

int main() {
	int nodeNum, inputNode1, inputNode2, weight;//๋…ธ๋“œ์ˆ˜, ๋…ธ๋“œ1, ๋…ธ๋“œ2, ๊ฐ€์ค‘์น˜
	cin >> nodeNum;
    
        //๋…ธ๋“œ๊ฐœ์ˆ˜๋งŒํผ pair ์ธ์ ‘๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
	vector<pair<int, int>> *graph = new vector<pair<int, int>> [nodeNum + 1];
	
	//๊ฐ’ ์ž…๋ ฅ
	for (int i = 1; i < nodeNum; i++) {
		cin >> inputNode1 >> inputNode2 >> weight;
		graph[inputNode1].push_back(make_pair(inputNode2, weight));
		graph[inputNode2].push_back(make_pair(inputNode1, weight));
	}
    
        delete[] graph;
	return 0;
}

 

*์ธ์ ‘ ํ–‰๋ ฌ/์ธ์ ‘ ๋ฆฌ์ŠคํŠธ ๊ณต๊ฐ„ ๋ณต์žก๋„
1) ์ธ์ ‘ ํ–‰๋ ฌ: O(V^2)
2) ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ: O(E)

 

๊ณต๊ฐ„๋ณต์žก๋„๋กœ๋งŒ ๋ณด๋ฉด ์ธ์ ‘ํ–‰๋ ฌ์ด ํ›จ์”ฌ ์ข‹์Œ. ํ•˜์ง€๋งŒ ์ธ์ ‘ํ–‰๋ ฌ์ด ์ข‹์„ ๋•Œ๋„ ์žˆ์Œ.

์˜ˆ๋ฅผ ๋“ค์–ด, u, v๊ฐ€ ์žˆ์„ ๋•Œ, u->v๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€๋ฅผ ์•Œ์•„๋ณด๋ ค๋ฉด O(1)์˜ ์‹œ๊ฐ„์œผ๋กœ ์•Œ ์ˆ˜ ์žˆ์Œ. A[u][v]๋งŒ ํ™•์ธํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ.
๋ฐ˜๋ฉด์— ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋Š” A[u] = { 0, 0, 0, ...} ๋กœ ํ‘œํ˜„๋˜๊ธฐ ๋•Œ๋ฌธ์— ์›์†Œ ํ•˜๋‚˜ํ•˜๋‚˜๋ฅผ ๋‹ค ์ฐพ์•„๋ด์•ผ ํ•จ. ๊ทธ๋ž˜์„œ O(์ฐจ์ˆ˜) ๊ฐ€ ๊ฑธ๋ฆผ.

๋˜ํ•œ, ๋ชจ๋“  ์ •์ ์ด ์—ฐ๊ฒฐ๋œ ์™„์ „ ๊ทธ๋ž˜ํ”„์ผ ๋•Œ๋Š” ์ธ์ ‘ํ–‰๋ ฌ์„ ์จ๋„ ๊ณต๊ฐ„๋ณต์žก๋„๊ฐ€ ๋™์ผํ•˜๋‹ค.

 

 

2. Tree(ํŠธ๋ฆฌ) ์ž๋ฃŒ๊ตฌ์กฐ

: ๋ฃจํ”„๋ฅผ ๊ฐ€์ง€์ง€ ์•Š๋Š” ๊ทธ๋ž˜ํ”„. ๋…ธ๋“œ๋“ค์„ ๊ฐ„์„ ์œผ๋กœ ์—ฐ๊ฒฐํ•œ ๊ณ„์ธตํ˜• ์ž๋ฃŒ๊ตฌ์กฐ.

์ถœ์ฒ˜:&nbsp;https://techdifferences.com/difference-between-tree-and-graph.html

Tree Graph
๋‘ ์ •์ ์‚ฌ์ด์— ํ•˜๋‚˜์˜ ๊ธธ๋งŒ ์กด์žฌ
ํ•˜๋‚˜์˜ ๋ฃจํŠธ ๋…ธ๋“œ๊ฐ€ ์กด์žฌ
๋ฃจํ”„๊ฐ€ ํ—ˆ์šฉ๋˜์ง€ ์•Š์Œ
๊ณ„์ธต์  ๋ชจ๋ธ
๋‘ ์ •์ ์‚ฌ์ด์— ๋‘˜ ์ด์ƒ์˜ ๊ฒฝ๋กœ๊ฐ€ ํ—ˆ์šฉ๋จ
๋ฃจํŠธ ๋…ธ๋“œ๊ฐ€ ์—†์Œ
๋ฃจํ”„๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Œ
ํšŒ๋กœ๋ง ๋ชจ๋ธ

* ์šฉ์–ด์ •๋ฆฌ

- Edge(๊ฐ„์„ ): ๋‘ ๋…ธ๋“œ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ์„ 

- Level: ๋…ธ๋“œ๊ฐ€ ์†ํ•œ ํŠธ๋ฆฌ์˜ ๊นŠ์ด. ๋ฃจํŠธ๋…ธ๋“œ์˜ ๋ ˆ๋ฒจ 0, ๊ทธ ๋‹ค์Œ ์ž์‹ ๋…ธ๋“œ๋ถ€ํ„ฐ +1 Level

- Degree(์ฐจ์ˆ˜): ํ•œ ๋…ธ๋“œ๊ฐ€ ๊ฐ€์ง„ ์ž์‹์˜ ์ˆ˜. ex) ์œ„ ๊ทธ๋ฆผ์—์„œ A๋…ธ๋“œ์˜ Degree๋Š” 2๊ฐœ.

- Depth: ํŠธ๋ฆฌ์˜ ์ตœ๋Œ€ ๋ ˆ๋ฒจ(๋†’์ด). ๋ฃจํŠธ ๋…ธ๋“œ๋ถ€ํ„ฐ์˜ ๊ฑฐ๋ฆฌ.

- Terminal Node: Degree(์ฐจ์ˆ˜)๊ฐ€ 0์ธ ๋…ธ๋“œ. ํ„ฐ๋ฏธ๋„ ๋…ธ๋“œ์™€ ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€๋ฅผ ๋น„ํ„ฐ๋ฏธ๋„ ๋…ธ๋“œ๋ผ๊ณ  ๋ถ€๋ฆ„.

- ๋ถ€๋ชจ๋…ธ๋“œ: ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋œ ํ•œ๋‹จ๊ณ„ ์ƒ์œ„ ๋ ˆ๋ฒจ์˜ ๋…ธ๋“œ. ex) B์˜ ๋ถ€๋ชจ๋…ธ๋“œ: A

- ์ž์‹๋…ธ๋“œ: ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋œ ์„œ๋ธŒํŠธ๋ฆฌ์˜ ๋ฃจํŠธ๋…ธ๋“œ๋“ค. ex) A์˜ ์ž์‹ ๋…ธ๋“œ: B,C

- ํ˜•์ œ๋…ธ๋“œ: ๋ถ€๋ชจ๊ฐ€ ๊ฐ™์€ ๋…ธ๋“œ. ex) B, C

 

* ๊ตฌํ˜„๋ฐฉ๋ฒ•์€ Graph์™€ ๊ฐ™์Œ.

์ธ์ ‘๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•œ ๊ฐ€์ค‘์น˜์—†๋Š” Tree, ์ธ์ ‘๋ฆฌ์ŠคํŠธ์™€ pair๋ฅผ ์ด์šฉํ•œ ๊ฐ€์ค‘์น˜ ์žˆ๋Š” Tree.

728x90