2024. 1. 2. 21:43ใCS - Roadmap.sh/1. Data structures
Data structures (with python) - 2. Graph - 2.2. Undirected graph
๐ก An undirected graph is graph, i.e., a set of objects (called vertices or nodes) that are connected together, where all the edges are bidirectional. An undirected graph is sometimes called an undirected network. In contrast, a graph where the edges point in a direction is called a directed graph.
๋ฐฉํฅ์ฑ ์๋ ๊ทธ๋ํ๋ ๊ทธ๋ํ, ์ฆ ๋ชจ๋ ๊ฐ์ฅ์๋ฆฌ๊ฐ ์๋ฐฉํฅ์ธ ๊ฐ์ฒด(์ ์ ๋๋ ๋ ธ๋๋ผ๊ณ ํจ)์ ์งํฉ์ผ๋ก ์๋ก ์ฐ๊ฒฐ๋์ด ์๋ ๊ทธ๋ํ์ ๋๋ค. ๋ฐฉํฅ์ฑ ์๋ ๊ทธ๋ํ๋ฅผ ๋ฐฉํฅ์ฑ ์๋ ๋คํธ์ํฌ๋ผ๊ณ ๋ ํฉ๋๋ค. ์ด์๋ ๋์กฐ์ ์ผ๋ก, ๊ฐ์ฅ์๋ฆฌ๊ฐ ํ ๋ฐฉํฅ์ ๊ฐ๋ฆฌํค๋ ๊ทธ๋ํ๋ฅผ ๋ฐฉํฅ์ฑ ๊ทธ๋ํ๋ผ๊ณ ํฉ๋๋ค.
ํ์ด์ฌ์์ ์ญ์ undirected graph ๋ฅผ ์ค๋ช
ํ๋ ๋ฐฉ๋ฒ์
์์ directed graph์์ ์ค๋ช
ํ ๋ ์ฌ์ฉํ๋ ์ธ์ ๋ฆฌ์คํธ(Adjacency List)์ ์ธ์ ํ๋ ฌ(Adjacency Matrix)๋ฅผ ์ด์ฉํ๋ค.
1. ์ธ์ ๋ฆฌ์คํธ (Adjacency List)
- ์ธ์ ๋ฆฌ์คํธ๋ ๊ทธ๋ํ์ ๊ฐ ๋ ธ๋์ ๋ํ ์ธ์ ๋ ธ๋์ ๋ฆฌ์คํธ๋ฅผ ์ ์ฅํ๋ ๋ฐฉ์.
- ํ์ด์ฌ์์๋ ๋์ ๋๋ฆฌ์ ๋ฆฌ์คํธ๋ฅผ ์ด์ฉํด์ ์ธ์ ๋ฆฌ์คํธ๋ฅผ ํํํ ์ ์๋ค.
graph = {
'A': ['B', 'C'],
'B': ['A', 'D', 'E'],
'C': ['A', 'F'],
'D': ['B'],
'E': ['B', 'F'],
'F': ['C', 'E'],
}
์ด graph๋ 'A':['B','C']๋ A ๋ ธ๋๊ฐ B์ C ๋ ธ๋์ ์ฐ๊ฒฐ๋์ด ์๋ฐ๋ ๊ฒ์ ์๋ฏธํ๋ค.
2. ์ธ์ ํ๋ ฌ (Adjacency Matrix)
- ์ธ์ ํ๋ ฌ์ ๊ทธ๋ํ์ ๊ฐ ๋ ธ๋๊ฐ ๋ค๋ฅธ ๋ ธ๋์ ์ฐ๊ฒฐ๋์ด ์๋์ง๋ฅผ ์ด์ง๊ฐ(0 ๋๋ 1)์ผ๋ก ํํํ 2์ฐจ์ ํ๋ ฌ
- ํ์ด์ฌ์์๋ 2์ฐจ์ ๋ฆฌ์คํธ๋ฅผ ์ด์ฉํด ์ธ์ ํ๋ ฌ์ ํํํ ์ ์๋ค.
graph = [
[0, 1, 1, 0, 0, 0],
[1, 0, 0, 1, 1, 0],
[1, 0, 0, 0, 0, 1],
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 1],
[0, 0, 1, 0, 1, 0]
]
์ด ์ธ์ ํ๋ ฌ์ 6๊ฐ์ ๋ ธ๋๊ฐ ์๋ ๋ฌด๋ฐฉํฅ ๊ทธ๋ํ์ด๋ค.
๋ ธ๋ 0 - 1,2 ์ฐ๊ฒฐ
1 - 0,3,4 ์ฐ๊ฒฐ
2 - 0,5 ์ฐ๊ฒฐ
3 - 1 ์ฐ๊ฒฐ
4 - 1,5 ์ฐ๊ฒฐ
5 - 2,4 ์ฐ๊ฒฐ
(์ฐจํ ๊ทธ๋ฆผ ์ถ๊ฐ)
class UndirectedGraph:
def __init__(self):
self.graph = {}
def add_edge(self, node, neighbour):
if node not in self.graph:
self.graph[node] = [neighbour]
else:
self.graph[node].append(neighbour)
if neighbour not in self.graph:
self.graph[neighbour] = [node]
else:
self.graph[neighbour].append(node)
์์ ์ฝ๋์์ add_edge ๋ฉ์๋๋ฅผ ์ฌ์ฉํด ๋
ธ๋ ๊ฐ์ ์ฃ์ง๋ฅผ ์ถ๊ฐํ ์ ์๋ค.
์ด ๋ฉ์๋๋ ๋ฌด๋ฐฉํฅ์ฑ ์ฃ์ง๋ฅผ ํํ, ๋ ธ๋์์ ์ด์๋ ธ๋๋ก ๊ทธ๋ฆฌ๊ณ ์ด์๋ ธ๋์์ ๋ ธ๋๋ก ์๋ฐฉํฅ์ผ๋ก ์ฐ๊ฒฐ๋๋ค.
์์ Directed graph์์๋ DFS๋ก ๊ทธ๋ํ๋ฅผ ํ์ฌํ๊ธฐ์, ์ด๋ฒ์๋ BFS๋ก ํ์ฌํด๋ณธ๋ค.
์ด ๊ทธ๋ํ๋ฅผ BFSํ๋ ๋ฉ์๋๋ก ๋ ธ๋๋ฅผ ์ฐจ๋ก๋๋ก ํ์ํ๋ ๋ฉ์๋๋ฅผ ์ถ๊ฐํ๋ค.
def bfs(self, node): # ๋ค์ฌ์ฐ๊ธฐ ์ฃผ์
visited = []
queue = [node]
while queue:
current_node = queue.pop(0)
if current_node not in visited:
visited.append(current_node)
if current_node in self.graph:
queue.extend(self.graph[current_node])
return visited
์์ ๋ฉ์๋์์ visited๋ ๋ฐฉ๋ฌธํ ๋
ธ๋๋ค์ ๋ชฉ๋ก์ ์ ์ฅํ๋ ๋ฆฌ์คํธ,
queue๋ ๋ฐฉ๋ฌธํ ๋
ธ๋๋ค์ ์ ์ฅํ๋ ๋ฆฌ์คํธ.
g = UndirectedGraph()
g.add_edge('A', 'B')
g.add_edge('A', 'C')
g.add_edge('B', 'D')
g.add_edge('C', 'D')
print(g.bfs('A')) # ['A', 'B', 'C', 'D']
์์ ์ฝ๋์์ 'A' ๋ ธ๋์์ ์์ํ์ฌ BFS๋ฅผ ์ํํ๋ฉด, 'B', 'D', 'C'์์๋ก ๋ ธ๋๋ฅผ ๋ฐฉ๋ฌธํ๊ฒ ๋ฉ๋๋ค.
'CS - Roadmap.sh > 1. Data structures' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Data structures (with python) - 3. Heap (0) | 2024.01.05 |
---|---|
Data structures (with python) - 2. Graph - 2.4. Graph Representation (1) | 2024.01.04 |
Data structures (with python) - 2. Graph - 2.3. Spanning Tree (1) | 2024.01.03 |
Data structures (with python) - 2. Graph - 2.1. Directed graph (0) | 2024.01.01 |
Data structures (with python) - 1. Array (0) | 2024.01.01 |