Skip to content

Commit ed5202e

Browse files
committed
Added method removeEdge.
1 parent 5991ae7 commit ed5202e

6 files changed

Lines changed: 104 additions & 67 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
## 0.5.5
2+
- Added [`DirectedGraph`][DirectedGraph], [`WeightedDirectedGraph`][WeightedDirectedGraph] method `removeEdge`.
3+
14
## 0.5.4
2-
- Fixed documentation of the graph method `clear`. This method removes all graph
5+
- Fixed the documentation of the graph method `clear`. This method removes all graph
36
vertices. The resulting graph will be empty.
47
- Added the graph method `clearEdges`. This method removes only the graph
58
edges, leaving the graph vertices in place.

lib/src/graph/directed_graph.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,14 @@ class DirectedGraph<T extends Object> extends DirectedGraphBase<T> {
114114
updateCache();
115115
}
116116

117+
/// Removes the edge pointing from [vertex] to [connectedVertex].
118+
/// Does not remove the vertices.
119+
void removeEdge(T vertex, T connectedVertex) {
120+
_edges[vertex]?.remove(connectedVertex);
121+
}
122+
117123
/// Removes edges (connections) pointing from [vertex] to [connectedVertices].
118-
/// * Note: Does not remove the vertices.
124+
/// Note: Does not remove the vertices.
119125
void removeEdges(T vertex, Set<T> connectedVertices) {
120126
_edges[vertex]?.removeAll(connectedVertices);
121127
updateCache();

lib/src/graph/weighted_directed_graph.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ class WeightedDirectedGraph<T extends Object, W extends Comparable>
172172
updateCache();
173173
}
174174

175+
/// Removes the edge pointing from [vertex] to [connectedVertex].
176+
/// Does not remove the vertices.
177+
void removeEdge(T vertex, T connectedVertex) {
178+
_edges[vertex]?.remove(connectedVertex);
179+
}
180+
175181
/// Removes edges pointing from [vertex] to [connectedVertices].
176182
///
177183
/// Does not remove any vertices from the graph.

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: directed_graph
33
description: Generic directed graph and weighted directed graph with algorithms
44
enabling sorting and topological ordering of vertices.
55

6-
version: 0.5.4
6+
version: 0.5.5
77

88
homepage: https://github.com/simphotonics/directed_graph
99

test/directed_graph_test.dart

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -93,40 +93,44 @@ void main() {
9393
});
9494
});
9595

