|
@ -9,6 +9,7 @@ import scala.io.BufferedSource |
|
|
import scala.reflect.ClassTag |
|
|
import scala.reflect.ClassTag |
|
|
import com.typesafe.scalalogging.Logger |
|
|
import com.typesafe.scalalogging.Logger |
|
|
import com.typesafe.scalalogging.LazyLogging |
|
|
import com.typesafe.scalalogging.LazyLogging |
|
|
|
|
|
import scala.collection.immutable.ArraySeq |
|
|
|
|
|
|
|
|
object Util extends LazyLogging { |
|
|
object Util extends LazyLogging { |
|
|
private val r = 6471.00 // km |
|
|
private val r = 6471.00 // km |
|
@ -129,6 +130,46 @@ object Util extends LazyLogging { |
|
|
(mst, num.fromInt(sum / n)) |
|
|
(mst, num.fromInt(sum / n)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
def mstUsingPrims2[T: ClassTag]( |
|
|
|
|
|
edges: Array[Array[T]] |
|
|
|
|
|
)(implicit num: Numeric[T]): (IndexedSeq[Seq[(Int, T)]], T) = { |
|
|
|
|
|
val n = edges.length |
|
|
|
|
|
val selected: ArrayBuffer[Boolean] = ArrayBuffer.fill(n)(false) |
|
|
|
|
|
|
|
|
|
|
|
selected(0) = true |
|
|
|
|
|
|
|
|
|
|
|
val adjList: Array[mutable.ListBuffer[(Int, T)]] = |
|
|
|
|
|
Array.fill(n)(mutable.ListBuffer()) |
|
|
|
|
|
|
|
|
|
|
|
var sum = 0 |
|
|
|
|
|
|
|
|
|
|
|
for (_ <- 0 until n - 1) { |
|
|
|
|
|
var min = 999999 |
|
|
|
|
|
var x = 0 |
|
|
|
|
|
var y = 0 |
|
|
|
|
|
for (i <- 0 until n) { |
|
|
|
|
|
if (selected(i) == true) { |
|
|
|
|
|
for (j <- 0 until n) { |
|
|
|
|
|
if (selected(j) == false && edges(i)(j) != 0) { |
|
|
|
|
|
if (num.gt(num.fromInt(min), edges(i)(j))) { |
|
|
|
|
|
min = num.toInt(edges(i)(j)) |
|
|
|
|
|
x = i |
|
|
|
|
|
y = j |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
sum += num.toInt(edges(x)(y)) |
|
|
|
|
|
adjList(x) += ((y, edges(x)(y))) |
|
|
|
|
|
adjList(y) += ((x, edges(x)(y))) |
|
|
|
|
|
selected(y) = true |
|
|
|
|
|
} |
|
|
|
|
|
// val map = list.foldLeft(Map[String, Int]()) { (m, s) => m(s) = s.length } |
|
|
|
|
|
val adjList2 = adjList.map(_.toList) |
|
|
|
|
|
(ArraySeq.unsafeWrapArray(adjList2), num.fromInt(sum / n)) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
def findClusters[T]( |
|
|
def findClusters[T]( |
|
|
mst: Array[Array[T]], |
|
|
mst: Array[Array[T]], |
|
|
centroids: IndexedSeq[Int], |
|
|
centroids: IndexedSeq[Int], |
|
@ -167,6 +208,27 @@ object Util extends LazyLogging { |
|
|
buf |
|
|
buf |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
def makeAdjacencyList2( |
|
|
|
|
|
mst: Array[Array[Double]] |
|
|
|
|
|
): ArrayBuffer[ArrayBuffer[(Int, Double)]] = { |
|
|
|
|
|
val n = mst.length |
|
|
|
|
|
// val selected: ArrayBuffer[Boolean] = ArrayBuffer.fill(n)(false) |
|
|
|
|
|
val buf: ArrayBuffer[ArrayBuffer[(Int, Double)]] = |
|
|
|
|
|
ArrayBuffer.fill(n)(ArrayBuffer.empty) |
|
|
|
|
|
// for (_ <- 0 until n -1) { |
|
|
|
|
|
|
|
|
|
|
|
// } |
|
|
|
|
|
for (i <- 0 until n) { |
|
|
|
|
|
for (j <- 0 until n) { |
|
|
|
|
|
if (mst(i)(j) != 0) { |
|
|
|
|
|
// println(s" $i $j = ${mst(i)(j)}") |
|
|
|
|
|
buf(i) += ((j, mst(i)(j))) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
buf |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
def findCentroids[T]( |
|
|
def findCentroids[T]( |
|
|
mst: Array[Array[T]], |
|
|
mst: Array[Array[T]], |
|
|
epsilon: T |
|
|
epsilon: T |
|
@ -181,7 +243,8 @@ object Util extends LazyLogging { |
|
|
if (ev.gt(mst(i)(j), epsilon) && mst(i)(j) != 0) { |
|
|
if (ev.gt(mst(i)(j), epsilon) && mst(i)(j) != 0) { |
|
|
// println(s" $i $j = ${mst(i)(j)}") |
|
|
// println(s" $i $j = ${mst(i)(j)}") |
|
|
centroids += i |
|
|
centroids += i |
|
|
centroids += j |
|
|
|
|
|
|
|
|
// centroids += j |
|
|
|
|
|
// if (j < n / 2) |
|
|
removed.append((i, j, mst(i)(j))) |
|
|
removed.append((i, j, mst(i)(j))) |
|
|
mst(i)(j) = ev.zero |
|
|
mst(i)(j) = ev.zero |
|
|
} |
|
|
} |
|
|