Skip to content

Commit 3dcfa70

Browse files
committed
Rename Graph to SimpleGraph
1 parent 22e5154 commit 3dcfa70

10 files changed

+40
-49
lines changed

lib/graph/bellman_ford.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import 'dart:collection';
22

3-
import 'graph.dart';
3+
import 'simple_graph.dart';
44
import 'vertex.dart';
55

66
/// Run the Bellman Ford algorithm to get the shortest paths from [source] to
77
/// all vertices.
8-
HashMap<Vertex, num> bellmanFord(Graph graph, Vertex source) {
8+
HashMap<Vertex, num> bellmanFord(SimpleGraph graph, Vertex source) {
99
var distances = HashMap<Vertex, num>();
1010
distances[source] = 0;
1111

lib/graph/bfs.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import '../lists/queue.dart';
2-
import 'graph.dart';
2+
import 'simple_graph.dart';
33
import 'traversal.dart';
44
import 'vertex.dart';
55

66
/// Recursively traverse graph in depth first.
7-
Traversal traverse(Graph graph, Vertex vertex) {
7+
Traversal traverse(SimpleGraph graph, Vertex vertex) {
88
var traversal = Traversal();
99

1010
_doBFS(graph, vertex, traversal);
1111

1212
return traversal;
1313
}
1414

15-
void _doBFS(Graph graph, Vertex vertex, Traversal traversal) {
15+
void _doBFS(SimpleGraph graph, Vertex vertex, Traversal traversal) {
1616
var queue = Queue<Vertex>();
1717
traversal.addVisited(vertex);
1818
queue.enqueue(vertex);

lib/graph/dfs.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import 'graph.dart';
1+
import 'simple_graph.dart';
22
import 'traversal.dart';
33
import 'vertex.dart';
44

55
/// Recursively traverse graph in depth first.
6-
Traversal traverse(Graph graph, Vertex vertex) {
6+
Traversal traverse(SimpleGraph graph, Vertex vertex) {
77
var traversal = Traversal();
88

99
_doDFS(graph, vertex, traversal);
1010

1111
return traversal;
1212
}
1313

14-
void _doDFS(Graph graph, Vertex vertex, Traversal traversal) {
14+
void _doDFS(SimpleGraph graph, Vertex vertex, Traversal traversal) {
1515
traversal.addVisited(vertex);
1616
traversal.addVisit(vertex);
1717
for (var connectedVertex in vertex.outgoingVertices) {

lib/graph/graph.dart renamed to lib/graph/simple_graph.dart

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'vertex.dart';
22

33
/// A Graph Type
4-
class Graph<T> {
4+
class SimpleGraph<T> {
55
Set<Vertex<T>> _vertices;
66

77
/// Vertices of this graph
@@ -11,11 +11,8 @@ class Graph<T> {
1111
/// Is this a Digraph?
1212
final bool isDigraph;
1313

14-
/// Does this graph allow loops?
15-
final bool allowLoops;
16-
1714
/// Create a new graph
18-
Graph({this.isDigraph = true, this.allowLoops = false}) {
15+
SimpleGraph({this.isDigraph = true}) {
1916
_vertices = <Vertex<T>>{};
2017
}
2118

@@ -29,7 +26,7 @@ class Graph<T> {
2926
/// Adds an edge
3027
void addEdge(Vertex src, Vertex dst, [num weight = 1]) {
3128
unlockVertices(<Vertex>{src, dst});
32-
if (src.key == dst.key && !allowLoops) throw Error();
29+
if (src.key == dst.key) throw Error();
3330

3431
src = _getOrAddVertex(src);
3532
dst = _getOrAddVertex(dst);

lib/graph/topological_sort.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import 'dart:collection';
22

3-
import 'graph.dart';
3+
import 'simple_graph.dart';
44
import 'vertex.dart';
55

66
/// Returns topological sort using Kahn's algorithm
7-
List<Vertex> topologicalSort(Graph graph) {
7+
List<Vertex> topologicalSort(SimpleGraph graph) {
88
var sorted = <Vertex>[];
99
var inDegreeData = inDegrees(graph);
1010
var noInDegrees = <Vertex>{};
@@ -37,7 +37,7 @@ bool _detectCycle(HashMap<Vertex, int> inDegreeData) =>
3737
inDegreeData.values.where((element) => element > 0).length > 0;
3838

3939
/// Returns in degrees of all vertices of [graph]
40-
HashMap<Vertex, int> inDegrees(Graph graph) {
40+
HashMap<Vertex, int> inDegrees(SimpleGraph graph) {
4141
var inDegrees = HashMap<Vertex, int>();
4242
var vertices = graph.vertices;
4343

test/graph/bellman_ford_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import 'package:test/test.dart';
22

33
import 'package:algorithms/graph/bellman_ford.dart';
4-
import 'package:algorithms/graph/graph.dart';
4+
import 'package:algorithms/graph/simple_graph.dart';
55
import 'package:algorithms/graph/vertex.dart';
66

77
void main() {
8-
Graph graph;
8+
SimpleGraph graph;
99
Vertex a, b, c, d, e;
1010

1111
void _initializeVertices() {
@@ -18,7 +18,7 @@ void main() {
1818

1919
setUp(() {
2020
_initializeVertices();
21-
graph = Graph();
21+
graph = SimpleGraph();
2222
graph.addEdge(a, b, -1);
2323
graph.addEdge(a, c, 4);
2424
graph.addEdge(b, c, 3);

test/graph/bfs_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import 'package:test/test.dart';
22

33
import 'package:algorithms/graph/bfs.dart';
4-
import 'package:algorithms/graph/graph.dart';
4+
import 'package:algorithms/graph/simple_graph.dart';
55
import 'package:algorithms/graph/vertex.dart';
66

77
void main() {
8-
Graph graph;
8+
SimpleGraph graph;
99
Vertex u, v, w, x, y, z;
1010

1111
void _initializeVertices() {
@@ -19,7 +19,7 @@ void main() {
1919

2020
setUp(() {
2121
_initializeVertices();
22-
graph = Graph(isDigraph: true);
22+
graph = SimpleGraph(isDigraph: true);
2323
/* v --- y
2424
/ |
2525
u ----- z

test/graph/dfs_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import 'package:test/test.dart';
22

33
import 'package:algorithms/graph/dfs.dart';
4-
import 'package:algorithms/graph/graph.dart';
4+
import 'package:algorithms/graph/simple_graph.dart';
55
import 'package:algorithms/graph/vertex.dart';
66

77
void main() {
8-
Graph graph;
8+
SimpleGraph graph;
99
Vertex u, v, w, x, y, z;
1010

1111
void _initializeVertices() {
@@ -19,7 +19,7 @@ void main() {
1919

2020
setUp(() {
2121
_initializeVertices();
22-
graph = Graph(isDigraph: true);
22+
graph = SimpleGraph(isDigraph: true);
2323
/* v --- y
2424
/ |
2525
u ----- z

test/graph/graph_test.dart renamed to test/graph/simple_graph_test.dart

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import 'package:test/test.dart';
22

3-
import 'package:algorithms/graph/graph.dart';
3+
import 'package:algorithms/graph/simple_graph.dart';
44
import 'package:algorithms/graph/vertex.dart';
55

66
void main() {
7-
Graph emptyGraph, simpleGraph;
7+
SimpleGraph emptyGraph, simpleGraph;
88
Vertex a, b, c, d, e, f, u, v;
99

1010
void _initializeVertices() {
@@ -20,15 +20,15 @@ void main() {
2020

2121
setUp(() {
2222
_initializeVertices();
23-
emptyGraph = Graph();
23+
emptyGraph = SimpleGraph();
2424

2525
/* b -4- c
2626
/ |
2727
a --2-- d
2828
\
2929
f -7- e
3030
*/
31-
simpleGraph = Graph();
31+
simpleGraph = SimpleGraph();
3232

3333
simpleGraph.addEdge(a, b);
3434
simpleGraph.addEdge(a, f);
@@ -48,31 +48,25 @@ void main() {
4848
expect(simpleGraph.numberOfEdges, equals(6));
4949
});
5050

51-
test('Loops are not allowed', () {
52-
var graph = Graph(allowLoops: false);
51+
test('Loops are not allowed for a simple graph', () {
52+
var graph = SimpleGraph();
5353
expect(() => graph.addEdge(a, a), throwsA(isA<Error>()));
5454
});
5555

56-
test('Loops are allowed if settings permits', () {
57-
var graph = Graph(allowLoops: true);
58-
graph.addEdge(u, u);
59-
expect(graph.numberOfVertices, equals(1));
60-
});
61-
6256
test('Digraphs add connections in one way', () {
63-
var graph = Graph(isDigraph: true);
57+
var graph = SimpleGraph(isDigraph: true);
6458
graph.addEdge(u, v);
6559
expect(graph.numberOfEdges, equals(1));
6660
});
6761

6862
test('Non-digraphs add connections in both ways', () {
69-
var graph = Graph(isDigraph: false);
63+
var graph = SimpleGraph(isDigraph: false);
7064
graph.addEdge(u, v);
7165
expect(graph.numberOfEdges, equals(2));
7266
});
7367

7468
test('Checks for vertex', () {
75-
var graph = Graph();
69+
var graph = SimpleGraph();
7670
graph.addEdge(u, v);
7771
expect(graph.containsVertex(u), isTrue);
7872
expect(graph.containsVertex(v), isTrue);
@@ -107,7 +101,7 @@ void main() {
107101
expect(emptyGraph.isSingleton, isFalse);
108102
expect(simpleGraph.isSingleton, isFalse);
109103

110-
var graph = Graph();
104+
var graph = SimpleGraph();
111105
graph.addVertex(u);
112106
expect(graph.isSingleton, isTrue);
113107
});
@@ -116,7 +110,7 @@ void main() {
116110
expect(emptyGraph.isEmpty, isTrue);
117111
expect(simpleGraph.isEmpty, isFalse);
118112

119-
var graph = Graph();
113+
var graph = SimpleGraph();
120114
graph.addVertex(u);
121115
graph.addVertex(v);
122116
expect(graph.isEmpty, isTrue);

test/graph/topological_sort_test.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import 'package:test/test.dart';
22

3-
import 'package:algorithms/graph/graph.dart';
3+
import 'package:algorithms/graph/simple_graph.dart';
44
import 'package:algorithms/graph/topological_sort.dart';
55
import 'package:algorithms/graph/vertex.dart';
66

77
void main() {
8-
Graph emptyGraph, singleGraph, graph;
8+
SimpleGraph emptyGraph, singleGraph, graph;
99
Vertex a, b, c, d, e, f, g, h, i, j, k, x, y;
1010

1111
void _initializeVertex() {
@@ -26,8 +26,8 @@ void main() {
2626

2727
setUp(() {
2828
_initializeVertex();
29-
emptyGraph = Graph(isDigraph: true);
30-
graph = Graph(isDigraph: true);
29+
emptyGraph = SimpleGraph(isDigraph: true);
30+
graph = SimpleGraph(isDigraph: true);
3131
/*
3232
(a) -> (b) -> (c) -> (f) <- (h)
3333
| | (i)
@@ -46,7 +46,7 @@ void main() {
4646
graph.addEdge(i, j);
4747
graph.addEdge(k, j);
4848

49-
singleGraph = Graph();
49+
singleGraph = SimpleGraph();
5050
singleGraph.addEdge(x, y);
5151
});
5252

@@ -99,7 +99,7 @@ void main() {
9999
});
100100

101101
test('Topological sort fails on undirected graph', () {
102-
var unDirectedGraph = Graph(isDigraph: false);
102+
var unDirectedGraph = SimpleGraph(isDigraph: false);
103103
unDirectedGraph.addEdge(Vertex('A'), Vertex('B'));
104104
expect(topologicalSort(unDirectedGraph), isNull);
105105
});

0 commit comments

Comments
 (0)