Skip to content

Commit

Permalink
Merge pull request #30 from spbu-coding-2023/dev
Browse files Browse the repository at this point in the history
Merge Dev branch with Main branch
  • Loading branch information
homka122 authored Sep 24, 2024
2 parents 4f95491 + fbcae97 commit d016283
Show file tree
Hide file tree
Showing 58 changed files with 1,718 additions and 387 deletions.
6 changes: 4 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ dependencies {
testImplementation(kotlin("test"))
testImplementation("org.junit.jupiter:junit-jupiter-api:5.7.1")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.1")
implementation("org.xerial:sqlite-jdbc:3.41.2.2")
implementation("org.neo4j.driver", "neo4j-java-driver", "5.6.0")
}

compose.desktop {
Expand All @@ -47,9 +49,9 @@ tasks.test {
finalizedBy("jacocoTestReport")
}

tasks.jacocoTestReport{
tasks.jacocoTestReport {
dependsOn(tasks.test)
reports{
reports {
xml.required.set(true)
html.required.set(true)
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/kotlin/Config.kt
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
}
}
24 changes: 9 additions & 15 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.*
import model.algorithm.Clustering
import model.algorithm.PageRank
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.WindowPlacement
import androidx.compose.ui.window.WindowState
import androidx.compose.ui.window.application
import model.graph.UndirectedGraph
import view.HeaderView
import view.MainView
import view.MenuView
import viewModel.graph.UndirectedViewModel
import viewModel.MainViewModel

val AMOUNT_NODES = 16
val EDGE_CHANGE = 5f
val EDGE_CHANGE = 5.0

val graph = UndirectedGraph().apply {
for (i in (0 until AMOUNT_NODES)) {
Expand All @@ -31,16 +29,12 @@ val graph = UndirectedGraph().apply {
}
}

val groups = Clustering(graph).calculate()
val ranks = PageRank(graph).computePageRank(3)
val undirectedViewModel = UndirectedViewModel(graph, false, groups, ranks)
val mainViewModel = MainViewModel(graph)

fun main() = application {
var isOpen by remember { mutableStateOf(true) }
var isMaximized by remember { mutableStateOf(true) }
var isMinimize by remember { mutableStateOf(false) }
var position: WindowPosition by remember { mutableStateOf(WindowPosition.PlatformDefault) }
var headerName by remember { mutableStateOf("Dimabase.db") }
val headerName by remember { mutableStateOf("Dimabase.db") }

val windowState = WindowState(
placement = if (isMaximized) WindowPlacement.Maximized else WindowPlacement.Floating,
Expand All @@ -60,7 +54,7 @@ fun main() = application {
isMaximized, { windowState.isMinimized = !windowState.isMinimized })
}
MainView(
undirectedViewModel,
mainViewModel,
)
}
}
Expand Down
32 changes: 32 additions & 0 deletions src/main/kotlin/components/MySlider.kt
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
)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package view
package components

import androidx.compose.material.Text
import androidx.compose.runtime.Composable
Expand Down
1 change: 0 additions & 1 deletion src/main/kotlin/model/algorithm/BellmanFord.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package model.algorithm
import model.graph.Edge
import model.graph.Graph
import model.graph.Vertex
import model.graph.WeightedGraph

class BellmanFord(private val graph: Graph) {
val parentMap = HashMap<Vertex, Vertex>()
Expand Down
62 changes: 62 additions & 0 deletions src/main/kotlin/model/algorithm/Dijkstra.kt
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()
}
}
4 changes: 3 additions & 1 deletion src/main/kotlin/model/algorithm/FindBridges.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package model.algorithm


import model.graph.*
import model.graph.Edge
import model.graph.Graph
import model.graph.Vertex


class FindBridges(
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/model/graph/DirectedGraph.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package model.graph

class DirectedGraph : UndirectedGraph() {
open class DirectedGraph : UndirectedGraph() {

override fun addEdge(first: Int, second: Int, weight: Long): Edge? {
if (first == second) return null
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/model/graph/Graph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ interface Graph {
fun updateVertex(key: Int, newKey: Int): Vertex?
fun addEdge(first: Int, second: Int, weight: Long = 1): Edge?
fun removeEdge(first: Int, second: Int): Edge?
fun getEdge(first: Int, second: Int): Edge?
}
6 changes: 6 additions & 0 deletions src/main/kotlin/model/graph/UndirectedGraph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ open class UndirectedGraph : Graph {

fun getEdges(vertex: Vertex) = _adjacencyList[vertex]

override fun getEdge(first: Int, second: Int): Edge? {
val firstVertex = findVertex(first) ?: return null

return getEdges(firstVertex)?.find { it.second.key == second }
}

private data class UndirectedVertex(override var key: Int) : Vertex

private data class UndirectedEdge(override val first: Vertex, override val second: Vertex) : Edge {
Expand Down
24 changes: 24 additions & 0 deletions src/main/kotlin/model/graph/WeightedDirectedGraph.kt
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
}
Loading

0 comments on commit d016283

Please sign in to comment.