Skip to content

Commit

Permalink
Merge pull request #48 from Seogeurim/DataStructure/Seogeurim
Browse files Browse the repository at this point in the history
Data structure/seogeurim
  • Loading branch information
ggjae authored Jul 12, 2021
2 parents c8607da + d191039 commit b620bf5
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
3 changes: 3 additions & 0 deletions contents/algorithm/basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -439,3 +439,6 @@ public class SubsetTest {
๋™์  ๊ณ„ํš๋ฒ•์€ ์ฃผ์–ด์ง„ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ•˜์œ„ ๋ฌธ์ œ๋กœ ๋‚˜๋ˆ„์–ด ํ‘ผ ๋‹ค์Œ ๊ฒฐํ•ฉํ•˜์—ฌ ๋‹ต์„ ์ฐพ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ์œ„ํ•ด์„œ๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ํ•˜์œ„ ๋ฌธ์ œ๋กœ ๋‚˜๋ˆ„์–ด ๋ณด๊ณ  ์ฃผ์–ด์ง„ ์‹œ๊ฐ„ ๋ณต์žก๋„ ๋‚ด์—์„œ ์ˆ˜ํ–‰์ด ๊ฐ€๋Šฅํ•œ ์ตœ์ ์˜ **์ ํ™”์‹**์„ ์ฐพ๋Š” ๊ฒƒ์ด ํ•ต์‹ฌ์ด๋‹ค.

๋™์  ๊ณ„ํš๋ฒ•์„ ์ ์šฉํ•œ ๋Œ€ํ‘œ์ ์ธ ๋ฌธ์ œ๋กœ๋Š” ๋ฐฐ๋‚ญ ์ง์‹ธ๊ธฐ ๋ฌธ์ œ(Knapsack Problem), ์ตœ์žฅ์ฆ๊ฐ€์ˆ˜์—ด(LIS, Longest Increasing Subsequence), ์™ธํŒ์› ์ˆœํšŒ ๋ฌธ์ œ(TSP, Traveling Salesman Problem) ๋“ฑ์ด ์žˆ๋‹ค. ์ด ๋ฐ–์—๋„ memoization ๋ฐ ์ ํ™”์‹์„ ํ†ตํ•ด ๋‹ค์–‘ํ•œ ๋ฌธ์ œ๋ฅผ ํ›จ์”ฌ ํšจ์œจ์ ์ธ ๋ฐฉ์‹์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

> **"ํฐ ๋ฌธ์ œ๋ฅผ ์—ฌ๋Ÿฌ๊ฐœ์˜ ํ•˜์œ„ ๋ฌธ์ œ๋กœ ๋‚˜๋ˆ„์–ด ํ‘ผ๋‹ค"** ๋ผ๋Š” ์ ์—์„œ
> DP๋Š” ๊ทธ ํ•˜์œ„์˜ ๊ฐ’์ด ๊ณ„์† ๋ณ€ํ•˜๊ณ , DAC๋Š” ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ฐจ์ด์ ์ด ์žˆ๋‹ค.
85 changes: 84 additions & 1 deletion contents/data-structure/basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,87 @@ String[] name = {"Stacy", "Tracy", "Dorothy"};

## Graph (๊ทธ๋ž˜ํ”„)

### Graph ์šฉ์–ด
## ๊ทธ๋ž˜ํ”„ ์ •์˜

ํ˜„์‹ค์„ธ๊ณ„์˜ ์‚ฌ๋ฌผ์ด๋‚˜ ๊ฐœ๋… ๊ฐ„์˜ **์—ฐ๊ฒฐ ๊ด€๊ณ„**๋ฅผ ์ˆ˜ํ•™์  ๋ชจ๋ธ๋กœ ๋‹จ์ˆœํ™”ํ•˜์—ฌ ํ‘œํ˜„ํ•œ ๊ฒƒ

- V : ์ •์  (Vertex / Node)
- E : ๊ฐ„์„  (Edge / Link / Arc)
- ๊ทธ๋ž˜ํ”„ G = (V, E)

## ๊ทธ๋ž˜ํ”„ ์šฉ์–ด

1. **์ •์ , ๋…ธ๋“œ (Vertex, Node)**
2. **๊ฐ„์„  (Edge)**
- ๋ฌดํ–ฅ ๊ฐ„์„  (Undirected Edge) : ๋ฐฉํ–ฅ์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฐ„์„ , ์–‘๋ฐฉํ–ฅ
- ์œ ํ–ฅ ๊ฐ„์„  (Directed Edge) : ๋ฐฉํ–ฅ์ด ์กด์žฌํ•˜๋Š” ๊ฐ„์„ 
3. **์ธ์ ‘ (Adjacent)** : (์ •์  ๊ด€์ ) ๋‘ ์ •์  A, B ์‚ฌ์ด์— ๊ฐ„์„ ์ด ์กด์žฌํ•œ๋‹ค๋ฉด A, B๋Š” ์ธ์ ‘ํ•œ๋‹ค.
4. **๋ถ€์† (Incident)** : (๊ฐ„์„  ๊ด€์ ) ๋‘ ์ •์  A, B ์‚ฌ์ด์— ๊ฐ„์„  e๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด ๊ฐ„์„  e๋Š” ์ •์  A, B์— ๋ถ€์†ํ•œ๋‹ค.
5. **์ฐจ์ˆ˜ (Degree)** : ํ•œ ์ •์ ์— ์—ฐ๊ฒฐ๋œ ๊ฐ„์„ ์˜ ์ˆ˜
- (๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„) in-degree : ์ •์ ์— ๋“ค์–ด์˜ค๋Š” ๊ฐ„์„ ์˜ ์ˆ˜, out-degree : ๋‚˜๊ฐ€๋Š” ๊ฐ„์„ ์˜ ์ˆ˜
6. **์ž๊ธฐ ๊ฐ„์„ ๊ณผ ๋‹ค์ค‘ ๊ฐ„์„ **
- ์ž๊ธฐ ๊ฐ„์„  (Self-loop) : ์ž์‹ ์œผ๋กœ ๋‹ค์‹œ ๋Œ์•„์˜ค๋Š” ๊ฐ„์„ 
- ๋‹ค์ค‘ ๊ฐ„์„  (Multiple / Parallel edges) : ๋‘ ๊ฐœ ์ด์ƒ์˜ ๊ฐ„์„ ์ด ๋˜‘๊ฐ™์€ ๋‘ ์ •์ ์— ๋ถ€์†ํ•  ๋•Œ
7. **๊ฒฝ๋กœ (Path)** : ์ •์  + ๊ฐ„์„ ์ด ๊ต๋Œ€๋กœ ๊ตฌ์„ฑ๋œ sequence
- ๋‹จ์ˆœ ๊ฒฝ๋กœ (Simple Path) : ๊ฐ™์€ ์ •์ ์„ ๋‘ ๋ฒˆ ์ด์ƒ ๊ฐ€์ง€ ์•Š๋Š” ๊ฒฝ๋กœ
8. **ํšŒ๋กœ (Cycle)** : A ์ •์ ์—์„œ ์ถœ๋ฐœํ–ˆ์„ ๋•Œ ๋‹ค์‹œ A ์ •์ ์œผ๋กœ ๋Œ์•„์˜ค๋Š” ๊ฒฝ๋กœ
- ๋‹จ์ˆœ ํšŒ๋กœ (Simple Cycle) : ๊ฐ™์€ ์ •์ ์„ ๋‘ ๋ฒˆ ์ด์ƒ ๊ฐ€์ง€ ์•Š๋Š” ์‹ธ์ดํด
9. **์—ฐ๊ฒฐ๋จ (Connected)** : ์ •์  A์—์„œ ์ •์  B๋กœ์˜ ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•  ๋•Œ A์™€ B๋Š” ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค.

## ๊ทธ๋ž˜ํ”„ ์ข…๋ฅ˜

1. **๋ฌดํ–ฅ ๊ทธ๋ž˜ํ”„ (Undirected Graph)** : ๋ฌด๋ฐฉํ–ฅ ๊ฐ„์„ ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๊ทธ๋ž˜ํ”„
2. **์œ ํ–ฅ ๊ทธ๋ž˜ํ”„ (Directed Graph)** : ๋ฐฉํ–ฅ ๊ฐ„์„ ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๊ทธ๋ž˜ํ”„
3. **๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„ (Weighted Graph)** : ๊ฐ€์ค‘์น˜(๋น„์šฉ)๋ฅผ ๊ฐ–๋Š” ๊ฐ„์„ ๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ ๊ทธ๋ž˜ํ”„
4. **์ •๊ทœ ๊ทธ๋ž˜ํ”„ (Regular Graph)** : ๋ชจ๋“  ์ •์ ์ด ๋™์ผํ•œ ์ฐจ์ˆ˜๋ฅผ ๊ฐ€์ง€๋Š” ๊ทธ๋ž˜ํ”„
5. **์™„์ „ ๊ทธ๋ž˜ํ”„ (Complete Graph)** : ๋ชจ๋“  ์ •์ ์ด ์„œ๋กœ ์ธ์ ‘ํ•ด์žˆ๋Š” ๊ทธ๋ž˜ํ”„, ์™„์ „ ๊ทธ๋ž˜ํ”„๋Š” ์ •๊ทœ ๊ทธ๋ž˜ํ”„
6. **์—ฐ๊ฒฐ ๊ทธ๋ž˜ํ”„ (Connected Graph)** : ๋ชจ๋“  ์ •์ ์ด ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์–ด์„œ ๋ชจ๋“  ์ •์ ๋ผ๋ฆฌ ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ทธ๋ž˜ํ”„
7. **๋ถ€๋ถ„ ๊ทธ๋ž˜ํ”„** : ์–ด๋–ค ๊ทธ๋ž˜ํ”„์˜ ๋ถ€๋ถ„ ๋ถ€๋ถ„
8. **ํŠธ๋ฆฌ ๊ทธ๋ž˜ํ”„** : ์‹ธ์ดํด์„ ๊ฐ€์ง€์ง€ ์•Š๋Š” ์—ฐ๊ฒฐ ๊ทธ๋ž˜ํ”„, ๋ชจ๋“  ์ •์ ์— ๋Œ€ํ•ด์„œ ๊ฒฝ๋กœ๊ฐ€ ์ •ํ™•ํžˆ 1๊ฐœ ์กด์žฌํ•œ๋‹ค.

## ๊ทธ๋ž˜ํ”„ ํ‘œํ˜„

๊ทธ๋ž˜ํ”„์˜ ํ‘œํ˜„ ๋ฐฉ์‹์—๋Š” **๊ฐ„์„  ๋ฆฌ์ŠคํŠธ, ์ธ์ ‘ ํ–‰๋ ฌ, ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ** 3๊ฐ€์ง€ ๋ฐฉ์‹์ด ์žˆ๋‹ค.

> ์ •์  ๊ฐœ์ˆ˜ : V๊ฐœ, ๊ฐ„์„  ๊ฐœ์ˆ˜ : E๊ฐœ
### ๊ฐ„์„  ๋ฆฌ์ŠคํŠธ (Edge List)

- E x 2 (or E x 3) ์ด์ฐจ์› ๋ฐฐ์—ด A์— ์ •๋ณด๋ฅผ ์ €์žฅํ•œ๋‹ค.
- ๋‘ ์ •์  x, y ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฐ„์„  k์— ๋Œ€ํ•ด์„œ A[k][0] = x, A[k][1] = y
- ๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„์˜ ๊ฒฝ์šฐ A[k][2] ์— ๊ฐ€์ค‘์น˜ ์ •๋ณด๋ฅผ ์ €์žฅํ•œ๋‹ค.

![แ„€แ…กแ†ซแ„‰แ…ฅแ†ซแ„…แ…ตแ„‰แ…ณแ„แ…ณ](./img/๊ทธ๋ž˜ํ”„.001.jpeg)


### ์ธ์ ‘ ํ–‰๋ ฌ (Adjacency Matrix)

- V x V ์ด์ฐจ์› ๋ฐฐ์—ด A์— ์ •๋ณด๋ฅผ ์ €์žฅํ•œ๋‹ค.
- Vi, Vj๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฐ„์„ ์ด ์กด์žฌํ•œ๋‹ค๋ฉด A[i][j] = 1, ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด A[i][j] = 0
- ๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„์˜ ๊ฒฝ์šฐ 1 ๋Œ€์‹  ๊ฐ€์ค‘์น˜ ์ •๋ณด๋ฅผ ์ €์žฅํ•œ๋‹ค.

> ๋ฉ”๋ชจ๋ฆฌ ๋ณต์žก๋„๊ฐ€ V<sup>2</sup> ์ด๊ธฐ ๋•Œ๋ฌธ์— V์˜ ๊ฐ’์ด ํด ๊ฒฝ์šฐ ์“ฐ์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
> 100 ์ดํ•˜์˜ ๊ฐ’์ผ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
![แ„‹แ…ตแ†ซแ„Œแ…ฅแ†ธแ„’แ…ขแ†ผแ„…แ…งแ†ฏ](./img/๊ทธ๋ž˜ํ”„.002.jpeg)

### ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ (Adjacent List)

- V ๊ฐœ์˜ Linked List๋กœ ๊ทธ๋ž˜ํ”„ ์ •๋ณด๋ฅผ ์ €์žฅํ•œ๋‹ค.
- ๊ฐ€์ค‘์น˜ ๊ทธ๋ž˜ํ”„์˜ ๊ฒฝ์šฐ (์ •์  ์ •๋ณด, ๊ฐ€์ค‘์น˜ ์ •๋ณด)๋ฅผ ํ•จ๊ป˜ ์ €์žฅํ•œ๋‹ค. (C++ : pair, Java : class)

![แ„‹แ…ตแ†ซแ„Œแ…ฅแ†ธแ„…แ…ตแ„‰แ…ณแ„แ…ณ](./img/๊ทธ๋ž˜ํ”„.003.jpeg)

### ๊ทธ๋ž˜ํ”„ ํ‘œํ˜„ ๋ฐฉ์‹ ๋น„๊ต

> ์ •์  ๊ฐœ์ˆ˜ : V๊ฐœ, ๊ฐ„์„  ๊ฐœ์ˆ˜ : E๊ฐœ
| | ๊ฐ„์„  ๋ฆฌ์ŠคํŠธ | ์ธ์ ‘ ํ–‰๋ ฌ | ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ |
| :---: | :------: | :-------------: | :------------: |
| ๊ณต๊ฐ„ | E | V<sup>2</sup> | V + E |
| ์ •์  Va ์˜ ๋ถ€์† ๊ฐ„์„  | E | V | Va ์ฐจ์ˆ˜ |
| ์ •์  Va, Vb ์˜ ์ธ์ ‘ ์—ฌ๋ถ€ | E | 1 | min(Va ์ฐจ์ˆ˜, Vb ์ฐจ์ˆ˜) |
| ์ •์  ์‚ฝ์ž… | 1 | V<sup>2</sup> | 1 |
| ๊ฐ„์„  ์‚ฝ์ž… | 1 | 1 | 1 |


Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b620bf5

Please sign in to comment.