Grafalgo
Library of useful data structures and algorithms
/Users/jst/src/grafalgo/cpp/include/Graph.h
Go to the documentation of this file.
00001 
00009 #ifndef GRAPH_H
00010 #define GRAPH_H
00011 
00012 #include "Adt.h"
00013 #include "Util.h"
00014 #include "List.h"
00015 #include "ClistSet.h"
00016 #include "SetPair.h"
00017 #include "HashSet.h"
00018 #include "Dheap.h"
00019 #include <list>
00020 #include <vector>
00021 
00022 namespace grafalgo {
00023 
00024 typedef int vertex;
00025 typedef int edge;
00026 
00036 class Graph : public Adt {
00037 public:         Graph(int=1,int=1);
00038                 ~Graph();
00039 
00040         // common methods
00041         void    clear();
00042         void    resize(int numv) { resize(numv, numv); }
00043         virtual void resize(int, int);
00044         void    expand(int numv) { expand(numv, max(numv,m())); }
00045         virtual void expand(int, int);
00046         void    copyFrom(const Graph&);
00047 
00048         // number of edges
00049         int     m() const;      
00050 
00051         // predicates
00052         bool    validVertex(int) const;
00053         bool    validEdge(int) const;
00054 
00055         // methods for iterating through overall edge list, adjacency lists
00056         edge    first() const;
00057         edge    next(edge) const;
00058         virtual edge firstAt(vertex) const;
00059         virtual edge nextAt(vertex,edge) const;
00060 
00061         // methods for accessing edge endpoints and length
00062         vertex  left(edge) const;       
00063         vertex  right(edge) const;      
00064         vertex  mate(vertex,edge) const;
00065         edge    getEdge(vertex,vertex) const;
00066 
00067         // methods for adding/removing edges
00068         virtual edge join(vertex,vertex);       
00069         bool    remove(edge);   
00070 
00071         // methods for computing properties
00072         int     getComponents(int*) const;
00073 
00074         // input
00075         friend istream& operator>>(istream&, Graph&);
00076 
00077         // create a string representation
00078         virtual string& edge2string(edge,string&) const;
00079         virtual string& edge2string(edge,vertex,string&) const;
00080         virtual string& toString(string&) const;
00081         virtual string& toDotString(string&) const;
00082         virtual string& elist2string(list<int>&, string&) const;
00083 
00084         void    sortAdjLists();
00085 
00086         // methods for creating random graphs
00087         void    scramble();
00088         void    rgraph(int, int, int);
00089         void    rgraph(int, int);
00090         void    addEdges(int);
00091         void    rbigraph(int, int);
00092         void    rtree(int);
00093         void    rcgraph(int,int);
00094 protected:
00095         int     mm;                     
00096         int     maxEdge;                
00097 
00098         edge    *fe;                    
00099 
00100         struct EdgeInfo {
00101         vertex  l;                      
00102         vertex  r;                      
00103         };
00104         EdgeInfo *evec;                 
00105 
00106         SetPair *edges;                 
00107 
00108         ClistSet *adjLists;             
00109 
00110 
00111 
00112 
00113         // internal helper methods
00114         void    makeSpace(int, int);
00115         void    freeSpace();
00116         virtual edge joinWith(vertex,vertex,edge);      
00117         void    shuffle(int*, int*);
00118         virtual bool readAdjList(istream&);
00119         virtual string& adjList2string(vertex,string&) const;
00120 
00121         // methods for sorting ajacency lists
00122         int     ecmp(edge, edge, vertex) const;
00123         void    sortAlist(vertex);
00124 };
00125 
00129 inline int Graph::m() const { return mm; }
00130 
00135 inline bool Graph::validVertex(int u) const { return 1 <= u && u <= n(); }
00136 
00141 inline bool Graph::validEdge(int e) const { return edges->isIn(e); }
00142 
00146 inline edge Graph::first() const { return edges->firstIn(); }
00147 
00153 inline edge Graph::next(edge e) const { return edges->nextIn(e); }
00154 
00159 inline edge Graph::firstAt(vertex v) const { 
00160         assert(1 <= v && v <= n());
00161         return fe[v]/2;
00162 }
00163 
00170 inline edge Graph::nextAt(vertex v, edge e) const {
00171         assert(1 <= v && v <= n() && 1 <= e && e <= maxEdge);
00172         if (v != evec[e].l && v != evec[e].r) return 0;
00173         int ee = (v == evec[e].l ? 2*e : 2*e+1);
00174         int ff = adjLists->suc(ee);
00175         return (fe[v] == ff ? 0 : ff/2);
00176 }
00177 
00182 inline vertex Graph::left(edge e) const {
00183         assert(0 <= e && e <= maxEdge);
00184         return evec[e].l;
00185 }
00186 
00191 inline vertex Graph::right(edge e) const {
00192         assert(0 <= e && e <= maxEdge);
00193         return (evec[e].l == 0 ? 0 : evec[e].r);
00194 }
00195 
00202 inline vertex Graph::mate(vertex v, edge e) const {
00203         assert(1 <= v && v <= n() && 1 <= e && e <= maxEdge);
00204         return (evec[e].l == 0 ?
00205                 0 : (v == evec[e].l ?
00206                      evec[e].r : (v == evec[e].r ? evec[e].l : 0)));
00207 }
00208 
00209 } // ends namespace
00210 
00211 #endif
 All Classes Files Functions Variables Typedefs Friends