|
| 1 | +# 그래프 (Graph) 이론 |
| 2 | + |
| 3 | +## 1. 그래프의 정의 |
| 4 | +- 정점(Vertex)과 간선(Edge)으로 구성된 데이터 구조 |
| 5 | +- 객체 간의 관계를 표현하는 추상적 네트워크 |
| 6 | + |
| 7 | +## 3. 그래프의 종류 |
| 8 | +### 3.1 방향성에 따른 분류 |
| 9 | +1. 방향 그래프(Directed Graph) |
| 10 | + - 간선에 방향성 존재 |
| 11 | + - 일방향 관계 표현 |
| 12 | + |
| 13 | +2. 무방향 그래프(Undirected Graph) |
| 14 | + - 간선에 방향성 없음 |
| 15 | + - 양방향 관계 표현 |
| 16 | + |
| 17 | +### 3.2 연결성에 따른 분류 |
| 18 | +1. 연결 그래프(Connected Graph) |
| 19 | + - 모든 정점이 서로 연결됨 |
| 20 | + |
| 21 | +2. 비연결 그래프(Disconnected Graph) |
| 22 | + - 일부 정점이 연결되지 않음 |
| 23 | + |
| 24 | +### 3.3 특수한 그래프 |
| 25 | +- 트리(Tree) |
| 26 | +- 완전 그래프(Complete Graph) |
| 27 | +- 이분 그래프(Bipartite Graph) |
| 28 | + |
| 29 | +## 4. 그래프 표현 방법 |
| 30 | +### 4.1 인접 행렬(Adjacency Matrix) |
| 31 | +```javascript |
| 32 | +class GraphAdjacencyMatrix { |
| 33 | + constructor(vertices) { |
| 34 | + this.vertices = vertices; |
| 35 | + this.matrix = Array(vertices).fill().map(() => Array(vertices).fill(0)); |
| 36 | + } |
| 37 | + |
| 38 | + addEdge(v1, v2) { |
| 39 | + this.matrix[v1][v2] = 1; |
| 40 | + this.matrix[v2][v1] = 1; // 무방향 그래프의 경우 |
| 41 | + } |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +### 4.2 인접 리스트(Adjacency List) |
| 46 | +```javascript |
| 47 | +class GraphAdjacencyList { |
| 48 | + constructor() { |
| 49 | + this.adjacencyList = {}; |
| 50 | + } |
| 51 | + |
| 52 | + addVertex(vertex) { |
| 53 | + if (!this.adjacencyList[vertex]) { |
| 54 | + this.adjacencyList[vertex] = []; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + addEdge(vertex1, vertex2) { |
| 59 | + this.adjacencyList[vertex1].push(vertex2); |
| 60 | + this.adjacencyList[vertex2].push(vertex1); // 무방향 그래프 |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +## 5. 그래프 탐색 알고리즘 |
| 66 | +### 5.1 깊이 우선 탐색 (DFS) |
| 67 | +```javascript |
| 68 | +function dfs(graph, startVertex) { |
| 69 | + const visited = new Set(); |
| 70 | + |
| 71 | + function explore(vertex) { |
| 72 | + visited.add(vertex); |
| 73 | + console.log(vertex); |
| 74 | + |
| 75 | + for (let neighbor of graph.adjacencyList[vertex]) { |
| 76 | + if (!visited.has(neighbor)) { |
| 77 | + explore(neighbor); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + explore(startVertex); |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +### 5.2 너비 우선 탐색 (BFS) |
| 87 | +```javascript |
| 88 | +function bfs(graph, startVertex) { |
| 89 | + const visited = new Set(); |
| 90 | + const queue = [startVertex]; |
| 91 | + visited.add(startVertex); |
| 92 | + |
| 93 | + while (queue.length > 0) { |
| 94 | + const currentVertex = queue.shift(); |
| 95 | + console.log(currentVertex); |
| 96 | + |
| 97 | + for (let neighbor of graph.adjacencyList[currentVertex]) { |
| 98 | + if (!visited.has(neighbor)) { |
| 99 | + visited.add(neighbor); |
| 100 | + queue.push(neighbor); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +## 6. 최단 경로 알고리즘 |
| 108 | +### 6.1 다익스트라 알고리즘 |
| 109 | +```javascript |
| 110 | +function dijkstra(graph, startVertex) { |
| 111 | + const distances = {}; |
| 112 | + const previous = {}; |
| 113 | + const pq = new PriorityQueue(); |
| 114 | + |
| 115 | + // 초기화 로직 |
| 116 | + for (let vertex in graph.adjacencyList) { |
| 117 | + if (vertex === startVertex) { |
| 118 | + distances[vertex] = 0; |
| 119 | + pq.enqueue(vertex, 0); |
| 120 | + } else { |
| 121 | + distances[vertex] = Infinity; |
| 122 | + } |
| 123 | + previous[vertex] = null; |
| 124 | + } |
| 125 | + |
| 126 | + // 최단 경로 탐색 로직 |
| 127 | + while (!pq.isEmpty()) { |
| 128 | + let currentVertex = pq.dequeue().val; |
| 129 | + |
| 130 | + for (let neighbor in graph.adjacencyList[currentVertex]) { |
| 131 | + // 거리 계산 및 업데이트 로직 |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return { distances, previous }; |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +## 7. 그래프 문제 주의 사항 |
| 140 | +1. 그래프 표현 방법 선택 |
| 141 | +2. 적절한 탐색 알고리즘 적용 |
| 142 | +3. 방문 체크로 무한 루프 방지 |
| 143 | +4. 메모리 효율성 고려 |
| 144 | + |
| 145 | +## 8. 시간 복잡도 |
| 146 | +- DFS/BFS: O(V + E) |
| 147 | +- 다익스트라: O((V + E)logV) |
| 148 | +- 플로이드-워셜: O(V³) |
0 commit comments