function factorial(n) {
result = 1
for (i = 0; i <= n; i++) {
result *= i
}
return result
}
function factorial_recursive(n) {
if (n <= 1) {
return 1
}
return n * factoriacl_recursive(n -1)
}
function gcd(a, b) {
if (a % b === 0) {
return b
}
return gcd(b, a % b)
}
-컴퓨터 함수를 연속적으로 호출하면 컴퓨터 메모리 내부의 스택프레임에 쌓임
-그래서 스택을 사용할 때 구현상 스택 라이브러리 대신에 재귀 함수를 이용하는 경우가 많음
let graph = [
[],
[2, 3, 8],
[1, 7],
[1, 4, 5],
[3, 5],
[3, 4],
[7],
[2, 6, 8],
[1, 7]
]
let visited = [False] * 9 //각 노드를 아직 방문하지 않음
function dfs(graph, v, visited) {
visited[v] = True
for (let i = 0; i < graph[v].length; i++) {
if (visited[i] != False) {
dfs(graph, i, visted)
}
}
}
let graph = [
[],
[2, 3, 8],
[1, 7],
[1, 4, 5],
[3, 5],
[3, 4],
[7],
[2, 6, 8],
[1, 7]
]
function bfs(graph, vertex, visited) {
const queue = [vertex];
visited[vertex] = true
while (queue.length > 0) {
const current = queue.shift()
for (let i = 0; i < graph[current].length; i++) {
if (!visited[graph[current][i]]) {
queue.push(graph[current][i]);
visited[graph[current][i]) = true
}
}
}
}