Advertisement
ziobrowskyy

Untitled

Nov 12th, 2020
1,055
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 5.21 KB | None | 0 0
  1. import java.util.*
  2.  
  3. class Graph {
  4.  
  5.     val vertices: MutableList<Cities> = mutableListOf()
  6.     val edges: MutableList<Edge> = mutableListOf()
  7.     val adjacencyList: MutableMap<Cities, MutableList<Pair<Cities, Int>>> = mutableMapOf()
  8.  
  9.     fun addVertex(vararg names: Cities) {
  10.         names.forEach {
  11.             if(!vertices.contains(it))
  12.                 vertices.add(it)
  13.         }
  14.     }
  15.  
  16.     fun addEdge(weight: Int, vertex1: Cities, vertex2: Cities = vertex1) {
  17.         if(!vertices.contains(vertex1) || !vertices.contains(vertex2)) {
  18.             error("""
  19.                Nie znaleziono jednego lub więcej z podanych wierzchołków!
  20.                [vertex1: $vertex1, vertex2: $vertex2, weight: $weight]
  21.            """.trimIndent())
  22.         }
  23.         edges.add(Edge(weight, vertex1, vertex2))
  24.     }
  25.  
  26.     fun showEdges() {
  27.         edges.forEach(::println)
  28.     }
  29.  
  30.     fun createAdjacencyList() {
  31.         adjacencyList.clear()
  32.         edges.forEach { edge ->
  33.                 adjacencyList.getOrPut(edge.vertex1, ::mutableListOf).add(Pair(edge.vertex2, edge.weight))
  34.                 adjacencyList.getOrPut(edge.vertex2, ::mutableListOf).add(Pair(edge.vertex1, edge.weight))
  35.         }
  36.     }
  37.     fun kruskalMST(){
  38.         val sorted = edges.toMutableList().also { it.sortBy { it.weight } }
  39.  
  40.         val mst = mutableListOf<Edge>()
  41.         val sets = vertices.map { listOf(it).toMutableList() }.toMutableList()
  42.  
  43.         fun find(vertex: Cities): MutableList<Cities>? {
  44.             sets.forEach {
  45.                 val result = it.contains(vertex)
  46.                 if(result)
  47.                     return it
  48.             }
  49.             return null
  50.         }
  51.  
  52.         sorted.forEach { edge ->
  53.             val set1 = find(edge.vertex1)
  54.             val set2 = find(edge.vertex2)
  55.             if(set1 != set2) {
  56.                 mst.add(edge)
  57.                 set1!!.addAll(set2!!)
  58.                 sets.remove(set2)
  59.             }
  60.         }
  61.         mst.forEach {
  62.             println(it)
  63.         }
  64.     }
  65.     fun primMST()
  66.     {
  67.         class CityPair(val city: Cities, val weight: Int) : Comparable<CityPair> {
  68.             override fun compareTo(other: CityPair): Int {
  69.                 if (weight < other.weight)
  70.                     return -1
  71.                 if (weight == other.weight)
  72.                     return 0
  73.                 return 1
  74.             }
  75.  
  76.             operator fun component1() = city
  77.             operator fun component2() = weight
  78.         }
  79.         class MSTProps (
  80.             var parent: Cities = Cities.NOT_FOUND,
  81.             var weight: Int = Int.MAX_VALUE,
  82.             var visited: Boolean = false
  83.         ) {
  84.             override fun toString() = "parent: $parent, dist: $weight, inMST?: $visited"
  85.         }
  86.  
  87.         val mst = vertices.map { it to MSTProps() }.toMap().toMutableMap()
  88.         val queue = PriorityQueue<CityPair>()
  89.         val src = vertices.random()
  90.  
  91.         queue.add(CityPair(src, 0))
  92.         mst[src]?.weight = 0
  93.  
  94.         println("Starting from: $src")
  95.  
  96.         while (!queue.isEmpty()) {
  97.  
  98.             val parent = queue.remove().city
  99.  
  100.             mst[parent]?.visited = true
  101.  
  102.             adjacencyList[parent]?.forEach { (child, weight) ->
  103.  
  104.                 if (!mst[child]!!.visited && mst[child]!!.weight > weight) {
  105.                     mst[child]?.weight = weight
  106.                     queue.add(CityPair(child, weight))
  107.                     mst[child]?.parent = parent
  108.                 }
  109.             }
  110.  
  111.         }
  112.  
  113.         var distanceSum = 0
  114.         mst.forEach { (city, info) ->
  115.             println("city: $city, info := [$info]")
  116.             distanceSum += info.weight
  117.         }
  118.         println("Total distance: $distanceSum")
  119.     }
  120.     fun primsAlgorithmOld() {
  121.  
  122.         createAdjacencyList()
  123.         class CityPair(val first: Cities, val second: Int): Comparable<CityPair> {
  124.             override fun compareTo(other: CityPair): Int {
  125.                 if(second < other.second)
  126.                     return -1
  127.                 if(second == other.second)
  128.                     return 0
  129.                 return 1
  130.             }
  131.             operator fun component1() = first
  132.             operator fun component2() = second
  133.         }
  134.         val pq: Queue<CityPair> = PriorityQueue()
  135.  
  136.         val src = vertices.random()
  137.         println("Starting from: $src")
  138.         val key: MutableMap<Cities, Int> = vertices.map { it to Int.MAX_VALUE }.toMap().toMutableMap()
  139.         val parent: MutableMap<Cities, Cities> = vertices.map { it to Cities.NOT_FOUND }.toMap().toMutableMap()
  140.         val inMST: MutableMap<Cities, Boolean> = vertices.map { it to false }.toMap().toMutableMap()
  141.        
  142.         pq.add(CityPair(src, 0))
  143.  
  144.         key[src] = 0
  145.  
  146.         while(!pq.isEmpty()) {
  147.  
  148.             val (u, w) = pq.remove()
  149.  
  150.             inMST[u] = true
  151.  
  152.             adjacencyList[u]?.forEach {
  153.                 val (v, weight) = it
  154.  
  155.                 if(inMST[v] == false && key[v]!! > weight) {
  156.                     key[v] = weight
  157.                     pq.add(CityPair(v, weight))
  158.                     parent[v] = u
  159.                 }
  160.             }
  161.         }
  162.  
  163.         parent.forEach { (city, parent) ->
  164.             println("city: $city, parent $parent")
  165.         }
  166.  
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement