Post

BFS

๐Ÿ™…โ€โ™‚๏ธํœด๋Œ€ํฐ์œผ๋กœ ๋ณผ ๋•Œ ํ˜น์‹œ ๊ธ€์ž๋‚˜ ์ˆซ์ž๊ฐ€ ํ™”๋ฉด์— ๋‹ค ์•ˆ๋‚˜์˜ค๋ฉด, ํœด๋Œ€ํฐ ๊ฐ€๋กœ๋กœ ๋Œ๋ฆฌ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค

BFS(Breadth-First Search) ๋ž€ย ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰ ์œผ๋กœ
์ž๋ฃŒ๋ฅผ ์ฐพ์„ ๋•Œ "์ˆ˜์ง" ๋ฐฉํ–ฅ์œผ๋กœ ์ž๋ฃŒ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” DFS์™€ ๋‹ฌ๋ฆฌ, โ€œ์ˆ˜ํ‰โ€ ๋ฐฉํ–ฅ์œผ๋กœ ์ž๋ฃŒ๋ฅผ ๊ฒ€์ƒ‰ํ•œ๋‹ค
Desktop View

์ž BFS๋Š” ๋งจ ์ฒ˜์Œ์‹œ์ž‘๋ถ€ํ„ฐ ์ˆ˜ํ‰๋ฐฉํ–ฅ์œผ๋กœ ํƒ์ƒ‰์„ ํ•œ๋‹ค๊ณ  ํ–ˆ์œผ๋‹ˆ ์•„๋ž˜ ์ˆœ์„œ์™€ ๊ฐ™์„ ๊ฒƒ์ด๋‹ค
A->B->C->D->E->F->G->H->I->J ๋ฅผ 1->2->3->4->5->6->7->8->9->10 ๊ฐ™์€ ์˜ค๋ฆ„์ฐจ์ˆœ์ด๋ผ ์ƒ๊ฐํ•˜์ž

Desktop View

python ๊ตฌํ˜„

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
29
30
graph = dict()

graph['Onion'] = ['Rice_ball', 'Poo']
graph['Rice_ball'] = ['Onion', 'Egg']
graph['Poo'] = ['Onion', 'Celery', 'Tofu', 'Pumpkin']
graph['Egg'] = ['Rice_ball', 'Kimchi', 'Peach']
graph['Kimchi'] = ['Egg']
graph['Peach'] = ['Egg']
graph['Celery'] = ['Poo']
graph['Tofu'] = ['Poo']
graph['Pumpkin'] = ['Poo', 'Tomato']
graph['Tomato'] = ['Pumpkin']

def bfs(graph, start_node):
    need_visited, visited = [], []
    need_visited.append(start_node)
     
    while need_visited:
        node = need_visited[0]
        del need_visited[0]

        # ๋…ธ๋“œ๊ฐ€ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด, ๋ฐฉ๋ฌธํ•˜๊ณ  visited์— ์ถ”๊ฐ€ํ•ด๋ผ
        if node not in visited:
            visited.append(node)
            # ๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น ๋…ธ๋“œ์— ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  ์ด์›ƒ๋“ค์„ need_visited์— ์ถ”๊ฐ€ํ•ด๋ผ
            # (append๋Š” ๋‹จ์ผ์š”์†Œ๋งŒ ์ถ”๊ฐ€ extend๋Š” ์—ฌ๋Ÿฌ๊ฐœ ์ถ”๊ฐ€) 
            need_visited.extend(graph[node])
    return visited

print(" -> ".join(bfs(graph, 'Onion')))

Desktop View

๐Ÿซ ๋ฐœ๊ฒฌํ•œ ๋ฌธ์ œ

ํ”ผ๋“œ๋ฐฑ์„ ๋ฐ›์•˜๋Š”๋ฐ \(O(E^2)\) ์ฆ‰ ๊ทธ๋ž˜ํ”„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ ๋„ˆ๋ฌด ๊ธธ๋‹ค๊ณ  ํ•œ๋‹ค
์ฆ‰ ์‹คํ–‰์‹œ๊ฐ„์ด ๊ฒ๋‚˜ ๊ธธ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค ๊ทธ๋ฆฌํ•˜์—ฌ ๊ธฐ์กด ์ฝ”๋“œ์—์„œ ์ˆ˜์ •์„ ํ–ˆ๋‹ค

๊ธฐ์กด ์ฝ”๋“œ๋Š” ๋งค๋ฒˆ visited ๋ฆฌ์ŠคํŠธ์—์„œ node๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ , ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ์ธ ๊ฒฝ์šฐ์—๋งŒ visited์— ์ถ”๊ฐ€ํ•ด์™”๋Š”๋ฐ
ํ•˜์ง€๋งŒ ์ด ๋ฐฉ๋ฒ•์€ ๋งค๋ฒˆ visited ๋ฆฌ์ŠคํŠธ๋ฅผ ์ˆœํšŒํ•˜์—ฌ ํ™•์ธํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๋ฆฌ์ŠคํŠธ์˜ ํฌ๊ธฐ์— ๋น„๋ก€ํ•˜์—ฌ ์‹คํ–‰ ์‹œ๊ฐ„์ด ์ฆ๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค
๊ทธ๋ฆฌํ•˜์—ฌ ๋Œ€์‹ ์— visited๋ฅผ ์ง‘ํ•ฉ(set)์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋” ํšจ์œจ์ ์ผ ์ˆ˜ ์žˆ๋‹ค.
why? -> ์ง‘ํ•ฉ์€ ์›์†Œ์˜ ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ๋น ๋ฅด๊ฒŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฒ€์ƒ‰ ์†๋„๊ฐ€ ๋น ๋ฆ„

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
graph = dict()

graph['Onion'] = ['Rice_ball', 'Poo']
graph['Rice_ball'] = ['Onion', 'Egg']
graph['Poo'] = ['Onion', 'Celery', 'Tofu', 'Pumpkin']
graph['Egg'] = ['Rice_ball', 'Kimchi', 'Peach']
graph['Kimchi'] = ['Egg']
graph['Peach'] = ['Egg']
graph['Celery'] = ['Poo']
graph['Tofu'] = ['Poo']
graph['Pumpkin'] = ['Poo', 'Tomato']
graph['Tomato'] = ['Pumpkin']

def bfs(graph, start_node):
    need_visited, visited = [], set()
    need_visited.append(start_node)

    while need_visited:
        node = need_visited.pop(0)

        if node not in visited:
            visited.add(node)
            need_visited.extend(graph[node])

    return list(visited)

print(" -> ".join(bfs(graph, 'Onion')))

Desktop View

์ฐธ๊ณ 

BK_Paul ย ย  BFS ์™„๋ฒฝ ๊ตฌํ˜„ํ•˜๊ธฐ - ํŒŒ์ด์ฌ

This post is licensed under CC BY 4.0 by the author.
3D GIF