next up previous contents
Next: Adding Visualization to the Up: The General Graph Class: Previous: The Graph Class Implementation:

Run Time Results:

Consider the following small fragment of code that illustrates how the graph model works using manual construction techniques.

#include "appgraph.h"

int main(void)
{
   Node T;
   cout << "Input the Node:" << endl;
   cin >> T;
   cout << "Node is " << T << endl;
   cout << "Node name is " << T.name() << endl;
   
   Edge E;
   cout << "Input the Edge:" << endl;
   cin >> E;
   cout << "Edge E is " << E << endl;
   cout << "Edge weight is " << E.weight() << endl;
   
   Graph g;
   Gnode *n1 = g.addNode("Quinn");
   Gnode *n2 = g.addNode("Qaitlin");
   Gnode *n3 = g.addNode("Pauli");
   
   g.addEdge(n2,n1,4.0);
   g.addEdge(n1,n3,5.0);
   g.addEdge(n3,n2,1.0);
   
   g.addNode("Jim");
   g.addNode("Clive");
   
   g.addEdge(g.findNode("Qaitlin"),g.findNode("Jim"),6.0);
   g.addEdge(g.findNode("Pauli"),g.findNode("Clive"),3.0);
   g.addEdge(g.findNode("Pauli"),g.findNode("Jim"),4.5);
   g.addEdge(g.findNode("Pauli"),g.findNode("Quinn"),3.5);
   g.addEdge(g.findNode("Pauli"),g.findNode("Pauli"),14.5);   
   
   cout << g;
   return 0;
}

First, we construct a node T and use the overloaded cin operator for the node class to input the value of the name of this node

   Node T;
   cout << "Input the Node:" << endl;
   cin >> T;
   cout << "Node is " << T << endl;
   cout << "Node name is " << T.name() << endl;

Next, we construct and edge with a weight we choose using the overloaded cin for the edge class.

  
   Edge E;
   cout << "Input the Edge:" << endl;
   cin >> E;
   cout << "Edge E is " << E << endl;
   cout << "Edge weight is " << E.weight() << endl;

The nest lines construct a graph and add three nodes to it.

   Graph g;
   Gnode *n1 = g.addNode("Quinn");
   Gnode *n2 = g.addNode("Qaitlin");
   Gnode *n3 = g.addNode("Pauli");

We then add edges and more nodes. Note how we use the findNode method here.

   g.addEdge(n2,n1,4.0);
   g.addEdge(n1,n3,5.0);
   g.addEdge(n3,n2,1.0);
   
   g.addNode("Jim");
   g.addNode("Clive");
   
   g.addEdge(g.findNode("Qaitlin"),g.findNode("Jim"),6.0);
   g.addEdge(g.findNode("Pauli"),g.findNode("Clive"),3.0);
   g.addEdge(g.findNode("Pauli"),g.findNode("Jim"),4.5);
   g.addEdge(g.findNode("Pauli"),g.findNode("Quinn"),3.5);
   g.addEdge(g.findNode("Pauli"),g.findNode("Pauli"),14.5);

Finally, we output the entire graph using the overloaded cout for the graph class.

   cout << g;

When this code is compiled into the executible graphs_shared and run, it generates the following output

Input the Node:
Please enter the character that will terminate the input string you want to use for a name
Z
Now enter the name you want to use for the node:
Tootsie
Z
Node is Tootsie

Node name is Tootsie
Input the Edge:
Please enter the edge weight:
123.5
Edge E is 123.5

Edge weight is 123.5
Graph: 
Nodes: 
  Clive  Jim  Pauli  Qaitlin  Quinn
  Edges:  Pauli-----(14.5)---> Pauli
          Pauli-----(3.5)---> Quinn
          Pauli-----(4.5)---> Jim
          Pauli-----(3)---> Clive
          Qaitlin-----(6)---> Jim
          Pauli-----(1)---> Qaitlin
          Quinn-----(5)---> Pauli
          Qaitlin-----(4)---> Quinn
End Graph 
[peterson@ozymanius App]$

Note the overloaded cin operator for the node class asks you for a single character which will be used to terminate the node entry mode.


Jim Peterson
1999-05-17