* ์์
๊ทธ๋ฐ๋ฐ ์ด ์ฝ๋, ์๋ฐฉํฅ ๋ฆฌ์คํธ์ธ ๊ฒฝ์ฐ ๋ฐฉ๋ฌธํ๋๊ณณ์ ๋ ๋ฐฉ๋ฌธํด๋ฒ๋ฆฌ๋ ์ค๋ฅ๊ฐ ์๋ค.
๊ทธ๋์ ์๋ฐฉํฅ ๋ฆฌ์คํธ์ธ ๊ฒฝ์ฐ, ๋ฐฉ๋ฌธํ๋๊ณณ์ ๋ ๋ฐฉ๋ฌธํ์ง ์๋ ์ฝ๋๋ฅผ ๋ฃ์๋ค.
# ์ ์ ๊ฐ์ N, ๊ฐ์ ๊ฐ์ M, ํ์ ์์ ๋ฒํธ V
N, M, V = map(int, input().split())
edgeList = []
graph = [[] for vertex in range(N+1)]
for i in range(M):
e0, e1 = map(int, input().split())
edgeList.append((e0, e1))
for edge in edgeList:
graph[edge[0]].append(edge[1])
graph[edge[1]].append(edge[0])
for i in range(len(graph)):
graph[i].sort(reverse=True)
print(graph)
#DFS
def DFS(graph, V) :
visited = []
stack = [V]
while stack :
current = stack.pop()
if current in visited: # ์ด๋ฏธ ๋ฐฉ๋ฌธํ ๋
ธ๋๋ ๋ฌด์
continue
for n in graph[current]:
if not n in visited:
stack.append(n)
visited.append(current)
return visited
print(DFS(graph, V))
๊น์ด์ฐ์ ํ์(DFS)
1. ์ฐ์ ์ ์ ๋ฆฌ์คํธ๊ฐ ํ๋ ํ์ํ๋ค.
vertexList = [’0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ’6’]
2. ์ฐ๊ฒฐ๋์ด์๋๊ฑฐ ๋ฆฌ์คํธ ์์ ๋ฆฌ์คํธ๋ก ํํ
0์์ 1๊ฐ๋๊ฑฐ, 0์์ 2๊ฐ๋๊ฑฐ, 1์์ 0๊ฐ๋๊ฑฐ, 1์์ 3๊ฐ๋๊ฑฐ
edgeList = [(0,1), (0,2), (1,0), (1,3), (2,0), (2,4), (2,5), (3,1), (4,2), (4,6), (5,2), (6,4) ]
3. ์ธ์ ๋ฆฌ์คํธ๋ฅผ ๋ง๋ ๋ค → ์ ์ ๋ฆฌ์คํธ์ edgeList๋ฅผ ์ด์ฉํด์ ๋ฆฌ์คํธ์์ ๋ฆฌ์คํธ๋ก ๋ง๋ค๋ฉด ๋๋ค.
adjacencyList = [[] for vertex in vertexList]
# vertex๋ ๋ณ์์. ๊ทธ๋ฅ List ๋งํผ ๋น ๋ฆฌ์คํธ [] ์์ฑํ๋๊ฑฐ
for edge in edgeList:
adjacencyList[edge[0]].append(edge[1])
# ์ธ๋ฑ์ค edge[0]๋ฒ์ edge[1]์ ๊ฐ์ ์ถ๊ฐํ๋๊ฒ.
# ๊ทธ๋ฌ๋ฉด ๋ง๋ค์ด์ง ๊ฒฐ๊ณผ๋ ์ ์ ์ธ๋ฑ์ค์ ๋ฆฌ์คํธ ์์ ์ฐ๊ฒฐ๋ ์ ์ ์ด ๋ค์ด๊ฐ๋๊ผด.
[[1, 2], [0, 3], [0, 4, 5], [1], [2, 6], [2], [4]]
# 0๋ฒ ์ ์ ์ 1,2 ์ฐ๊ฒฐ
# 1๋ฒ ์ ์ ์ 0,3 ์ฐ๊ฒฐ
4. DFS ์คํ ์ฝ๋
์ธ์ ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค๋ฉด DFS๋ฅผ ์คํํ ์ ์๋ค.
์คํ ์ค๋น, current๋ผ๋ ๋ณ์๋ฅผ ๋ง๋ ๋ค.
0์ด ์ฒ์ ์คํ์ ๋ค์ด๊ฐ๋ค → 0์ popํ๋ค. → 0์ ์ด์๋ค 1,2๋ฅผ ์คํ์ ๋ฃ๋๋ค. → 0์ด ๋ฐฉ๋ฌธ๋์๋ค๊ณ ํ์ํ๋ค.
๊ทธํ 2๋ฅผ popํ๋ค → 2 ์์์๋ ์ด์๋ค 4,5๋ฅผ ์คํ์ ๋ฃ๋๋ค. (๋ฐฉ๋ฌธ๋์๋ค๊ณ ํ์๋๊ฑฐ ์ ์ธ) → 2๊ฐ ๋ฐฉ๋ฌธ๋์๋ค๊ณ ํ์ํ๋ค.
5๋ฅผ popํ๋ค → 5 ์์ ์๋ ์ด์๋ค ์์
4๋ฅผ popํ๋ค → 4์ ๊ทผ์ vertex๋ฅผ ์คํ์ ๋ฃ๋๋ค (๋ฐฉ๋ฌธ๋์๋ค ํ์ํ 2 ์ ์ธ) → 4๊ฐ ๋ฐฉ๋ฌธ๋์๋ค๊ณ ํ์ํ๋ค.
๋ฐ๋ณต..
def DFS() :
visitedVertex = []
stack = [0]
while stack:
current = stack.pop()
for neighbor in adjacencyList[current]:
if not neighbor in visitedVertex:
stack.append(neighbor)
visitedVertex.append(current)
return visitedVertex
์ด๋์ ์ฌ์ฉ๋ ๊น? → ์ฒด์ค์ ์, ๊ทธ๋ํ๊ฐ ์ฃผ์ด์ง๊ณ ๊ทธ๋ํ ์์ ์ฌ์ดํด์ด ์๋์ง ์๋์ง
์ต์ข ์์ฑ ์ฝ๋
vertexList = ['0', '1', '2', '3', '4', '5', '6']
edgeList = [(0,1), (0,2), (1,0), (1,3), (2,0), (2,4), (2,5), (3,1), (4,2), (4,6), (5,2), (6,4) ]
# vertex๋ ๋ณ์์. ๊ทธ๋ฅ List ๋งํผ ๋น ๋ฆฌ์คํธ [] ์์ฑํ๋๊ฑฐ
adjacencyList = [[] for vertex in vertexList]
for edge in edgeList:
adjacencyList[edge[0]].append(edge[1])
# ์ธ๋ฑ์ค edge[0]๋ฒ์ edge[1]์ ๊ฐ์ ์ถ๊ฐํ๋๊ฒ.
# ๊ทธ๋ฌ๋ฉด ๋ง๋ค์ด์ง ๊ฒฐ๊ณผ๋ ์ ์ ์ธ๋ฑ์ค์ ๋ฆฌ์คํธ ์์ ์ฐ๊ฒฐ๋ ์ ์ ์ด ๋ค์ด๊ฐ๋๊ผด.
def DFS() :
visitedVertex = []
stack = [0]
while stack:
current = stack.pop()
for neighbor in adjacencyList[current]:
if not neighbor in visitedVertex:
stack.append(neighbor)
visitedVertex.append(current)
return visitedVertex
print(DFS())
๋๋น์ฐ์ ํ์(BFS)
1~3๋ฒ์ ๋์ผํ๋ค.
4. BFS ์คํ์ฝ๋
ํ๊ฐ ์์ด์ผํ๋ค.
0 → 0์ ์ด์ 1,2 ํ์ ๋ค์ด๊ฐ → 1 ๋์ด → 1์ ์ด์ 3 ํ์ ๋ค์ด๊ฐ → 2 ๋์ด
์ด๋์ ์ฌ์ฉ๋ ๊น? ์ต๊ทผ์ ๊ฑฐ๋ฆฌ ๊ตฌํ ๋ - ๋ค์ต์คํธ๋ผ๋ BFS์ด๋ค.
def BFS():
visitedList = []
queue = [0]
while queue:
current = queue.pop()
for neigbor in adjacencyList[current]:
if not neigbor in visitedList:
queue.insert(0, neigbor)
visitedList.append(current)
return visitedList
print(BFS())
์ต์ข ์์ฑ ์ฝ๋
vertexList = ['0', '1', '2', '3', '4', '5', '6']
edgeList = [(0,1), (0,2), (1,0), (1,3), (2,0), (2,4), (2,5), (3,1), (4,2), (4,6), (5,2), (6,4) ]
# vertex๋ ๋ณ์์. ๊ทธ๋ฅ List ๋งํผ ๋น ๋ฆฌ์คํธ [] ์์ฑํ๋๊ฑฐ
adjacencyList = [[] for vertex in vertexList]
for edge in edgeList:
adjacencyList[edge[0]].append(edge[1])
# ์ธ๋ฑ์ค edge[0]๋ฒ์ edge[1]์ ๊ฐ์ ์ถ๊ฐํ๋๊ฒ.
# ๊ทธ๋ฌ๋ฉด ๋ง๋ค์ด์ง ๊ฒฐ๊ณผ๋ ์ ์ ์ธ๋ฑ์ค์ ๋ฆฌ์คํธ ์์ ์ฐ๊ฒฐ๋ ์ ์ ์ด ๋ค์ด๊ฐ๋๊ผด.
def BFS():
visitedList = []
queue = [0]
while queue:
current = queue.pop()
for neigbor in adjacencyList[current]:
if not neigbor in visitedList:
queue.insert(0, neigbor)
visitedList.append(current)
return visitedList
print(BFS())
https://youtu.be/BLc3wzvycH8?si=eF15hTVnQ7IBjHP-
์์ ๊ฐ์๋ฅผ ๋ณด๊ณ ์์ฑํ ์ฝ๋์ ๋๋ค
๊ฐ์์๋ถ์ด ์์ฑํ ์ฝ๋
vertexList = ['0', '1', '2', '3', '4', '5', '6']
edgeList = [(0,1), (0,2), (1,0) , (1,3) , (2,0) , (2,4) , (2,5) , (3,1), (4,2) , (4,6), (5,2), (6,4)]
graphs = (vertexList, edgeList)
def dfs(graph, start):
vertexList, edgeList = graph
visitedVertex = []
stack = [start]
adjacencyList = [[] for vertex in vertexList]
for edge in edgeList:
adjacencyList[edge[0]].append(edge[1])
while stack:
current = stack.pop()
for neighbor in adjacencyList[current]:
if not neighbor in visitedVertex:
stack.append(neighbor)
visitedVertex.append(current)
return visitedVertex
print(dfs(graphs, 0))
vertexList = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
edgeList = [(0,1), (1,2), (1,3), (3,4), (4,5), (1,6)]
graphs = (vertexList, edgeList)
def bfs(graph, start):
vertexList, edgeList = graph
visitedList = []
queue = [start]
adjacencyList = [[] for vertex in vertexList]
# fill adjacencyList from graph
for edge in edgeList:
adjacencyList[edge[0]].append(edge[1])
# bfs
while queue:
current = queue.pop()
for neighbor in adjacencyList[current]:
if not neighbor in visitedList:
queue.insert(0,neighbor)
visitedList.append(current)
return visitedList
print(bfs(graphs, 0))
์ด๊ฑด start ์ง์ ์ ์ง์ ํ ์ ์๋ค. ์ฝ๋ฉํ ์คํธ๋ฅผ ๋ณผ๋ ์ด๋ ๊ฒ ์์ฑํด์ผํ ๋ฏ.
'๐ป ์ฝ๋ฉํ ์คํธ > ์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
DFS ์์ ์ซ์ ๋จผ์ ํ์ (0) | 2025.03.28 |
---|