Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
shortest_paths.cpp
Go to the documentation of this file.
1/*
2 * vim: ts=4 sw=4 et tw=0 wm=0
3 *
4 * libcola - A library providing force-directed network layout using the
5 * stress-majorization method subject to separation constraints.
6 *
7 * Copyright (C) 2006-2008 Monash University
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library in the file LICENSE; if not,
21 * write to the Free Software Foundation, Inc., 59 Temple Place,
22 * Suite 330, Boston, MA 02111-1307 USA
23 *
24*/
25
26#include <fstream>
27#include <iostream>
28#include <iomanip>
29//#define TEST_AGAINST_BOOST
30#ifdef TEST_AGAINST_BOOST
31#include <boost/config.hpp>
32#include <boost/property_map.hpp>
33#include <boost/graph/adjacency_list.hpp>
34#include <boost/graph/graphviz.hpp>
35#include <boost/graph/johnson_all_pairs_shortest.hpp>
36using namespace boost;
37#endif // TEST_AGAINST_BOOST
39#include <cmath>
40#include <time.h>
41#include <assert.h>
42
43using namespace std;
44// creates a (not necessarily connected random graph) with n nodes.
45vector<shortest_paths::Edge> random_graph(unsigned n) {
46 vector<shortest_paths::Edge> edges;
47 for(unsigned i=0;i<n;i++) {
48 for(unsigned j=i+1;j<n;j++) {
49 double r=(double)rand()/(double)RAND_MAX;
50 if(r < 0.1) {
51 edges.push_back(make_pair(i,j));
52 }
53 }
54 }
55
56 return edges;
57}
58clock_t lastTime;
59static void resetClock() {
60 lastTime=clock();
61}
62static double getRunTime() {
63 clock_t time = clock()-lastTime;
64 return (double)time/(double)CLOCKS_PER_SEC;
65}
66
67int
68main(void)
69{
70 bool dump=false;
71 srand(time(0));
72#ifdef TEST_AGAINST_BOOST
73 typedef adjacency_list<vecS, vecS, undirectedS, no_property,
74 property< edge_weight_t, double, property< edge_weight2_t, double > > > Graph;
75 Graph g;
76#endif
77 unsigned V = 100;
78 vector<shortest_paths::Edge> es = random_graph(V);
79 unsigned E=es.size();
80 cout << " Test graph |V|="<<V<<",|E|="<<E<<endl;
81 valarray<double> weights(E);
82 for(unsigned i=0;i<E;i++) {
83 weights[i]=round((static_cast<double>(rand())/static_cast<double>(RAND_MAX))*10);
84#ifdef TEST_AGAINST_BOOST
85 add_edge(es[i].first,es[i].second,weights[i],g);
86#endif
87 }
88
89#ifdef TEST_AGAINST_BOOST
90 vector < double >d(V, (numeric_limits < double >::max)());
91 typedef vector<double> weight_vec;
92 vector<weight_vec> D(V,weight_vec(V));
93 cout<<"Running boost::johnson_all_pairs_shortest_paths..."<<endl;
94 resetClock();
95 johnson_all_pairs_shortest_paths(g, D, distance_map(&d[0]));
96 cout<<" ...done, time="<<getRunTime()<<endl;
97#endif
98 double** D1=new double*[V];
99 for(unsigned i=0;i<V;i++) {
100 D1[i]=new double[V];
101 }
102 cout<<"Running shortest_paths::johnsons..."<<endl;
103 resetClock();
104 shortest_paths::johnsons(V,D1,es,weights);
105 cout<<" ...done, time="<<getRunTime()<<endl;
106 double** D2=new double*[V];
107 for(unsigned i=0;i<V;i++) {
108 D2[i]=new double[V];
109 }
110 cout<<"Running shortest_paths::floyd_warshall..."<<endl;
111 resetClock();
113 cout<<" ...done, time="<<getRunTime()<<endl;
114
115 for (unsigned int i = 0; i < V; ++i) {
116 if(dump) cout << i << " -> ";
117 for (unsigned int j = 0; j < V; ++j) {
118 if(dump) cout << setw(5) << D1[i][j];
119 assert(D1[i][j]==D2[i][j]);
120#ifdef TEST_AGAINST_BOOST
121 assert(D[i][j]==D2[i][j]);
122#endif
123 }
124 if(dump) cout << endl;
125 }
126#ifdef TEST_AGAINST_BOOST
127 if(dump) {
128 ofstream fout("figs/johnson-eg.dot");
129 fout << "graph A {\n" << "node[shape=\"circle\"]\n";
130
131 graph_traits < Graph >::edge_iterator ei, ei_end;
132 for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
133 fout << source(*ei, g) << " -- " << target(*ei, g)
134 << "[label=" << get(edge_weight, g)[*ei] << "]\n";
135
136 fout << "}\n";
137 }
138#endif
139 return 0;
140}
vector< Edge > es
void johnsons(unsigned const n, T **D, std::vector< Edge > const &es, std::valarray< T > const &eweights=std::valarray< T >())
find all pairs shortest paths, faster, uses dijkstra
void floyd_warshall(unsigned const n, T **D, std::vector< Edge > const &es, std::valarray< T > const &eweights=std::valarray< T >())
find all pairs shortest paths, n^3 dynamic programming approach
STL namespace.
Edges edges(Path const &p, Crossings const &cr, unsigned ix)
Definition sanitize.cpp:36
static void resetClock()
vector< shortest_paths::Edge > random_graph(unsigned n)
int main(void)
static double getRunTime()
clock_t lastTime