In this tutorial, we will, display on the standard output, all the structure using iterators. For each node, we will display its ancestors, successors, neighbors, and, its incoming and outgoing edges.

In this tutorial, the graph created is the same that in Tutorial 1 (after the 3 edges were added) see the following picture :

1. Header files (Same as Tutorial 1)

Let's start with the files we need to include :

#include <iostream>
#include <tulip/Graph.h>

iostream : This "file" contains the C++ standard declarations for in and out streams. We need it in this tutorial to print the final graph on the standard output.

tulip/Graph.h : This file is the core of the tulip graph API. It provides declarations for graphs (edges , nodes) and functions to load one from a file, to save one, and a lot more. You can find a list in the doxygen documentation.

As you can see, we just need the "Graph.h" header file to create a graph and iterate over its nodes, even though the declaration of the abstract class "Iterator" is in Iterator.h

2. Iterating over all nodes

To iterate over all nodes, we need to create an Iterator on the graph nodes with the member function Iterator* Graph::getNodes () const, we will make it point on the graphs nodes.

3. Iterating through a node predecessors

To iterate through predecessors of node, we use the same type of Iterator, but, instead of using the function getNodes() of the class Graph, we will use the function Iterator<node>* getInNodes (const node) const that will return an iterator on the predecessors of a node.

Don't forget memory leaks

As we are still in the first while (iterating through all nodes) we need to delete the Iterator on Nodes :

}// end while
delete itNodes; //!!!Warning : do not forget to delete iterators (memory leak)

9. Iterating on edges (all edges).

Some times it can be useful to iterate on edges, for example in the algorithm of Kruskal. That is why the graph class owns the function Iterator<edge>* Graph::getEdges (const node) const, that return a pointer on an Iterator of type edge. Following is an exemple of its use.