Data structures (with python) - 6. Queue

2024. 1. 8. 17:49ใ†CS - Roadmap.sh/1. Data structures

Data structures (with python) - 6. Queue

 

๐Ÿ’ก Queue is a linear collection of items where items are inserted and removed in a particular order. The queue is also called a FIFO Data Structure because it follows the “First In, First Out” principle i.e., the item that is inserted in the first is the one that is taken out first.

ํ๋Š” ํŠน์ • ์ˆœ์„œ๋กœ ํ•ญ๋ชฉ์ด ์‚ฝ์ž…๋˜๊ณ  ์ œ๊ฑฐ๋˜๋Š” ํ•ญ๋ชฉ์˜ ์„ ํ˜• ์ปฌ๋ ‰์…˜์ž…๋‹ˆ๋‹ค. ํ๋Š” "์„ ์ž…์„ ์ถœ" ์›์น™, ์ฆ‰ ๊ฐ€์žฅ ๋จผ์ € ์‚ฝ์ž…๋œ ํ•ญ๋ชฉ์ด ๊ฐ€์žฅ ๋จผ์ € ์ œ๊ฑฐ๋˜๋Š” ์›์น™์„ ๋”ฐ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— FIFO ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค.

 

์œ„ํ‚คํ”ผ๋””์•„

 

 

์ž๋ฃŒ๊ตฌ์กฐ์˜ ๋Œ€ํ‘œ ์ฃผ์ž 2๊ฐœ ํ & ์Šคํƒ,  ํ ์ด๋‹ค

 

ํ(Queue) ๋Š” '์„ ์ž…์„ ์ถœ' (First In First Out, FIFO) ์›์น™์„ ๋”ฐ๋ฅด๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค.

์ฆ‰, ๊ฐ€์žฅ ๋จผ์ € ๋“ค์–ด๊ฐ„(์„ ์ž…) ๋ฐ์ดํ„ฐ๊ฐ€ ๊ฐ€์žฅ ๋จผ์ € ๋‚˜์˜ค๊ฒŒ(์„ ์ถœ) ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์‹ค์ƒํ™œ์—์„œ ์‚ฌ๋žŒ๋“ค์ด ์ค„์„ ์„œ์„œ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ์›๋ฆฌ์ž…๋‹ˆ๋‹ค.

 

1. Enqueue: ํ์˜ ๋’ค์ชฝ์— ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

2. Dequeue: ํ์˜ ์•ž์ชฝ์—์„œ ํ•ญ๋ชฉ์„ ์ œ๊ฑฐํ•˜๊ณ  ๊ทธ ํ•ญ๋ชฉ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

In Python

Python์—์„œ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฆฌ์ŠคํŠธ์˜ append() ๋ฉ”์„œ๋“œ๋ฅด ์‚ฌ์šฉํ•˜์—ฌ enqueue ์—ฐ์‚ฐ์„ ๊ตฌํ˜„ํ•˜๊ณ ,

pop() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ dequeue ์—ฐ์‚ฐ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์ž‡์Šต๋‹ˆ๋‹ค.

 

class Queue:
    def __init__(self):
        self.queue = []

    # ํ์— ์š”์†Œ ์ถ”๊ฐ€ (enqueue)
    def enqueue(self, data):
        self.queue.append(data)

    # ํ์˜ ๋งจ ์•ž ์š”์†Œ ์ œ๊ฑฐ ๋ฐ ๋ฐ˜ํ™˜ (dequeue)
    def dequeue(self):
        if self.is_empty():
            return None
        else:
            return self.queue.pop(0)

    # ํ๊ฐ€ ๋น„์–ด์žˆ๋Š”์ง€ ํ™•์ธ
    def is_empty(self):
        return len(self.queue) == 0

 

์œ„์— ์ •์˜ํ•œ Queue ํด๋ž˜์Šค๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ƒํŽด ๋ณด์ž

 

q = Queue()

q.enqueue(1)  # 1์„ ํ์— ์ถ”๊ฐ€
q.enqueue(2)  # 2๋ฅผ ํ์— ์ถ”๊ฐ€
q.enqueue(3)  # 3์„ ํ์— ์ถ”๊ฐ€

print(q.dequeue())  # 1์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ํ์—์„œ ์ œ๊ฑฐ
print(q.dequeue())  # 2๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ํ์—์„œ ์ œ๊ฑฐ
print(q.dequeue())  # 3์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ํ์—์„œ ์ œ๊ฑฐ

 

์œ„ ์ฝ”๋“œ์—์„œ๋Š” 1,2,3 ์ˆœ์„œ๋Œ€๋กœ ํ์— ์ถ”๊ฐ€(enqueue)ํ–ˆ๊ณ , ์ดํ›„ ์„ธ ๋ฒˆ dequeue ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜์—ฌ 1,2,3 ์ˆœ์„œ๋Œ€๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฐ˜ํ™˜๋˜๊ณ  ํ์—์„œ ์ œ๊ฑฐ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ฝ”ํ…Œ & ์•Œ๊ณ ๋ฆฌ์ฆ˜

๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰ (BFS)์— ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

from collections import deque

def bfs(graph, root):
    visited = []
    queue = deque([root])

    while queue:
        vertex = queue.popleft()
        if vertex not in visited:
            visited.append(vertex)
            queue.extend(graph[vertex] - set(visited))
    return visited

graph = {'A': {'B', 'C'}, 'B': {'A', 'D', 'E'}, 'C': {'A', 'F'}, 'D': {'B'}, 'E': {'B', 'F'}, 'F': {'C', 'E'}}
print(bfs(graph, 'A'))  # A์—์„œ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๋…ธ๋“œ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅ

 

์ด์™ธ์—๋„, ํ๋Š” ๋‹ค์–‘ํ•œ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ๋ฌธ์ œ์—์„œ ํ™œ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

1. ํƒ์ƒ‰ ๋ฌธ์ œ : ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ๋ฌธ์ œ ๋“ฑ์—์„œ ํ๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

2. ์‹œ๋ฌผ๋ ˆ์ด์…˜ ๋ฌธ์ œ : ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์˜ค๊ณ  ์ฒ˜๋ฆฌ๋˜๋Š” ์‹œ์Šคํ…œ์„ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ํ•˜๋Š” ๋ฌธ์ œ์—์„œ ํ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

3. ์Šค์ผ€์ฅด๋ง ๋ฌธ์ œ : ํŠน์ •ํ•œ ์šฐ์„ ์ˆœ์œ„์— ๋”ฐ๋ผ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์—์„œ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

728x90