This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <random> | |
#include <list> | |
#include <array> | |
#include <queue> | |
#include <deque> | |
using namespace std; | |
class Graph{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <benchmark/benchmark.h> | |
#include <algorithm> | |
#include <iostream> | |
#include <vector> | |
#include <random> | |
#include <list> | |
#include <array> | |
#include <queue> | |
#include <deque> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Ceres Solver - A fast non-linear least squares minimizer | |
// Copyright 2015 Google Inc. All rights reserved. | |
// http://ceres-solver.org/ | |
// | |
// Redistribution and use in source and binary forms, with or without | |
// modification, are permitted provided that the following conditions are met: | |
// | |
// * Redistributions of source code must retain the above copyright notice, | |
// this list of conditions and the following disclaimer. | |
// * Redistributions in binary form must reproduce the above copyright notice, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Jet operator*(Jet& f, Jet& g) { | |
return Jet(f.a * g.a, // The scalar part | |
f.a * g.v + f.v * g.a // The infinitesimal vector part | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Rosenbrock function, a = 1.0, b = 100 | |
class Rosenbrock { | |
public: | |
template <typename T> | |
bool operator()(const T *const x, const T *const y, T *cost) const { | |
cost[0] = (T(1.0) - x[0]) * (T(1.0) - x[0]) + | |
T(100.0) * (y[0] - x[0] * x[0]) * (y[0] - x[0] * x[0]); | |
return true; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def rosenbrock(x): | |
return 100*(x[1] - x[0]**2)**2 + (1 - x[0])**2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
auto rosenbrockCostFunction = new ceres::AutoDiffCostFunction<Rosenbrock, 1, 1, 1>(new Rosenbrock()); | |
constexpr double x = 1, y = 3; | |
parameters[0][0] = x; | |
parameters[1][0] = y; | |
rosenbrockCostFunction->Evaluate(parameters, &residuals, jacobians); | |
cout << residuals << " df/dx=" << jacobians[0][0] << " df/dy" << jacobians[1][0] << "\n"; |