00001 #ifndef LEDACOMPARISONS_H
00002 #define LEDACOMPARISONS_H
00003
00004 #include <Utilities/mersenneTwister.h>
00005 #include <Structs/Graphs/dynamicGraph.h>
00006 #include <LEDA/graph/graph_iterator.h>
00007 #include <fstream>
00008 #include <vector>
00009
00010 template< typename GraphType, class Vtype, class Etype>
00011 void graphFromLEDA( GraphType& G,
00012 leda::GRAPH< Vtype, Etype>& ledaGraph,
00013 leda::node_array<typename GraphType::NodeDescriptor>& map)
00014 {
00015 typedef typename GraphType::NodeIterator NodeIterator;
00016 typedef typename GraphType::EdgeIterator EdgeIterator;
00017 typedef typename GraphType::NodeDescriptor NodeDescriptor;
00018 typedef typename GraphType::EdgeDescriptor EdgeDescriptor;
00019
00020 leda::node ledaNode;
00021 leda::edge ledaEdge;
00022 NodeIterator u;
00023 EdgeIterator e;
00024 NodeDescriptor uDesc;
00025 EdgeDescriptor eDesc;
00026
00027 unsigned int numnodes = ledaGraph.number_of_nodes();
00028 unsigned int numedges = ledaGraph.number_of_edges();
00029 unsigned int i = 0;
00030
00031 std::cout << "\rCopying nodes..." << std::flush;
00032 for (leda::NodeIt ledaNode(ledaGraph); ledaNode.valid(); ledaNode++)
00033 {
00034
00035
00036
00037
00038
00039 uDesc = G.insertNode();
00040 map[ledaNode] = uDesc;
00041
00042
00043
00044 u = G.getNodeIterator( uDesc);
00045
00046
00047
00048
00049
00050
00051 if( i % (numnodes/100) == 0)
00052 {
00053 std::cout << "\rCopying nodes..." << i / (numnodes/100) << "%" << std::flush;
00054 }
00055
00056 ++i;
00057 }
00058 std::cout << "\rCopying nodes...done!\n";
00059
00060 i = 0;
00061
00062 forall_edges( ledaEdge, ledaGraph)
00063 {
00064
00065
00066
00067
00068
00069 NodeIterator u = G.getNodeIterator(map[ leda::source(ledaEdge)]);
00070 NodeIterator v = G.getNodeIterator(map[ leda::target(ledaEdge)]);
00071
00072
00073
00074
00075 eDesc = G.insertEdge( map[ leda::source(ledaEdge)], map[ leda::target(ledaEdge)]);
00076
00077 e = G.getEdgeIterator( eDesc);
00078
00079
00080
00081
00082
00083
00084 if( i % (numedges/100) == 0)
00085 {
00086 std::cout << "\rCopying edges..." << i / (numedges/100) << "%" << std::flush;
00087 }
00088
00089 ++i;
00090 }
00091 std::cout << "\rCopying edges...done!\n";
00092 }
00093
00094
00095 template< typename GraphType, class Vtype, class Etype>
00096 bool compareGraphs( GraphType& G,
00097 leda::GRAPH< Vtype, Etype>& ledaGraph,
00098 leda::node_array<typename GraphType::NodeDescriptor>& map)
00099 {
00100
00101 typedef typename GraphType::NodeIterator NodeIterator;
00102 typedef typename GraphType::EdgeIterator EdgeIterator;
00103 typedef typename GraphType::NodeDescriptor NodeDescriptor;
00104
00105 leda::edge ledaEdge;
00106 EdgeIterator e,end;
00107 bool found;
00108
00109 unsigned int numedges = ledaGraph.number_of_edges();
00110 unsigned int i = 0;
00111
00112 G.addNodeProperty("marked",false);
00113 G.addEdgeProperty("marked",false);
00114
00115 forall_edges( ledaEdge, ledaGraph)
00116 {
00117 NodeIterator u = G.getNodeIterator(map[ leda::source(ledaEdge)]);
00118 NodeIterator v = G.getNodeIterator(map[ leda::target(ledaEdge)]);
00119
00120
00121
00122 found = false;
00123 end = G.endEdges(u);
00124 for( e = G.beginEdges(u); e != end; ++e)
00125 {
00126 if ( v == G.opposite( u, e))
00127 {
00128 G.setProperty( e, "marked", true);
00129 found = true;
00130 break;
00131 }
00132 }
00133
00134
00135
00136 if( !found) return false;
00137
00138 if( i % (numedges/100) == 0)
00139 {
00140 std::cout << "\rChecking edges..." << i / (numedges/100) << "%" << std::flush;
00141 }
00142
00143 ++i;
00144 }
00145 std::cout << "\rChecking LEDA edges...done!\n";
00146
00147 i = 0;
00148 NodeIterator lastNode = G.endNodes();
00149 for( NodeIterator u = G.beginNodes(); u != lastNode; ++u)
00150 {
00151 end = G.endEdges(u);
00152 for( e = G.beginEdges(u); e != end; ++e)
00153 {
00154 if( G.getProperty( e, "marked") == false)
00155 {
00156
00157 return false;
00158 }
00159 if( i % (numedges/100) == 0)
00160 {
00161 std::cout << "\rChecking marked edges..." << i / (numedges/100) << "%" << std::flush;
00162 }
00163
00164 ++i;
00165 }
00166 }
00167
00168 std::cout << "\rChecking marked edges...done!\nGraphs are the same!\n";
00169 G.removeNodeProperty("marked");
00170 G.removeEdgeProperty("marked");
00171
00172 return true;
00173 }
00174
00175
00176
00177
00178 template< typename GraphType, class Vtype, class Etype>
00179 void readGraphwin( GraphType& G, const std::string& filename)
00180 {
00181 typedef typename GraphType::NodeIterator NodeIterator;
00182 typedef typename GraphType::EdgeIterator EdgeIterator;
00183 typedef typename GraphType::NodeDescriptor NodeDescriptor;
00184 typedef typename GraphType::SizeType SizeType;
00185
00186 std::ifstream in(filename.c_str());
00187 SizeType numnodes, numedges, source, target;
00188 std::vector<NodeDescriptor> V;
00189
00190 for( unsigned int i = 0; i < 4; i++)
00191 {
00192 in.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
00193 }
00194
00195 in >> numnodes;
00196 std::cout << "\rReading nodes..." << std::flush;
00197 for( SizeType i = 0; i < numnodes + 1; i++)
00198 {
00199
00200
00201
00202
00203
00204 V.push_back(G.insertNode());
00205 in.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
00206
00207
00208
00209
00210 }
00211 std::cout << "\rReading nodes...done!\n";
00212
00213
00214 in >> numedges;
00215 std::cout << "\rReading edges..." << std::flush;
00216 for( SizeType i = 0; i < numedges; i++)
00217 {
00218
00219
00220
00221
00222
00223 in >> source;
00224 in >> target;
00225 G.insertEdge( V[source], V[target]);
00226 in.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
00227 if( i % (numedges/100) == 0)
00228 {
00229 std::cout << "\rReading edges..." << i / (numedges/100) << "%" << std::flush;
00230 }
00231 }
00232 std::cout << "\rReading edges...done!\n";
00233 in.close();
00234 }
00235
00236
00237 #endif //LEDACOMPARISONS_H