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

Example Node Arrays

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

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

using namespace leda;

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

  int i=0;
  node v;
  
  forall_nodes(v,G) {
    A[v]=i++; //assign new values
  }
  std::cout << A[v1] << " " << A[v2] << " " << A[v3] << std::endl;

  //if new nodes are generated for G, we need to make A
  //valid for the new nodes
  node v4=G.new_node();
  A.init(G);
  
  std::cout << A[v1] << " " << A[v2] << " " << A[v3] << " " << A[v4] << std::endl;
  //outputs "1 1 1 1"
 
  return 0;
}  

See also:

Node Arrays

Associate Information with Graphs


Manual Entries:

Manual Page Node Arrays

User Defined Parameter Types




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