Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

Graph.cpp

Go to the documentation of this file.
00001 /* ****************************************************************** **
00002 **    OpenSees - Open System for Earthquake Engineering Simulation    **
00003 **          Pacific Earthquake Engineering Research Center            **
00004 **                                                                    **
00005 **                                                                    **
00006 ** (C) Copyright 1999, The Regents of the University of California    **
00007 ** All Rights Reserved.                                               **
00008 **                                                                    **
00009 ** Commercial use of this program without express permission of the   **
00010 ** University of California, Berkeley, is strictly prohibited.  See   **
00011 ** file 'COPYRIGHT'  in main directory for information on usage and   **
00012 ** redistribution,  and for a DISCLAIMER OF ALL WARRANTIES.           **
00013 **                                                                    **
00014 ** Developed by:                                                      **
00015 **   Frank McKenna (fmckenna@ce.berkeley.edu)                         **
00016 **   Gregory L. Fenves (fenves@ce.berkeley.edu)                       **
00017 **   Filip C. Filippou (filippou@ce.berkeley.edu)                     **
00018 **                                                                    **
00019 ** ****************************************************************** */
00020                                                                         
00021 // $Revision: 1.1.1.1 $
00022 // $Date: 2000/09/15 08:23:21 $
00023 // $Source: /usr/local/cvs/OpenSees/SRC/graph/graph/Graph.cpp,v $
00024                                                                         
00025                                                                         
00026 // File: ~/graph/graph/Graph.C
00027 // 
00028 // Written: fmk 
00029 // Created: 11/96
00030 // Revision: A
00031 //
00032 // Description: This file contains the class implementation for Graph.
00033 //
00034 // What: "@(#) Graph.C, revA"
00035 
00036 #include <Graph.h>
00037 #include <Vertex.h>
00038 #include <VertexIter.h>
00039 #include <iostream.h>
00040 #include <ArrayOfTaggedObjects.h>
00041 
00042 Graph::Graph()
00043 :myVertices(0), theVertexIter(0), numEdge(0)
00044 {
00045     myVertices = new ArrayOfTaggedObjects(32);
00046     theVertexIter = new VertexIter(myVertices);
00047 }
00048 
00049 
00050 Graph::Graph(int numVertices)
00051 :myVertices(0), theVertexIter(0), numEdge(0)
00052 {
00053     myVertices = new ArrayOfTaggedObjects(numVertices);
00054     theVertexIter = new VertexIter(myVertices);
00055 }
00056 
00057 
00058 Graph::Graph(TaggedObjectStorage &theVerticesStorage)
00059 :myVertices(&theVerticesStorage), theVertexIter(0), numEdge(0)
00060 {
00061     theVerticesStorage.clearAll();
00062     theVertexIter = new VertexIter(myVertices);
00063 }
00064     
00065 
00066 Graph::~Graph()
00067 {
00068     // invoke delete on the Vertices
00069     myVertices->clearAll();
00070     
00071     if (myVertices != 0)
00072  delete myVertices;
00073     
00074     if (theVertexIter != 0)
00075  delete theVertexIter;
00076 }
00077 
00078 
00079 // bool addVertex(int vertexTag, int vertexRef, 
00080 //   int vertexWeight=0, int vertexColor = 0) 
00081 // Method to add a vertex to the graph. If the adjacency list
00082 // of the vertex is not empty the graph will first check to see all
00083 // vertices in the the the vertices adjacency list exist in the graph
00084 // before the vertex is added. 
00085 
00086 bool
00087 Graph::addVertex(Vertex *vertexPtr, bool checkAdjacency)
00088 {
00089     // check the vertex * and its adjacency list
00090     if (vertexPtr == 0) {
00091  cerr << "WARNING ArrayGraph::addVertex";
00092  cerr << " - attempting to add a NULL vertex*\n";
00093  return false;
00094     }
00095 
00096     if (checkAdjacency == true) {
00097  if (vertexPtr->getDegree() != 0) {
00098      const ID &adjacency = vertexPtr->getAdjacency();
00099      int size = adjacency.Size();
00100      for (int i=0; i<size; i++) {
00101   Vertex *other = this->getVertexPtr(adjacency(i));
00102   if (other == 0) {
00103       cerr << "WARNING ArrayGraph::addVertex";
00104       cerr << " - vertex with adjacent vertex not in graph\n";
00105       return false;
00106   }  
00107      }
00108  }
00109     }
00110 
00111     bool result = myVertices->addComponent(vertexPtr);
00112     if (result == false) {
00113  cerr << "WARNING ArrayGraph::addVertex";
00114  cerr << " - vertex could not be stored in TaggedObjectStorage object\n";
00115     }
00116     return result;
00117 }
00118 
00119 
00120 // int addEdge(int vertexTag, int otherVertexTag);
00121 // Causes the Graph to add an edge {\em (vertexTag,otherVertexTag)} to
00122 // the Graph. A check is first made to see if vertices with tags given by
00123 // {\em vertexTag} and {\em otherVertexTag} exist in the graph. If they
00124 // do not exist a $-1$ is returned, otherwise the method invokes {\em
00125 // addEdge()} on each of the corresponding vertices in the 
00126 // graph. Returns $0$ if sucessfull, a negative number if not.
00127 
00128 int 
00129 Graph::addEdge(int vertexTag, int otherVertexTag)
00130 {
00131     // get pointers to the vertices, if one does not exist return
00132 
00133     Vertex *vertex1 = this->getVertexPtr(vertexTag);
00134     Vertex *vertex2 = this->getVertexPtr(otherVertexTag);
00135     if ((vertex1 == 0) || (vertex2 == 0)) {
00136  cerr << "WARNING Graph::addEdge() - one or both of the vertices ";
00137  cerr << vertexTag << " " << otherVertexTag << " not in Graph\n";
00138  return -1;
00139     }
00140 
00141     // add an edge to each vertex
00142     int result;
00143     if ((result = vertex1->addEdge(otherVertexTag)) == 0) {
00144  if ((result = vertex2->addEdge(vertexTag)) == 0) 
00145      numEdge++;
00146  else {
00147      cerr << "WARNING Graph::addEdge() - " << vertexTag;
00148      cerr << " has not been added to " << otherVertexTag;
00149      cerr << " adjacency - yet vica-versa ok.\n";
00150      return -2;
00151  }
00152     }
00153 
00154     return result;
00155 }
00156 
00157 
00158 Vertex *
00159 Graph::getVertexPtr(int vertexTag)
00160 {
00161     TaggedObject *res = myVertices->getComponentPtr(vertexTag);
00162     if (res == 0) return 0;
00163     Vertex *result = (Vertex *)res;
00164     return result;
00165 }
00166 
00167 
00168 VertexIter &
00169 Graph::getVertices(void) 
00170 {
00171     // reset the iter and then return it
00172     theVertexIter->reset();
00173     return *theVertexIter;
00174 }
00175 
00176 
00177 int 
00178 Graph::getNumVertex(void) const
00179 {
00180     return myVertices->getNumComponents();
00181 }
00182 
00183 int 
00184 Graph::getNumEdge(void) const
00185 {
00186     return numEdge;
00187 }
00188 
00189 Vertex *
00190 Graph::removeVertex(int tag, bool flag)
00191 {
00192     TaggedObject *mc = myVertices->removeComponent(tag);
00193     if (mc == 0) return 0;
00194     Vertex *result = (Vertex *)mc;
00195     
00196     if (flag == true) { // remove all edges associated with the vertex
00197  cerr << "Graph::removeVertex(int tag, bool flag = true)";
00198  cerr << " - no code to remove edges yet\n";
00199  return 0;
00200     }
00201     return result;
00202 }
00203 
00204 
00205 
00206 
00207 void 
00208 Graph::Print(ostream &s, int flag)
00209 {
00210     myVertices->Print(s, flag);
00211 }
00212 
00213 
00214 ostream &operator<<(ostream &s, Graph &M)
00215 {
00216   M.Print(s);
00217   return s;
00218 }
00219 
Copyright Contact Us