Algorithmic Solutions > LEDA > LEDA Guide > Graphs and Related Data Types > Graphs > Example

Example of How to Use Graphs

The following program is a very simple example of how to use a graph. It generates a star-like directed graph G. Then it iterates over all edges of G, computes the source and target of each edge, and outputs them.

#include <LEDA/graph/graph.h>

using namespace leda;

int main()
{
  graph G;    //define directed graph G

  node center=G.new_node();   //create new node "center" of G

  int i; 
  for (i=0;i<100;i++) {
    node v=G.new_node();     //create new node v of G
    G.new_edge(center,v);    //create new edge of G 
                             //with source center and target v
  }
  
  edge e;
  forall_edges(e,G) {           //iterate over all edges e of G
    node source=G.source(e);  //compute source of e
    node target=G.target(e);  //compute target of e

    std::cout << "edge "; 
    G.print_edge(e);          //print edge
    std::cout << " has source ";  
    G.print_node(source);     //print source
    std::cout << " and target ";
    G.print_node(target);     //print target
    std::cout << std::endl;
  }

  return 0;
} 

See also:

Manual Entries:

Manual Page Graphs

LEDA Item Concept

Iteration




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