Skip to content

Commit 95c3b88

Browse files
author
Jegors Čemisovs
authored
Merge pull request #29 from rabestro/develop
Updated docs
2 parents 9f77f7c + eb5fd1f commit 95c3b88

28 files changed

+492
-344
lines changed

.idea/gradle.xml

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

algorithm/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
group 'lv.id.jc'
8-
version '1.1'
8+
version '1.1-SNAPSHOT'
99

1010
repositories {
1111
mavenCentral()

algorithm/src/main/java/lv/id/jc/algorithm/graph/Graph.java

+15-15
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,22 @@
1414
@FunctionalInterface
1515
public interface Graph<T> {
1616
/**
17-
* The schema of this graph.
17+
* Creates a Graph object by given schema.
18+
* <p>
19+
* In a graph schema, each vertex is assigned an edge map.
20+
* If the vertex has no edges, then it should be assigned an empty map.
1821
*
22+
* @param schema of the graph
23+
* @param <T> the type of vertex in this graph
24+
* @return graph object with given schema
25+
*/
26+
static <T> Graph<T> of(Map<T, Map<T, Number>> schema) {
27+
return () -> schema;
28+
}
29+
30+
/**
31+
* The schema of this graph.
32+
* <p>
1933
* In a graph schema, each vertex is assigned an edge map.
2034
* If the vertex has no edges, then it should be assigned an empty map.
2135
*
@@ -54,18 +68,4 @@ default double getDistance(List<T> path) {
5468
.mapToDouble(Number::doubleValue)
5569
.sum();
5670
}
57-
58-
/**
59-
* Creates a Graph object by given schema.
60-
*
61-
* In a graph schema, each vertex is assigned an edge map.
62-
* If the vertex has no edges, then it should be assigned an empty map.
63-
*
64-
* @param schema of the graph
65-
* @param <T> the type of vertex in this graph
66-
* @return graph object with given schema
67-
*/
68-
static <T> Graph<T> of(Map<T, Map<T, Number>> schema) {
69-
return () -> schema;
70-
}
7171
}

algorithm/src/main/java/lv/id/jc/algorithm/graph/SearchAlgorithm.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
/**
66
* A functional interface for graph search algorithm
77
*
8-
* @author Jegors Čemisovs
98
* @param <T> the type of vertex
9+
* @author Jegors Čemisovs
1010
* @since 1.0
1111
*/
1212
@FunctionalInterface
1313
public interface SearchAlgorithm<T> {
1414
/**
1515
* Find the path from the source node to the target
1616
*
17-
* @param graph The graph in which we search for the path
17+
* @param graph The graph in which we search for the path
1818
* @param source Search starting point identifier
1919
* @param target Search finish point identifier
2020
* @return Path found or empty list if path cannot be found

algorithm/src/test/groovy/lv/id/jc/graph/BreadthFirstSearchSpec.groovy algorithm/src/test/groovy/lv/id/jc/algorithm/graph/BreadthFirstSearchSpec.groovy

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package lv.id.jc.graph
1+
package lv.id.jc.algorithm.graph
22

33
import lv.id.jc.algorithm.graph.BreadthFirstSearch
44
import lv.id.jc.algorithm.graph.Graph
@@ -14,20 +14,20 @@ class BreadthFirstSearchSpec extends Specification {
1414
def algorithm = new BreadthFirstSearch()
1515

1616
def 'should find a route for simple graph'() {
17-
given: 'A simple graph'
17+
given:
1818
def graph = Graph.of([
1919
A: [B: 7, C: 2],
2020
B: [A: 3, C: 5],
2121
C: [A: 1, B: 3]
2222
])
2323

24-
when: 'we use Breadth First Search algorithm to find a path'
24+
when:
2525
def path = algorithm.findPath(graph, source, target)
2626

27-
then: 'we get the shortest path'
27+
then:
2828
path == shortest
2929

30-
and: 'the distance calculated correctly'
30+
and:
3131
graph.getDistance(path) == time as double
3232

3333
where:
@@ -39,7 +39,7 @@ class BreadthFirstSearchSpec extends Specification {
3939
}
4040

4141
def 'should find a route for complex graph'() {
42-
given: 'A complex graph'
42+
given:
4343
def graph = Graph.of([
4444
A: [B: 1],
4545
B: [A: 1, D: 1],
@@ -48,13 +48,13 @@ class BreadthFirstSearchSpec extends Specification {
4848
E: [F: 1],
4949
F: [D: 1, E: 1]])
5050

51-
when: 'we use Breadth First Search algorithm to find a path'
51+
when:
5252
def path = algorithm.findPath(graph, source, target)
5353

54-
then: 'we get the shortest path'
54+
then:
5555
path == shortest
5656

57-
and: 'the distance calculated correctly'
57+
and:
5858
graph.getDistance(path) == time as double
5959

6060
where:
@@ -72,24 +72,24 @@ class BreadthFirstSearchSpec extends Specification {
7272
}
7373

7474
def 'should thrown NPE path for an empty graph'() {
75-
given: 'an empty graph'
75+
given:
7676
def graph = Graph.of([:])
7777

78-
when: "we use Dijkstra's algorithm to find a path"
78+
when:
7979
algorithm.findPath(graph, 'A', 'B')
8080

81-
then: 'the exception was thrown'
81+
then:
8282
thrown NullPointerException
8383
}
8484

8585
def "should return an empty path if can't find a route"() {
8686
given: 'a simple graph with no edge between nodes'
8787
def graph = Graph.of([A: [:], B: [:]])
8888

89-
when: 'we use Breadth First Search algorithm to find a path'
89+
when:
9090
def path = algorithm.findPath(graph, 'A', 'B')
9191

92-
then: 'we get an empty path'
92+
then:
9393
path == []
9494
}
9595
}

algorithm/src/test/groovy/lv/id/jc/graph/DijkstrasAlgorithmSpec.groovy algorithm/src/test/groovy/lv/id/jc/algorithm/graph/DijkstrasAlgorithmSpec.groovy

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package lv.id.jc.graph
1+
package lv.id.jc.algorithm.graph
22

33
import lv.id.jc.algorithm.graph.DijkstrasAlgorithm
44
import lv.id.jc.algorithm.graph.Graph
@@ -12,20 +12,20 @@ class DijkstrasAlgorithmSpec extends Specification {
1212
def algorithm = new DijkstrasAlgorithm()
1313

1414
def 'should find a route for a simple graph'() {
15-
given: 'A simple graph'
15+
given:
1616
def graph = Graph.of([
1717
A: [B: 7, C: 2],
1818
B: [A: 3, C: 5],
1919
C: [A: 1, B: 3]
2020
])
2121

22-
when: "we use Dijkstra's algorithm to find a path"
22+
when:
2323
def path = algorithm.findPath(graph, source, target)
2424

25-
then: 'we get the fastest way'
25+
then:
2626
path == fastest
2727

28-
and: 'the distance calculated correctly'
28+
and:
2929
graph.getDistance(path) == time as double
3030

3131
where:
@@ -37,7 +37,7 @@ class DijkstrasAlgorithmSpec extends Specification {
3737
}
3838

3939
def 'should find a route for a medium graph'() {
40-
given: 'A medium graph'
40+
given:
4141
def graph = Graph.of([
4242
A: [B: 5],
4343
B: [A: 5, C: 10],
@@ -46,13 +46,13 @@ class DijkstrasAlgorithmSpec extends Specification {
4646
E: [B: 5]
4747
])
4848

49-
when: "we use Dijkstra's algorithm to find a path"
49+
when:
5050
def path = algorithm.findPath(graph, source, target)
5151

52-
then: 'we get the fastest way'
52+
then:
5353
path == fastest
5454

55-
and: 'the distance calculated correctly'
55+
and:
5656
graph.getDistance(path) == time as double
5757

5858
where:
@@ -66,7 +66,7 @@ class DijkstrasAlgorithmSpec extends Specification {
6666
}
6767

6868
def 'should find a route for a complex graph'() {
69-
given: 'A complex graph'
69+
given:
7070
def graph = Graph.of([
7171
A: [B: 5, H: 2],
7272
B: [A: 5, C: 7],
@@ -78,13 +78,13 @@ class DijkstrasAlgorithmSpec extends Specification {
7878
H: [G: 3]
7979
])
8080

81-
when: "we use Dijkstra's algorithm to find a path"
81+
when:
8282
def path = algorithm.findPath(graph, source, target)
8383

84-
then: 'we get the fastest way'
84+
then:
8585
path == fastest
8686

87-
and: 'the distance calculated correctly'
87+
and:
8888
graph.getDistance(path) == time as double
8989

9090
where:
@@ -104,24 +104,24 @@ class DijkstrasAlgorithmSpec extends Specification {
104104
}
105105

106106
def 'should thrown NPE for an empty graph'() {
107-
given: 'an empty graph'
107+
given:
108108
def graph = Graph.of([:])
109109

110-
when: "we use Dijkstra's algorithm to find a path"
110+
when:
111111
algorithm.findPath(graph, 'A', 'B')
112112

113-
then: 'the exception was thrown'
113+
then:
114114
thrown NullPointerException
115115
}
116116

117117
def "should return an empty path if can't find a route"() {
118118
given: 'a simple graph with no edge between nodes'
119119
def graph = Graph.of([A: [:], B: [:]])
120120

121-
when: "we use Dijkstra's algorithm to find a path"
121+
when:
122122
def path = algorithm.findPath(graph, 'A', 'B')
123123

124-
then: 'we get an empty path'
124+
then:
125125
path == []
126126
}
127127
}

algorithm/src/test/groovy/lv/id/jc/graph/GraphSpec.groovy algorithm/src/test/groovy/lv/id/jc/algorithm/graph/GraphSpec.groovy

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package lv.id.jc.graph
1+
package lv.id.jc.algorithm.graph
22

33
import lv.id.jc.algorithm.graph.Graph
44
import spock.lang.Narrative

algorithm/src/test/groovy/lv/id/jc/graph/SearchAlgorithmSpec.groovy algorithm/src/test/groovy/lv/id/jc/algorithm/graph/SearchAlgorithmSpec.groovy

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package lv.id.jc.graph
1+
package lv.id.jc.algorithm.graph
22

33
import lv.id.jc.algorithm.graph.BreadthFirstSearch
44
import lv.id.jc.algorithm.graph.DijkstrasAlgorithm
@@ -16,7 +16,7 @@ class SearchAlgorithmSpec extends Specification {
1616
def dijkstras = new DijkstrasAlgorithm()
1717

1818
def 'should find a route for a complex graph'() {
19-
given: 'A complex graph sample'
19+
given:
2020
def graph = Graph.of([
2121
A: [B: 5, H: 2],
2222
B: [A: 5, C: 7],
@@ -28,19 +28,19 @@ class SearchAlgorithmSpec extends Specification {
2828
H: [G: 3]
2929
])
3030

31-
when: 'we use Breadth First Search algorithm for the first route'
31+
when:
3232
def routeOne = bfsAlgorithm.findPath(graph, source, target)
3333

34-
and: 'we use Dijkstras algorithm for the second route'
34+
and:
3535
def routeTwo = dijkstras.findPath(graph, source, target)
3636

37-
then: "the first route is the shortest"
37+
then:
3838
routeOne == shortest
3939

40-
and: 'the second route is the fastest'
40+
and:
4141
routeTwo == fastest
4242

43-
and: 'the distance calculated correctly'
43+
and:
4444
graph.getDistance(routeOne) == d1 as double
4545
graph.getDistance(routeTwo) == d2 as double
4646

algorithm/src/test/resources/SpockConfig.groovy

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
spockReports {
22

33
set(['com.athaydes.spockframework.report.showCodeBlocks' : true,
4-
'com.athaydes.spockframework.report.outputDir' : 'docs/spock-reports',
4+
'com.athaydes.spockframework.report.outputDir' : '../docs/spock-reports',
55
'com.athaydes.spockframework.report.projectName' : 'Graph search algorithms',
66
'com.athaydes.spockframework.report.projectVersion' : 1.1,
77
'com.athaydes.spockframework.report.internal.HtmlReportCreator.enabled': true,

application/build.gradle

-13
This file was deleted.

application/src/main/java/module-info.java

-4
This file was deleted.

docs/inspection/index.html

+1-1
Large diffs are not rendered by default.

docs/spock-reports/aggregated_report.json

+1-1
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)