Data structures (with python) - 2. Graph - 2.4. Graph Representation

2024. 1. 4. 18:39ใ†CS - Roadmap.sh/1. Data structures

Data structures (with python) - 2. Graph - 2.4. Graph Representation

 

๐Ÿ’ก A graph can either be represented as an adjacency matrix or an adjacency list.

The adjacency matrix is a 2D array of size V x V where V is the number of vertices in a graph. Let the 2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j.

Adjacency list is an array of vectors. Size of the array is equal to the number of vertices. Let the array be array[]. An entry array[i] represents the list of vertices adjacent to the ith vertex. This representation can also be used to represent a weighted graph. The weights of edges can be represented as lists of pairs.

 

๊ทธ๋ž˜ํ”„๋Š” ์ธ์ ‘์„ฑ ํ–‰๋ ฌ ๋˜๋Š” ์ธ์ ‘์„ฑ list์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ธ์ ‘ ํ–‰๋ ฌ์€ V x V ํฌ๊ธฐ์˜ 2D ๋ฐฐ์—ด๋กœ, ์—ฌ๊ธฐ์„œ V๋Š” ๊ทธ๋ž˜ํ”„์˜ ์ •์  ์ˆ˜์ž…๋‹ˆ๋‹ค. 2D ๋ฐฐ์—ด์„ adj[][]๋ผ๊ณ  ํ•  ๋•Œ, ์Šฌ๋กฏ adj[i][j] = 1์€ ์ •์  i์—์„œ ์ •์  j๊นŒ์ง€ ์—์ง€๊ฐ€ ์žˆ์Œ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

์ธ์ ‘์„ฑ list๋Š” ๋ฒกํ„ฐ์˜ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค. ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋Š” ์ •์ ์˜ ์ˆ˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ฐฐ์—ด์„ array[]๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ•ญ๋ชฉ array[i]๋Š” ์งธ ์ •์ ์— ์ธ์ ‘ํ•œ ์ •์ ์˜ list์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ด ํ‘œํ˜„์€ ๊ฐ€์ค‘ ๊ทธ๋ž˜ํ”„๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐ์—๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ€์žฅ์ž๋ฆฌ์˜ ๊ฐ€์ค‘์น˜๋Š” ์Œ์˜ list์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

Graph Representation

๊ทธ๋ž˜ํ”„๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ํฌ๊ฒŒ 2๊ฐ€์ง€ ๊ฐ€ ์žˆ๋‹ค.

1. adjacency matrix

2. adjacency list

 

1. ์ธ์ ‘ํ–‰๋ ฌ (Adjacency matrix)๋Š” ๊ทธ๋ž˜ํ”„์˜ node๋“ค์ด ์„œ๋กœ ์–ด๋–ป๊ฒŒ ์—ฐ๊ฒฐ ๋˜์–ด ์žˆ๋Š”์ง€๋ฅผ ์ด์ฐจ์› array ๋กœ ํ‘œํ˜„ ํ•œ ๊ฒƒ

  - ํ–‰๊ณผ ์—ด์ด ๊ฐ๊ฐ ๊ทธ๋ž˜ํ”„์˜ ๋…ธ๋“œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ๋…ธ๋“œ i ์™€ ๋…ธ๋“œ j๊ฐ€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค๋ฉด matrix[i][j] (๊ทธ๋ฆฌ๊ณ  ๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์˜ ๊ฒฝ์šฐ matrix[j][i]) ์œ„์น˜์—๋Š” 1์„, ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค๋ฉด 0์„ ๋„ฃ๋Š”๋‹ค.

  - ์ธ์ ‘ ํ–‰๋ ฌ์€ ๊ทธ๋ž˜ํ”„์˜ ๋ชจ๋“  ๋…ธ๋“œ์— ๋Œ€ํ•ด ์—ฐ๊ฒฐ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๋ฏ€๋กœ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ์ด ๋งŽ์ง€๋งŒ, ๋‘ ๋…ธ๋“œ ๊ฐ„์˜ ์—ฐ๊ฒฐ ์—ฌ๋ถ€๋ฅผ ๋ฐ”๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.

 

ํŒŒ์ด์ฌ์—์„œ adjacency matrix๋Š” 2์ฐจ์› list๋ฅผ ์‚ฌ์šฉํ•ด ๊ตฌํ˜„ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

