Skip to content

Commit 38720be

Browse files
Merge branch 'feature/CycleSearch' into develop
2 parents b09c882 + 6d14a92 commit 38720be

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/main/kotlin/model/algorithms/CycleSearch.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import model.graph.Vertex
1818

1919
class CycleSearch(private val graph: UndirectedGraph) {
2020

21+
private var cycleIsFound: Boolean = false
2122
/**
2223
* This auxiliary function for the function [findAnyCycle] aims to get from
2324
* some hashmap with extra elements the hashmap the elements of which
@@ -63,12 +64,16 @@ class CycleSearch(private val graph: UndirectedGraph) {
6364

6465
for (idAdjacency in graph.adjacency[currentVertexId]!!.keys) {
6566

67+
if (cycleIsFound){
68+
return
69+
}
6670
if (visited[idAdjacency] == false) {
6771
visited[idAdjacency] = true
6872
cyclePath[currentVertexId] = idAdjacency
6973
dfs(cycleVertexId, idAdjacency, visited, cyclePath)
7074
} else if (idAdjacency == cycleVertexId && cyclePath[cycleVertexId] != currentVertexId) {
7175
cyclePath[currentVertexId] = idAdjacency
76+
cycleIsFound = true
7277
return
7378
}
7479
}
@@ -89,6 +94,7 @@ class CycleSearch(private val graph: UndirectedGraph) {
8994
}
9095
visited[vertexId] = true
9196
dfs(vertexId, vertexId, visited, devCyclePath)
97+
cycleIsFound = false
9298

9399
if (devCyclePath.filter { it.value == vertexId }.isEmpty()) {
94100
return null

0 commit comments

Comments
 (0)