Skip to content

Instantly share code, notes, and snippets.

@tuxdna
Created March 25, 2014 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuxdna/9764091 to your computer and use it in GitHub Desktop.
Save tuxdna/9764091 to your computer and use it in GitHub Desktop.
Simple Canopy Clustering algorithm in Scala
package clustering
import scala.collection.mutable
import scala.util.Random
object canopy {
type Point = Tuple2[Int, Int]
def distance(p1: Point, p2: Point) = {
val xdiff = p1._1 - p2._1
val ydiff = p1._2 - p2._2
math.sqrt(xdiff * xdiff + ydiff * ydiff)
}
def main(args: Array[String]) {
var points = List(
(1, 1), (2, 1), (1, 2), (2, 2), (3, 3),
(8, 8), (9, 8), (8, 9), (9, 9), (5, 6))
// T2 < T1
val T1 = 7.0
val T2 = 3.0
val canopies = mutable.Map[Point, mutable.Set[Point]]()
while (points.size > 0) {
// new canopy
val r = Random.shuffle(points)
val C = r.head
canopies foreach { x =>
canopies(x._1).remove(C)
}
points = points filter (x => x != C)
val canopy = mutable.Set[Point](C)
canopies(C) = canopy
for (P <- r.tail) {
val dist = distance(C, P)
if (dist <= T1) { canopy.add(P) }
if (dist < T2) {
points = points filter (x => x != P)
}
}
}
canopies foreach { x =>
println("Cluster: %s => %s".format(x._1, x._2))
}
}
}
@tuxdna
Copy link
Author

tuxdna commented Mar 25, 2014

Sample OUTPUT:

Cluster: (5,6) => Set((1,2), (2,1), (1,1), (2,2), (5,6))
Cluster: (3,3) => Set((1,2), (3,3), (2,1), (1,1), (2,2))
Cluster: (8,8) => Set((9,8), (8,9), (9,9), (8,8))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment