A friend of mine defended Fortran against half-literate coders on an example of Prim’s allgorithm.
Good pretext for another language comparison. Let’s see Groovy vs Java vs good old Fortran.
Great Java implementation by Bruno R. Preiss, P.Eng. and scientist.
Some beginner’s C+ implementation (or is it just a link farm? Whatever).
And here’s the algorithm in Groovy, copied as precisely as possible from pseudocode in Russian Wikipedia article.
[sourcecode language=’groovy’]
class Edge {
Integer v1
Integer v2
Integer weight
Edge(Integer v1, Integer v2, Integer w) {
this.v1 = v1
this.v2 = v2
this.weight = w
}
boolean contains(Integer v) { v1 == v || v2 == v }
Integer otherThan(Integer v) { v1 == v ? v2 : v1 }
}
class Graph {
List
Collection
// Dumbest implementation
List
Edge findEdge(Integer v1, Integer v2) { E.find{ it.contains(v1) && it.contains(v2) } }
Number weight(Integer v1, Integer v2) { findEdge(v1, v2)?.weight ?: Double.POSITIVE_INFINITY }
}
Collection
Set
Map
Map
d[G.V[0]] = 0
List
Q.metaClass.extractMin = { ->
// Dumbest implementation
int minimum = Q.min { d[it] }
int indexOfMinimum = Q.indexOf(minimum) // here Groovy fails to make a shortcut.
return Q.remove(indexOfMinimum)
}
Integer v = Q.extractMin()
while (!Q.isEmpty()) {
G.getIncident(v).each { Integer u ->
if(Q.contains(u) && G.weight(u, v) < d[u]) {
d[u] = G.weight(u, v)
p[u] = v
}
}
v = Q.extractMin()
T << G.findEdge(p[v], v)
}
return T
}
void testPrim() {
Set
new Edge(1, 2, 1),
new Edge(2, 3, 1),
new Edge(3, 4, 1),
new Edge(4, 5, 1),
]
Set
new Edge(1, 3, 2),
new Edge(1, 4, 3),
new Edge(1, 5, 2),
new Edge(2, 4, 7),
new Edge(3, 5, 2),
]
Graph G = new Graph(V: 1..5, E: expected + edges)
Set
assert expected.equals(T)
println “it worked!”
}
testPrim()[/sourcecode]
Not too bad, er? I see only a couple of Groovy overhead points: constructing a Map from collect{}, and absence of findIndexOfMin().
Of course, we could avoid long type declarations, but I like strict typing.
Make your conclusions.
2 Comments
That’s a bit strange – there is no Edge(1, 2, 1) in initial data.
Why it appears in expected data?
It’s there, look how I construct G: new Graph(V: 1..5, E: expected + edges)
Post a Comment