96-
group('Manipulating edges/vertices.', () {
97-
test('addEdges():', () {
96+
group('Manipulating edges/vertices:', () {
97+
test('addEdge', () {
98+
final graph = DirectedGraph.of(graph0);
99+
graph.addEdge(i, k);
100+
expect(graph.edges(i), {l, k});
101+
});
102+
103+
test('addEdges', () {
98104
final graph = DirectedGraph.of(graph0);
99105
graph.addEdges(i, {k});
100106
expect(graph.edges(i), {l, k});
101-
'{\n'
102-
' a: [b, h, c, e],\n'
103-
' b: [h],\n'
104-
' c: [h, g],\n'
105-
' d: [e, f],\n'
106-
' e: [],\n'
107-
' f: [i],\n'
108-
' g: [],\n'
109-
' h: [],\n'
110-
' i: {l},\n'
111-
' k: [g, f],\n'
112-
' l: [],\n'
113-
'}';
114-
});
115-
test('remove(l).', () {
107+
});
108+
test('removeEdge', () {
109+
final graph = DirectedGraph.of(graph0);
110+
graph.removeEdge(a, b);
111+
expect(graph.edges(a), {h, c, e});
112+
});
113+
test('removeEdges', () {
114+
final graph = DirectedGraph.of(graph0);
115+
graph.removeEdges(a, {b, h, c});
116+
expect(graph.edges(a), {e});
117+
});
118+
119+
test('remove(l)', () {
116120
final graph = DirectedGraph.of(graph0);
117121
graph.remove(l);
118122
expect(graph.edges(i), <String>{});
119123
expect(graph.sortedVertices.toList(), [
120-
'a',
121-
'b',
122-
'c',
123-
'd',
124-
'e',
125-
'f',
126-
'g',
127-
'h',
128-
'i',
129-
'k',
124+
a,
125+
b,
126+
c,
127+
d,
128+
e,
129+
f,
130+
g,
131+
h,
132+
i,
133+
k,
130134
]);
131135
});
132136
test('clear', () {
@@ -429,17 +433,17 @@ void main() {
429433
vertex = '${vertex}1';
430434
}
431435
expect(graph.sortedVertices, {
432-
'a',
433-
'b',
434-
'c',
435-
'd',
436-
'e',
437-
'f',
438-
'g',
439-
'h',
440-
'i',
441-
'k',
442-
'l',
436+
a,
437+
b,
438+
c,
439+
d,
440+
e,
441+
f,
442+
g,
443+
h,
444+
i,
445+
k,
446+
l,
443447
});
444448
});
445449
});

test/weighted_directed_graph_test.dart

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,30 @@ void main() {
9696
expect(graph.vertices.contains(l), false);
9797
// Restore graph:
9898
});
99+
test('addEdge', () {
100+
final graph = WeightedDirectedGraph.of(graph0);
101+
graph.addEdge(g, h, 1);
102+
expect(graph.edges(g), {h});
103+
expect(graph.weightAlong([g, h]), 1);
104+
});
105+
99106
test('addEdges(\'g\',{\'h\': 1}):', () {
100107
final graph = WeightedDirectedGraph.of(graph0);
101-
graph.addEdges('g', {'h': 1});
102-
expect(graph.edges('g'), {'h'});
103-
expect(graph.data['g']?['h'], 1);
108+
graph.addEdges(g, {h: 1});
109+
expect(graph.edges(g), {h});
110+
expect(graph.weightAlong([g, h]), 1);
111+
});
112+
test('removeEdge', () {
113+
final graph = WeightedDirectedGraph.of(graph0);
114+
graph.removeEdge(a, b);
115+
expect(graph.edges(a), {h, c, e});
116+
});
117+
test('removeEdges', () {
118+
final graph = WeightedDirectedGraph.of(graph0);
119+
graph.removeEdges(a, {b, h, c});
120+
expect(graph.edges(a), {e});
104121
});
122+
105123
test('clear', () {
106124
final graph = WeightedDirectedGraph.of(graph0);
107125
expect(graph.sortedVertices, graph0.sortedVertices);
@@ -301,17 +319,17 @@ void main() {
301319
expect(
302320
WeightedDirectedGraph.transitiveClosure(graph).data,
303321
<String, Map<String, int>>{
304-
'a': {'b': 1, 'h': 7, 'c': 2, 'g': 6, 'e': 4},
305-
'b': {'h': 6},
306-
'c': {'h': 5, 'g': 4},
307-
'd': {'e': 1, 'g': 3, 'f': 2, 'i': 5, 'l': 8},
308-
'e': {'g': 2},
309-
'f': {'i': 3, 'l': 6},
310-
'g': {},
311-
'h': {},
312-
'i': {'l': 3},
313-
'k': {'g': 4, 'f': 5, 'i': 8, 'l': 11},
314-
'l': {},
322+
a: {b: 1, h: 7, c: 2, g: 6, e: 4},
323+
b: {h: 6},
324+
c: {h: 5, g: 4},
325+
d: {e: 1, g: 3, f: 2, i: 5, l: 8},
326+
e: {g: 2},
327+
f: {i: 3, l: 6},
328+
g: {},
329+
h: {},
330+
i: {l: 3},
331+
k: {g: 4, f: 5, i: 8, l: 11},
332+
l: {},
315333
},
316334
);
317335
});
@@ -320,7 +338,7 @@ void main() {
320338
group('path:', () {
321339
test('min. weight', () {
322340
final graph = WeightedDirectedGraph.of(graph0);
323-
expect(graph.lightestPath('a', 'g'), [a, c, g]);
341+
expect(graph.lightestPath(a, g), [a, c, g]);
324342
expect(graph.weightAlong([a, c, g]), 6);
325343
});
326344
test('max. weight', () {
@@ -351,17 +369,17 @@ void main() {
351369
vertex = '${vertex}1';
352370
}
353371
expect(graph.sortedVertices, {
354-
'a',
355-
'b',
356-
'c',
357-
'd',
358-
'e',
359-
'f',
360-
'g',
361-
'h',
362-
'i',
363-
'k',
364-
'l',
372+
a,
373+
b,
374+
c,
375+
d,
376+
e,
377+
f,
378+
g,
379+
h,
380+
i,
381+
k,
382+
l,
365383
});
366384
});
367385
});

0 commit comments

Comments
 (0)