-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.ts
More file actions
117 lines (93 loc) · 2.99 KB
/
Copy pathgraph.ts
File metadata and controls
117 lines (93 loc) · 2.99 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'use strict'
interface IGraph<T> {
addVertex(v: T): T | null
addEdge(v: T, w: T): void
dfs(startV: T): T[]
dfsRecur(v: T, visited: Set<T>, result: T[]): T[]
bfs(startV: T): T[]
getSize(): number
getAdjList(): Map<T, T[]>
}
class Graph<T> implements IGraph<T> {
private size: number
private adjList: Map<T, T[]>
constructor(size: number) {
this.size = size
this.adjList = new Map()
}
addVertex(v: T): T | null {
if (this.adjList.has(v)) {
return null
}
this.adjList.set(v, [])
return v
}
addEdge(v: T, w: T) {
this.adjList.get(v)?.push(w)
this.adjList.get(w)?.push(v)
}
dfs(startV: T): T[] {
if (!this.adjList.has(startV)) return []
const result: T[] = []
const stack: T[] = []
const visited: Set<T> = new Set()
visited.add(startV)
stack.push(startV)
while (stack.length > 0) {
// 가장 마지막에 쌓인 노드부터 추출하고
const node = stack.pop()!
result.push(node)
// 방금 꺼낸 노드의 모든 인접 노드를 검사하자
const adjs = this.adjList.get(node)!
for (let adj of adjs) {
if (!visited.has(adj)) {
stack.push(adj) // 인접 노드들을 Stack 위에 쌓자
visited.add(adj) // 중복 검사를 피하기 위한 방문 처리
}
}
}
return result
}
dfsRecur(v: T, visited: Set<T> = new Set(), result: T[] = []): T[] {
if (!this.adjList.has(v)) return result
visited.add(v) // 방문 처리
result.push(v)
// 모든 인접 노드를 검사하자
const adjs = this.adjList.get(v)!
for (let adj of adjs) {
// 인접 노드를 핸들링하는 함수를 호출 스택에 쌓자
if (!visited.has(adj)) {
this.dfsRecur(adj, visited, result)
}
}
return result
}
bfs(startV: T): T[] {
if (!this.adjList.has(startV)) return []
const result: T[] = []
const queue: T[] = []
const visited: Set<T> = new Set()
visited.add(startV)
queue.push(startV)
while (queue.length > 0) {
// 가장 먼저 넣었던 노드부터 빼내고
const node = queue.shift()!
result.push(node)
// 이 노드의 인접 노드들을 Queue의 뒤에 추가해준다
const adjs = this.adjList.get(node)!
for (let adj of adjs) {
if (!visited.has(adj)) {
queue.push(adj)
visited.add(adj) // 중복 검사를 피하기 위한 방문 처리
}
}
}
return result
}
getSize(): number {
return this.size
}
getAdjList(): Map<T, T[]> {
return this.adjList
}
}