TSPLIB
src/ValidateGraph.cpp
Go to the documentation of this file.
00001 
00012 #include "ValidateGraph.hpp"
00013 
00014 using namespace std;
00015 
00016 
00017 Graph::Graph(const std::vector<std::vector<double> > &adjacencyMatrix) {
00018         if (adjacencyMatrix.size() < 1) {
00019                 throw GraphNotValid();
00020         }
00021 
00022         this->adjacencyMatrix = adjacencyMatrix;
00023         n = adjacencyMatrix.size();
00024 
00025         if ((adjacencyMatrix.at(0).size() == 1) || (adjacencyMatrix.at(0).size() < n)) {
00026                 isUndirected = true;
00027 
00028                 //size check
00029                 for (vector<vector<double> >::size_type i = 0; i < n; i++) {
00030                         if (adjacencyMatrix.at(i).size() != i + 1) {
00031                                 throw GraphNotValid();
00032                         }
00033                 }
00034         }
00035         else {
00036                 isUndirected = false;
00037 
00038                 //size check
00039                 for (vector<vector<double> >::size_type i = 0; i < n; i++) {
00040                         if (adjacencyMatrix.at(i).size() != n) {
00041                                 throw GraphNotValid();
00042                         }
00043                 }
00044         }
00045 
00046 }
00047 
00048 Graph::Graph(const Graph &graph) {
00049         isUndirected = graph.getIsUndirected();
00050 
00051         n = graph.getN();
00052 
00053         adjacencyMatrix.resize(n);
00054 
00055         if (isUndirected) {
00056                 for (vector<vector<double> >::size_type i = 0; i < n; i++) {
00057                         adjacencyMatrix.at(i).resize(i + 1);
00058                         for (
00059                                         vector<double>::size_type j = 0;
00060                                         j <= static_cast<vector<double>::size_type>(i);
00061                                         j++) {
00062                                 setAdjacencyMatrixElement(i, j, graph.getAdjacencyMatrixElement(i, j));
00063                         }
00064                 }
00065         }
00066         else {
00067                 for (vector<vector<double> >::size_type i = 0; i < n; i++) {
00068                         adjacencyMatrix.at(i).resize(n);
00069                         for (
00070                                         vector<double>::size_type j = 0;
00071                                         j < static_cast<vector<double>::size_type>(n);
00072                                         j++) {
00073                                 setAdjacencyMatrixElement(i, j, graph.getAdjacencyMatrixElement(i, j));
00074                         }
00075                 }
00076         }
00077 }
00078 
00079 Graph &Graph::operator=(const Graph &graph) {
00080         if (this == &graph) {
00081                 return (*this);
00082         }
00083         else {
00084                 isUndirected = graph.getIsUndirected();
00085 
00086                 n = graph.getN();
00087 
00088                 adjacencyMatrix.resize(n);
00089 
00090                 if (isUndirected) {
00091                         for (vector<vector<double> >::size_type i = 0; i < n; i++) {
00092                                 adjacencyMatrix.at(i).resize(i + 1);
00093                                 for (
00094                                                 vector<double>::size_type j = 0;
00095                                                 j <= static_cast<vector<double>::size_type>(i);
00096                                                 j++) {
00097                                         setAdjacencyMatrixElement(i, j, graph.getAdjacencyMatrixElement(i, j));
00098                                 }
00099                         }
00100                 }
00101                 else {
00102                         for (vector<vector<double> >::size_type i = 0; i < n; i++) {
00103                                 adjacencyMatrix.at(i).resize(n);
00104                                 for (
00105                                                 vector<double>::size_type j = 0;
00106                                                 j < static_cast<vector<double>::size_type>(n);
00107                                                 j++) {
00108                                         setAdjacencyMatrixElement(i, j, graph.getAdjacencyMatrixElement(i, j));
00109                                 }
00110                         }
00111                 }
00112 
00113                 return (*this);
00114         }
00115 }
00116 
00117 std::ostream &operator<<(std::ostream &os, Graph &graph) {
00118         os << "<";
00119         os << "isUndirected: " << (graph.getIsUndirected() ? "true" : "false") << endl;
00120         os << "n: " << graph.getN() << endl;
00121         os << "adjacencyMatrix: ";
00122         if (graph.getIsUndirected()) {
00123                 for (vector<vector<double> >::size_type i = 0; i < graph.getN(); i++) {
00124                         string s = "[";
00125                         for (
00126                                         vector<double>::size_type j = 0;
00127                                         j <= static_cast<vector<double>::size_type>(i);
00128                                         j++) {
00129                                 ostringstream costOStringstream;
00130                                 costOStringstream << graph.getEdgeCost(i, j);
00131                                 s.append(costOStringstream.str());
00132                                 if (j < static_cast<vector<double>::size_type>(i)) {
00133                                         s.append(", ");
00134                                 }
00135                                 else {
00136                                         s.append("]");
00137                                 }
00138                         }
00139                         os << s;
00140                         if (i < graph.getN() - 1) {
00141                                 os << endl;
00142                         }
00143                 }
00144         }
00145         else {
00146                 for (vector<vector<double> >::size_type i = 0; i < graph.getN(); i++) {
00147                         string s = "[";
00148                         for (
00149                                         vector<double>::size_type j = 0;
00150                                         j < static_cast<vector<double>::size_type>(graph.getN());
00151                                         j++) {
00152                                 ostringstream costOStringstream;
00153                                 costOStringstream << graph.getEdgeCost(i, j);
00154                                 s.append(costOStringstream.str());
00155                                 if (j < static_cast<vector<double>::size_type>(graph.getN() - 1)) {
00156                                         s.append(", ");
00157                                 }
00158                                 else {
00159                                         s.append("]");
00160                                 }
00161                         }
00162                         os << s;
00163                         if (i < graph.getN() - 1) {
00164                                 os << endl;
00165                         }
00166                 }
00167         }
00168         os << ">" << endl;
00169         return (os);
00170 }
 All Classes Files Functions Variables Friends Defines