Skip to content

Instantly share code, notes, and snippets.

View ahmadyan's full-sized avatar

Adel Ahmadyan ahmadyan

View GitHub Profile
@ahmadyan
ahmadyan / bfs_vector_no_ordering.cpp
Last active December 13, 2016 00:09
Fastest implementation of BFS with vector (no ordering)
// Fastest implementation of the BFS algorithm with std::vector, if we don't care about the ordering
int bfs_vector_no_order(Graph* g){
int sig=0;
vector<bool> visited(g->getSize(), false);
vector<int> q;
q.reserve(10);
auto init = rand()%g->getSize();
q.push_back(init);
visited[init]=true;
while(!q.empty()){
@ahmadyan
ahmadyan / BFS_deque_vector.cpp
Last active December 13, 2016 00:06
BFS implementation with std::vector and std::deque
// This is the optimal implementation of BFS with deque
int bfs_deque(Graph* g){
int sig=0;
vector<bool> visited(g->getSize(), false);
deque<int> q;
auto init = rand()%g->getSize();
q.push_back(init);
visited[init]=true;
while(!q.empty()){
auto u = q.front();
@ahmadyan
ahmadyan / bfs_deque.cpp
Created December 13, 2016 00:04
BFS Implementation with std::dequeu
int bfs_deque(Graph* g){
int sig=0;
vector<bool> visited(g->getSize(), false);
deque<int> q;
auto init = rand()%g->getSize();
q.push_back(init);
visited[init]=true;
while(!q.empty()){
auto u = q.front();
@ahmadyan
ahmadyan / bfs_list.cpp
Created December 8, 2016 00:25
The BFS algorithm implemented using std::list
//list is basically a linked-list
int bfs_list(Graph* g){
int sig=0;
vector<bool> visited(g->getSize(), false);
list<int> q;
auto init = rand()%g->getSize();
q.push_back(init);
visited[init]=true;
while(!q.empty()){
auto u = q.front();
size_t count() const
{ // count number of set bits
static char _Bitsperhex[] = "\0\1\1\2\1\2\2\3\1\2\2\3\2\3\3\4";
size_t _Val = 0;
for (int _Wpos = _Words; 0 <= _Wpos; --_Wpos)
for (_Ty _Wordval = _Array[_Wpos]; _Wordval != 0; _Wordval >>= 4)
_Val += _Bitsperhex[_Wordval & 0xF];
return (_Val);
}
#include<bitset>
unsigned long long int v;
bitset<64> n(v);
int count = n.count();
auto v2 = n.to_ullong();
@ahmadyan
ahmadyan / count-set-bits.cpp
Created May 8, 2016 20:36
Count the number of set bits in an integer
unsigned long long int v; //counts the number of bits set in v
unsigned int count=0; // counts is the total bits set in v
while(v){
v &= v-1;
c++;
}
void mc(double r, double** samples){
cout << "Monte Carlo simulation" << endl ;
using namespace boost::math;
double sample_inside=0;
cout << "sizeof(sample_count)=" << sizeof(samples) << endl ;
for(int i=0;i<sample_count;i++){
double x = samples[i][0];
double y = samples[i][1];
void verify(double r, double** samples){
DT dt2;
VD vd;
Cropped_voronoi_from_delaunay vor;
Iso_rectangle_2 bbox(0,0,1,1);
vor = Cropped_voronoi_from_delaunay(bbox);
for(int i=0;i< sample_count;i++){
vd.insert( Site_2( samples[i][0], samples[i][1] ) );
}
void demo(){
double r = 0.5 ;
double area = M_PI * r * r;
double** samples = new double*[sample_count];
for(int i=0;i<sample_count;i++){
samples[i] = new double[2];
samples[i][0] = 1.0*rand()/INT_MAX;
samples[i][1] = 1.0*rand()/INT_MAX;
}
cout << "The exact area is " << area << endl ;