Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gyim1345] 그래프 구현 #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions 자료구조/Graph.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@


class Graph {
constructor() {
this.map = new Map();
this.vertex = {};
}

addVertex(vertex) {
if(this.vertex[vertex]) {
return;
}
this.vertex[vertex] = true;
}

removeVertex(vertex) {
if(!this.vertex[vertex]) {
return;
}

delete this.vertex[vertex];
this.map.delete(vertex);
this.map.forEach((value, key)=> {
if(value.includes(vertex)) {
this.removeEdge(key, value[0]);
}
})
}

removeEdge(key,value) {
if(!this.hasEdge(key,value)){
return;
}

const filteredKeys = [...this.map.get(key).filter(x=> x !== value)];

if(!filteredKeys[0]) {
Comment on lines +35 to +37
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filter 대신 find를 쓰면 조금 더 명시적일 것 같습니다!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filter 하나로 퉁 칠려고 했는데 명시적으로 쓰기 위해서 find 와 filter 둘다 쓰는게 좋을까요?

this.map.delete(key);
return;
}
this.map.set(key, filteredKeys);
}

hasVertex(a) {
return !!this.vertex[a];
}

hasEdge(a,b) {
if(!this.vertex[a]) {
return false;
}

const values = this.map.get(a);
if(values && values.filter(el => el === b)[0]) {
return true;
}
return false
}

addEdge(a,b) {
if(!this.vertex[a] || !this.vertex[b]) {
return;
}

const arr = this.map.get(a);
if(arr) {
Comment on lines +65 to +66
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (this.map.has(a)) { ... }

요렇게 퉁쳐도 될 것 같네여!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍
아 이 부분 사실 바로 밑줄에 this.map.get(a) 를 한번 더 써서 변수에 담아서 쓸려고 했는데 정작 변수에 담아 놓고 안썼네요 ㅋㅋ...
한번 더 쓰게 되면 변수에 담아서 쓰는게 좋겠죠?

this.map.set(a,[...this.map.get(a),b ])
return;
}
this.map.set(a,[b])
}
}

test('removeVertex', () => {
const graph = new Graph();
[1,2,3,4,5,6].forEach(num => {
graph.addVertex(num);
})

graph.addEdge(1,2);
graph.addEdge(1,5);
graph.addEdge(2,3);
graph.addEdge(3,4);
graph.addEdge(4,5);
graph.addEdge(5,6);
graph.addEdge(6,1);

graph.removeVertex(1)

expect(graph.hasEdge(6,1)).toBe(false);
expect(graph.hasVertex(1)).toBe(false);

graph.addVertex(7);
expect(graph.hasEdge(7,6)).toBe(false);
});

test('removeEdge', () => {
const graph = new Graph();
[1,2,3,4,5,6].forEach(num => {
graph.addVertex(num);
})

graph.addEdge(1,2);
graph.addEdge(1,5);
graph.addEdge(2,3);
graph.addEdge(3,4);
graph.addEdge(4,5);
graph.addEdge(5,6);
graph.addEdge(6,1);

graph.removeEdge(6,1)
expect(graph.hasEdge(6,1)).toBe(false);
});