Skip to content

Commit f0ee15a

Browse files
committed
Add remove vertex and edge function in graph
1 parent 3dcfa70 commit f0ee15a

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

lib/graph/simple_graph.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ class SimpleGraph<T> {
3636
lockVertices(<Vertex>{src, dst});
3737
}
3838

39+
/// Removes an Edge from [this], returns `false` if edge does not exists.
40+
bool removeEdge(Vertex src, Vertex dst) {
41+
unlockVertices(<Vertex>{src, dst});
42+
var removed = src.removeConnection(dst);
43+
lockVertices(<Vertex>{src, dst});
44+
45+
return removed;
46+
}
47+
3948
/// Checks if vertex is included
4049
bool containsVertex(Vertex vertex) => _vertices.contains(vertex);
4150

@@ -51,6 +60,17 @@ class SimpleGraph<T> {
5160
/// Adds a new vertex
5261
bool addVertex(Vertex vertex) => _vertices.add(vertex);
5362

63+
/// Removes a vertex
64+
bool removeVertex(Vertex vertex) {
65+
var edgesRemoved = vertices.contains(vertex)
66+
? [
67+
...vertex.outgoingConnections.entries.map((e) => [vertex, e.key]),
68+
...vertex.incomingVertices.map((e) => [e, vertex])
69+
].fold(true, (acc, v) => acc && removeEdge(v[0], v[1]))
70+
: false;
71+
return edgesRemoved ? _vertices.remove(vertex) : edgesRemoved;
72+
}
73+
5474
Vertex<T> _getOrAddVertex(Vertex vertex) =>
5575
_vertices.add(vertex) ? vertex : vertex;
5676

test/graph/simple_graph_test.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,44 @@ void main() {
9292
expect(simpleGraph.numberOfVertices, equals(6));
9393
});
9494

95+
test('Remove a vertex from graph', () {
96+
expect(simpleGraph.numberOfVertices, equals(6));
97+
expect(simpleGraph.numberOfEdges, equals(6));
98+
expect(simpleGraph.removeVertex(a), isTrue);
99+
expect(simpleGraph.numberOfEdges, equals(3));
100+
expect(simpleGraph.numberOfVertices, equals(5));
101+
});
102+
103+
test('Remove an isolated vertex from graph', () {
104+
expect(u.isIsolated, isTrue);
105+
expect(simpleGraph.addVertex(u), isTrue);
106+
expect(simpleGraph.numberOfVertices, equals(7));
107+
expect(simpleGraph.numberOfEdges, equals(6));
108+
expect(simpleGraph.removeVertex(u), isTrue);
109+
expect(simpleGraph.numberOfVertices, equals(6));
110+
expect(simpleGraph.numberOfEdges, equals(6));
111+
});
112+
113+
test('Removing a non-existent vertex has no effect', () {
114+
expect(simpleGraph.numberOfVertices, equals(6));
115+
expect(simpleGraph.numberOfEdges, equals(6));
116+
expect(simpleGraph.removeVertex(u), isFalse);
117+
expect(simpleGraph.numberOfVertices, equals(6));
118+
expect(simpleGraph.numberOfEdges, equals(6));
119+
});
120+
121+
test('Remove an edge from graph', () {
122+
expect(simpleGraph.numberOfEdges, equals(6));
123+
expect(simpleGraph.removeEdge(a, b), isTrue);
124+
expect(simpleGraph.numberOfEdges, equals(5));
125+
});
126+
127+
test('Do no remove edge that does not exist', () {
128+
expect(simpleGraph.numberOfEdges, equals(6));
129+
expect(simpleGraph.removeEdge(a, c), isFalse);
130+
expect(simpleGraph.numberOfEdges, equals(6));
131+
});
132+
95133
test('Check for NULL graph', () {
96134
expect(emptyGraph.isNull, isTrue);
97135
expect(simpleGraph.isNull, isFalse);

0 commit comments

Comments
 (0)