Algorithmic Solutions > LEDA > LEDA Guide > Graphs and Related Data Types > Sets of Nodes > Example


Example Sets of Nodes

The following example shows how a set of nodes can be used.

First a random graph with 12 nodes and 30 edges is generated. Then a node_set S is defined for G and all nodes of G are inserted into S. The program iterates over all members of S and prints them. Finally, the member() function of S is illustrated. First S.member() is called for a random node v of G. The result is true. Then S.member() is called for a new node w of G. Here, the result is false. If w is inserted into S, then S.member(w) also returns true. If S.member() is called for a node of another graph, the program terminates with an error message.

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

using namespace leda;

int main()
{
  graph G;
  random_graph(G,12,30);

  node_set S(G);

  node v;
  forall_nodes(v,G) S.insert(v);

  forall(v,S) { G.print_node(v); std::cout << std::endl; }
  std::cout << std::endl;

  v=G.choose_node();
  G.print_node(v);
  if (S.member(v)) std::cout << " is a member of S\n";
  else std::cout << " is no member of S\n";

  node w=G.new_node();
  G.print_node(w);
  if (S.member(w)) std::cout << " is a member of S\n";
  else std::cout << " is no member of S\n";

  return 0;
}

See also:

Graph


Manual Entries:

Manual Page Sets of Nodes

Iteration




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