I'm new to Ruby and I've been playing with the Kruskal's Algorithm a little bit but at the moment I've hit a bump and I can't understand what I need to do at this stage.
The code I've been working on is this:
def partition(arr, clusters)
edgeValues = []
index1 = 0
index2 = 1
arr = arr.sort # [3, 4, 5, 5, 6, 10, 15, 20, 75, 80, 85]
arr.length.times{
val = (arr[index1]-arr[index2]).abs
edgeValues << [ val, index1, index2]
index1 += 1
index2 += 1
break if (arr.length == index2)
}
edgeValues = edgeValues.sort
#p edgeValues[0][0] # value cost
#p edgeValues[0][1] # index 1
#p edgeValues[0][2] # index 2
end
array = [5, 4, 3, 5, 15, 20, 10, 80, 75, 6, 85]
clusters = 3
partition( array, clusters ) #end result: [ [3, 4, 5, 5, 6], [10, 15, 20], [75, 80, 85] ]
I managed to do everything except the last part.
I have no idea how to manipulate the sorted array:
[3, 4, 5, 5, 6, 10, 15, 20, 75, 80, 85]
In order to achieve the end result:
[ [3, 4, 5, 5, 6], [10, 15, 20], [75, 80, 85] ]
I have all the calculations handled and those values stored in the edgeValues array.
Any help would be appreciated.
edgeValues = edgeValues.sort. It is enough to finish withedgeValues.sortor in your case probablyedgeValues.sort.each_slice(clusters)- Saša Zejnilović