File tree Expand file tree Collapse file tree 1 file changed +6
-0
lines changed
src/main/kotlin/model/algorithms Expand file tree Collapse file tree 1 file changed +6
-0
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ import model.graph.Vertex
18
18
19
19
class CycleSearch (private val graph : UndirectedGraph ) {
20
20
21
+ private var cycleIsFound: Boolean = false
21
22
/* *
22
23
* This auxiliary function for the function [findAnyCycle] aims to get from
23
24
* some hashmap with extra elements the hashmap the elements of which
@@ -63,12 +64,16 @@ class CycleSearch(private val graph: UndirectedGraph) {
63
64
64
65
for (idAdjacency in graph.adjacency[currentVertexId]!! .keys) {
65
66
67
+ if (cycleIsFound){
68
+ return
69
+ }
66
70
if (visited[idAdjacency] == false ) {
67
71
visited[idAdjacency] = true
68
72
cyclePath[currentVertexId] = idAdjacency
69
73
dfs(cycleVertexId, idAdjacency, visited, cyclePath)
70
74
} else if (idAdjacency == cycleVertexId && cyclePath[cycleVertexId] != currentVertexId) {
71
75
cyclePath[currentVertexId] = idAdjacency
76
+ cycleIsFound = true
72
77
return
73
78
}
74
79
}
@@ -89,6 +94,7 @@ class CycleSearch(private val graph: UndirectedGraph) {
89
94
}
90
95
visited[vertexId] = true
91
96
dfs(vertexId, vertexId, visited, devCyclePath)
97
+ cycleIsFound = false
92
98
93
99
if (devCyclePath.filter { it.value == vertexId }.isEmpty()) {
94
100
return null
You can’t perform that action at this time.
0 commit comments