|
1 | 1 | package model.algorithms
|
2 | 2 |
|
3 |
| -import model.graph.Graph |
4 | 3 | import model.graph.Edge
|
| 4 | +import model.graph.UndirectedGraph |
| 5 | +import model.graph.Vertex |
| 6 | + |
| 7 | +/** |
| 8 | + * The class [CycleSearch] implements the algorithm for finding a cycle around |
| 9 | + * selected vertex of the undirected graph. |
| 10 | + * |
| 11 | + * At the same time, the algorithm has some minimization of the desired cycle |
| 12 | + * by iterating through possible pairs of neighbors of the selected vertex |
| 13 | + * through which the desired cycle will pass. |
| 14 | + * @param D input type |
| 15 | + * @property [graph] a undirected graph for whose vertex we will search for a cycle |
| 16 | + * @constructor Creates a graph, based on [graph],for which it will be possible to apply |
| 17 | + * a cycle search algorithm around the vertex selected in it. |
| 18 | + */ |
| 19 | + |
| 20 | +class CycleSearch<D>(private val graph: UndirectedGraph<D>) { |
| 21 | + |
| 22 | + /** |
| 23 | + * This auxiliary function for the function [findAnyCycle] aims to get from |
| 24 | + * some hashmap with extra elements the hashmap the elements of which |
| 25 | + * accurately describe found cycle path. |
| 26 | + * |
| 27 | + * @param cyclePath hashmap with extra elements |
| 28 | + * @param cycleVertexId the index of the vertex around which the cycle must be found |
| 29 | + * @return cycle path |
| 30 | + * @receiver private fun [findAnyCycle] |
| 31 | + */ |
| 32 | + private fun getCyclePath(cyclePath: HashMap<Int, Int>, cycleVertexId: Int): HashMap<Int, Int> { |
| 33 | + |
| 34 | + var current: Int = cycleVertexId |
| 35 | + var next: Int |
| 36 | + val returnCyclePath = HashMap<Int, Int>() |
| 37 | + do { |
| 38 | + next = cyclePath.filter { it.key == current }.values.first() |
| 39 | + returnCyclePath[current] = next |
| 40 | + current = next |
| 41 | + } while (current != cycleVertexId) |
| 42 | + |
| 43 | + return returnCyclePath |
5 | 44 |
|
6 |
| -class CycleSearch<D>(private val graph: Graph<D>) { |
| 45 | + } |
7 | 46 |
|
8 |
| - fun findCycle(cycleVertexId: Int): List<Edge<D>>? { |
| 47 | + /**Searches for a cycle by dfs algorithm and writes it into [cyclePath] |
| 48 | + * |
| 49 | + * |
| 50 | + * @param cycleVertexId the index of the vertex around which the cycle must be found |
| 51 | + * @param currentVertexId using for recording the [cyclePath] |
| 52 | + * @param visited stores information about which vertices of the graph |
| 53 | + * have already been processed by [dfs] |
| 54 | + * @param cyclePath hashmap consisting of elements that can be used to restore the found cycle path |
| 55 | + * by applying the auxiliary function [getCyclePath] to it in function [findAnyCycle]. |
| 56 | + * @receiver private fun [findAnyCycle] |
| 57 | + */ |
| 58 | + private fun dfs( |
| 59 | + cycleVertexId: Int, |
| 60 | + currentVertexId: Int, |
| 61 | + visited: HashMap<Int, Boolean>, |
| 62 | + cyclePath: HashMap<Int, Int> |
| 63 | + ) { |
| 64 | + |
| 65 | + for (idAdjacency in graph.adjacency[currentVertexId]!!.keys) { |
9 | 66 |
|
10 |
| - if (cycleVertexId !in graph.vertices.keys) { |
11 |
| - throw IllegalArgumentException("Vertex with index = $cycleVertexId doesn't exist in the graph. The cycle can't be found") |
| 67 | + if (visited[idAdjacency] == false) { |
| 68 | + visited[idAdjacency] = true |
| 69 | + cyclePath[currentVertexId] = idAdjacency |
| 70 | + dfs(cycleVertexId, idAdjacency, visited, cyclePath) |
| 71 | + } else if (idAdjacency == cycleVertexId && cyclePath[cycleVertexId] != currentVertexId) { |
| 72 | + cyclePath[currentVertexId] = idAdjacency |
| 73 | + return |
| 74 | + } |
12 | 75 | }
|
| 76 | + } |
13 | 77 |
|
14 |
| - val visited = HashMap<Int, Boolean>() |
15 |
| - for (vertexId in graph.vertices.keys) { |
16 |
| - visited[vertexId] = false |
| 78 | + /** Finds any existing cycle in the graph around a given vertex |
| 79 | + * |
| 80 | + * @param vertexId the index of the vertex around which the cycle must be found |
| 81 | + * @return found cycle path or null if the cycle doesn't exist around vertex with id = [vertexId] |
| 82 | + * @receiver fun [findCycle] |
| 83 | + */ |
| 84 | + private fun findAnyCycle(vertexId: Int): HashMap<Int, Int>? { |
| 85 | + |
| 86 | + val devCyclePath = HashMap<Int, Int>() |
| 87 | + val visited = hashMapOf<Int, Boolean>() |
| 88 | + for (idVertex in graph.vertices.keys) { |
| 89 | + visited[idVertex] = false |
17 | 90 | }
|
18 |
| - val cycleWayFrom = |
19 |
| - HashMap<Int, Int>() |
20 |
| - val cycleIsFound = Array<Boolean>(1) { false } |
21 |
| - val returnList = mutableListOf<Edge<D>>() |
22 |
| - |
23 |
| - dfs(cycleVertexId, visited, cycleWayFrom, cycleVertexId, cycleIsFound, returnList) |
24 |
| - if (cycleIsFound[0]) { |
25 |
| - return returnList |
| 91 | + visited[vertexId] = true |
| 92 | + dfs(vertexId, vertexId, visited, devCyclePath) |
| 93 | + |
| 94 | + if (devCyclePath.filter { it.value == vertexId }.isEmpty()) { |
| 95 | + return null |
26 | 96 | }
|
27 |
| - return null |
| 97 | + return getCyclePath(devCyclePath, vertexId) |
28 | 98 |
|
29 | 99 | }
|
30 | 100 |
|
31 |
| - private fun dfs( |
32 |
| - vertexId: Int, |
33 |
| - visited: HashMap<Int, Boolean>, |
34 |
| - cycleWayFrom: HashMap<Int, Int>, |
35 |
| - cycleVertexId: Int, |
36 |
| - cycleIsFound: Array<Boolean>, |
37 |
| - returnList: MutableList<Edge<D>> |
38 |
| - ) { |
| 101 | + /** |
| 102 | + * The function implements the algorithm for finding a cycle around |
| 103 | + * vertex with id = [vertex] of the undirected graph. |
| 104 | + * |
| 105 | + * The function searches for a cycle and performs some minimization of it. |
| 106 | + * During the cycle minimization we iterate through variants of cycle, |
| 107 | + * based on the choice of a pair of neighbors of [vertex] through which the cycle will pass. |
| 108 | + * Each of the found cycle variants is written to a variable 'currentCyclePath' |
| 109 | + * and its size compared with 'minCycleSize'. |
| 110 | + * @param vertex the vertex around which the cycle must be found |
| 111 | + * @return cycle path or null if it doesn't exist |
| 112 | + */ |
| 113 | + fun findCycle(vertex: Vertex<D>): UndirectedGraph<D>? { |
| 114 | + |
| 115 | + var returnCyclePath = hashMapOf<Int, Int>() |
| 116 | + var currentCyclePath: HashMap<Int, Int>? |
| 117 | + var minCycleSize: Int = Int.MAX_VALUE |
| 118 | + |
| 119 | + if (vertex.id !in graph.vertices.keys) { |
| 120 | + throw IllegalArgumentException("Vertex with id = ${vertex.id} doesn't exist in the graph") |
| 121 | + } |
| 122 | + val returnGraph = UndirectedGraph<D>() |
| 123 | + if (graph.adjacency[vertex.id]!!.size < 2) { |
| 124 | + return null |
| 125 | + } else { // we consider all possible cases of a cycle by choosing a pair of neighbors to minimize the found cycle |
| 126 | + val adjacencyOfVertex: MutableList<Int> = arrayListOf() |
| 127 | + graph.adjacency[vertex.id]!!.keys.forEach { adjacencyOfVertex.add(it) } |
| 128 | + val adjacencyWas: MutableList<Int> = arrayListOf() |
| 129 | + val removedEdges: MutableList<Edge<Int>> = arrayListOf() |
| 130 | + for (firstAdjacency in adjacencyOfVertex) { |
| 131 | + adjacencyWas.add(firstAdjacency) |
| 132 | + for (secondAdjacency in adjacencyOfVertex.filter { it !in adjacencyWas && it != vertex.id }) { |
| 133 | + |
| 134 | + for (adjacency in graph.adjacency[vertex.id]!!.filter { it.key != firstAdjacency && it.key != secondAdjacency }) { |
| 135 | + removedEdges.add(Edge(vertex.id to adjacency.key, adjacency.value)) |
| 136 | + graph.removeEdge(vertex.id to adjacency.key, adjacency.value) |
| 137 | + } |
39 | 138 |
|
40 |
| - visited[vertexId] = true |
| 139 | + currentCyclePath = findAnyCycle(vertex.id) |
| 140 | + if (currentCyclePath != null && currentCyclePath.size < minCycleSize) { |
| 141 | + minCycleSize = currentCyclePath.size |
| 142 | + returnCyclePath = currentCyclePath |
| 143 | + } |
41 | 144 |
|
42 |
| - if (!cycleIsFound[0]) { |
43 |
| - for (adjacencyVertexId in graph.adjacency[vertexId]!!.keys) { |
44 |
| - |
45 |
| - if (visited[adjacencyVertexId] == false) { |
46 |
| - cycleWayFrom[adjacencyVertexId] = vertexId |
47 |
| - dfs(adjacencyVertexId, visited, cycleWayFrom, cycleVertexId, cycleIsFound, returnList) |
48 |
| - } else if (adjacencyVertexId == cycleVertexId && adjacencyVertexId != cycleWayFrom[vertexId]) { |
49 |
| - cycleWayFrom[cycleVertexId] = vertexId |
50 |
| - var id = cycleWayFrom.keys.last() |
51 |
| - while (id != cycleVertexId) { |
52 |
| - returnList.add(Edge<D>(cycleWayFrom[id]!! to id, graph.adjacency[cycleWayFrom[id]]!![id])) |
53 |
| - id = cycleWayFrom[id]!! |
| 145 | + for (edge in removedEdges) { |
| 146 | + graph.addEdge(edge.vertices, edge.weight) |
54 | 147 | }
|
55 |
| - returnList.add(Edge<D>(cycleWayFrom[id]!! to id, graph.adjacency[cycleWayFrom[id]]!![id])) |
56 |
| - cycleIsFound[0] = true |
| 148 | + removedEdges.clear() |
57 | 149 | }
|
58 |
| - |
59 | 150 | }
|
60 | 151 | }
|
61 |
| - } |
62 |
| - |
63 |
| - |
64 |
| -} |
65 |
| - |
66 | 152 |
|
| 153 | + if (minCycleSize == Int.MAX_VALUE) { |
| 154 | + return null |
| 155 | + } |
| 156 | + returnCyclePath.forEach { returnGraph.addVertex(it.key, graph.vertices[it.key]!!.data) } |
| 157 | + returnCyclePath.forEach { returnGraph.addEdge(it.key to it.value, graph.adjacency[it.key]!![it.value]) } |
| 158 | + return returnGraph |
67 | 159 |
|
| 160 | + } |
68 | 161 |
|
| 162 | +} |
0 commit comments