title: Creating a graph representing all possible paths from a 2D matrix
body: <p>I am trying to generate a graph representing all possible paths from a matrix.</p>
  
  <ul>
  <li>The possible paths include: up,down,left,right.</li>
  <li>My algorithm traverses the matrix, but I've realized this won't work.</li>
  <li>While code is welcome, even ideas or suggestions are welcome. I've been looking at online resources but haven't been able to find an example solution for this particular problem that I am able to understand.</li>
  <li>My code should run as is, though it's flawed ofcourse :)</li>
  </ul>
  
  <p>C++</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;vector&gt;
  
  struct Tile
  {
      int tileId;
      int moveCost;
  };
  
  class Node
  {
  
  public:
  
      Node();
  
      void setTile(Tile *tile);
      void addLink(Node *node);
  
  private:
      std::vector&lt;Node *&gt; mLinks;
      Tile *mTile;
  };
  
  Node::Node()
  {
  
  }
  
  void Node::setTile(Tile *tile)
  {
      mTile = tile;
  }
  
  void Node::addLink(Node *node)
  {
      mLinks.push_back(node);
  }
  
  
  
  int main()
  {
      int boardSize = 5;
  
      std::vector&lt; std::vector&lt;Tile *&gt; &gt; board;
      board.resize(boardSize, std::vector&lt;Tile *&gt;(boardSize, NULL));
  
      // generate the board
      int i = 0;
      for (int x = 0; x &lt; boardSize; x++)
      {
          for (int y = 0; y &lt; boardSize; y++)
          {
              int mc = 1;
              if (i == 7 || i == 8 || i == 12 || i == 17 || i == 18)
              {
                  mc = 9;
              }
              Tile *tile = new Tile();
              tile-&gt;tileId = i;
              tile-&gt;moveCost = mc;
              board[x][y] = tile;
              i++;
          }
      }
  
      // create graph from the board, with each node having a link to it's nieghboring tile
      Node *rootNode = new Node(); // only serves as the entry point
      for (int x = 0; x &lt; boardSize; x++)
      {
          for (int y = 0; y &lt; boardSize; y++)
          {
              // this tile node
              Node *thisNode = new Node();
              thisNode-&gt;setTile(board[x][y]);
  
              // up neighbor node
              if (y - 1 &gt;= 0)
              {
                  Node *upNode = new Node();
                  upNode-&gt;setTile(board[x][y - 1]);
                  thisNode-&gt;addLink(upNode);
              }
  
              // down neighbor node
              if (y + 1 &lt; boardSize)
              {
                  Node *downNode = new Node();
                  downNode-&gt;setTile(board[x][y + 1]);
                  thisNode-&gt;addLink(downNode);
              }
  
              // left neighbor node
              if (x - 1 &gt;= 0)
              {
                  Node *leftNode = new Node();
                  leftNode-&gt;setTile(board[x - 1][y]);
                  thisNode-&gt;addLink(leftNode);
              }
  
              // right neighbor node
              if (x + 1 &lt; boardSize)
              {
                  Node *rightNode = new Node();
                  rightNode-&gt;setTile(board[x + 1][y]);
                  thisNode-&gt;addLink(rightNode);
  
              }
  
              // only add the first node to the rootNode
              if (x + y == 0)
              {
                  rootNode-&gt;addLink(thisNode);
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Edit:</strong> For example, given this visualization of the matrix:</p>
  
  <pre><code>[0][1][2][3][4]
  [5][6][7][8][9]
  [10][11][12][13][14]
  [15][16][17][18][19]
  [20][21][22][23][24]
  </code></pre>
  
  <p>I would like each graph node to contain a list of pointers to each of it's neighbors (up,down, left, right)</p>
  
  <pre><code>Tile 0 neighbors: 1,5
  Tile 1 neighbors: 0,6,2
  Tile 2 neighbors: 1,7,3
  </code></pre>
  
  <p>And so on</p>