title: Accessing specific edges in boost::graph with integer index
body: <p>This is related to a question I had yesterday about accessing vertices using integer indices. That thread is here: <a href="https://stackoverflow.com/questions/45925625/accessing-specific-vertices-in-boostgraph">Accessing specific vertices in boost::graph</a></p>
  
  <p>The solution there indicated that using vecS as the type for vertices, it is indeed possible to access specific vertices using the integer index. I was wondering if there is a similar method provided by boost to access arbitrary edges efficiently using integer indices.</p>
  
  <p>Attached is a code that depicts the former (valid access of vertices with integer indices) and accessing the edges based on the developer explicitly maintaining two arrays, <code>from[]</code> and <code>to[]</code>, that store the source and the target, respectively of the edges.</p>
  
  <p>The code creates the following graph:</p>
  
  <p><a href="https://i.stack.imgur.com/rKM3J.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rKM3J.jpg" alt="Max Flow Problem"></a></p>
  
  <pre><code>#include &lt;boost/config.hpp&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  #include &lt;boost/graph/graph_traits.hpp&gt;
  #include &lt;boost/graph/adjacency_list.hpp&gt;
  
  using namespace boost;
  
  typedef adjacency_list_traits&lt;vecS, vecS, directedS&gt; Traits;
  
  typedef adjacency_list&lt;
      vecS, vecS, directedS,
      property&lt;
      vertex_name_t, std::string,
      property&lt;vertex_index_t, int,
      property&lt;vertex_color_t, boost::default_color_type,
      property&lt;vertex_distance_t, double,
      property&lt;vertex_predecessor_t, Traits::edge_descriptor&gt; &gt; &gt; &gt; &gt;,
  
      property&lt;
      edge_index_t, int,
      property&lt;edge_capacity_t, double,
      property&lt;edge_weight_t, double,
      property&lt;edge_residual_capacity_t, double,
      property&lt;edge_reverse_t, Traits::edge_descriptor&gt; &gt; &gt; &gt; &gt; &gt;
      Graph;
  
  int main() {
      int nonodes = 4;
      const int maxnoedges = 4;//I want to avoid using this.
      Graph g(nonodes);
  
      property_map&lt;Graph, edge_index_t&gt;::type             E = get(edge_index, g);
  
      int from[maxnoedges], to[maxnoedges];//I want to avoid using this.
  
  
      // Create edges
      Traits::edge_descriptor ed;
  
      int eindex = 0;
  
      ed = (add_edge(0, 1, g)).first;
      from[eindex] = 0; to[eindex] = 1;//I want to avoid using this.
      E[ed] = eindex++;
  
  
      ed = (add_edge(0, 2, g)).first;
      from[eindex] = 0; to[eindex] = 2;//I want to avoid using this.
      E[ed] = eindex++;
  
      ed = (add_edge(1, 3, g)).first;
      from[eindex] = 1; to[eindex] = 3;//I want to avoid using this.
      E[ed] = eindex++;
  
      ed = (add_edge(2, 3, g)).first;
      from[eindex] = 2; to[eindex] = 3;//I want to avoid using this.
      E[ed] = eindex++;
  
      graph_traits &lt; Graph &gt;::out_edge_iterator ei, e_end;
      for (int vindex = 0; vindex &lt; num_vertices(g); vindex++) {
          printf("Number of outedges for vertex %d is %d\n", vindex, out_degree(vindex, g));
          for (tie(ei, e_end) = out_edges(vindex, g); ei != e_end; ++ei)
              printf("From %d to %d\n", source(*ei, g), target(*ei, g));
      }
  
      printf("Number of edges is %d\n", num_edges(g));
  
      //Is there any efficient method boost provides 
      //in lieu of having to explicitly maintain from and to arrays
      //on part of the developer?
      for (int eindex = 0; eindex &lt; num_edges(g); eindex++)
          printf("Edge %d is from %d to %d\n", eindex, from[eindex], to[eindex]);
  
  }
  </code></pre>
  
  <p>The code builds and compiles without error. The <code>for</code> loop with <code>vindex</code> works fine with <code>out_edges</code> and <code>out_degree</code> working fine taking as parameters integer indices.</p>
  
  <p>Is there a way to do likewise for the next <code>for</code> loop that prints the edges using boost::graph data structures directly?</p>
  
  <p>I looked at the following thread dealing with a similar question:</p>
  
  <p><a href="https://stackoverflow.com/questions/37336463/boost-graph-library-get-edge-descriptor-or-access-edge-by-index-of-type-int">Boost graph library: Get edge_descriptor or access edge by index of type int</a></p>
  
  <p>The suggested answer there was to use an <code>unordered_map</code>. Is there any tradeoff in using this as opposed to having the <code>from[]</code> and <code>to[]</code> arrays? Are there any other computationally efficient methods of accessing edges?</p>