Search code examples
javascriptjavarubysimplification

Logic to simplify item replacement chain


I have some items that are being replaced by other items hence creating a replacement chain as:

+---------------+------------------+
| Replaced Item | Replacement Item |
+---------------+------------------+
| A             | B                |
| B             | C                |
| C             | D                |
| G             | H                |
+---------------+------------------+

If we simplify it relationship chains we found as
A -> B -> C -> D
& G -> H

Finally, I want to achieve output as simplified table Like:

+---------------+------------------+
| Replaced Item | Replacement Item |
+---------------+------------------+
| A             | D                |
| B             | D                |
| C             | D                |
| G             | H                |
+---------------+------------------+

My question is: Is any existing API or algorithm to solve this type of chain simplification problem in javascript/java/ruby etc.

What I tried is: I thought i could solve it by making use of Java references. when we assign a reference to another reference so both references will point same object, Hence Object ID will be same. I have created several references as:

String ref1 = "A";
String ref2 = "B";
String ref3 = "C";
String ref4 = "D";
String ref5 = "G";
String ref6 = "H";

I got hashcodes from ref.hashCode() method.

//A = 65
//B = 66
//C = 67
//D = 68
//E = 71
//F = 72
//----

// Now A --> B means
ref2 = ref1;
//A = 65
//B = 65
//C = 67
//D = 68
//E = 71
//F = 72
//----

// Now B --> C means
ref3 = ref2;
//A = 65
//B = 65
//C = 65
//D = 68
//E = 71
//F = 72
//----


// Now C --> D means
ref4 = ref3;
//A = 65
//B = 65
//C = 65
//D = 65
//E = 71
//F = 72
//----

// Now C --> D means
ref6 = ref5;
//A = 65
//B = 65
//C = 65
//D = 65
//E = 71
//F = 71
//----

Now I would need to iterate through all references and put hashcodes into a set which contains unique values. so i got 65 and 71 only.

Now, 65 -> A,B,C,D and precedence-wise D is last element. 71 -> G,H precedence-wise H is last element.

so i could conclude it as:

+---------------+------------------+
| Replaced Item | Replacement Item |
+---------------+------------------+
| A             | D                |
| B             | D                |
| C             | D                |
| G             | H                |
+---------------+------------------+

Solution

  • You can get a new "map" with some simple recursion:

    var replacements = {
          A: 'B',
          B: 'C',
          C: 'D',
          G: 'H'
        },
        map = {};
    
    function replace(letter){
        if(!replacements[letter]) // If the replacements don't contain the current letter,
          return letter;          // Just use the current letter
        return replace(replacements[letter]); // Otherwise, get the replacement letter.
    }
    
    for(var key in replacements){  // 'key' being: A, B, C, G
        map[key] = replace(key);   // Assign a new replacement value in the map.
    }
    
    console.log(map);