Skip to content

Commit

Permalink
Merge pull request #29 from rabestro/develop
Browse files Browse the repository at this point in the history
Updated docs
  • Loading branch information
Jegors Čemisovs committed Jan 6, 2022
2 parents 9f77f7c + eb5fd1f commit 95c3b88
Show file tree
Hide file tree
Showing 28 changed files with 492 additions and 344 deletions.
3 changes: 2 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion algorithm/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group 'lv.id.jc'
version '1.1'
version '1.1-SNAPSHOT'

repositories {
mavenCentral()
Expand Down
30 changes: 15 additions & 15 deletions algorithm/src/main/java/lv/id/jc/algorithm/graph/Graph.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,22 @@
@FunctionalInterface
public interface Graph<T> {
/**
* The schema of this graph.
* Creates a Graph object by given schema.
* <p>
* In a graph schema, each vertex is assigned an edge map.
* If the vertex has no edges, then it should be assigned an empty map.
*
* @param schema of the graph
* @param <T> the type of vertex in this graph
* @return graph object with given schema
*/
static <T> Graph<T> of(Map<T, Map<T, Number>> schema) {
return () -> schema;
}

/**
* The schema of this graph.
* <p>
* In a graph schema, each vertex is assigned an edge map.
* If the vertex has no edges, then it should be assigned an empty map.
*
Expand Down Expand Up @@ -54,18 +68,4 @@ default double getDistance(List<T> path) {
.mapToDouble(Number::doubleValue)
.sum();
}

/**
* Creates a Graph object by given schema.
*
* In a graph schema, each vertex is assigned an edge map.
* If the vertex has no edges, then it should be assigned an empty map.
*
* @param schema of the graph
* @param <T> the type of vertex in this graph
* @return graph object with given schema
*/
static <T> Graph<T> of(Map<T, Map<T, Number>> schema) {
return () -> schema;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
/**
* A functional interface for graph search algorithm
*
* @author Jegors Čemisovs
* @param <T> the type of vertex
* @author Jegors Čemisovs
* @since 1.0
*/
@FunctionalInterface
public interface SearchAlgorithm<T> {
/**
* Find the path from the source node to the target
*
* @param graph The graph in which we search for the path
* @param graph The graph in which we search for the path
* @param source Search starting point identifier
* @param target Search finish point identifier
* @return Path found or empty list if path cannot be found
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lv.id.jc.graph
package lv.id.jc.algorithm.graph

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

def 'should find a route for simple graph'() {
given: 'A simple graph'
given:
def graph = Graph.of([
A: [B: 7, C: 2],
B: [A: 3, C: 5],
C: [A: 1, B: 3]
])

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

then: 'we get the shortest path'
then:
path == shortest

and: 'the distance calculated correctly'
and:
graph.getDistance(path) == time as double

where:
Expand All @@ -39,7 +39,7 @@ class BreadthFirstSearchSpec extends Specification {
}

def 'should find a route for complex graph'() {
given: 'A complex graph'
given:
def graph = Graph.of([
A: [B: 1],
B: [A: 1, D: 1],
Expand All @@ -48,13 +48,13 @@ class BreadthFirstSearchSpec extends Specification {
E: [F: 1],
F: [D: 1, E: 1]])

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

then: 'we get the shortest path'
then:
path == shortest

and: 'the distance calculated correctly'
and:
graph.getDistance(path) == time as double

where:
Expand All @@ -72,24 +72,24 @@ class BreadthFirstSearchSpec extends Specification {
}

def 'should thrown NPE path for an empty graph'() {
given: 'an empty graph'
given:
def graph = Graph.of([:])

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

then: 'the exception was thrown'
then:
thrown NullPointerException
}

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

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

then: 'we get an empty path'
then:
path == []
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lv.id.jc.graph
package lv.id.jc.algorithm.graph

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

def 'should find a route for a simple graph'() {
given: 'A simple graph'
given:
def graph = Graph.of([
A: [B: 7, C: 2],
B: [A: 3, C: 5],
C: [A: 1, B: 3]
])

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

then: 'we get the fastest way'
then:
path == fastest

and: 'the distance calculated correctly'
and:
graph.getDistance(path) == time as double

where:
Expand All @@ -37,7 +37,7 @@ class DijkstrasAlgorithmSpec extends Specification {
}

def 'should find a route for a medium graph'() {
given: 'A medium graph'
given:
def graph = Graph.of([
A: [B: 5],
B: [A: 5, C: 10],
Expand All @@ -46,13 +46,13 @@ class DijkstrasAlgorithmSpec extends Specification {
E: [B: 5]
])

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

then: 'we get the fastest way'
then:
path == fastest

and: 'the distance calculated correctly'
and:
graph.getDistance(path) == time as double

where:
Expand All @@ -66,7 +66,7 @@ class DijkstrasAlgorithmSpec extends Specification {
}

def 'should find a route for a complex graph'() {
given: 'A complex graph'
given:
def graph = Graph.of([
A: [B: 5, H: 2],
B: [A: 5, C: 7],
Expand All @@ -78,13 +78,13 @@ class DijkstrasAlgorithmSpec extends Specification {
H: [G: 3]
])

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

then: 'we get the fastest way'
then:
path == fastest

and: 'the distance calculated correctly'
and:
graph.getDistance(path) == time as double

where:
Expand All @@ -104,24 +104,24 @@ class DijkstrasAlgorithmSpec extends Specification {
}

def 'should thrown NPE for an empty graph'() {
given: 'an empty graph'
given:
def graph = Graph.of([:])

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

then: 'the exception was thrown'
then:
thrown NullPointerException
}

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

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

then: 'we get an empty path'
then:
path == []
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lv.id.jc.graph
package lv.id.jc.algorithm.graph

import lv.id.jc.algorithm.graph.Graph
import spock.lang.Narrative
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package lv.id.jc.graph
package lv.id.jc.algorithm.graph

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

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

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

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

then: "the first route is the shortest"
then:
routeOne == shortest

and: 'the second route is the fastest'
and:
routeTwo == fastest

and: 'the distance calculated correctly'
and:
graph.getDistance(routeOne) == d1 as double
graph.getDistance(routeTwo) == d2 as double

Expand Down
2 changes: 1 addition & 1 deletion algorithm/src/test/resources/SpockConfig.groovy
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
spockReports {

set(['com.athaydes.spockframework.report.showCodeBlocks' : true,
'com.athaydes.spockframework.report.outputDir' : 'docs/spock-reports',
'com.athaydes.spockframework.report.outputDir' : '../docs/spock-reports',
'com.athaydes.spockframework.report.projectName' : 'Graph search algorithms',
'com.athaydes.spockframework.report.projectVersion' : 1.1,
'com.athaydes.spockframework.report.internal.HtmlReportCreator.enabled': true,
Expand Down
13 changes: 0 additions & 13 deletions application/build.gradle

This file was deleted.

4 changes: 0 additions & 4 deletions application/src/main/java/module-info.java

This file was deleted.

2 changes: 1 addition & 1 deletion docs/inspection/index.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/spock-reports/aggregated_report.json

Large diffs are not rendered by default.

Loading

0 comments on commit 95c3b88

Please sign in to comment.