Note that there are some explanatory texts on larger screens.

plurals
  1. POTricky algorithm for sorting symbols in an array while preserving relationships via order
    text
    copied!<h2>The problem</h2> <p>I have multiple groups which specify the relationships of symbols.. for example:</p> <pre><code>[A B C] [A D E] [X Y Z] </code></pre> <p>What these groups mean is that (for the first group) the symbols, A, B, and C are related to each other. (The second group) The symbols A, D, E are related to each other.. and so forth.</p> <p>Given all these data, I would need to put all the <em>unique</em> symbols into a 1-dimension array wherein the symbols which are somehow related to each other would be placed closer to each other. Given the example above, the result should be something like:</p> <pre><code>[B C A D E X Y Z] </code></pre> <p>or</p> <pre><code>[X Y Z D E A B C] </code></pre> <p>In this resulting array, since the symbol A has multiple relationships (namely with B and C in one group and with D and E in another) it's now located between those symbols, somewhat preserving the relationship.</p> <p><em>Note that the order is not important. In the result, X Y Z can be placed first or last since those symbols are not related to any other symbols. However, the closeness of the related symbols is what's important.</em></p> <h2>What I need help in</h2> <p><strong>I need help in determining an algorithm that takes groups of symbol relationships, then outputs the 1-dimension array using the logic above. I'm pulling my hair out on how to do this since with real data, the number of symbols in a relationship group can vary, there is also no limit to the number of relationship groups and a symbol can have relationships with any other symbol.</strong></p> <h2>Further example</h2> <p>To further illustrate the trickiness of my dilemma, IF you add another relationship group to the example above. Let's say:</p> <pre><code>[C Z] </code></pre> <p>The result now should be something like:</p> <pre><code>[X Y Z C B A D E] </code></pre> <p>Notice that the symbols Z and C are now closer together since their relationship was reinforced by the additional data. All previous relationships are still retained in the result also.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload