math - Tricky algorithm for sorting symbols in an array while preserving relationships via order -
math - Tricky algorithm for sorting symbols in an array while preserving relationships via order -
the problem
i have multiple groups specify relationships of symbols.. example:
[a b c] [a d e] [x y z] what these groups mean (for first group) symbols, a, b, , c related each other. (the sec group) symbols a, d, e related each other.. , forth.
given these data, need set unique symbols 1-dimension array wherein symbols somehow related each other placed closer each other. given illustration above, result should like:
[b c d e x y z] or
[x y z d e b c] in resulting array, since symbol has multiple relationships (namely b , c in 1 grouping , d , e in another) it's located between symbols, preserving relationship.
note order not important. in result, x y z can placed first or lastly since symbols not related other symbols. however, closeness of related symbols what's important.
what need help ini need help in determining algorithm takes groups of symbol relationships, outputs 1-dimension array using logic above. i'm pulling hair out on how since real data, number of symbols in relationship grouping can vary, there no limit number of relationship groups , symbol can have relationships other symbol.
further exampleto farther illustrate trickiness of dilemma, if add together relationship grouping illustration above. let's say:
[c z] the result should like:
[x y z c b d e] notice symbols z , c closer since relationship reinforced additional data. previous relationships still retained in result also.
the first thing need exactly define result want.
you defining how result is, know best one. mathematically cost function. in case 1 typically take sum of distances between related elements, sum of squares of these distances, or maximal distance. list little value of cost function desired result.
it not clear whether in case feasible compute best solution special method (maybe if take maximal distance or sum of distances cost function).
in case should easy find approximation standard methods.
a simple greedy approach insert each element in position resulting cost function whole list minimal.
once have starting point can seek improve farther modifying list towards improve solutions, illustration swapping elements or rotating parts of list (local search, hill climbing, simulated annealing, other).
algorithm math graph
Comments
Post a Comment