CS - Roadmap.sh/1. Data structures

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

ParkParksu 2024. 1. 1. 22:41

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 sometimes called a digraph or a directed network. In contrast, a graph where the edges are bidirectional is called an undirected graph.

 

λ°©ν–₯μ„± κ·Έλž˜ν”„λŠ” μ„œλ‘œ μ—°κ²°λœ 객체(정점 λ˜λŠ” λ…Έλ“œλΌκ³  함)의 μ§‘ν•©μœΌλ‘œ, λͺ¨λ“  κ°€μž₯μžλ¦¬κ°€ ν•œ μ •μ μ—μ„œ λ‹€λ₯Έ μ •μ μœΌλ‘œ ν–₯ν•˜λŠ” κ·Έλž˜ν”„μž…λ‹ˆλ‹€. λ°©ν–₯μ„± κ·Έλž˜ν”„λŠ” μ΄μ€‘κ·Έλž˜ν”„(digraph) λ˜λŠ” λ°©ν–₯μ„± λ„€νŠΈμ›Œν¬λΌκ³ λ„ λΆˆλ¦½λ‹ˆλ‹€. 이와 λŒ€μ‘°μ μœΌλ‘œ, κ°€μž₯μžλ¦¬κ°€ μ–‘λ°©ν–₯인 κ·Έλž˜ν”„λ₯Ό 무방ν–₯ κ·Έλž˜ν”„λΌκ³  ν•©λ‹ˆλ‹€.

 

좜처 : μœ„ν‚€ν”Όλ””μ•„
Oriented graph with corresponding incidence matrix

 

with python μ΄λ‹ˆ νŒŒμ΄μ¬μ—μ„œ λ°©ν–₯μ„± κ·Έλž˜ν”„μ— λŒ€ν•΄ μ•Œμ•„λ΄…μ‹œλ‹€.

νŒŒμ΄μ¬μ—μ„œ λ°©ν–₯μ„± κ·Έλž˜ν”„ (Directed Graph) λ₯Ό κ΅¬ν˜„ν•˜κ³  νƒμƒ‰ν•˜λŠ” 방법.

인접 리슀트(Adjacency List)을 μ‚¬μš©ν•΄μ„œ μ„€λͺ…ν•˜κ² μŠ΅λ‹ˆλ‹€.

(μΈμ ‘λ¦¬μŠ€νŠΈ μΆ”ν›„ 닀루기)

class DirectedGraph:
    def __init__(self):
        self.graph = {}

    def add_edge(self, node, neighbour):   
        if node not in self.graph:
            self.graph[node] = [neighbour]
        else:
            self.graph[node].append(neighbour)

 

λ¨Όμ €, λ°©ν–₯μ„± κ·Έλž˜ν”„λ₯Ό μ €μž₯ν•˜λŠ” 클래슀λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.

이 ν΄λž˜μŠ€λŠ” λ…Έλ“œλ“€μ˜ λͺ©λ‘μ„ μ €μž₯ν•˜κ³ , λ…Έλ“œλ“€ κ°„μ˜ μ—°κ²° 관계λ₯Ό κ΄€λ¦¬ν•©λ‹ˆλ‹€.

 

μœ„μ˜ μ½”λ“œμ—μ„œ add_edge λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•΄ λ…Έλ“œκ°„μ˜ μ—£μ§€λ₯Ό μΆ”κ°€ν• μˆ˜ μžˆμŠ΅λ‹ˆλ‹€.

이 λ©”μ„œλ“œλŠ” λ°©ν–₯μ„± μ—£μ§€λ₯Ό ν‘œν˜„ν•˜λ―€λ‘œ, λ…Έλ“œμ—μ„œ 이웃 λ…Έλ“œλ‘œλ§Œ μ—°κ²°λ©λ‹ˆλ‹€.

 

λ‹€μŒμœΌλ‘œ, 이 κ·Έλž˜ν”„λ₯Ό 깊이 μš°μ„  탐색(DFS)ν•˜λŠ” λ©”μ„œλ“œλ₯Ό μΆ”κ°€ν•΄ λ³΄κ² μŠ΅λ‹ˆλ‹€.

DFSλŠ” ν˜„μž¬ λ…Έλ“œμ—μ„œ κ°€λŠ₯ν•œ 깊게 νƒμƒ‰ν•˜κ³ , 더 이상 λ°©λ¬Έν•  λ…Έλ“œκ°€ 없을 λ•Œ 이전 λ…Έλ“œλ‘œ λŒμ•„κ°€λŠ” λ°©μ‹μœΌλ‘œ μž‘λ™ν•©λ‹ˆλ‹€.

(좔후에 DFS, BFSλ₯Ό λ‹€λ£Ήλ‹ˆλ‹€)

 

    def dfs(self, node, visited): # μœ„ μ½”λ“œμ™€ μ΄μ–΄μ§‘λ‹ˆλ‹€ λ“€μ—¬μ“°κΈ° 주의
        if node not in visited:
            visited.append(node)
            if node in self.graph:
                for neighbour in self.graph[node]:
                    self.dfs(neighbour, visited)
        return visited

 

μœ„μ˜ λ©”μ„œλ“œμ—μ„œ visitedλŠ” λ°©λ¬Έν•œ λ…Έλ“œλ“€μ˜ λͺ©λ‘μ„ μ €μž₯ν•˜λŠ” λ¦¬μŠ€νŠΈμž…λ‹ˆλ‹€.

μž¬κ·€μ μœΌλ‘œ dfsλ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•˜μ—¬ κ·Έλž˜ν”„λ₯Ό νƒμƒ‰ν•©λ‹ˆλ‹€.

이 클래슀λ₯Ό μ‚¬μš©ν•˜μ—¬ λ°©ν–₯μ„± κ·Έλž˜ν”„λ₯Ό μƒμ„±ν•˜κ³  탐색해 λ΄…μ‹œλ‹€.

 

g = DirectedGraph()
g.add_edge('A', 'B')
g.add_edge('A', 'C')
g.add_edge('B', 'D')
g.add_edge('C', 'D')

print(g.dfs('A', []))  # ['A', 'B', 'D', 'C']

 

μœ„μ˜ μ½”λ“œμ—μ„œ 'A' λ…Έλ“œμ—μ„œ μ‹œμž‘ν•˜μ—¬ DFSλ₯Ό μˆ˜ν–‰ν•˜λ©΄, 'B', 'D', 'C'μˆœμ„œλ‘œ λ…Έλ“œλ₯Ό λ°©λ¬Έν•˜κ²Œ λ©λ‹ˆλ‹€.

 

728x90