3.5 Graph & 3.6 Tree

2024. 1. 27. 21:16ใ†CS - Roadmap.sh/3. Common Algorithms

CS - 3. Common Algorithms - 3.5 Graph & 3.6 Tree

Graph Algorithms

๐Ÿ’กGraphs in data structures are non-linear data structures made up of a finite number of nodes or vertices and the edges that connect them. Graphs in data structures are used to address real-world problems in which it represents the problem area as a network like telephone networks, circuit networks, and social networks.

๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์˜ ๊ทธ๋ž˜ํ”„๋Š” ํ•œ์ •๋œ ์ˆ˜์˜ ๋…ธ๋“œ ๋˜๋Š” ์ •์ ๊ณผ ์ด๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฐ€์žฅ์ž๋ฆฌ๋กœ ๊ตฌ์„ฑ๋œ ๋น„์„ ํ˜• ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์˜ ๊ทธ๋ž˜ํ”„๋Š” ์ „ํ™”๋ง, ํšŒ๋กœ๋ง, ์†Œ์…œ ๋„คํŠธ์›Œํฌ์™€ ๊ฐ™์ด ๋ฌธ์ œ ์˜์—ญ์„ ๋„คํŠธ์›Œํฌ๋กœ ํ‘œํ˜„ํ•˜๋Š” ์‹ค์ œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

 

Tree Algorithms

๐Ÿ’กA tree is non-linear and a hierarchical data structure consisting of a collection of nodes such that each node of the tree stores a value and a list of references to other nodes (the “children”).

Here is the list of common tree algorithms:

 

ํŠธ๋ฆฌ๋Š” ๋น„์„ ํ˜•์ ์ด๋ฉฐ ๋…ธ๋“œ ๋ชจ์Œ์œผ๋กœ ๊ตฌ์„ฑ๋œ ๊ณ„์ธต์  ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋กœ, ํŠธ๋ฆฌ์˜ ๊ฐ ๋…ธ๋“œ๊ฐ€ ๊ฐ’๊ณผ ๋‹ค๋ฅธ ๋…ธ๋“œ์— ๋Œ€ํ•œ ์ฐธ์กฐ ๋ชฉ๋ก("์ž์‹")์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

 

 

1.2 ๊ทธ๋ž˜ํ”„ ์™€ 1.8 ํŠธ๋ฆฌ๋ฅผ ์ž๋ฃŒ๊ตฌ์กฐ์—์„œ ๋‹ค๋ค˜์Šต๋‹ˆ๋‹ค.

https://parkpakrsu.tistory.com/327

 

Data structures (with python) - 2. Graph - 2.1. Directed graph

Data structures (with python) - 2. Graph - 2.1. Directed graph ๐Ÿ’ก A directed graph is graph, i.e., a set of objects (called vertices or nodes) that are connected together, where all the edges are directed from one vertex to another. A directed graph is s

parkpakrsu.tistory.com

https://parkpakrsu.tistory.com/336

 

Data structures (with python) - 8.Tree - 8.1 Binary Tree

