ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90

1) ์žฌ๊ท€๋ฅผ ์ด์šฉํ•œ DFS ๊ตฌํ˜„ Pseudo code

 

G๋Š” ๊ทธ๋ž˜ํ”„, v๋Š” ์ •์ (vertex)์ผ ๋•Œ,

DFS(G,v)
	lable v as discovered
	for all directed edges from v to w that are in G.adjacentEdges(v) do
		if vertex w is not labeled as discovered then
			recursively call DFS(G,w)

 

์žฌ๊ท€๋ฅผ ์ด์šฉํ•œ DFS ๊ตฌํ˜„ Python code

def recursive_dfs(v, discovered=[]):
	discovered.append(v)
    	for w in graph[v]:
    		if w not in discovered:
        		discovered = recusrive_dfs(w, discovered)
    	return discovered

 

 

2) ์Šคํƒ์„ ์ด์šฉํ•œ ๋ฐ˜๋ณต ๊ตฌ์กฐ๋กœ DFS ๊ตฌํ˜„ Pseudo code

DFS-iterative(G, v)
	let S be a stack
    	S.push(v)
    	While S is not empty do
    		v = S.pop()
        	if v is not labeled as discovered then
        		label v as discovered
            		for all edges from v to w in G.adjacentEdges(v) do
            			S.push(w)

 

 

์Šคํƒ์„ ์ด์šฉํ•œ ๋ฐ˜๋ณต ๊ตฌ์กฐ๋ก  DFS ๊ตฌํ˜„ Python code

def iterative_dfs(graph, start_v):
    visited = []
    stack = [start_v]
    while stack:
        v = stack.pop()
        if v not in visited:
            visited.append(v)
            for w in graph[v]:
                stack.append(w)
    return visited

 

728x90
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
ยซ   2025/02   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
๊ธ€ ๋ณด๊ด€ํ•จ
250x250