title: Traversing a tree javascript
body: <p>I am trying to traverse a graph in javascript. My job is to traverse and parse all the possible outcomes of the following graph. </p>
  
  <p><a href="https://i.stack.imgur.com/CmQD2.png" rel="noreferrer"><img src="https://i.stack.imgur.com/CmQD2.png" alt="This is a rough visualization of the graph"></a>
  This is how I am storing the graph in a JS object.</p>
  
  <pre><code>var graph = {
      alpha: {
          in: [],
          out: ['a', 'b']
      },
      a: {
          in: ['alpha'],
          out: []
      },
      b: {
          in: ['alpha'],
          out: ['c', 'e']
      },
      c: {
          in: ['b'],
          out: ['d']
      },
      d: {
          in: ['c'],
          out: []
      },
      e: {
          in: ['b'],
          out: ['f', 'g']
      },
      f: {
          in: ['e'],
          out: []
      },
      g: {
          in: ['e'],
          out: []
      }
  };
  </code></pre>
  
  <p>I need to parse it to get the following output. </p>
  
  <pre><code>output = [
      ['alpha', 'a'],
      ['alpha', 'b', 'c', 'd'],
      ['alpha', 'b', 'e', 'f'],
      ['alpha', 'b', 'e', 'g']
  ];
  </code></pre>
  
  <p>Key things to note are : </p>
  
  <ol>
  <li><code>alpha</code> is always the parent node. </li>
  <li>any node can have only <code>one</code> input connection and maximum of <code>n</code>-number of output connections</li>
  </ol>
  
  <p>Right now my approach is to use recursion. BUt I just can't get my head around it. Can anyone give me a hand here? </p>
  
  <pre><code>var getChild = function (Obj) {
  
      if ( Obj['out'].length == 0){
          console.log('END');
          return
      }else{
          var shifted = Obj['out'].shift();
          console.log(shifted);
          return getChild(graph[shifted]);
      }
  
  }
  </code></pre>
  
  <p>Can anyone guide me to traverse the graph in maximum efficient way</p>