3.5 Graph - 3.5.1 Breadth First Search(BFS)

2024. 1. 28. 21:21ใ†CS - Roadmap.sh/3. Common Algorithms

CS - 3. Common Algorithms - 3.5 Graph - 3.5.1 Breadth First Search(BFS)

Breadth First Search

๐Ÿ’กBreadth first search for a graph is a way to traverse the graph. It starts at the root node and explores all of the neighbor nodes at the present depth prior to moving on to the nodes at the next depth level.

 

๋„“์ด ์šฐ์„  ํƒ์ƒ‰์€ ๊ทธ๋ž˜ํ”„๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋ฃจํŠธ ๋…ธ๋“œ์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ๋‹ค์Œ ์‹ฌ๋„ ์ˆ˜์ค€์˜ ๋…ธ๋“œ๋กœ ์ด๋™ํ•˜๊ธฐ ์ „์— ํ˜„์žฌ ์‹ฌ๋„์— ์žˆ๋Š” ๋ชจ๋“  ์ด์›ƒ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.

 

์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ•˜๋ฉด ๋Œ€์ค‘์ ์œผ๋กœ ์ž˜ ์•Œ๋ ค์ง„ BFS, ๋„“์ด ์šฐ์„  ํƒ์ƒ‰ ์ž…๋‹ˆ๋‹ค.

https://ko.wikipedia.org/wiki/%EB%84%88%EB%B9%84_%EC%9A%B0%EC%84%A0_%ED%83%90%EC%83%89

 

 

In Python
def bfs(graph: dict, start: int):
    # ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๋ฅผ ๊ธฐ๋กํ•˜๊ธฐ ์œ„ํ•œ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค.
    visited = {node: False for node in graph.keys()}

    # ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•  ๋…ธ๋“œ๋ฅผ ํ์— ๋„ฃ์Šต๋‹ˆ๋‹ค.
    queue = [start]

    # ์‹œ์ž‘ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ–ˆ๋‹ค๊ณ  ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.
    visited[start] = True

    # ๋ฐฉ๋ฌธ ์ˆœ์„œ๋ฅผ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.
    order_of_visit = []

    # ํ๊ฐ€ ๋น„์–ด์žˆ์ง€ ์•Š์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.
    while queue:
        # ํ์—์„œ ๋…ธ๋“œ๋ฅผ ํ•˜๋‚˜ ๊บผ๋ƒ…๋‹ˆ๋‹ค. (ํ˜„์žฌ ๋…ธ๋“œ)
        current_node = queue.pop(0)
        order_of_visit.append(current_node)

        # ํ˜„์žฌ ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋“ค์„ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
        for adjacent_node in graph[current_node]:
            # ๋งŒ์•ฝ ์ธ์ ‘ํ•œ ๋…ธ๋“œ๋ฅผ ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด,
            if not visited[adjacent_node]:
                # ํ•ด๋‹น ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•ฉ๋‹ˆ๋‹ค.
                queue.append(adjacent_node)
                visited[adjacent_node] = True
                
    return order_of_visit

BFS ํƒ์ƒ‰ ์˜ˆ์‹œ 1

์œ„์—์„œ ์ •์˜ํ•œ BFS ํ•จ์ˆ˜๋Œ€๋กœ, ์‹œ์ž‘ ๋…ธ๋“œ๋กœ๋ถ€ํ„ฐ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๋…ธ๋“œ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๊ณ , ๊ทธ๋‹ค์Œ ๊ฐ€๊นŒ์šด ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ง„๋ฉ๋‹ˆ๋‹ค.

1-2,3,4 ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๋ฐฉ๋ฌธํ•œ ๋’ค, 2,3,4 ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•˜๋ ค๊ณ ํ•˜๋‚˜ ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๋…ธ๋“œ๊ธฐ์— ๊ทธ๋‹ค์Œ ๊ฐ€๊นŒ์šด 5๋ฒˆ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•ฉ๋‹ˆ๋‹ค.

5-6,7 ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์–ด ๊ทธ๋‹ค์Œ 6,7 ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•ฉ๋‹ˆ๋‹ค.

 

์‚ด์ง ์ด๋ ‡๊ฒŒ ๋ณด๋ฉด 1,2,3,4,5,6,7 ๊ทธ๋ƒฅ ์ˆœ์„œ๋Œ€๋กœ ๋ฐฉ๋ฌธํ•˜๋Š”๊ฑฐ ์•„๋‹ˆ๋ƒ? ๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

 

BFS ํƒ์ƒ‰ ์˜ˆ์‹œ2

 

์ด ๊ฒฝ์šฐ ์‹œ์ž‘ ๋…ธ๋“œ๋Š” 1์ด๋ฏ€๋กœ, 1๊ณผ ์ง์ ‘ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ์ธ 2, 3์„ ๋จผ์ € ๋ฐฉ๋ฌธํ•ฉ๋‹ˆ๋‹ค. 

๊ทธ ๋‹ค์Œ์œผ๋กœ๋Š” 2์™€ 3์—์„œ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๋…ธ๋“œ์ธ 4์™€ 6์„ ๋ฐฉ๋ฌธํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 4์—์„œ๋Š” ๋‹ค์‹œ 5๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ 5๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

์™œ 7๊ณผ 8 ๋…ธ๋“œ๊ฐ€ ์—†์„๊นŒ์š”?

import networkx as nx
import matplotlib.pyplot as plt

graph_example2 = {
    1: [2, 3],
    2: [4],
    3: [6],
    4: [5],
    5: [],
    6: [],
    7: [],
    8: [6, 7]
}

G = nx.DiGraph()

for node, edges in graph_example2.items():
    for edge in edges:
        G.add_edge(node, edge)

nx.draw(G, with_labels=True)
plt.show()

๊ทธ๋ž˜ํ”„์˜ ์—ฃ์ง€ ํŠน์„ฑ์ธ ๋ฐฉํ–ฅ์„ฑ์˜ ์กด์žฌ ๋•Œ๋ฌธ์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.

https://parkpakrsu.tistory.com/327

 

Data structures (with python) - 2. Graph - 2.1. Directed graph

Data structures (with python) - 2. Graph - 2.1. Directed graph ๐Ÿ’ก A directed graph is graph, i.e., a set of objects (called vertices or nodes) that are connected together, where all the edges are directed from one vertex to another. A directed graph is s

parkpakrsu.tistory.com

 

์„ ๋งŒ ๋ณธ๋‹ค๋ฉด 1 - 3 - 6 - 8 ์ด๋Ÿฐ์‹์œผ๋กœ ๊ฐˆ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๊ฒ ์ง€๋งŒ, ๊ทธ๋ž˜ํ”„์˜ ์—ฃ์ง€๋Š” ๋ฐฉํ–ฅ์„ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

ํ™”์‚ดํ‘œ์˜ ๋ฐฉํ–ฅ์œผ๋กœ๋งŒ ๋…ธ๋“œ์˜ ์ด๋™์„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๊ธฐ์—,  ํƒ์ƒ‰ ์‹œ์ž‘๋…ธ๋“œ์ธ 1์—์„œ 8์œผ๋กœ ๊ฐˆ ๋ฐฉ๋ฒ•์ด ์—†๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๊ฐ™์€ ๊ทธ๋ž˜ํ”„์˜ ํƒ์ƒ‰์„ 8๋ฒˆ์—์„œ ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค๋ฉด 8,6,7 ์ˆœ์„œ๋กœ ํƒ์ƒ‰์ด ๋๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

 

ํ™œ์šฉ

 

BFS ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ํ™œ์šฉ๋˜๋Š” ๊ณณ์€ ๋‹ค์–‘ํ•ฉ๋‹ˆ๋‹ค.

1. ๋‘ ๋…ธ๋“œ ์‚ฌ์ด์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ ํ˜น์€ ์ž„์˜์˜ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์•„์•ผ ํ•  ๋•Œ
2. ๊ทธ๋ž˜ํ”„์— ํฌํ•จ๋œ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•ด์•ผ ํ•  ๋•Œ
3. ๋‘ ๋…ธ๋“œ๊ฐ€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•  ๋•Œ

 

์ข€๋” ์‹ค์ œ์ ์ธ ์˜ˆ์‹œ๋ฅผ ๋“ ๋‹ค๋ฉด,

 

1. SNS ์นœ๊ตฌ ์ถ”์ฒœ ์‹œ์Šคํ…œ

- ์‚ฌ์šฉ์ž๋ฅผ ์‹œ์ž‘ ๋…ธ๋“œ๋กœ BFS๋ฅผ ์ˆ˜ํ–‰, ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๋…ธ๋“œ(์ฆ‰, ์นœ๊ตฌ)๋ฅผ ๋จผ์ € ๋ฐฉ๋ฌธํ•˜๊ณ , ๊ทธ ๋‹ค์Œ์œผ๋กœ ๊ฐ€๊นŒ์šด ๋…ธ๋“œ(์นœ๊ตฌ์˜ ์นœ๊ตฌ)๋ฅผ ๋ฐฉ๋ฌธํ•˜๋Š” ํ˜•์‹์œผ๋กœ ์นœ๊ตฌ ์ถ”์ฒœ์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋งํฌ๋“œ์ธ์—์„œ 1์ดŒ, 2์ดŒ, 3์ดŒ, 3์ดŒ์ด์ƒ ๊ฐ™์€ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

2. GPS ๋„ค๋น„๊ฒŒ์ด์…˜ 

- BFS ๋Š” GPS ๋„ค๋น„๊ฒŒ์ด์…˜ ์‹œ์Šคํ…œ์—์„œ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ํŠน์ • ์œ„์น˜์—์„œ ๋ชฉ์ ์ง€ ๊นŒ์ง€ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๊ธฐ์œ„ํ•ด, ์‹œ์ž‘ ์œ„์น˜์—์„œ BFS๋ฅผ ์ˆ˜ํ–‰ํ•˜์—ฌ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์œ„์น˜๋ถ€ํ„ฐ ๋ฐฉ๋ฌธ, ๊ทธ ๋‹ค์Œ์œผ๋กœ ๊ฐ€๊นŒ์šด ์œ„์น˜๋ฅผ ๋ฐฉ๋ฌธ ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

3. ๋„คํŠธ์›Œํฌ ๋ผ์šฐํŒ…

- ๋„คํŠธ์›Œํฌ์—์„œ ํŒจํ‚ท์„ ์ „์†กํ•˜๋Š” ์ตœ์ ์˜ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ๋ฐ BFS๊ฐ€ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

๋ผ์šฐํ„ฐ๋Š” BFS๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๋ผ์šฐํ„ฐ ๋ถ€ํ„ฐ ๋ฐฉ๋ฌธํ•˜๊ณ , ๊ทธ ๋‹ค์Œ์œผ๋กœ ๊ฐ€๊นŒ์šด ๋ผ์šฐํ„ฐ๋ฅผ ๋ฐฉ๋ฌธํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ตœ์ ์˜ ๊ฒฝ๋กœ๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.

728x90

'CS - Roadmap.sh > 3. Common Algorithms' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

3.5 Graph - 3.5.3 Bellman Fordโ€™s Algorithm  (1) 2024.01.29
3.5 Graph - 3.5.2 Depth First Search(DFS)  (1) 2024.01.29
3.5 Graph & 3.6 Tree  (0) 2024.01.27
3.4. Cache - 3.4.3 MFU Cache  (0) 2024.01.27
3.4. Cache - 3.4.2 LFU Cache  (2) 2024.01.26