-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.js
71 lines (49 loc) · 1.01 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
class Graph {
this.nodeLookup = {};
constructor(){}
addNode(x){
}
removeNode(x){}
addEdge(x, y){
var source = getNode(x);
var destination = getNode(y);
if(!source || !destination) throw new Error("Invalid node ID");
source.adjacent.push(destination);
}
removeEdge(x, y){}
getNode(x){
return this.nodeLookup[x.id];
}
setNode(x){}
areAdjacent(x, y){}
getNeibours(x){}
hasPathDFS(x, y){
var source = getNode(x);
var destination = getNode(y);
var visited = {};
return hasPathDFSHelper(source, destination, visited);
}
hasPathDFSHelper(source, destination, visited){
if(visited[source.id]){
return false;
}
visited[source.id] = true;
if(source.id === destination.id){
return true;
}
source.adjacentNodes.forEach((adjacent) => {
if(hasPathDFSHelper(adjacent, destination, visited)){
return true;
}
});
return false;
}
hasPathBFS(source, destination){}
}
class Node {
this.id;
this.adjacentNodes = [];
constructor(id){
this.id = id;
}
}