-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path1-directedGraph.js
More file actions
162 lines (158 loc) · 4.45 KB
/
1-directedGraph.js
File metadata and controls
162 lines (158 loc) · 4.45 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
'use strict';
class DirectedGraph {
constructor(vertexN) {
this._adjacency = []; // _adjacency[v] = adjacency array for vertex v
this._indegree = []; // _indegree[v] = indegree of vertex v
this._weight = []; // _weight[v][w] = weight of the edge (v; w)
if (vertexN === undefined) return this;
let i;
for (i = 0; i < vertexN; ++i) {
this.addVertex();
}
return this;
}
addVertex() {
this._adjacency.push([]);
this._weight.push([]);
const vertexIndex = this._adjacency.length - 1;
this._indegree[vertexIndex] = 0;
return this;
}
hasVertex(v) {
if (v < 0 || v >= this.size) {
return false;
}
return true;
}
get size() {
return this._adjacency.length;
}
connect(v1, v2, weight) {
if (!this.hasVertex(v1) || !this.hasVertex(v2)) {
return false;
}
this._adjacency[v1].push(v2);
this._weight[v1][v2] = weight;
++this._indegree[v2];
return true;
}
disconnect(v1, v2) {
if (!this.hasVertex(v1) || !this.hasVertex(v2)) {
return false;
}
const vertexIndex = this._adjacency[v1].indexOf(v2);
if (vertexIndex < 0) return false;
this._adjacency[v1].splice(vertexIndex, 1);
this._weight[v1][v2] = undefined;
--this._indegree[v2];
return true;
}
isConnected(v1, v2) {
if (!this.hasVertex(v1) || !this.hasVertex(v2)) {
return false;
}
const vertexIndex = this._adjacency[v1].indexOf(v2);
if (vertexIndex < 0) return false;
return true;
}
getWeight(v1, v2) {
return this._weight[v1][v2];
}
transpose() {
const size = this.size;
const transposedGraph = new DirectedGraph(size);
let v, w, weight;
for (v = 0; v < size; ++v) {
for (w of this._adjacency[v]) {
weight = this.getWeight(v, w);
transposedGraph.connect(w, v, weight);
}
}
return transposedGraph;
}
/**
* Computes shortest paths from a single source vertex
* to all of the other vertices (Bellman-Ford inplementation).
*
* @param from = the vertex
* @return distanceArr[v] = minimum distance to v
* parentArr[v] = parent vertex for v in the shortest path
*/
minimumDistance(from) {
if (!this.hasVertex(from)) {
return null;
}
const distance = new Array(this.size);
const parent = new Array(this.size);
let i;
for (i = 0; i < this.size; ++i) {
distance[i] = Infinity;
parent[i] = -1;
}
distance[from] = 0;
for (i = 0; i < this.size - 1; ++i) {
for (let v in this._adjacency) { // for each vertex v
for (let w of this._adjacency[v]) { // for each incident edge for v
if (distance[w] > distance[v] + this.getWeight(v, w)) {
distance[w] = distance[v] + this.getWeight(v, w);
parent[w] = v;
}
}
}
}
// if any distance[i] changes, the graph has negative cycles
for (let v in this._adjacency) {
for (let w of this._adjacency[v]) {
if (distance[w] > distance[v] + this.getWeight(v, w)) {
return null;
}
}
}
return { distanceArr: distance, parentArr: parent };
}
toposort() {
const grey = 1,
black = 2,
sorted = new Array(),
marked = new Array(this.size);
const dfs = (v) => {
if (marked[v] === grey) return 0;
if (marked[v] === black) return 1;
marked[v] = grey;
for (let w of this._adjacency[v]) {
if (!dfs(w)) return undefined;
}
marked[v] = black;
sorted.unshift(v);
return marked[v];
};
for (let v in this._adjacency) {
if (marked[v]) continue;
if (!dfs(v)) return null;
}
return sorted;
}
}
const myGraph = new DirectedGraph(4);
myGraph.connect(0, 1, 2);
myGraph.connect(0, 2, 3);
myGraph.connect(1, 2, -2);
myGraph.connect(1, 3, 2);
myGraph.connect(2, 3, 3);
const checkVertex = 0;
const checkEdge = [2, 3];
if (myGraph.hasVertex(checkVertex)) {
console.log('I have vertex ' + checkVertex);
} else {
console.log('I do not have a vertex ' + checkVertex + ' :(');
}
if (myGraph.isConnected(...checkEdge)) {
console.log(checkEdge[0] + ' is connected with ' + checkEdge[1]);
console.log('It has weight ' + myGraph.getWeight(...checkEdge));
}
console.log('Lets find the shortest paths for checkVertex!');
console.time('Belman-Ford');
const result = myGraph.minimumDistance(checkVertex);
console.timeEnd('Belman-Ford');
console.dir(result);
console.log('Toposort test: ' + myGraph.toposort());