I would like to arrange the following items, forming the longest chain possible starting with 12-8 and matching the numbers end to end.
My items are 7-4, 11-8, 11-11
you need recursion, but it might not work on a bigger data set: something like this.
DISCLAIMER: This is probably not the most optimized solution (complexity O(N!)) but it is very simple to implement if you are allowed to use recursion
(this is not objective-c code, it's an algorithm, translate it yourself, sorry i don't know objective-c)
list function sortTupleList(list a, list b) //b is the current list
list biggest = newlist()
int target = b.last()[1]
for(tuple k in a)
if (k[0] == target)
list n = sortTupleList(a.remove(k), b.add(k))
if(n.size > biggest.size())
biggest = n
end if
end if
end for
if (biggest == emptylist)
return b
else
return biggest
end function
list function caller(list a)
list b = newlist()
b.add(12-8)
a.remove(12-8)
return sortTupleList(a,b)
end function
This function will test every single pattern starting from 12-8 and compare their size