matrix = [[0, 1, 1, 0, 0],  # ๋…ธ๋“œ 0์€ ๋…ธ๋“œ 1, 2์™€ ์—ฐ๊ฒฐ
          [1, 0, 1, 1, 1],  # ๋…ธ๋“œ 1์€ ๋…ธ๋“œ 0, 2, 3, 4์™€ ์—ฐ๊ฒฐ
          [1, 1, 0, 0, 1],  # ๋…ธ๋“œ 2๋Š” ๋…ธ๋“œ 0, 1, 4์™€ ์—ฐ๊ฒฐ
          [0, 1, 0, 0, 1],  # ๋…ธ๋“œ 3์€ ๋…ธ๋“œ 1, 4์™€ ์—ฐ๊ฒฐ
          [0, 1, 1, 1, 0]]  # ๋…ธ๋“œ 4๋Š” ๋…ธ๋“œ 1, 2, 3๊ณผ ์—ฐ๊ฒฐ

2-2 undirected graph์˜ ์˜ˆ์‹œ์˜€๋˜  5๊ฐœ์˜ ๋…ธ๋“œ๋ฅผ ๊ฐ€์ง„ ๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„(undirected graph)๋ฅผ ์ธ์ ‘ ํ–‰๋ ฌ๋กœ ํ‘œํ˜„ํ•œ ์˜ˆ์‹œ.

 

2. ์ธ์ ‘๋ฆฌ์ŠคํŠธ (Adjacency List)๋Š” ๊ทธ๋ž˜ํ”„์˜ ๊ฐ ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ํ‘œํ˜„ํ•œ ๊ฒƒ.

  - ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋Š” ๊ฐ ๋…ธ๋“œ์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

  - ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋Š” ์ธ์ ‘ ํ–‰๋ ฌ์— ๋น„ํ•ด ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ์ด ์ ์ง€๋งŒ, ๋‘ ๋…ธ๋“œ ๊ฐ„์˜ ์—ฐ๊ฒฐ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ๋ฆฌ์ŠคํŠธ๋ฅผ ํƒ์ƒ‰ํ•ด์•ผ ํ•˜๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.

 

ํŒŒ์ด์ฌ์—์„œ ๊ตฌํ˜„์€ list ์˜ list๋กœ ๊ตฌํ˜„ ๊ฐ€๋Šฅํ•˜๋‹ค.

list = [[1, 2],     # ๋…ธ๋“œ 0์€ ๋…ธ๋“œ 1, 2์™€ ์—ฐ๊ฒฐ
        [0, 2, 3, 4],  # ๋…ธ๋“œ 1์€ ๋…ธ๋“œ 0, 2, 3, 4์™€ ์—ฐ๊ฒฐ
        [0, 1, 4],  # ๋…ธ๋“œ 2๋Š” ๋…ธ๋“œ 0, 1, 4์™€ ์—ฐ๊ฒฐ
        [1, 4],     # ๋…ธ๋“œ 3์€ ๋…ธ๋“œ 1, 4์™€ ์—ฐ๊ฒฐ
        [1, 2, 3]]  # ๋…ธ๋“œ 4๋Š” ๋…ธ๋“œ 1, 2, 3๊ณผ ์—ฐ๊ฒฐ

์™œ list์˜ list๋ผ๊ณ  ๋งํ–ˆ๋ƒ๋ฉด, array ์˜ ํ˜•ํƒœ๋ผ๊ธฐ๋ณด๋‹ค ๋…ธ๋“œ์˜ ์—ฐ๊ฒฐ์„ ํ‘œํ˜„ํ•œ list ๋“ค์˜ list ์ด๊ธฐ ๋–„๋ฌธ.

 

๊ทธ๋ ‡๋‹ค๋ฉด 

adjacency matrix ์™€ adjacency list์˜ ์ฐจ์ด์ ์€ ๋ฌด์—‡์ผ๊นŒ?

 

1. ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰

  - ์ธ์ ‘ ํ–‰๋ ฌ์€ ๊ทธ๋ž˜ํ”„์˜ ๋ชจ๋“  ๋…ธ๋“œ ๊ฐ„์˜ ์—ฐ๊ฒฐ ๊ด€๊ณ„๋ฅผ ์ €์žฅํ•˜๋ฏ€๋กœ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ๋งŽ๋‹ค.

๋…ธ๋“œ์˜ ์ˆ˜๊ฐ€ n ์ผ ๋•Œ, ์ธ์ ‘ ํ–‰๋ ฌ์€ n^2 ์˜ ๊ณต๊ฐ„์„ ์ฐจ์ง€ํ•œ๋‹ค.

  - ์ธ์ ‘ list๋Š” ๋…ธ๋“œ์˜ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ํƒ์ƒ‰ํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๋‘ ๋…ธ๋“œ์˜ ์—ฐ๊ฒฐ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๋Š” ๋ฐ์— ๋” ๋งŽ์€ ์‹œ๊ฐ„์ด ์†Œ์š”๋œ๋‹ค.

  ์ตœ์•…์˜ ๊ฒฝ์šฐ O(n)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง„๋‹ค.

 

โ€ปO(n) ์€ big-O ํ‘œ๊ธฐ๋ฒ•์œผ๋กœ ์ ๊ทผ์  ๋ถ„์„์œผ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์ˆ˜ํ–‰ ์‹œ๊ฐ„์„ ๋ถ„์„ํ• ๋–„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ์ถ”ํ›„ ๋‹ค๋ฃฌ๋‹ค.

 

2. ์—ฐ์‚ฐ ์†๋„:

  - ์ธ์ ‘ ํ–‰๋ ฌ์€ ๋‘ ๋…ธ๋“œ ๊ฐ„์˜ ์—ฐ๊ฒฐ ์—ฌ๋ถ€๋ฅผ ๋ฐ”๋กœ ์กฐํšŒํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ด ์ž‘์—…์˜ ์†๋„๊ฐ€ ๋น ๋ฅด๋‹ค.
์ฆ‰ ๋…ธ๋“œ A์™€ ๋…ธ๋“œ B๊ฐ€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ์ž‘์—…์€ O(1)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง„๋‹ค.

  - ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋Š” ๋…ธ๋“œ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ํƒ์ƒˆํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๋‘ ๋…ธ๋“œ์˜ ์—ฐ๊ฒฐ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๋Š” ๋ฐ ๋” ๋งŽ์€ ์‹œ๊ฐ„์ด ์†Œ์š”๋œ๋‹ค.

 ์ตœ์•…์˜ ๊ฒฝ์šฐ O(n)์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐ€์ง„๋‹ค.

 

3. ๊ทธ๋ž˜ํ”„์˜ ์ข…๋ฅ˜

  - ์ธ์ ‘ ํ–‰๋ ฌ์€ ์ฃผ๋กœ ์™„์ „ ๊ทธ๋ž˜ํ”„์—์„œ ์‚ฌ์šฉ๋˜๋ฉฐ, ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋Š” ์ตœ์†Œ ๊ทธ๋ž˜ํ”„์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค.

์™„์ „ ๊ทธ๋ž˜ํ”„ : ๋ชจ๋“  ๋…ธ๋“œ๊ฐ€ ์„œ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๊ทธ๋ž˜ํ”„,

์ตœ์†Œ ๊ทธ๋ž˜ํ”„ : ๋Œ€๋ถ€๋ถ„์˜ ๋…ธ๋“œ๊ฐ€ ์„œ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์ง€ ์•Š์€ ๊ทธ๋ž˜ํ”„.

 

์ธ์ ‘ ํ–‰๋ ฌ๊ณผ ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ์˜ ์ •์˜์™€ ํ˜•ํƒœ๋ฅผ ๋ณด๋ฉด ์ž์—ฐ์Šค๋ ˆ ์ดํ•ด๊ฐˆ ๊ฒƒ์ด๋‹ค.

 

appendix

์ข€ ๋” ๊นŠ์€ ํ™œ์šฉ์˜ ์˜ˆ๋ฅผ ๋“ค์ž๋ฉด,

 

1. ์ธ์ ‘ํ–‰๋ ฌ :

 - ํ”Œ๋กœ์ด๋“œ-์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜(Floyd-Warshall) : ๋ชจ๋“  ๋…ธ๋“œ ์Œ์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜

 -  ๊ทธ๋ž˜ํ”„๊ฐ€ ์ž‘๊ณ  ๋…ธ๋“œ ๊ฐ„์˜ ์—ฐ๊ฒฐ ์—ฌ๋ถ€๋ฅผ ์ž์ฃผ ํ™•์ธํ•ด์•ผ ํ•˜๋Š” ๊ฒจ์šฐ ์ธ์ ‘ ํ–‰๋ ฌ์„ ์‚ฌ์šฉ

 

2. ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ:

  - ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜(Dikstra's Algoritm), ๋ฒจ๋งŒ-ํฌ๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜(Bellman-Ford) ๊ฐ™์€ ์ตœ๋‹จ ๊ฒฝ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜

  - ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰(DFS), ๋„“์ด ์šฐ์„  ํƒ์ƒ‰(BFS) ๊ฐ™์€ ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ๋„ ํšจ๊ณผ์ .

728x90