Data structures (with python) - 8.Tree - 8.1 Binary Tree Tree ๐Ÿ’ก A tree is non-linear and a hierarchical data structure consisting of a collection of nodes such that each node of the tree stores a value and a list of references to other nodes (the “chi

parkpakrsu.tistory.com

๋‘˜๋‹ค node์™€ edge๋กœ ์ด๋ค„์ง„ ๋…ธ๋“œ๊ฐ„์˜ ๊ด€๊ฒŒ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ž๋ฃŒ ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค.

 

๊ทธ๋ž˜ํ”„์™€ ํŠธ๋ฆฌ๋Š” ๋ชจ๋‘ ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ด์ง€๋งŒ, ๊ทธ ์„ฑ๊ฒฉ๊ณผ ํŠน์ง•์— ์žˆ์–ด์„œ ๋ช‡ ๊ฐ€์ง€ ์ค‘์š”ํ•œ ์ฐจ์ด์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ž˜ํ”„(Graph): ๊ทธ๋ž˜ํ”„๋Š” ๋…ธ๋“œ(Node)์™€ ์ด๋“ค์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฐ„์„ (Edge)์œผ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. 

๊ทธ๋ž˜ํ”„๋Š” ๋…ธ๋“œ ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฐ ์œ ์šฉํ•˜๋ฉฐ, ๋…ธ๋“œ์™€ ๊ฐ„์„ ์€ ๊ฐ๊ฐ ๊ฐ์ฒด์™€ ๊ฐ์ฒด ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๊ทธ๋ž˜ํ”„๋Š” ๋ฐฉํ–ฅ์„ฑ์ด ์žˆ์„ ์ˆ˜๋„ (๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„), ์—†์„ ์ˆ˜๋„ (๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„) ์žˆ์œผ๋ฉฐ, ์ˆœํ™˜(์‚ฌ์ดํด) ๊ตฌ์กฐ๋ฅผ ๊ฐ€์งˆ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ๊ทธ๋ž˜ํ”„์—์„œ๋Š” ์‹œ์ž‘ ๋…ธ๋“œ์—์„œ ์ถœ๋ฐœํ•ด ๋‹ค์‹œ ๊ฐ™์€ ๋…ธ๋“œ๋กœ ๋Œ์•„์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


ํŠธ๋ฆฌ(Tree): ํŠธ๋ฆฌ๋Š” ๊ทธ๋ž˜ํ”„์˜ ํ•œ ์ข…๋ฅ˜๋กœ, ๋…ธ๋“œ์™€ ๊ฐ„์„ ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์ง€๋งŒ, ๋ช‡ ๊ฐ€์ง€ ์ถ”๊ฐ€์ ์ธ ์ œ์•ฝ ์กฐ๊ฑด์ด ์žˆ์Šต๋‹ˆ๋‹ค.

ํŠธ๋ฆฌ๋Š” ํ•œ ๊ฐœ์˜ ๋ฃจํŠธ ๋…ธ๋“œ์—์„œ ์‹œ์ž‘๋˜๋ฉฐ, ๋ฃจํŠธ ๋…ธ๋“œ์—์„œ ๋‹ค๋ฅธ ๋ชจ๋“  ๋…ธ๋“œ๋กœ์˜ ๊ฒฝ๋กœ๊ฐ€ ์œ ์ผํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ํŠธ๋ฆฌ๊ฐ€ ์ˆœํ™˜ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์—†์Œ์„ ์˜๋ฏธํ•˜๋ฉฐ, ๋”ฐ๋ผ์„œ ํŠธ๋ฆฌ์—์„œ๋Š” ์–ด๋–ค ๋…ธ๋“œ์—์„œ ์‹œ์ž‘ํ•ด ๋‹ค์‹œ ๊ฐ™์€ ๋…ธ๋“œ๋กœ ๋Œ์•„์˜ค๋Š” ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, ํŠธ๋ฆฌ๋Š” ๋ฐฉํ–ฅ์„ฑ์ด ์žˆ์œผ๋ฉฐ, ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฃจํŠธ ๋…ธ๋“œ์—์„œ ๋‹ค๋ฅธ ๋…ธ๋“œ๋กœ '๋‚ด๋ ค๊ฐ€๋Š”' ๋ฐฉํ–ฅ์„ฑ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค.


์ด๋Ÿฌํ•œ ์ฐจ์ด์ ๋“ค๋กœ ์ธํ•ด, ๊ทธ๋ž˜ํ”„์™€ ํŠธ๋ฆฌ๋Š” ๊ฐ๊ฐ ๋‹ค๋ฅธ ์ข…๋ฅ˜์˜ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐ ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. 

๊ทธ๋ž˜ํ”„๋Š” ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ๋ฅผ ๋ชจ๋ธ๋งํ•˜๊ฑฐ๋‚˜ ์ตœ๋‹จ ๊ฒฝ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐ,

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

 

 

์•ž์œผ๋กœ ๋‹ค๋ค„๋ณผ ๊ทธ๋ž˜ํ”„ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ํŠธ๋ฆฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์ข…๋ฅ˜๋Š” ์ด๋ ‡์Šต๋‹ˆ๋‹ค.

 

Graph algorithm

- Breath First Search

- Depth First Search

- Bellman Ford's Algorithm

- Dijkstra's Algorithm

- A* Algorithm

 

Tree algorithm
- Pre-Order Traversal
- In-Order Traversal
- Post-Order Traversal
- Breadth First Search
- Depth First Search

 

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

728x90

'CS - Roadmap.sh > 3. Common Algorithms' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

3.5 Graph - 3.5.2 Depth First Search(DFS)  (1) 2024.01.29
3.5 Graph - 3.5.1 Breadth First Search(BFS)  (0) 2024.01.28
3.4. Cache - 3.4.3 MFU Cache  (0) 2024.01.27
3.4. Cache - 3.4.2 LFU Cache  (2) 2024.01.26
3.4. Cache - 3.4.1 LRU Cache  (2) 2024.01.25