-
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 pull request #30 from spbu-coding-2023/dev
Merge Dev branch with Main branch
- Loading branch information
Showing
58 changed files
with
1,718 additions
and
387 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,13 +1,12 @@ | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.unit.dp | ||
|
||
object Config { | ||
val headerHeight = 40f | ||
val menuWidth = 80f | ||
val optimizeCanvas = false | ||
|
||
object Edge { | ||
val color = Color(0xFF00E0FF) | ||
val dijkstraColor = Color.Green | ||
val strokeWidth = 8f | ||
} | ||
} |
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,32 @@ | ||
package components | ||
|
||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material.Slider | ||
import androidx.compose.material.SliderDefaults | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.MutableState | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.unit.dp | ||
|
||
@Composable | ||
fun MySlider(text: String, state: MutableState<Float>, range: ClosedFloatingPointRange<Float> = (0f..1f)) { | ||
Row( | ||
Modifier.padding(start = 5f.dp, end = 5f.dp), | ||
verticalAlignment = Alignment.CenterVertically | ||
) { | ||
MyText(text = text) | ||
Slider( | ||
modifier = Modifier.padding(0f.dp), | ||
value = state.value, onValueChange = { change -> state.value = change }, | ||
colors = SliderDefaults.colors( | ||
thumbColor = Color.White, | ||
activeTrackColor = Color.White, | ||
inactiveTrackColor = Color.White, | ||
), | ||
valueRange = range | ||
) | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/main/kotlin/view/MyText.kt → src/main/kotlin/components/MyText.kt
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
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,62 @@ | ||
package model.algorithm | ||
|
||
import model.graph.Edge | ||
import model.graph.Graph | ||
import model.graph.Vertex | ||
|
||
class Dijkstra(private val graph: Graph) { | ||
fun triplesToEdges(list: List<Triple<Int, Int, Long>>): List<Edge> { | ||
return list.map { | ||
graph.getEdge(it.first, it.second) | ||
?: throw Error("There is no edge from ${it.first} node to ${it.second} node") | ||
} | ||
} | ||
|
||
fun findShortestPath(startKey: Int, endKey: Int): List<Triple<Int, Int, Long>>? { | ||
val startVertex = graph.vertices.find { it.key == startKey } ?: return null | ||
val endVertex = graph.vertices.find { it.key == endKey } ?: return null | ||
|
||
val distances = mutableMapOf<Vertex, Long>().withDefault { Long.MAX_VALUE } | ||
val previous = mutableMapOf<Vertex, Vertex?>() | ||
val visited = mutableSetOf<Vertex>() | ||
|
||
distances[startVertex] = 0 | ||
|
||
val priorityQueue = java.util.PriorityQueue(compareBy<Vertex> { distances.getValue(it) }) | ||
priorityQueue.add(startVertex) | ||
|
||
while (priorityQueue.isNotEmpty()) { | ||
val currentVertex = priorityQueue.poll() | ||
if (currentVertex == endVertex) break | ||
|
||
visited.add(currentVertex) | ||
|
||
val edges = graph.adjacencyList[currentVertex] ?: continue | ||
for (edge in edges) { | ||
val neighbor = edge.second | ||
if (neighbor in visited) continue | ||
|
||
val newDist = distances.getValue(currentVertex) + edge.weight | ||
if (newDist < distances.getValue(neighbor)) { | ||
distances[neighbor] = newDist | ||
previous[neighbor] = currentVertex | ||
priorityQueue.add(neighbor) | ||
} | ||
} | ||
} | ||
|
||
val path = mutableListOf<Triple<Int, Int, Long>>() | ||
var currentVertex: Vertex? = endVertex | ||
|
||
while (currentVertex != null && currentVertex != startVertex) { | ||
val prevVertex = previous[currentVertex] ?: break | ||
val edge = graph.adjacencyList[prevVertex]?.find { it.second == currentVertex } | ||
if (edge != null) { | ||
path.add(Triple(edge.first.key, edge.second.key, edge.weight)) | ||
} | ||
currentVertex = prevVertex | ||
} | ||
|
||
return if (path.isEmpty()) null else path.reversed() | ||
} | ||
} |
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
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
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,24 @@ | ||
package model.graph | ||
|
||
class WeightedDirectedGraph: DirectedGraph() { | ||
override fun addEdge(first: Int, second: Int, weight: Long): Edge? { | ||
if (first == second) return null | ||
|
||
val vertex1 = _vertices[first] ?: return null | ||
val vertex2 = _vertices[second] ?: return null | ||
|
||
// edge already exists | ||
if (_adjacencyList[vertex1]?.find { it.second.key == second } != null) return null | ||
|
||
_adjacencyList[vertex1]?.add(WeightedDirectedEdge(vertex1, vertex2, weight)) | ||
|
||
|
||
return _adjacencyList[vertex1]?.last() | ||
} | ||
|
||
private data class WeightedDirectedEdge( | ||
override val first: Vertex, | ||
override val second: Vertex, | ||
override var weight: Long | ||
) : Edge | ||
} |
Oops, something went wrong.