next up previous contents
Next: Graphs of Graphs: Up: Arbitrary Graph Objects Via Previous: Implementing the Access Methods

A Run-time Example Using the ACCESS Class:

It is much cleaner to use this access function iterface. Consider the new implementation of the callback method run(). First we instantiate the access methods we need: we will be using type EdgeStruct for the edges and type NodeStruct for the nodes. This is done by constructing the appropriate objects via these code lines.

  //instantiate the access functions
  ACCESS_EDGESTRUCT *EdgeAccess = new ACCESS_EDGESTRUCT;
  ACCESS_NODESTRUCT *NodeAccess = new ACCESS_NODESTRUCT;

We then construct the graph object. Note the constructor call is shorter and more understandable and does not require unpleasant casts to the typedefed methods EQ_FN, DISP_FN and ENTRY_FN like before.

   
  if(T->T==NULL){
    printf("Building the XGraph object.\n");
    T->T = new Graph(EdgeAccess,NodeAccess,
                     T->radius,T->center,        //node circle
                     T->foreground,              //fg
                     T->background,              //bg
                     graph_colors,               //graph colors
                     T->display,                 //graphics
                     T->drawable,
                     T->pixmap,
                     T->pixmap2,
                     T->gc);          
    }

The rest of the file is as before: we construct vectors of appropriate data and load it into our graph object.

  
  cout << "Graph Object Constructed." << endl;  
  cout << "Checking Random Graph Generation" << endl;
  T->T->GenerateRandomGraph("RandomGraph");	     
  cout << "Checking file read method" << endl;   
  T->T->ReadInputFile("RandomGraph");
   
  int NumberNodes = T->T->NumberNodes();
  int NumberEdges = T->T->NumberEdges();
  cout << "Number of Nodes in the Graph is " << NumberNodes << endl;
  cout << "Number of Edges in the Graph is " << NumberEdges << endl;
  
  EdgeStruct *edge_data = new EdgeStruct[NumberEdges];
  NodeStruct *node_data = new NodeStruct[NumberNodes];
  
  for(int i = 0;i< NumberNodes; ++i){
    node_data[i].load = i;
    node_data[i].time = float(i)*float(i);
    node_data[i].V.x = i-1;
    node_data[i].V.y = i+1;
    }
  for(int i = 0;i< NumberEdges; ++i){
    edge_data[i].capacity = 10.0;
    edge_data[i].loss = -i;
    edge_data[i].time = float(i)*float(i);
    edge_data[i].V.x =10*i-1;
    edge_data[i].V.y =10*i+1;
    }
  
  T->T->FillData(node_data,sizeof(NodeStruct),edge_data,sizeof(EdgeStruct));

When we run this code on a simple graph of say 4 nodes and 3 edges, we obtain this output:

How many nodes do you want in the graph?
4
You have chosen 4 for this graph
We will generate a random number of edges between nodes. 
This number will be between a lower and upper bound.
Enter the lower bound for the number of edges:
1
Enter the upper bound for the number of edges:
1
Your design parameter is 1 < number of edges < 1
We will generate a random edge weight between chosen nodes.
This number will be between a lower and upper bound.
Enter the lower bound for the edge weight:
1
Enter the upper bound for the edge weight:
1
Your design parameter is 1 < edge_weight < 1
number_of_nodes = 4
Checking file read method
number_of_nodes = 4
number_of_edges = 3

Graph: 
Nodes: 
  n4 Load = 0
     Time = 0
     Vertex = (-1,1)
  n3 Load = 1
     Time = 1
     Vertex = (0,2)
  n2 Load = 2
     Time = 4
     Vertex = (1,3)
  n1 Load = 3
     Time = 9
     Vertex = (2,4)

  Edges:  n2-----(1)---> n1 Capacity = 10
                            Loss = 0
                            time = 0
                            Vertex = (-1,1)

          n3-----(1)---> n1 Capacity = 10
                            Loss = -1
                            time = 1
                            Vertex = (9,11)

          n4-----(1)---> n1 Capacity = 10
                            Loss = -2
                            time = 4
                            Vertex = (19,21)

End Graph


Jim Peterson
1999-05-17