alpar@1956
|
1 |
/* -*- C++ -*-
|
alpar@1956
|
2 |
*
|
alpar@1956
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
alpar@1956
|
4 |
*
|
alpar@2553
|
5 |
* Copyright (C) 2003-2008
|
alpar@1956
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@1956
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
alpar@1956
|
8 |
*
|
alpar@1956
|
9 |
* Permission to use, modify and distribute this software is granted
|
alpar@1956
|
10 |
* provided that this copyright notice appears in all copies. For
|
alpar@1956
|
11 |
* precise terms see the accompanying LICENSE file.
|
alpar@1956
|
12 |
*
|
alpar@1956
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@1956
|
14 |
* express or implied, and with no claim as to its suitability for any
|
alpar@1956
|
15 |
* purpose.
|
alpar@1956
|
16 |
*
|
alpar@1956
|
17 |
*/
|
alpar@742
|
18 |
|
alpar@1632
|
19 |
#include<queue>
|
alpar@2569
|
20 |
#include<lemon/math.h>
|
alpar@921
|
21 |
#include<lemon/smart_graph.h>
|
alpar@742
|
22 |
#include"bench_tools.h"
|
alpar@742
|
23 |
|
alpar@742
|
24 |
using namespace std;
|
alpar@921
|
25 |
using namespace lemon;
|
alpar@742
|
26 |
|
alpar@742
|
27 |
inline int numOfOnes(int n,int dim)
|
alpar@742
|
28 |
{
|
alpar@742
|
29 |
int s=0;
|
alpar@742
|
30 |
for(int i=0;i<dim;i++) {
|
alpar@742
|
31 |
s+=n%2;
|
alpar@742
|
32 |
n>>=1;
|
alpar@742
|
33 |
}
|
alpar@742
|
34 |
return s;
|
alpar@742
|
35 |
}
|
alpar@742
|
36 |
|
alpar@742
|
37 |
inline int numOfZeros(int n,int dim)
|
alpar@742
|
38 |
{
|
alpar@742
|
39 |
int s=dim;
|
alpar@742
|
40 |
for(int i=0;i<dim;i++) {
|
alpar@742
|
41 |
s-=n&1;
|
alpar@742
|
42 |
n>>=1;
|
alpar@742
|
43 |
}
|
alpar@742
|
44 |
return s;
|
alpar@742
|
45 |
}
|
alpar@742
|
46 |
|
alpar@742
|
47 |
template<class Graph>
|
alpar@742
|
48 |
void bfsStlQueue(Graph &G,typename Graph::Node source)
|
alpar@742
|
49 |
{
|
alpar@1756
|
50 |
GRAPH_TYPEDEFS(typename Graph);
|
alpar@742
|
51 |
|
alpar@742
|
52 |
using namespace std;
|
alpar@742
|
53 |
|
alpar@751
|
54 |
typename Graph::template NodeMap<bool> visited(G,false);
|
alpar@742
|
55 |
|
alpar@742
|
56 |
queue<typename Graph::Node> Q;
|
alpar@742
|
57 |
|
alpar@742
|
58 |
Q.push(source);
|
alpar@742
|
59 |
visited[source]=true;
|
alpar@742
|
60 |
do {
|
alpar@742
|
61 |
Node n(Q.front());
|
alpar@742
|
62 |
Node m;
|
alpar@742
|
63 |
Q.pop();
|
alpar@774
|
64 |
for(OutEdgeIt e(G,n);e!=INVALID;++e)
|
alpar@986
|
65 |
if(!visited[m=G.target(e)]) {
|
alpar@742
|
66 |
Q.push(m);
|
alpar@742
|
67 |
visited.set(m,true);
|
alpar@742
|
68 |
}
|
alpar@742
|
69 |
} while(!Q.empty());
|
alpar@742
|
70 |
}
|
alpar@742
|
71 |
|
alpar@742
|
72 |
template<class Graph>
|
alpar@742
|
73 |
void bfsOwnQueue(Graph &G,typename Graph::Node source)
|
alpar@742
|
74 |
{
|
alpar@1756
|
75 |
GRAPH_TYPEDEFS(typename Graph);
|
alpar@742
|
76 |
|
alpar@742
|
77 |
using namespace std;
|
alpar@742
|
78 |
|
alpar@751
|
79 |
typename Graph::template NodeMap<bool> visited(G,false);
|
alpar@742
|
80 |
|
alpar@742
|
81 |
int N=G.nodeNum();
|
alpar@742
|
82 |
vector<typename Graph::Node> Q(N);
|
alpar@742
|
83 |
int Qh=0;
|
alpar@742
|
84 |
int Qt=0;
|
alpar@742
|
85 |
|
alpar@742
|
86 |
|
alpar@742
|
87 |
Q[Qh++]=source;
|
alpar@742
|
88 |
visited.set(source,true);
|
alpar@742
|
89 |
do {
|
alpar@742
|
90 |
Node m;
|
alpar@742
|
91 |
Node n=Q[Qt++];
|
alpar@774
|
92 |
for(OutEdgeIt e(G,n);e!=INVALID;++e)
|
alpar@986
|
93 |
if(!visited[m=G.target(e)]) {
|
alpar@742
|
94 |
Q[Qh++]=m;
|
alpar@742
|
95 |
visited.set(m,true);
|
alpar@742
|
96 |
}
|
alpar@742
|
97 |
} while(Qt!=Qh);
|
alpar@742
|
98 |
}
|
alpar@742
|
99 |
|
alpar@751
|
100 |
template<class Graph>
|
alpar@751
|
101 |
void iteratorBench(Graph &G)
|
alpar@751
|
102 |
{
|
alpar@1756
|
103 |
GRAPH_TYPEDEFS(typename Graph);
|
alpar@751
|
104 |
|
alpar@751
|
105 |
int i=0;
|
alpar@751
|
106 |
|
alpar@774
|
107 |
for(NodeIt n(G);n!=INVALID;++n)
|
alpar@774
|
108 |
for(OutEdgeIt e(G,n);e!=INVALID;++e)
|
alpar@751
|
109 |
i++;
|
alpar@751
|
110 |
}
|
alpar@751
|
111 |
|
alpar@742
|
112 |
int main(int argc, char *argv[])
|
alpar@742
|
113 |
{
|
alpar@742
|
114 |
typedef SmartGraph Graph;
|
alpar@742
|
115 |
|
alpar@1756
|
116 |
GRAPH_TYPEDEFS(Graph);
|
alpar@742
|
117 |
|
alpar@742
|
118 |
Graph G;
|
alpar@742
|
119 |
|
alpar@742
|
120 |
Timer T;
|
alpar@742
|
121 |
|
alpar@751
|
122 |
if(argc!=3) {
|
alpar@751
|
123 |
cout << "Usage: " << argv[0] << " dim mul\n";
|
alpar@742
|
124 |
return 1;
|
alpar@742
|
125 |
}
|
alpar@742
|
126 |
|
alpar@742
|
127 |
int dim=atoi(argv[1]);
|
alpar@751
|
128 |
int mul=atoi(argv[2]);
|
alpar@742
|
129 |
|
alpar@742
|
130 |
// cout << "Creating Hipercube ("<< (1<<dim) << " nodes, "
|
alpar@742
|
131 |
// << dim*(1<<dim) << " edges):";
|
alpar@742
|
132 |
|
alpar@1847
|
133 |
T.restart();
|
alpar@742
|
134 |
vector<Node> nodes;
|
alpar@1689
|
135 |
addBiDirHyperCube(G,dim,nodes);
|
alpar@742
|
136 |
|
alpar@742
|
137 |
PrintTime("GENGRAPH",T);
|
alpar@742
|
138 |
|
alpar@1847
|
139 |
T.restart();
|
alpar@742
|
140 |
{
|
alpar@751
|
141 |
for(int i=0;i<mul;i++)
|
alpar@742
|
142 |
bfsStlQueue(G,nodes[0]);
|
alpar@742
|
143 |
}
|
alpar@742
|
144 |
PrintTime("BFS-STL",T);
|
alpar@1847
|
145 |
T.restart();
|
alpar@742
|
146 |
{
|
alpar@751
|
147 |
for(int i=0;i<mul;i++)
|
alpar@742
|
148 |
bfsOwnQueue(G,nodes[0]);
|
alpar@742
|
149 |
}
|
alpar@742
|
150 |
PrintTime("BFS-OWN",T);
|
alpar@1847
|
151 |
T.restart();
|
alpar@751
|
152 |
{
|
alpar@751
|
153 |
for(int i=0;i<mul;i++)
|
alpar@751
|
154 |
iteratorBench(G);
|
alpar@751
|
155 |
}
|
alpar@751
|
156 |
PrintTime("ITERATE",T);
|
alpar@751
|
157 |
|
alpar@751
|
158 |
|
alpar@742
|
159 |
}
|