forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'eugenp/master'
- Loading branch information
Showing
93 changed files
with
2,750 additions
and
207 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
...scellaneous-3/src/main/java/com/baeldung/algorithms/graphcycledetection/domain/Graph.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.baeldung.algorithms.graphcycledetection.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Graph { | ||
|
||
private List<Vertex> vertices; | ||
|
||
public Graph() { | ||
this.vertices = new ArrayList<>(); | ||
} | ||
|
||
public Graph(List<Vertex> vertices) { | ||
this.vertices = vertices; | ||
} | ||
|
||
public void addVertex(Vertex vertex) { | ||
this.vertices.add(vertex); | ||
} | ||
|
||
public void addEdge(Vertex from, Vertex to) { | ||
from.addNeighbour(to); | ||
} | ||
|
||
public boolean hasCycle() { | ||
for (Vertex vertex : vertices) { | ||
if (!vertex.isVisited() && hasCycle(vertex)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public boolean hasCycle(Vertex sourceVertex) { | ||
sourceVertex.setBeingVisited(true); | ||
|
||
for (Vertex neighbour : sourceVertex.getAdjacencyList()) { | ||
if (neighbour.isBeingVisited()) { | ||
// backward edge exists | ||
return true; | ||
} else if (!neighbour.isVisited() && hasCycle(neighbour)) { | ||
return true; | ||
} | ||
} | ||
|
||
sourceVertex.setBeingVisited(false); | ||
sourceVertex.setVisited(true); | ||
return false; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...cellaneous-3/src/main/java/com/baeldung/algorithms/graphcycledetection/domain/Vertex.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.baeldung.algorithms.graphcycledetection.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Vertex { | ||
|
||
private String label; | ||
|
||
private boolean visited; | ||
|
||
private boolean beingVisited; | ||
|
||
private List<Vertex> adjacencyList; | ||
|
||
public Vertex(String label) { | ||
this.label = label; | ||
this.adjacencyList = new ArrayList<>(); | ||
} | ||
|
||
public String getLabel() { | ||
return label; | ||
} | ||
|
||
public void setLabel(String label) { | ||
this.label = label; | ||
} | ||
|
||
public boolean isVisited() { | ||
return visited; | ||
} | ||
|
||
public void setVisited(boolean visited) { | ||
this.visited = visited; | ||
} | ||
|
||
public boolean isBeingVisited() { | ||
return beingVisited; | ||
} | ||
|
||
public void setBeingVisited(boolean beingVisited) { | ||
this.beingVisited = beingVisited; | ||
} | ||
|
||
public List<Vertex> getAdjacencyList() { | ||
return adjacencyList; | ||
} | ||
|
||
public void setAdjacencyList(List<Vertex> adjacencyList) { | ||
this.adjacencyList = adjacencyList; | ||
} | ||
|
||
public void addNeighbour(Vertex adjacent) { | ||
this.adjacencyList.add(adjacent); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...rc/test/java/com/baeldung/algorithms/graphcycledetection/GraphCycleDetectionUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.baeldung.algorithms.graphcycledetection; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import org.junit.Test; | ||
|
||
import com.baeldung.algorithms.graphcycledetection.domain.Graph; | ||
import com.baeldung.algorithms.graphcycledetection.domain.Vertex; | ||
|
||
public class GraphCycleDetectionUnitTest { | ||
|
||
@Test | ||
public void givenGraph_whenCycleExists_thenReturnTrue() { | ||
|
||
Vertex vertexA = new Vertex("A"); | ||
Vertex vertexB = new Vertex("B"); | ||
Vertex vertexC = new Vertex("C"); | ||
Vertex vertexD = new Vertex("D"); | ||
|
||
Graph graph = new Graph(); | ||
graph.addVertex(vertexA); | ||
graph.addVertex(vertexB); | ||
graph.addVertex(vertexC); | ||
graph.addVertex(vertexD); | ||
|
||
graph.addEdge(vertexA, vertexB); | ||
graph.addEdge(vertexB, vertexC); | ||
graph.addEdge(vertexC, vertexA); | ||
graph.addEdge(vertexD, vertexC); | ||
|
||
assertTrue(graph.hasCycle()); | ||
} | ||
|
||
@Test | ||
public void givenGraph_whenNoCycleExists_thenReturnFalse() { | ||
|
||
Vertex vertexA = new Vertex("A"); | ||
Vertex vertexB = new Vertex("B"); | ||
Vertex vertexC = new Vertex("C"); | ||
Vertex vertexD = new Vertex("D"); | ||
|
||
Graph graph = new Graph(); | ||
graph.addVertex(vertexA); | ||
graph.addVertex(vertexB); | ||
graph.addVertex(vertexC); | ||
graph.addVertex(vertexD); | ||
|
||
graph.addEdge(vertexA, vertexB); | ||
graph.addEdge(vertexB, vertexC); | ||
graph.addEdge(vertexA, vertexC); | ||
graph.addEdge(vertexD, vertexC); | ||
|
||
assertFalse(graph.hasCycle()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>core-groovy-2</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<name>core-groovy-2</name> | ||
<packaging>jar</packaging> | ||
|
||
<parent> | ||
<groupId>com.baeldung</groupId> | ||
<artifactId>parent-modules</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-lang3</artifactId> | ||
<version>${commons-lang3.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-classic</artifactId> | ||
<version>${logback.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.codehaus.groovy</groupId> | ||
<artifactId>groovy-all</artifactId> | ||
<version>${groovy.version}</version> | ||
<type>pom</type> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.platform</groupId> | ||
<artifactId>junit-platform-runner</artifactId> | ||
<version>${junit.platform.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.hsqldb</groupId> | ||
<artifactId>hsqldb</artifactId> | ||
<version>${hsqldb.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.spockframework</groupId> | ||
<artifactId>spock-core</artifactId> | ||
<version>${spock-core.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<scriptSourceDirectory>src/main/groovy</scriptSourceDirectory> | ||
<sourceDirectory>src/main/java</sourceDirectory> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.codehaus.gmavenplus</groupId> | ||
<artifactId>gmavenplus-plugin</artifactId> | ||
<version>1.7.0</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>execute</goal> | ||
<goal>addSources</goal> | ||
<goal>addTestSources</goal> | ||
<goal>generateStubs</goal> | ||
<goal>compile</goal> | ||
<goal>generateTestStubs</goal> | ||
<goal>compileTests</goal> | ||
<goal>removeStubs</goal> | ||
<goal>removeTestStubs</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.codehaus.groovy</groupId> | ||
<artifactId>groovy-all</artifactId> | ||
<!-- any version of Groovy \>= 1.5.0 should work here --> | ||
<version>${groovy.version}</version> | ||
<scope>runtime</scope> | ||
<type>pom</type> | ||
</dependency> | ||
</dependencies> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-failsafe-plugin</artifactId> | ||
<version>${maven-failsafe-plugin.version}</version> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.junit.platform</groupId> | ||
<artifactId>junit-platform-surefire-provider</artifactId> | ||
<version>${junit.platform.version}</version> | ||
</dependency> | ||
</dependencies> | ||
<executions> | ||
<execution> | ||
<id>junit5</id> | ||
<goals> | ||
<goal>integration-test</goal> | ||
<goal>verify</goal> | ||
</goals> | ||
<configuration> | ||
<includes> | ||
<include>**/*Test5.java</include> | ||
</includes> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>2.20.1</version> | ||
<configuration> | ||
<useFile>false</useFile> | ||
<includes> | ||
<include>**/*Test.java</include> | ||
<include>**/*Spec.java</include> | ||
</includes> | ||
</configuration> | ||
</plugin> | ||
<!-- Maven Assembly Plugin: needed to run the jar through command line --> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-assembly-plugin</artifactId> | ||
<version>3.1.0</version> | ||
<configuration> | ||
<!-- get all project dependencies --> | ||
<descriptorRefs> | ||
<descriptorRef>jar-with-dependencies</descriptorRef> | ||
</descriptorRefs> | ||
<!-- MainClass in mainfest make a executable jar --> | ||
<archive> | ||
<manifest> | ||
<mainClass>com.baeldung.MyJointCompilationApp</mainClass> | ||
</manifest> | ||
</archive> | ||
|
||
</configuration> | ||
<executions> | ||
<execution> | ||
<id>make-assembly</id> | ||
<!-- bind to the packaging phase --> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>single</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<repositories> | ||
<repository> | ||
<id>central</id> | ||
<url>http://jcenter.bintray.com</url> | ||
</repository> | ||
</repositories> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<junit.platform.version>1.0.0</junit.platform.version> | ||
<hsqldb.version>2.4.0</hsqldb.version> | ||
<spock-core.version>1.1-groovy-2.4</spock-core.version> | ||
<commons-lang3.version>3.9</commons-lang3.version> | ||
<java.version>1.8</java.version> | ||
<logback.version>1.2.3</logback.version> | ||
<groovy.version>2.5.7</groovy.version> | ||
<gmavenplus-plugin.version>1.6</gmavenplus-plugin.version> | ||
</properties> | ||
</project> | ||
|
Oops, something went wrong.