1
1
import { Vertex , vertexCompareTo , vertexEqual } from '../Vertex/Vertex/Vertex' ;
2
2
import { Edge , edgeEqual } from '../Edge' ;
3
- import * as _ from 'lodash' ;
3
+ import * as cloneDeep from 'lodash.clonedeep ' ;
4
4
import { AdjacencyMatrix } from '../Matrix/AdjacencyMatrix' ;
5
5
import { VertexNotFoundError } from '../../Errors' ;
6
6
import { AdjacencyList } from '../AdjacencyList/AdjacencyList' ;
7
7
import { TarjanStronglyConnectedComponentsAlgorithm } from './algorithms' ;
8
8
import { MutableHashMap } from '@tgillespie/hash-data-structures' ;
9
+ import { MutableHashSet } from "@tgillespie/hash-data-structures/lib/lib/mutable-hash-set/mutable-hash-set" ;
9
10
10
11
export class Graph < V extends Vertex , E extends Edge < V > > {
11
12
// Redundant information storage for performance
@@ -28,9 +29,15 @@ export class Graph<V extends Vertex, E extends Edge<V>> {
28
29
}
29
30
30
31
addVertex ( ...vertex : V [ ] ) : Graph < V , E > {
31
- const uniqueVertices = _ . uniqWith ( vertex , vertexEqual ) ;
32
- const filteredVertices = _ . differenceWith ( uniqueVertices , this . _listOfVertices , vertexEqual ) ;
33
- filteredVertices . forEach ( ( singleVertex ) => {
32
+ const uniqueVertices = new MutableHashSet < V > ( ) ;
33
+ const setOfGraphVertices = new MutableHashSet < V > ( ) ;
34
+
35
+ vertex . forEach ( x => uniqueVertices . add ( x ) ) ;
36
+ this . _listOfVertices . forEach ( x => setOfGraphVertices . add ( x ) ) ;
37
+
38
+ const filteredVertices = uniqueVertices . difference ( setOfGraphVertices ) . toArray ( ) ;
39
+
40
+ filteredVertices . forEach ( ( singleVertex ) => {
34
41
this . _listOfVertices . push ( singleVertex ) ;
35
42
this . _adjacencyList . initVertex ( singleVertex ) ;
36
43
} ) ;
@@ -39,8 +46,13 @@ export class Graph<V extends Vertex, E extends Edge<V>> {
39
46
}
40
47
41
48
deleteVertex ( ...vertex : V [ ] ) : Graph < V , E > {
42
- const uniqueVertices = _ . uniqWith ( vertex , vertexEqual ) ;
43
- const filteredVertices = _ . intersectionWith ( uniqueVertices , this . _listOfVertices , vertexEqual ) ;
49
+ const uniqueVertices = new MutableHashSet < V > ( ) ;
50
+ const setOfGraphVertices = new MutableHashSet < V > ( ) ;
51
+
52
+ vertex . forEach ( x => uniqueVertices . add ( x ) ) ;
53
+ this . _listOfVertices . forEach ( x => setOfGraphVertices . add ( x ) ) ;
54
+
55
+ const filteredVertices = uniqueVertices . intersection ( setOfGraphVertices ) . toArray ( ) ;
44
56
filteredVertices . forEach ( ( singleVertex ) => {
45
57
this . _listOfVertices = this . _listOfVertices . filter ( ( x ) => ! x . equals ( singleVertex ) ) ;
46
58
this . _listOfEdges = this . _listOfEdges . filter (
@@ -56,8 +68,13 @@ export class Graph<V extends Vertex, E extends Edge<V>> {
56
68
}
57
69
58
70
addEdge ( ...edge : E [ ] ) : Graph < V , E > {
59
- const uniqueEdges = _ . uniqWith ( edge , edgeEqual ) ;
60
- const filteredEdges = _ . differenceWith ( uniqueEdges , this . _listOfEdges , edgeEqual ) ;
71
+ const uniqueEdges = new MutableHashSet < E > ( ) ;
72
+ const setOfGraphEdges = new MutableHashSet < E > ( ) ;
73
+
74
+ edge . forEach ( x => uniqueEdges . add ( x ) ) ;
75
+ this . _listOfEdges . forEach ( x => setOfGraphEdges . add ( x ) ) ;
76
+
77
+ const filteredEdges = uniqueEdges . difference ( setOfGraphEdges ) . toArray ( ) ;
61
78
62
79
if ( ! this . addUnknownVerticesInEdges )
63
80
filteredEdges . forEach ( ( singleEdge ) => this . validateEdgeVerticesAreContainedInGraph ( singleEdge ) ) ;
@@ -78,8 +95,13 @@ export class Graph<V extends Vertex, E extends Edge<V>> {
78
95
}
79
96
80
97
deleteEdge ( ...edge : E [ ] ) : Graph < V , E > {
81
- const uniqueEdges = _ . uniqWith ( edge , edgeEqual ) ;
82
- const filteredEdges = _ . intersectionWith ( uniqueEdges , this . _listOfEdges , edgeEqual ) ;
98
+ const uniqueEdges = new MutableHashSet < E > ( ) ;
99
+ const setOfGraphEdges = new MutableHashSet < E > ( ) ;
100
+
101
+ edge . forEach ( x => uniqueEdges . add ( x ) ) ;
102
+ this . _listOfEdges . forEach ( x => setOfGraphEdges . add ( x ) ) ;
103
+
104
+ const filteredEdges = uniqueEdges . intersection ( setOfGraphEdges ) . toArray ( ) ;
83
105
filteredEdges . forEach ( ( singleEdge ) => {
84
106
this . _listOfEdges = this . _listOfEdges . filter ( ( x ) => ! x . equals ( singleEdge ) ) ;
85
107
@@ -117,7 +139,10 @@ export class Graph<V extends Vertex, E extends Edge<V>> {
117
139
} ) ;
118
140
}
119
141
120
- return _ . uniqWith ( result , vertexEqual ) ;
142
+ const resultSet = new MutableHashSet < V > ( ) ;
143
+ result . forEach ( x => resultSet . add ( x ) ) ;
144
+
145
+ return resultSet . toArray ( ) ;
121
146
}
122
147
123
148
getEdges ( vertexA : V , vertexB : V ) : E [ ] {
@@ -179,7 +204,7 @@ export class Graph<V extends Vertex, E extends Edge<V>> {
179
204
let takenEdge = null ;
180
205
let previousVertex = null ;
181
206
182
- while ( ! [ ...visited . values ( ) ] . every ( _ . identity ) ) {
207
+ while ( ! [ ...visited . values ( ) ] . every ( x => x ) ) {
183
208
currentVertex = stack . pop ( ) ;
184
209
if ( currentVertex === undefined ) {
185
210
takenEdge = null ;
@@ -238,23 +263,23 @@ export class Graph<V extends Vertex, E extends Edge<V>> {
238
263
}
239
264
240
265
copy ( ) : this {
241
- return _ . cloneDeep ( this ) ;
266
+ return cloneDeep ( this ) ;
242
267
}
243
268
244
269
getListOfEdges ( ) : E [ ] {
245
- return _ . cloneDeep ( this . _listOfEdges ) ;
270
+ return cloneDeep ( this . _listOfEdges ) ;
246
271
}
247
272
248
273
getListOfVertices ( ) : V [ ] {
249
- return _ . cloneDeep ( this . _listOfVertices ) ;
274
+ return cloneDeep ( this . _listOfVertices ) ;
250
275
}
251
276
252
277
getAdjacencyMatrix ( ) : AdjacencyMatrix < V > {
253
- return _ . cloneDeep ( this . _adjacencyMatrix ) ;
278
+ return cloneDeep ( this . _adjacencyMatrix ) ;
254
279
}
255
280
256
281
getAdjacencyList ( ) : AdjacencyList < V > {
257
- return _ . cloneDeep ( this . _adjacencyList ) ;
282
+ return cloneDeep ( this . _adjacencyList ) ;
258
283
}
259
284
260
285
get addUnknownVerticesInEdges ( ) : boolean {
0 commit comments