-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.js
More file actions
175 lines (175 loc) · 4.53 KB
/
graph.js
File metadata and controls
175 lines (175 loc) · 4.53 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const queue = require('./queue.js');
let q = new queue.structure();
class graph{
data;
edges;
constructor(data){
this.data=data;
this.edges=[];
}
addEdge(node){
this.edges.push(node);
node.edges.push(this);
}
print(){
q.clear();
//Using BFS;
q.enqueue(this);
let visited=[];
while(!q.isEmpty()){
let temp=q.dequeue();
if(visited.includes(temp.data)){
continue;
}
console.log(temp.data);
visited.push(temp.data);
for(let i=0;i<temp.edges.length;i++){
q.enqueue(temp.edges[i]);
}
}
}
bfs(){
q.clear();
q.enqueue(this);
let visited=[];
while(!q.isEmpty()){
let temp=q.dequeue();
if(visited.includes(temp.data)){
continue;
}
console.log(temp.data);
visited.push(temp.data);
for(let i=0;i<temp.edges.length;i++){
q.enqueue(temp.edges[i]);
}
}
}
dfs(){
let visited=[];
this.dfsgenerator(this,visited);
}
dfsgenerator(node,visited){
if(visited.includes(node.data)){
return;
}
console.log(node.data);
visited.push(node.data);
for(let i=0;i<node.edges.length;i++){
this.dfsgenerator(node.edges[i],visited);
}
}
sum(){
let sum=0;
let visited=[];
q.clear();
q.enqueue(this);
while(!q.isEmpty()){
let temp=q.dequeue();
if(visited.includes(temp.data)){
continue;
}
sum+=temp.data;
visited.push(temp.data);
for(let i=0;i<temp.edges.length;i++){
q.enqueue(temp.edges[i]);
}
}
return sum;
}
max(){
let max=-Infinity;
let visited=[];
q.clear();
q.enqueue(this);
while(!q.isEmpty()){
let temp=q.dequeue();
if(visited.includes(temp.data)){
continue;
}
if(temp.data>max){
max=temp.data;
}
visited.push(temp.data);
for(let i=0;i<temp.edges.length;i++){
q.enqueue(temp.edges[i]);
}
}
return max;
}
min(){
let min=Infinity;
let visited=[];
q.clear();
q.enqueue(this);
while(!q.isEmpty()){
let temp=q.dequeue();
if(visited.includes(temp.data)){
continue;
}
if(temp.data<min){
min=temp.data;
}
visited.push(temp.data);
for(let i=0;i<temp.edges.length;i++){
q.enqueue(temp.edges[i]);
}
}
return min;
}
count(){
let count=0;
let visited=[];
q.clear();
q.enqueue(this);
while(!q.isEmpty()){
let temp=q.dequeue();
if(visited.includes(temp.data)){
continue;
}
count++;
visited.push(temp.data);
for(let i=0;i<temp.edges.length;i++){
q.enqueue(temp.edges[i]);
}
}
return count;
}
average(){
return this.sum()/this.count();
}
sort(){
let visited=[];
q.clear();
q.enqueue(this);
let arr=[];
while(!q.isEmpty()){
let temp=q.dequeue();
if(visited.includes(temp.data)){
continue;
}
arr.push(temp.data);
visited.push(temp.data);
for(let i=0;i<temp.edges.length;i++){
q.enqueue(temp.edges[i]);
}
}
arr.sort();
return arr;
}
}
module.exports={
structure:graph,
description:"Graph data structure",
methods:{
addEdge:"Adds an edge to the graph",
print:"Prints the graph using BFS",
bfs:"Prints the graph using BFS",
dfs:"Prints the graph using DFS",
sum:"Returns the sum of all the nodes in the graph",
max:"Returns the maximum value of all the nodes in the graph",
min:"Returns the minimum value of all the nodes in the graph",
count:"Returns the number of nodes in the graph",
average:"Returns the average of all the nodes in the graph",
sort:"Returns the sorted array of all the nodes in the graph"
}
};