forked from Graph-Visualization/graph-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
floodfill.js
76 lines (54 loc) · 1.82 KB
/
floodfill.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
const GraphBase = require('./graph-base');
class FloodFill extends GraphBase {
constructor(numVertices = 0, numEdges = 0, startingPoint = 0) {
super(numVertices, numEdges);
this.startingPoint = startingPoint;
}
floodFill() {
let Adj = this.getSimpleAdj();
let visited = [];
let vertex = [];
Adj.forEach((value, key) => {
visited[key] = false;
vertex.push(key);
});
let root = this.startingPoint;
let listOfNodes = [];
this.floodFillHelper(root, visited, listOfNodes, Adj);
return listOfNodes;
}
floodFillHelper(node, visited, listOfNodes, Adj) {
visited[node] = true;
for (let i = 0; i < Adj.get(node).length; i++) {
if (!visited[Adj.get(node)[i]]) {
this.floodFillHelper(Adj.get(node)[i], visited, listOfNodes, Adj);
}
}
listOfNodes.push(node);
}
}
/*-------------Test Case Starts---------------------*/
// const input_file = require('./testfiles/floodFillTest.json')
// const fs = require("fs")
// output_data = new Map()
// for(let testCase=0;testCase<input_file.length;testCase++)
// {
// let input = input_file[testCase]
// const g = new FloodFill(input.numVertices,input.numEdges,input.startingPoint)
// vertices = input.vertices
// for(let i=0;i<vertices.length;i++)
// {
// g.addVertex(vertices[i])
// }
// for(let i=0;i<input.numEdges;i++)
// {
// let edge = input.edges[i]
// g.addEdge(edge[0],edge[1])
// }
// // console.log("Output For TestCase " + (testCase+1)+ " : g.floodFill()")
// output_data.set("Testcase "+ testCase , g.floodFill())
// }
// // console.log(output_data)
// fs.writeFileSync("testfiles/floodFillOutput.json",JSON.stringify(Array.from(output_data)))
/*+++++++++++++++++Test Case Ends++++++++++++++++++++++*/
module.exports = FloodFill;