Skip to content

Commit 9f77f7c

Browse files
author
Jegors Čemisovs
authored
Merge pull request #28 from rabestro/develop
Added sample application
2 parents 16cd028 + ea5a251 commit 9f77f7c

20 files changed

+148
-40
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ out/
3232
##############################
3333
## OS X
3434
##############################
35-
.DS_Store
35+
.DS_Store
36+
/algorithm/gradle.properties
37+
/sample/gradle.properties

.idea/gradle.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/jarRepositories.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

algorithm/build.gradle

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
plugins {
2+
id 'groovy'
3+
id 'java-library'
4+
id 'maven-publish'
5+
}
6+
7+
group 'lv.id.jc'
8+
version '1.1'
9+
10+
repositories {
11+
mavenCentral()
12+
}
13+
14+
// Configures the publishing
15+
publishing {
16+
repositories {
17+
// The target repository
18+
maven {
19+
// Choose whatever name you want
20+
name = "Algorithms"
21+
// The url of the repository, where the artifacts will be published
22+
url = "https://maven.pkg.github.com/rabestro/algorithms"
23+
credentials {
24+
// The credentials (described in the next section)
25+
username = project.findProperty("gpr.user")
26+
password = project.findProperty("gpr.key")
27+
}
28+
}
29+
}
30+
publications {
31+
gpr(MavenPublication) {
32+
from(components.java)
33+
// Fixes the error with dynamic versions when using Spring Boot
34+
versionMapping {
35+
usage('java-api') {
36+
fromResolutionOf('runtimeClasspath')
37+
}
38+
usage('java-runtime') {
39+
fromResolutionResult()
40+
}
41+
}
42+
}
43+
}
44+
}
45+
46+
dependencies {
47+
// Spock Framework
48+
testImplementation 'org.spockframework:spock-core:2.0-groovy-3.0'
49+
testImplementation 'org.codehaus.groovy:groovy-all:3.0.9'
50+
51+
// Spock Reports
52+
testRuntimeClasspath( "com.athaydes:spock-reports:2.1.1-groovy-3.0" ) {
53+
transitive = false // this avoids affecting your version of Groovy/Spock
54+
}
55+
// Required for spock-reports
56+
testImplementation 'org.slf4j:slf4j-api:1.7.32'
57+
testRuntimeClasspath 'org.slf4j:slf4j-simple:1.7.32'
58+
}
59+
60+
test {
61+
useJUnitPlatform()
62+
}

src/main/java/lv/id/jc/algorithm/graph/BreadthFirstSearch.java renamed to algorithm/src/main/java/lv/id/jc/algorithm/graph/BreadthFirstSearch.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package lv.id.jc.algorithm.graph;
22

3+
import java.util.ArrayDeque;
34
import java.util.HashMap;
45
import java.util.HashSet;
56
import java.util.LinkedList;
@@ -11,18 +12,18 @@
1112

