경로
사이클
단순경로(단순사이클)
가중치
차수(degree)
방향 없는 그래프
방향이 있는 경우 방향도 같이 저장해줘야 함, 간선 2개로 나누어 저장
그래프 저장의 목적
1. 인접행렬
2. 인접리스트
Stack 사용, 재귀함수
구현
const search = (graph, node, check, order) => {
check[node] = true;
order.push(node);
for (let i = 0; i < graph[node].length; i++) {
const next = graph[node][i];
if (check[next] === false) {
search(graph, next, check, order);
}
}
};
const dfs = (graph, start) => {
const check = Array(graph.length).fill(false);
const order = [];
search(graph, start, check, order);
return order;
};
Queue 사용
구현
const bfs = (graph, start) => {
const queue = [];
const check = Array(graph.length).fill(false);
const order = []; // 방문 순서
check[start] = true;
queue.push(start);
while (queue.length > 0) {
const x = queue.shift();
order.push(x);
for (let i = 0; i < graph[x].length; i++) {
const y = graph[x][i];
if (check[y] === false) {
check[y] = true;
queue.push(y);
}
}
}
return order;
};
연결요소
이분 그래프
플러드필