grtest.cpp

Go to the documentation of this file.
00001 
00015 #include <iostream>
00016 #include <OpenAnalysis/Utils/DGraph/DGraphStandard.hpp>
00017 
00018 /*
00019 using std::endl;
00020 using std::cout;
00021 using std::cerr;
00022 */
00023 using namespace std;
00024 //using namespace OA;
00025 
00026 
00027 class myGraph : public OA::DGraph::DGraphStandard {
00028 public:
00029   class Node : public OA::DGraph::DGraphStandard::Node {
00030   public:
00031     Node (int v) : OA::DGraph::DGraphStandard::Node() { val = v; }
00032     int val;
00033   };
00034   class Edge : public OA::DGraph::DGraphStandard::Edge {
00035   public:
00036     Edge (Node* src, Node* sink, int v) : OA::DGraph::DGraphStandard::Edge(src, sink) { val = v; }
00037     int val;
00038   };
00039 
00040   myGraph (Node* root) : OA::DGraph::DGraphStandard (root) {}
00041 
00042   //int edge_count () { return edge_set.size(); }
00043   //int node_count () { return node_set.size(); }
00044   int edge_count () { return -1; }
00045   int node_count () { return -1; }
00046 };
00047 
00048 
00049 void list_DFS_nodes (myGraph&);
00050 void list_ReversePostDFS_nodes (myGraph&);
00051 void dump_graph (myGraph&);
00052 
00053 int
00054 main (int argc, char* argv[])
00055 {
00056   try {
00057     // must allocate on heap because the graph datastructures
00058     // manipulate Edge and Node ptrs, including calling delete
00059     myGraph::Node *n1 = new myGraph::Node(1);
00060     myGraph::Node *n2 = new myGraph::Node(2);
00061     myGraph::Node *n3 = new myGraph::Node(3);
00062     myGraph::Node *n4 = new myGraph::Node(4);
00063     myGraph::Edge *e1 = new myGraph::Edge(n1, n2, 1);
00064     myGraph::Edge *e2 = new myGraph::Edge(n1, n3, 1);
00065     myGraph::Edge *e3 = new myGraph::Edge(n2, n4, 1);
00066     myGraph::Edge *e4 = new myGraph::Edge(n3, n4, 1);
00067 
00068     myGraph g(n1);
00069 
00070     g.add(e1);
00071     g.add(e2);
00072     g.add(e3);
00073     g.add(e4);
00074 
00075     dump_graph(g);
00076     list_DFS_nodes(g);
00077     list_ReversePostDFS_nodes(g);
00078 //    cout << "Initial graph" << endl;
00079 //    dump_graph(g);
00080     g.remove(n2);
00081     cout << "After deleting node 2" << endl;
00082 //    //list_DFS_nodes(g);
00083     dump_graph(g);
00084 
00085     cout << "Graph 2" << endl;
00086     myGraph::Node *nn = new myGraph::Node(5);
00087     //  myGraph2::node nn2(1.3);
00088     myGraph g2(nn);
00089     cout << "number of edges in g = " << g.edge_count() << ", number of edges in g2 = " << g2.edge_count() << endl;
00090     cout << "number of nodes in g = " << g.node_count() << ", number of nodes in g2 = " << g2.node_count() << endl;
00091 
00092     //  g2.add_node(&nn2);
00093     //  g2.add_edge(new myGraph2::edge(&nn, &nn2, 2.5));
00094     //list_DFS_nodes(g2);
00095     dump_graph(g2);
00096 
00097     //    g2.add(&e4);
00098     g2.add(n3);
00099 
00100   }
00101   catch (OA::Exception& e) {
00102     e.report(cerr);
00103   }
00104 
00105 }
00106 
00107 
00108 void
00109 list_DFS_nodes (myGraph& g)
00110 {
00111   cout << "Enumerating Nodes in DFS order:" << endl;
00112   myGraph::DFSIterator iter(g);
00113   while ((bool)iter) {
00114     myGraph::Node* n = dynamic_cast<myGraph::Node*>(iter.current());
00115     cout << "\tnode " << n->val << endl;
00116     ++iter;
00117   }
00118 }
00119 
00120 void
00121 list_ReversePostDFS_nodes (myGraph& g)
00122 {
00123 
00124   cout << "Enumerating Nodes in ReversePostDFS order using local iter:" << endl;
00125   OA::DGraph::DGraphStandard::ReversePostDFSIterator iter(g,OA::DGraph::DEdgeOrg);
00126   while ((bool)iter) {
00127     myGraph::Node* n = dynamic_cast<myGraph::Node*>(iter.current());
00128     cout << "\tnode " << n->val << endl;
00129     ++iter;
00130   }
00131 
00132 
00133   cout << "Enumerating Nodes in ReversePostDFS order getReversePostDFSIterator:" << endl;
00134   OA::DGraph::Interface::ReversePostDFSIterator * iterPtr = g.getReversePostDFSIterator(OA::DGraph::DEdgeOrg);
00135   while ((bool)(*iterPtr)) {
00136     myGraph::Node* n = dynamic_cast<myGraph::Node*>(iterPtr->current());
00137     cout << "\tnode " << n->val << endl;
00138     ++(*iterPtr);
00139   }
00140 }
00141 
00142 void
00143 dump_graph (myGraph& g)
00144 {
00145   cout << "Enumerating all Nodes in arbitrary order:" << endl;
00146   myGraph::NodesIterator n_iter(g);
00147   while ((bool)n_iter) {
00148     //myGraph::Node* n = dynamic_cast<myGraph::Node*>((OA::DGraph::DGraphStandard::Node*)n_iter);
00149     myGraph::Node* n = dynamic_cast<myGraph::Node*>(n_iter.current());
00150     cout << "\tnode " << n << " = " << n->val << endl;
00151     ++n_iter;
00152   }
00153   cout << "Enumerating all Edges in arbitrary order:" << endl;
00154   myGraph::EdgesIterator e_iter(g);
00155   while ((bool)e_iter) {
00156     //myGraph::Edge* e = dynamic_cast<myGraph::Edge*>((OA::DGraph::DGraphStandard::Edge*)e_iter);
00157     myGraph::Edge* e = dynamic_cast<myGraph::Edge*>(e_iter.current());
00158     cout << "\tedge " << e << " = " << e->val << endl;
00159     ++e_iter;
00160   }
00161 }