mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-05 20:52:21 +08:00
feat(ko): add the Korean online edition (#2006)
* feat(ko): add the complete Korean online edition * Use UTF-8 for Python CI output on Windows * Remove print-book promotion from the Korean site * fix(ko): preserve emphasis before Korean particles
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
/**
|
||||
* File: graph_adjacency_list.js
|
||||
* Created Time: 2023-02-09
|
||||
* Author: Justin ([email protected])
|
||||
*/
|
||||
|
||||
const { Vertex } = require('../modules/Vertex');
|
||||
|
||||
/* 인접 리스트로 구현한 무방향 그래프 클래스 */
|
||||
class GraphAdjList {
|
||||
// 인접 리스트의 key: 정점, value: 해당 정점의 모든 인접 정점
|
||||
adjList;
|
||||
|
||||
/* 생성자 */
|
||||
constructor(edges) {
|
||||
this.adjList = new Map();
|
||||
// 모든 정점과 간선을 추가한다
|
||||
for (const edge of edges) {
|
||||
this.addVertex(edge[0]);
|
||||
this.addVertex(edge[1]);
|
||||
this.addEdge(edge[0], edge[1]);
|
||||
}
|
||||
}
|
||||
|
||||
/* 정점 개수 반환 */
|
||||
size() {
|
||||
return this.adjList.size;
|
||||
}
|
||||
|
||||
/* 간선 추가 */
|
||||
addEdge(vet1, vet2) {
|
||||
if (
|
||||
!this.adjList.has(vet1) ||
|
||||
!this.adjList.has(vet2) ||
|
||||
vet1 === vet2
|
||||
) {
|
||||
throw new Error('잘못된 인수');
|
||||
}
|
||||
// 간선 vet1 - vet2를 추가한다
|
||||
this.adjList.get(vet1).push(vet2);
|
||||
this.adjList.get(vet2).push(vet1);
|
||||
}
|
||||
|
||||
/* 간선 삭제 */
|
||||
removeEdge(vet1, vet2) {
|
||||
if (
|
||||
!this.adjList.has(vet1) ||
|
||||
!this.adjList.has(vet2) ||
|
||||
vet1 === vet2 ||
|
||||
this.adjList.get(vet1).indexOf(vet2) === -1
|
||||
) {
|
||||
throw new Error('잘못된 인수');
|
||||
}
|
||||
// 간선 vet1 - vet2를 삭제한다
|
||||
this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);
|
||||
this.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);
|
||||
}
|
||||
|
||||
/* 정점 추가 */
|
||||
addVertex(vet) {
|
||||
if (this.adjList.has(vet)) return;
|
||||
// 인접 리스트에 새 연결 리스트를 추가한다
|
||||
this.adjList.set(vet, []);
|
||||
}
|
||||
|
||||
/* 정점 삭제 */
|
||||
removeVertex(vet) {
|
||||
if (!this.adjList.has(vet)) {
|
||||
throw new Error('잘못된 인수');
|
||||
}
|
||||
// 인접 리스트에서 정점 vet에 해당하는 연결 리스트를 삭제한다
|
||||
this.adjList.delete(vet);
|
||||
// 다른 정점의 연결 리스트를 순회하여 vet를 포함하는 모든 간선을 삭제한다
|
||||
for (const set of this.adjList.values()) {
|
||||
const index = set.indexOf(vet);
|
||||
if (index > -1) {
|
||||
set.splice(index, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 인접 리스트 출력 */
|
||||
print() {
|
||||
console.log('인접 리스트 =');
|
||||
for (const [key, value] of this.adjList) {
|
||||
const tmp = [];
|
||||
for (const vertex of value) {
|
||||
tmp.push(vertex.val);
|
||||
}
|
||||
console.log(key.val + ': ' + tmp.join());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
/* 실행 코드 */
|
||||
/* 무방향 그래프 초기화 */
|
||||
const v0 = new Vertex(1),
|
||||
v1 = new Vertex(3),
|
||||
v2 = new Vertex(2),
|
||||
v3 = new Vertex(5),
|
||||
v4 = new Vertex(4);
|
||||
const edges = [
|
||||
[v0, v1],
|
||||
[v1, v2],
|
||||
[v2, v3],
|
||||
[v0, v3],
|
||||
[v2, v4],
|
||||
[v3, v4],
|
||||
];
|
||||
const graph = new GraphAdjList(edges);
|
||||
console.log('\n초기화한 후의 그래프');
|
||||
graph.print();
|
||||
|
||||
/* 간선 추가 */
|
||||
// 정점 1, 2는 각각 v0, v2이다
|
||||
graph.addEdge(v0, v2);
|
||||
console.log('\n간선 1-2를 추가한 후의 그래프');
|
||||
graph.print();
|
||||
|
||||
/* 간선 삭제 */
|
||||
// 정점 1, 3은 각각 v0, v1이다
|
||||
graph.removeEdge(v0, v1);
|
||||
console.log('\n간선 1-3을 삭제한 후의 그래프');
|
||||
graph.print();
|
||||
|
||||
/* 정점 추가 */
|
||||
const v5 = new Vertex(6);
|
||||
graph.addVertex(v5);
|
||||
console.log('\n정점 6을 추가한 후의 그래프');
|
||||
graph.print();
|
||||
|
||||
/* 정점 삭제 */
|
||||
// 정점 3은 v1이다
|
||||
graph.removeVertex(v1);
|
||||
console.log('\n정점 3을 삭제한 후의 그래프');
|
||||
graph.print();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
GraphAdjList,
|
||||
};
|
||||
@@ -0,0 +1,132 @@
|
||||
/**
|
||||
* File: graph_adjacency_matrix.js
|
||||
* Created Time: 2023-02-09
|
||||
* Author: Zhuo Qinyue ([email protected])
|
||||
*/
|
||||
|
||||
/* 인접 행렬로 구현한 무방향 그래프 클래스 */
|
||||
class GraphAdjMat {
|
||||
vertices; // 정점 리스트의 원소는 정점 값을, 인덱스는 정점 인덱스를 나타낸다
|
||||
adjMat; // 인접 행렬의 행과 열 인덱스는 정점 인덱스에 해당한다
|
||||
|
||||
/* 생성자 */
|
||||
constructor(vertices, edges) {
|
||||
this.vertices = [];
|
||||
this.adjMat = [];
|
||||
// 정점 추가
|
||||
for (const val of vertices) {
|
||||
this.addVertex(val);
|
||||
}
|
||||
// 간선 추가
|
||||
// edges의 원소는 정점 인덱스, 즉 vertices 원소의 인덱스를 나타낸다
|
||||
for (const e of edges) {
|
||||
this.addEdge(e[0], e[1]);
|
||||
}
|
||||
}
|
||||
|
||||
/* 정점 개수 반환 */
|
||||
size() {
|
||||
return this.vertices.length;
|
||||
}
|
||||
|
||||
/* 정점 추가 */
|
||||
addVertex(val) {
|
||||
const n = this.size();
|
||||
// 정점 리스트에 새 정점의 값을 추가한다
|
||||
this.vertices.push(val);
|
||||
// 인접 행렬에 행 하나를 추가한다
|
||||
const newRow = [];
|
||||
for (let j = 0; j < n; j++) {
|
||||
newRow.push(0);
|
||||
}
|
||||
this.adjMat.push(newRow);
|
||||
// 인접 행렬에 열 하나를 추가한다
|
||||
for (const row of this.adjMat) {
|
||||
row.push(0);
|
||||
}
|
||||
}
|
||||
|
||||
/* 정점 삭제 */
|
||||
removeVertex(index) {
|
||||
if (index >= this.size()) {
|
||||
throw new RangeError('인덱스가 범위를 벗어났다');
|
||||
}
|
||||
// 정점 리스트에서 인덱스 index의 정점을 제거한다
|
||||
this.vertices.splice(index, 1);
|
||||
|
||||
// 인접 행렬에서 인덱스 index의 행을 삭제한다
|
||||
this.adjMat.splice(index, 1);
|
||||
// 인접 행렬에서 인덱스 index의 열을 삭제한다
|
||||
for (const row of this.adjMat) {
|
||||
row.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
/* 간선 추가 */
|
||||
// 매개변수 i, j는 vertices 원소의 인덱스에 해당한다
|
||||
addEdge(i, j) {
|
||||
// 인덱스가 범위를 벗어나거나 서로 같은 경우를 처리한다
|
||||
if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {
|
||||
throw new RangeError('인덱스가 범위를 벗어났다');
|
||||
}
|
||||
// 무방향 그래프의 인접 행렬은 주대각선을 기준으로 대칭이므로 (i, j) === (j, i)를 만족한다
|
||||
this.adjMat[i][j] = 1;
|
||||
this.adjMat[j][i] = 1;
|
||||
}
|
||||
|
||||
/* 간선 삭제 */
|
||||
// 매개변수 i, j는 vertices 원소의 인덱스에 해당한다
|
||||
removeEdge(i, j) {
|
||||
// 인덱스가 범위를 벗어나거나 서로 같은 경우를 처리한다
|
||||
if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {
|
||||
throw new RangeError('인덱스가 범위를 벗어났다');
|
||||
}
|
||||
this.adjMat[i][j] = 0;
|
||||
this.adjMat[j][i] = 0;
|
||||
}
|
||||
|
||||
/* 인접 행렬 출력 */
|
||||
print() {
|
||||
console.log('정점 리스트 = ', this.vertices);
|
||||
console.log('인접 행렬 =', this.adjMat);
|
||||
}
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
/* 무방향 그래프 초기화 */
|
||||
// edges의 원소는 정점 인덱스, 즉 vertices 원소의 인덱스를 나타낸다
|
||||
const vertices = [1, 3, 2, 5, 4];
|
||||
const edges = [
|
||||
[0, 1],
|
||||
[1, 2],
|
||||
[2, 3],
|
||||
[0, 3],
|
||||
[2, 4],
|
||||
[3, 4],
|
||||
];
|
||||
const graph = new GraphAdjMat(vertices, edges);
|
||||
console.log('\n초기화한 후의 그래프');
|
||||
graph.print();
|
||||
|
||||
/* 간선 추가 */
|
||||
// 정점 1, 2의 인덱스는 각각 0, 2이다
|
||||
graph.addEdge(0, 2);
|
||||
console.log('\n간선 1-2를 추가한 후의 그래프');
|
||||
graph.print();
|
||||
|
||||
/* 간선 삭제 */
|
||||
// 정점 1, 3의 인덱스는 각각 0, 1이다
|
||||
graph.removeEdge(0, 1);
|
||||
console.log('\n간선 1-3을 삭제한 후의 그래프');
|
||||
graph.print();
|
||||
|
||||
/* 정점 추가 */
|
||||
graph.addVertex(6);
|
||||
console.log('\n정점 6을 추가한 후의 그래프');
|
||||
graph.print();
|
||||
|
||||
/* 정점 삭제 */
|
||||
// 정점 3의 인덱스는 1이다
|
||||
graph.removeVertex(1);
|
||||
console.log('\n정점 3을 삭제한 후의 그래프');
|
||||
graph.print();
|
||||
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* File: graph_bfs.js
|
||||
* Created Time: 2023-02-21
|
||||
* Author: Zhuo Qinyue ([email protected])
|
||||
*/
|
||||
|
||||
const { GraphAdjList } = require('./graph_adjacency_list');
|
||||
const { Vertex } = require('../modules/Vertex');
|
||||
|
||||
/* 너비 우선 순회 */
|
||||
// 지정한 정점의 모든 인접 정점을 얻기 쉽도록 그래프를 인접 리스트로 표현한다
|
||||
function graphBFS(graph, startVet) {
|
||||
// 정점 순회 순서
|
||||
const res = [];
|
||||
// 방문한 정점을 기록하는 해시 집합
|
||||
const visited = new Set();
|
||||
visited.add(startVet);
|
||||
// 큐를 사용하여 BFS를 구현한다
|
||||
const que = [startVet];
|
||||
// 정점 vet에서 시작하여 모든 정점을 방문할 때까지 반복한다
|
||||
while (que.length) {
|
||||
const vet = que.shift(); // 큐 맨 앞의 정점을 꺼낸다
|
||||
res.push(vet); // 방문한 정점을 기록한다
|
||||
// 해당 정점의 모든 인접 정점을 순회한다
|
||||
for (const adjVet of graph.adjList.get(vet) ?? []) {
|
||||
if (visited.has(adjVet)) {
|
||||
continue; // 이미 방문한 정점은 건너뛴다
|
||||
}
|
||||
que.push(adjVet); // 방문하지 않은 정점만 큐에 삽입한다
|
||||
visited.add(adjVet); // 해당 정점을 방문했다고 표시한다
|
||||
}
|
||||
}
|
||||
// 정점 순회 순서를 반환한다
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
/* 무방향 그래프 초기화 */
|
||||
const v = Vertex.valsToVets([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
||||
const edges = [
|
||||
[v[0], v[1]],
|
||||
[v[0], v[3]],
|
||||
[v[1], v[2]],
|
||||
[v[1], v[4]],
|
||||
[v[2], v[5]],
|
||||
[v[3], v[4]],
|
||||
[v[3], v[6]],
|
||||
[v[4], v[5]],
|
||||
[v[4], v[7]],
|
||||
[v[5], v[8]],
|
||||
[v[6], v[7]],
|
||||
[v[7], v[8]],
|
||||
];
|
||||
const graph = new GraphAdjList(edges);
|
||||
console.log('\n초기화한 후의 그래프');
|
||||
graph.print();
|
||||
|
||||
/* 너비 우선 순회 */
|
||||
const res = graphBFS(graph, v[0]);
|
||||
console.log('\n너비 우선 순회(BFS)의 정점 순서');
|
||||
console.log(Vertex.vetsToVals(res));
|
||||
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* File: graph_dfs.js
|
||||
* Created Time: 2023-02-21
|
||||
* Author: Zhuo Qinyue (1403450829@qq.com)
|
||||
*/
|
||||
|
||||
const { Vertex } = require('../modules/Vertex');
|
||||
const { GraphAdjList } = require('./graph_adjacency_list');
|
||||
|
||||
/* 깊이 우선 순회 */
|
||||
// 지정한 정점의 모든 인접 정점을 얻기 쉽도록 그래프를 인접 리스트로 표현한다
|
||||
function dfs(graph, visited, res, vet) {
|
||||
res.push(vet); // 방문한 정점을 기록한다
|
||||
visited.add(vet); // 해당 정점을 방문했다고 표시한다
|
||||
// 해당 정점의 모든 인접 정점을 순회한다
|
||||
for (const adjVet of graph.adjList.get(vet)) {
|
||||
if (visited.has(adjVet)) {
|
||||
continue; // 이미 방문한 정점은 건너뛴다
|
||||
}
|
||||
// 인접 정점을 재귀적으로 방문한다
|
||||
dfs(graph, visited, res, adjVet);
|
||||
}
|
||||
}
|
||||
|
||||
/* 깊이 우선 순회 */
|
||||
// 지정한 정점의 모든 인접 정점을 얻기 쉽도록 그래프를 인접 리스트로 표현한다
|
||||
function graphDFS(graph, startVet) {
|
||||
// 정점 순회 순서
|
||||
const res = [];
|
||||
// 방문한 정점을 기록하는 해시 집합
|
||||
const visited = new Set();
|
||||
dfs(graph, visited, res, startVet);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* 실행 코드 */
|
||||
/* 무방향 그래프 초기화 */
|
||||
const v = Vertex.valsToVets([0, 1, 2, 3, 4, 5, 6]);
|
||||
const edges = [
|
||||
[v[0], v[1]],
|
||||
[v[0], v[3]],
|
||||
[v[1], v[2]],
|
||||
[v[2], v[5]],
|
||||
[v[4], v[5]],
|
||||
[v[5], v[6]],
|
||||
];
|
||||
const graph = new GraphAdjList(edges);
|
||||
console.log('\n초기화한 후의 그래프');
|
||||
graph.print();
|
||||
|
||||
/* 깊이 우선 순회 */
|
||||
const res = graphDFS(graph, v[0]);
|
||||
console.log('\n깊이 우선 순회(DFS)의 정점 순서');
|
||||
console.log(Vertex.vetsToVals(res));
|
||||
Reference in New Issue
Block a user