Algorithmic Solutions > LEDA > LEDA Guide > Graphs and Related Data Types > Associate Information with Graphs > Edge Arrays > Example

Example Edge Arrays

The following program shows how an edge_array can be used. First a graph G with three nodes and three edges is defined. Then a edge_array A of type int is created. The default value for A is 1. We output the values of A for the three edge of G. Afterwards we assign new values to the edges and output the result. Finally, we create a new edge of G and make A valid for the all edges of G. All edges are assigned the default value 1.

#include <LEDA/graph/graph.h>
#include <LEDA/graph/edge_array.h>

using namespace leda;

int main()
{
  graph G;
  node v1=G.new_node();
  node v2=G.new_node();
  node v3=G.new_node();
  edge e1=G.new_edge(v1,v2);
  edge e2=G.new_edge(v2,v3);
  edge e3=G.new_edge(v3,v1);
  
  edge_array<int> A(G,1);  //define edge array A for G
                           //and assign 1 to every edge
  std::cout << A[e1] << " " << A[e2] << " " << A[e3] << std::endl;

  //assign new values
  int i=0;edge e;
  forall_edges(e,G) {
    A[e]=i++; 
  }
  std::cout << A[e1] << " " << A[e2] << " " << A[e3] << std::endl;

  //if new edges are generated for G, we need to make A
  //valid for the new edges
  edge e4=G.new_edge(v2,v1);
  A.init(G);
  std::cout << A[e1] << " " << A[e2] << " " << A[e3] << " " << A[e4] << std::endl;
  //outputs "1 1 1 1"
 
  return 0;
}  

See also:

Edge Arrays

Associate Information with Graphs


Manual Entries:

Manual Page Edge Arrays

User Defined Parameter Types




Please send any suggestions, comments or questions to leda@algorithmic-solutions.com
© Copyright 2001-2003, Algorithmic Solutions Software GmbH