Skip to content

Commit

Permalink
Merge pull request #28 from rabestro/develop
Browse files Browse the repository at this point in the history
Added sample application
  • Loading branch information
Jegors Čemisovs authored Jan 6, 2022
2 parents 16cd028 + ea5a251 commit 9f77f7c
Show file tree
Hide file tree
Showing 20 changed files with 148 additions and 40 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ out/
##############################
## OS X
##############################
.DS_Store
.DS_Store
/algorithm/gradle.properties
/sample/gradle.properties
2 changes: 2 additions & 0 deletions .idea/gradle.xml

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

5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

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

62 changes: 62 additions & 0 deletions algorithm/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
plugins {
id 'groovy'
id 'java-library'
id 'maven-publish'
}

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

repositories {
mavenCentral()
}

// Configures the publishing
publishing {
repositories {
// The target repository
maven {
// Choose whatever name you want
name = "Algorithms"
// The url of the repository, where the artifacts will be published
url = "https://maven.pkg.github.com/rabestro/algorithms"
credentials {
// The credentials (described in the next section)
username = project.findProperty("gpr.user")
password = project.findProperty("gpr.key")
}
}
}
publications {
gpr(MavenPublication) {
from(components.java)
// Fixes the error with dynamic versions when using Spring Boot
versionMapping {
usage('java-api') {
fromResolutionOf('runtimeClasspath')
}
usage('java-runtime') {
fromResolutionResult()
}
}
}
}
}

dependencies {
// Spock Framework
testImplementation 'org.spockframework:spock-core:2.0-groovy-3.0'
testImplementation 'org.codehaus.groovy:groovy-all:3.0.9'

// Spock Reports
testRuntimeClasspath( "com.athaydes:spock-reports:2.1.1-groovy-3.0" ) {
transitive = false // this avoids affecting your version of Groovy/Spock
}
// Required for spock-reports
testImplementation 'org.slf4j:slf4j-api:1.7.32'
testRuntimeClasspath 'org.slf4j:slf4j-simple:1.7.32'
}

test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package lv.id.jc.algorithm.graph;

import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
Expand All @@ -11,18 +12,18 @@

/**
* Algorithm for finding the shortest paths between nodes in a graph.
*
* <p>
* The algorithm doesn't take into account the distance between nodes.
*
* @author Jegors Čemisovs
* @param <T> the type of vertex
* @author Jegors Čemisovs
* @since 1.0
*/
public class BreadthFirstSearch<T> implements SearchAlgorithm<T> {

@Override
public List<T> findPath(Graph<T> graph, T source, T target) {
var queue = new LinkedList<T>();
var queue = new ArrayDeque<T>();
var visited = new HashSet<T>();
var previous = new HashMap<T, T>();
queue.add(source);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package lv.id.jc.algorithm.graph;

import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -12,15 +13,15 @@
* <p>
* The algorithm uses information about edge's distance to find the fastest path.
*
* @author Jegors Čemisovs
* @param <T> the type of vertex
* @author Jegors Čemisovs
* @since 1.0
*/
public class DijkstrasAlgorithm<T> implements SearchAlgorithm<T> {

@Override
public List<T> findPath(Graph<T> graph, T source, T target) {
var queue = new LinkedList<T>();
var queue = new ArrayDeque<T>();
var distances = new HashMap<T, Double>();
var previous = new HashMap<T, T>();
queue.add(source);
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package graph
package lv.id.jc.graph

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

import lv.id.jc.algorithm.graph.DijkstrasAlgorithm
import lv.id.jc.algorithm.graph.Graph
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package graph
package lv.id.jc.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 graph
package lv.id.jc.graph

import lv.id.jc.algorithm.graph.BreadthFirstSearch
import lv.id.jc.algorithm.graph.DijkstrasAlgorithm
Expand Down
File renamed without changes.
13 changes: 13 additions & 0 deletions application/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
plugins {
id 'application'
}

dependencies {
implementation project(':algorithm')
}

application {
mainModule = 'lv.id.jc.application'
mainClass = 'lv.id.jc.application.GraphApp'
applicationDefaultJvmArgs = ['-Dgreeting.language=en']
}
46 changes: 46 additions & 0 deletions application/src/main/java/lv/id/jc/application/GraphApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package lv.id.jc.application;

import lv.id.jc.algorithm.graph.BreadthFirstSearch;
import lv.id.jc.algorithm.graph.DijkstrasAlgorithm;
import lv.id.jc.algorithm.graph.Graph;
import lv.id.jc.algorithm.graph.SearchAlgorithm;

import java.util.Map;

public class GraphApp {
private static final Graph<Character> graph = Graph.of(Map.of(
'A', Map.of('B', 5, 'H', 2),
'B', Map.of('A', 5, 'C', 7),
'C', Map.of('B', 7, 'D', 3, 'G', 4),
'D', Map.of('C', 20, 'E', 4),
'E', Map.of('F', 5),
'F', Map.of('G', 6),
'G', Map.of('C', 4),
'H', Map.of('G', 3)
));
private static final SearchAlgorithm<Character> fastest = new DijkstrasAlgorithm<>();
private static final SearchAlgorithm<Character> shortest = new BreadthFirstSearch<>();

public static void main(String[] args) {
printRoute('D', 'C');
printRoute('A', 'G');
printRoute('D', 'H');
}

@SuppressWarnings("squid:S106")
private static void printRoute(Character source, Character target) {
var routeOne = shortest.findPath(graph, source, target);
var routeTwo = fastest.findPath(graph, source, target);
var message = """
Find the path from %s to %s
- the shortest take %.0f min and the path is %s
- the fastest take %.0f min and the path is %s"""
.formatted(
source, target,
graph.getDistance(routeOne), routeOne,
graph.getDistance(routeTwo), routeTwo);

System.out.println(message);
}
}
4 changes: 4 additions & 0 deletions application/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module lv.id.jc.application {
requires lv.id.jc.algorithm.graph;
exports lv.id.jc.application;
}
29 changes: 0 additions & 29 deletions build.gradle

This file was deleted.

3 changes: 2 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
rootProject.name = 'search-algorithm'
rootProject.name = 'graphs-algorithms'
include 'algorithm', 'application'

0 comments on commit 9f77f7c

Please sign in to comment.