1213
/**
1314
* Algorithm for finding the shortest paths between nodes in a graph.
14-
*
15+
* <p>
1516
* The algorithm doesn't take into account the distance between nodes.
1617
*
17-
* @author Jegors Čemisovs
1818
* @param <T> the type of vertex
19+
* @author Jegors Čemisovs
1920
* @since 1.0
2021
*/
2122
public class BreadthFirstSearch<T> implements SearchAlgorithm<T> {
2223

2324
@Override
2425
public List<T> findPath(Graph<T> graph, T source, T target) {
25-
var queue = new LinkedList<T>();
26+
var queue = new ArrayDeque<T>();
2627
var visited = new HashSet<T>();
2728
var previous = new HashMap<T, T>();
2829
queue.add(source);

src/main/java/lv/id/jc/algorithm/graph/DijkstrasAlgorithm.java renamed to algorithm/src/main/java/lv/id/jc/algorithm/graph/DijkstrasAlgorithm.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package lv.id.jc.algorithm.graph;
22

3+
import java.util.ArrayDeque;
34
import java.util.HashMap;
45
import java.util.LinkedList;
56
import java.util.List;
@@ -12,15 +13,15 @@
1213
* <p>
1314
* The algorithm uses information about edge's distance to find the fastest path.
1415
*
15-
* @author Jegors Čemisovs
1616
* @param <T> the type of vertex
17+
* @author Jegors Čemisovs
1718
* @since 1.0
1819
*/
1920
public class DijkstrasAlgorithm<T> implements SearchAlgorithm<T> {
2021

2122
@Override
2223
public List<T> findPath(Graph<T> graph, T source, T target) {
23-
var queue = new LinkedList<T>();
24+
var queue = new ArrayDeque<T>();
2425
var distances = new HashMap<T, Double>();
2526
var previous = new HashMap<T, T>();
2627
queue.add(source);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package graph
1+
package lv.id.jc.graph
22

33
import lv.id.jc.algorithm.graph.BreadthFirstSearch
44
import lv.id.jc.algorithm.graph.Graph

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package graph
1+
package lv.id.jc.graph
22

33
import lv.id.jc.algorithm.graph.DijkstrasAlgorithm
44
import lv.id.jc.algorithm.graph.Graph

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package graph
1+
package lv.id.jc.graph
22

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package graph
1+
package lv.id.jc.graph
22

33
import lv.id.jc.algorithm.graph.BreadthFirstSearch
44
import lv.id.jc.algorithm.graph.DijkstrasAlgorithm

application/build.gradle

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
plugins {
2+
id 'application'
3+
}
4+
5+
dependencies {
6+
implementation project(':algorithm')
7+
}
8+
9+
application {
10+
mainModule = 'lv.id.jc.application'
11+
mainClass = 'lv.id.jc.application.GraphApp'
12+
applicationDefaultJvmArgs = ['-Dgreeting.language=en']
13+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package lv.id.jc.application;
2+
3+
import lv.id.jc.algorithm.graph.BreadthFirstSearch;
4+
import lv.id.jc.algorithm.graph.DijkstrasAlgorithm;
5+
import lv.id.jc.algorithm.graph.Graph;
6+
import lv.id.jc.algorithm.graph.SearchAlgorithm;
7+
8+
import java.util.Map;
9+
10+
public class GraphApp {
11+
private static final Graph<Character> graph = Graph.of(Map.of(
12+
'A', Map.of('B', 5, 'H', 2),
13+
'B', Map.of('A', 5, 'C', 7),
14+
'C', Map.of('B', 7, 'D', 3, 'G', 4),
15+
'D', Map.of('C', 20, 'E', 4),
16+
'E', Map.of('F', 5),
17+
'F', Map.of('G', 6),
18+
'G', Map.of('C', 4),
19+
'H', Map.of('G', 3)
20+
));
21+
private static final SearchAlgorithm<Character> fastest = new DijkstrasAlgorithm<>();
22+
private static final SearchAlgorithm<Character> shortest = new BreadthFirstSearch<>();
23+
24+
public static void main(String[] args) {
25+
printRoute('D', 'C');
26+
printRoute('A', 'G');
27+
printRoute('D', 'H');
28+
}
29+
30+
@SuppressWarnings("squid:S106")
31+
private static void printRoute(Character source, Character target) {
32+
var routeOne = shortest.findPath(graph, source, target);
33+
var routeTwo = fastest.findPath(graph, source, target);
34+
var message = """
35+
36+
Find the path from %s to %s
37+
- the shortest take %.0f min and the path is %s
38+
- the fastest take %.0f min and the path is %s"""
39+
.formatted(
40+
source, target,
41+
graph.getDistance(routeOne), routeOne,
42+
graph.getDistance(routeTwo), routeTwo);
43+
44+
System.out.println(message);
45+
}
46+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module lv.id.jc.application {
2+
requires lv.id.jc.algorithm.graph;
3+
exports lv.id.jc.application;
4+
}

build.gradle

Lines changed: 0 additions & 29 deletions
This file was deleted.

settings.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
rootProject.name = 'search-algorithm'
1+
rootProject.name = 'graphs-algorithms'
2+
include 'algorithm', 'application'

0 commit comments

Comments
 (0)