Guest User

Untitled

a guest
Jun 14th, 2024
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.40 KB | None | 0 0
  1. var curComponentInfectedNodeCnt = 0
  2. var infectedNode = Int.MAX_VALUE
  3.  
  4. fun minMalwareSpread(graph: Array<IntArray>, initial: IntArray): Int {
  5.     // To adjacency list
  6.     val adjacencyList =  Array<ArrayList<Int>>(graph.size) {
  7.         ArrayList()
  8.     }
  9.     // For each vertex
  10.     for (i in 0..graph.lastIndex) {
  11.         val curVertexNeighbors = graph[i]
  12.         // For each neighbor
  13.         for (j in 0..graph.lastIndex) {
  14.             if (curVertexNeighbors[j] == 0) continue
  15.             adjacencyList[i].add(j)
  16.         }
  17.     }
  18.  
  19.     val initialInfectedNodes = initial.toHashSet()
  20.     val used = HashSet<Int>()
  21.     val connectivityComponents = ArrayList<ArrayList<Int>>()
  22.     var maxComponentSize = Int.MIN_VALUE
  23.     var nodeToBeRemoved = Int.MAX_VALUE
  24.     // For each vertex
  25.     for (i in 0..adjacencyList.lastIndex) {
  26.         val neighbors = adjacencyList[i]
  27.         // For each neighbor
  28.         for (j in 0..neighbors.lastIndex) {
  29.             if (used.contains(neighbors[j])) continue
  30.             connectivityComponents.add(ArrayList())
  31.             curComponentInfectedNodeCnt = 0
  32.             dfs(adjacencyList, connectivityComponents.last(), used, initialInfectedNodes, neighbors[j])
  33.  
  34.             if (curComponentInfectedNodeCnt == 1) {
  35.                 val curComponentSize = connectivityComponents.last().size
  36.                 if (curComponentSize == maxComponentSize && infectedNode < nodeToBeRemoved) {
  37.                     nodeToBeRemoved = infectedNode
  38.                 } else if (curComponentSize > maxComponentSize) {
  39.                     nodeToBeRemoved = infectedNode
  40.                     maxComponentSize = curComponentSize
  41.                 }
  42.             }
  43.         }
  44.     }
  45.     return if (nodeToBeRemoved == Int.MAX_VALUE) initial.min() else nodeToBeRemoved
  46. }
  47.  
  48. fun dfs(
  49.     adjacencyList: Array<ArrayList<Int>>,
  50.     componentNodes: ArrayList<Int>,
  51.     used: HashSet<Int>,
  52.     initialInfectedNodes: HashSet<Int>,
  53.     curNode: Int
  54. ) {
  55.     if (initialInfectedNodes.contains(curNode)) {
  56.         ++curComponentInfectedNodeCnt
  57.         infectedNode = curNode
  58.     }
  59.  
  60.     used.add(curNode)
  61.     componentNodes.add(curNode)
  62.  
  63.     val neighbors = adjacencyList[curNode]
  64.     for (i in 0..neighbors.lastIndex) {
  65.         val neighborVertex = neighbors[i]
  66.         if (used.contains(neighborVertex)) continue
  67.         dfs(adjacencyList, componentNodes, used, initialInfectedNodes, neighborVertex)
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment