-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.js
111 lines (97 loc) · 3.05 KB
/
Graph.js
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const util=require('util');
class Graph {
constructor(){
this.edges=[];
this.nodes=[];
}
addNode(nodeArr,marked=false){
for(let i=0;i<nodeArr.length;i++){
if(this.nodes.length===0){
this.nodes.push({nodeName:nodeArr[i],marked});
this.edges[nodeArr[i]]=[];
}
for(let j=0,len=this.nodes.length;j<len;j++){
if(this.nodes[j].nodeName===nodeArr[i]) break;
if(j===len-1){
this.nodes.push({nodeName:nodeArr[i],marked});
this.edges[nodeArr[i]]=[];
}
}
}
}
initializeNode(){
this.nodes.map(node=>{
node.marked=false
});
};
addEdge(node1,node2){
const alreadyExist=this.edges[node1].includes(node2)
if(alreadyExist===true) return;
this.edges[node1].push(node2);
this.edges[node2].push(node1);
}
addDirectedEdge(node1,node2){
const alreadyExist=this.edges[node1].includes(node2);
if(alreadyExist===true) return;
this.edges[node1].push(node2);
}
DFS(startNode=this.nodes[0].nodeName){
this.initializeNode();
process.stdout.write("START -> ");
const root=this.getNode(startNode);
const stack=[];
stack.push(root);
root.marked=true;
while(stack.length!==0){
const node=stack.pop();
const edges=this.getEdges(node.nodeName);
edges.map(edge=>{
const edgeNode=this.getNode(edge);
if(edgeNode.marked===false){
edgeNode.marked=true;
stack.push(edgeNode);
}
})
process.stdout.write(" "+node.nodeName+" ->");
}
process.stdout.write(" END\n");
}
BFS(startNode=this.nodes[0].nodeName){
this.initializeNode();
process.stdout.write("START -> ");
const root=this.getNode(startNode);
const queue=[];
queue.push(root);
root.marked=true;
while(queue.length!==0){
const node=queue.shift();
const edges=this.getEdges(node.nodeName);
edges.map(edge=>{
const edgeNode=this.getNode(edge);
if(edgeNode.marked===false){
edgeNode.marked=true;
queue.push(edgeNode);
}
})
process.stdout.write(" "+node.nodeName+" ->");
}
process.stdout.write(" END\n");
}
printNodes(){
console.log(`\n Nodes : \n ${util.inspect(this.nodes,{showHidden:false,depth:null})} \n`);
}
printEdges(){
console.log(`\n Edges : \n ${util.inspect(this.edges,{showHidden:false,depth:null})} \n`);
}
getNode(nodeName){
const node=this.nodes.find(node=>{
return node.nodeName===nodeName
})
return node;
}
getEdges(nodeName){
const edge=this.edges[nodeName];
return edge;
}
}
module.exports=Graph;