-
Notifications
You must be signed in to change notification settings - Fork 0
/
topology.js
103 lines (102 loc) · 2.48 KB
/
topology.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
function Dictionary(){
var items={};
this.has=function(key){
return items.hasOwnProperty(key);
};
this.set=function(key,value){
items[key]=value;
};
this.get=function(v){
return items[v];
};
this.delete=function(key){
if(this,has(key)){
delete items[key];
return true;
}
return false;
};
}
function Graph(){
var vertices=[];
var adjList=new Dictionary();
this.addVertex=function(v){
vertices.push(v);
adjList.set(v,[]);
};
this.addEdge=function(v,w){
adjList.get(v).push(w);
adjList.get(w).push(v);
};
var initializeColor=function(){
var color=[];
for(var i=0;i<vertices.length;i++){
color[vertices[i]]='white';
}
return color;
}
this.toString=function(){
var string='';
for(var i=0;i<vertices.length;i++){
var u=vertices[i];
string+=u+': ';
var neighbors=adjList.get(u);
for(var j=0;j<neighbors.length;j++){
string+=neighbors[j]+' ';
}
string+='\n';
}
return string;
}
var time=0;
var DFSVisit=function(u,color,d,f,p){
console.log('discoverd '+u);
color[u]='grey';
d[u]=++time;
var neighbors=adjList.get(u);
for(var i=0;i<neighbors.length;i++){
var w=neighbors[i];
if(color[w]==='white'){
p[w]=u;
DFSVisit(w,color,d,f,p);
}
}
color[w]='black';
f[u]=++time;
console.log('explored '+u);
};
this.DFS=function(){
var color=initializeColor(),
d=[],
f=[],
p=[],
time=0;
for(var i=0;i<vertices.length;i++){
f[vertices[i]]=0;
d[vertices[i]]=0;
p[vertices[i]]=null;
}
for(i=0;i<vertices.length;i++){
if(color[vertices[i]]==='white'){
DFSVisit(vertices[i],color,d,f,p);
}
}
return {
discovery:d,
finished:f,
predecessors:p
}
}
}
graph=new Graph();
myVertices=['A','B','C','D','E','F'];
for(i=0;i<myVertices.length;i++){
graph.addVertex(myVertices[i]);
}
graph.addEdge('A','C');
graph.addEdge('A','D');
graph.addEdge('B','D');
graph.addEdge('B','E');
graph.addEdge('C','F');
graph.addEdge('F','E');
console.log(graph.DFS());