-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
83 lines (83 loc) · 2.46 KB
/
index.ts
File metadata and controls
83 lines (83 loc) · 2.46 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
export default class AdjacencyMatrixGraph {
nodes: Array<Array<number>>;
size: number;
vertexMap: Array<string | null>;
constructor() {
this.nodes;
this.size;
this.vertexMap;
}
show() {
return this.nodes;
}
display() {
console.log(' ', this.vertexMap.join(' '));
this.nodes.forEach((node, idx) => {
if (this.vertexMap[idx]) {
console.log(this.vertexMap[idx], node.join(' '));
}
});
return this;
}
create(size: number) {
this.nodes = Array.from(Array(size), () => Array(size).fill(0));
this.vertexMap = Array(size).fill(null);
this.size = 0;
return this;
}
findVertexIdx(vertex: string) {
return this.vertexMap.findIndex((e) => e === vertex);
}
addVertex(vertex: string) {
if (this.size > this.vertexMap.length - 1) {
console.log('선언한 graph의 크기를 초과했습니다. ');
return this;
}
const existed = this.vertexMap.find((e) => e === vertex);
if (existed !== undefined) {
console.log('중복되는 vertex는 추가할 수 없습니다.');
return this;
}
this.vertexMap[this.size] = vertex;
this.size++;
return this;
}
addEdge(vertex1: string, vertex2: string) {
const vertex1Idx = this.findVertexIdx(vertex1);
const vertex2Idx = this.findVertexIdx(vertex2);
if (vertex1Idx === -1 || vertex2Idx === -1) {
console.log('입력한 정점이 없습니다.');
return this;
}
this.nodes[vertex1Idx][vertex2Idx] = 1;
this.nodes[vertex2Idx][vertex1Idx] = 1;
return this;
}
removeVertex(vertex: string) {
const targetVertexIdx = this.findVertexIdx(vertex);
if (this.size === 0) {
console.log('그래프에 지울 정점이 없습니다.');
return this;
}
if (targetVertexIdx === -1) {
console.log('입력한 vertex가 없습니다.');
return this;
}
this.vertexMap[targetVertexIdx] = null;
this.nodes.forEach((node) => (node[targetVertexIdx] = 0));
this.nodes[targetVertexIdx] = this.nodes[targetVertexIdx].map((node) => 0);
this.size--;
return this;
}
removeEdge(vertex1: string, vertex2: string) {
const vertex1Idx = this.findVertexIdx(vertex1);
const vertex2Idx = this.findVertexIdx(vertex2);
if (vertex1Idx === -1 || vertex2Idx === -1) {
console.log('입력한 정점이 없습니다.');
return this;
}
this.nodes[vertex1Idx][vertex2Idx] = 0;
this.nodes[vertex2Idx][vertex1Idx] = 0;
return this;
}
}