Skip to content

Commit

Permalink
Merge pull request #27 from spbu-coding-2023/feature/DijkstraViewModel
Browse files Browse the repository at this point in the history
Add Dijkstra algorithm ViewModel
  • Loading branch information
Demon32123 authored Sep 24, 2024
2 parents bacb104 + e28f802 commit a5310ba
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/main/kotlin/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ object Config {

object Edge {
val color = Color(0xFF00E0FF)
val dijkstraColor = Color.Green
val strokeWidth = 8f
}
}
7 changes: 7 additions & 0 deletions src/main/kotlin/model/algorithm/Dijkstra.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
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
Expand Down
11 changes: 8 additions & 3 deletions src/main/kotlin/view/MainView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ val MENU_WIDTH = Config.menuWidth

@Composable
fun displayAlgorithmMenu(name: String, viewModel: MenuViewModel) {
var isBridgeFinded by viewModel::isFinded
var isBridgeFinded by viewModel::isBridgeFinded
var isDijkstraMode by viewModel::isDijkstraMode

data class ImageResource(val icon: String, val onClick: () -> Unit)

val imageResources = listOf(
ImageResource("FindBridge.svg") { isBridgeFinded = !isBridgeFinded },
ImageResource("Dijkstra.svg") {},
ImageResource("Dijkstra.svg") {
isDijkstraMode = !isDijkstraMode
if (!isDijkstraMode) viewModel.canvasViewModel.resetEdgesColorToDefault()
},
ImageResource("Bellman-Ford.svg") {},
ImageResource("IslandTree.svg") {},
ImageResource("StrongConnectivityComponent.svg") {},
Expand Down Expand Up @@ -68,7 +72,8 @@ fun ImageButton(imageResourceId: String, onClick: () -> Unit, viewModel: MenuVie
.background(Color(0x00))
) {
val modifier = when (imageResourceId) {
"FindBridge.svg" -> Modifier.glowRec(viewModel.isFinded)
"FindBridge.svg" -> Modifier.glowRec(viewModel.isBridgeFinded)
"Dijkstra.svg" -> Modifier.glowRec(viewModel.isDijkstraMode)
else -> Modifier.alpha(0.2f)
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/view/canvas/VertexCanvasView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ fun VertexCanvasView(
.size(viewModel.radius * 2)
.offset(viewModel.offset.x.dp, viewModel.offset.y.dp)
.border(
color = if (viewModel.canvasViewModel.pickedNodeForEdgeCreating != viewModel) viewModel.color else Color.Green,
color = if (viewModel.canvasViewModel.pickedNodeForDijkstra != viewModel) viewModel.color else Color.Green,
width = viewModel.strokeWidth.dp,
shape = CircleShape
)
.background(color = Color(0xFF242424), shape = CircleShape)
.onClick { viewModel.onClickWhenEdgeCreating() }
.onClick { viewModel.onClick() }
.onDrag(onDrag = viewModel::onDrag),
contentAlignment = Alignment.Center
) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/viewModel/MenuViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class MenuViewModel(
var isEdgeCreating by canvasViewModel::isEdgeCreatingMode
var isClustering by canvasViewModel::isClustering
var isRanked by canvasViewModel::isRanked
var isFinded by canvasViewModel::isFinded
var isBridgeFinded by canvasViewModel::isFinded
var isDijkstraMode by canvasViewModel::isDijkstraMode
var isAlgorithmMenuOpen by mutableStateOf(false)
}
33 changes: 33 additions & 0 deletions src/main/kotlin/viewModel/canvas/CanvasViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package viewModel.canvas

import Config
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.PointerMatcher
import androidx.compose.foundation.gestures.detectDragGestures
Expand All @@ -14,6 +15,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.AwaitPointerEventScope
import androidx.compose.ui.input.pointer.PointerEvent
import androidx.compose.ui.input.pointer.PointerInputScope
import model.algorithm.Dijkstra
import model.graph.Edge
import model.graph.UndirectedGraph
import view.HEADER_HEIGHT
Expand All @@ -32,7 +34,9 @@ class CanvasViewModel(
var isFinded by graphViewModel::bridgeFinded

var isEdgeCreatingMode by mutableStateOf(false)
var isDijkstraMode by mutableStateOf(false)
var pickedNodeForEdgeCreating by mutableStateOf<VertexCanvasViewModel?>(null)
var pickedNodeForDijkstra by mutableStateOf<VertexCanvasViewModel?>(null)

var isNodeCreatingMode by mutableStateOf(false)
var edgesCount by mutableStateOf(0)
Expand Down Expand Up @@ -158,4 +162,33 @@ class CanvasViewModel(
createEdge(pickedNodeForEdgeCreating ?: return, vm)
pickedNodeForEdgeCreating = null
}

fun onClickNodeDijkstraOn(vm: VertexCanvasViewModel) {
if (!isDijkstraMode) {
resetEdgesColorToDefault()
return
}

if (pickedNodeForDijkstra == vm) {
pickedNodeForDijkstra = null
return
}

if (pickedNodeForDijkstra == null) {
pickedNodeForDijkstra = vm
return
}

val firstVertex =
pickedNodeForDijkstra ?: throw IllegalStateException("there is no node in pickedNodeForDijkstra method")

val dijksta = Dijkstra(graph)
val path = dijksta.findShortestPath(firstVertex.vertexViewModel.getKey(), vm.vertexViewModel.getKey()) ?: return
val edges = dijksta.triplesToEdges(path)

val PathWthColor = edges.map { it to Config.Edge.dijkstraColor }
resetEdgesColorToDefault()
changeEdgesColor(PathWthColor.toMutableList())
pickedNodeForDijkstra = null
}
}
11 changes: 10 additions & 1 deletion src/main/kotlin/viewModel/canvas/VertexCanvasViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,19 @@ class VertexCanvasViewModel(
vertexViewModel.onDrag(it * (1f / canvasViewModel.zoom))
}

fun onClickWhenEdgeCreating() {
fun onClick() {
onClickWhenDijkstraOn()
onClickWhenEdgeCreating()
}

private fun onClickWhenEdgeCreating() {
canvasViewModel.onClickNodeEdgeCreating(this)
}

private fun onClickWhenDijkstraOn() {
canvasViewModel.onClickNodeDijkstraOn(this)
}

private fun calculateOffset() = Offset(
(canvasViewModel.canvasSize.x / 2) + ((vertexViewModel.x - canvasViewModel.center.x) * canvasViewModel.zoom),
(canvasViewModel.canvasSize.y / 2) + ((vertexViewModel.y - canvasViewModel.center.y) * canvasViewModel.zoom)
Expand Down
12 changes: 8 additions & 4 deletions src/main/kotlin/viewModel/graph/UndirectedViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import model.algorithm.Clustering
import model.algorithm.Dijkstra
import model.algorithm.FindBridges
import model.algorithm.PageRank
import model.graph.Edge
Expand All @@ -19,17 +20,19 @@ class UndirectedViewModel(
val showVerticesLabels: Boolean,
var groups: HashMap<Vertex, Int> = hashMapOf(),
var ranks: List<Pair<Vertex, Double>> = listOf(),
var bridges: List<Edge> = listOf()
var bridges: List<Edge> = listOf(),
) {
private val _vertices = hashMapOf<Vertex, VertexViewModel>()
private val _adjacencyList = hashMapOf<VertexViewModel, ArrayList<EdgeViewModel>>()
private val groupColors = hashMapOf<Int, Color>(0 to Color.Black)
private val BridgesWthColor = mutableListOf<Pair<Edge, Color>>()
private val PathWthColor = mutableListOf<Pair<Edge, Color>>()

private val _color = mutableStateOf(Color.Black)
private val _clustering = mutableStateOf(false)
private val _ranked = mutableStateOf(false)
private val _bridgeFinded = mutableStateOf(false)
private val _shortestPathFinded = mutableStateOf(false)

private var size by mutableStateOf(10f)

Expand All @@ -55,7 +58,7 @@ class UndirectedViewModel(

updateSizes()
}

var bridgeFinded
get() = _bridgeFinded.value
set(value) {
Expand All @@ -66,12 +69,13 @@ class UndirectedViewModel(
}
if (bridgeFinded) {
changeEdgesColor(BridgesWthColor)
}
else{
} else {
resetEdgesColorToDefault()
}
}

var shortestPathFinded by mutableStateOf(false)

fun createEdge(first: VertexViewModel, second: VertexViewModel): Pair<EdgeViewModel, EdgeViewModel>? {
val edge = graph.addEdge(first.getKey(), second.getKey()) ?: return null

Expand Down

0 comments on commit a5310ba

Please sign in to comment.