Skip to content

Commit d06c2d9

Browse files
committed
update with runtimes
1 parent bee03a6 commit d06c2d9

12 files changed

Lines changed: 133 additions & 32 deletions

File tree

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package: SPR
22
Type: Package
33
Title: Shortest Path Algorithms using R
4-
Version: 1.1.0
5-
Date: 2018-01-14
4+
Version: 1.2.0
5+
Date: 2018-02-16
66
Author: Keith O'Hara [aut, cre]
77
Maintainer: Keith O'Hara <keith.ohara@nyu.edu>
88
Description: A package for shortest path computation with sparse graphs.

man/SPR-package.Rd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ The SPR package is a collection of shortest path algorithms.
1515
\tabular{ll}{
1616
Package: \tab SPR\cr
1717
Type: \tab Package\cr
18-
Version: \tab 1.1.0\cr
19-
Date: \tab 2018-01-14\cr
18+
Version: \tab 1.2.0\cr
19+
Date: \tab 2018-02-16\cr
2020
License: \tab GPL (>= 2)\cr
2121
LazyLoad: \tab Yes\cr
2222
Depends: \tab Rcpp, RcppArmadillo \cr }
@@ -26,7 +26,7 @@ Depends: \tab Rcpp, RcppArmadillo \cr }
2626
author = {Keith O'Hara},
2727
title = {{SPR}: Shortest Path Algorithms using R.},
2828
year = {2018},
29-
note = {R package version 1.1.0.}}}
29+
note = {R package version 1.2.0.}}}
3030
\emph{}
3131
}
3232

man/bellman_ford.Rd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ The function returns a list of objects:
3232
A numeric vector containing the minimum distance from the source node to other nodes in the network.}
3333
\item{path_list}{
3434
The minimum distance path for each node.}
35+
\item{elapsed_time}{
36+
Runtime of the algorithm.}
3537
}
3638

3739
\author{Keith O'Hara}

man/dijkstra.Rd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,16 @@ The function returns a list of objects. If the destination node is provided,
3333
The minimum distance solution from the source node to the destination node.}
3434
\item{path_list}{
3535
The minimum distance path from source_ind to dest_ind.}
36+
\item{elapsed_time}{
37+
Runtime of the algorithm.}
3638

3739
If the destination node is not provided,
3840
\item{min_dist}{
3941
A numeric vector containing the minimum distance from the source node to other nodes in the network.}
4042
\item{path_list}{
4143
The minimum distance path for each node.}
44+
\item{elapsed_time}{
45+
Runtime of the algorithm.}
4246
}
4347

4448
\author{Keith O'Hara}

src/R_src/bellman_ford_R.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,20 @@ SEXP bellman_ford_R(SEXP n_R, SEXP source_ind_R, SEXP arcs_R)
3535

3636
std::vector<double> min_distance;
3737
std::vector<int> path_list;
38+
sp::comptime_t algo_runtime;
3839

39-
sp::bellman_ford::compute_paths(as<int>(source_ind_R), arc_list, min_distance, path_list);
40+
sp::bellman_ford::compute_paths(as<int>(source_ind_R), arc_list, min_distance, path_list, &algo_runtime);
4041

4142
//
4243

4344
for (int i=0; i < path_list.size(); i++) {
4445
path_list[i]++;
4546
}
4647

47-
return Rcpp::List::create(Rcpp::Named("min_dist") = min_distance,Rcpp::Named("path_list") = path_list);
48+
double runtime_out = algo_runtime.count();
49+
50+
return Rcpp::List::create(Rcpp::Named("min_dist") = min_distance, Rcpp::Named("path_list") = path_list,
51+
Rcpp::Named("elapsed_time") = runtime_out);
4852
} catch( std::exception &ex ) {
4953
forward_exception_to_r( ex );
5054
} catch(...) {

src/R_src/dijkstra_R.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,20 @@ SEXP dijkstra_R(SEXP n_R, SEXP source_ind_R, SEXP arcs_R)
3535

3636
std::vector<double> min_distance;
3737
std::vector<int> path_list;
38+
sp::comptime_t algo_runtime;
3839

39-
sp::dijkstra::compute_paths(as<int>(source_ind_R), arc_list, min_distance, path_list);
40+
sp::dijkstra::compute_paths(as<int>(source_ind_R), arc_list, min_distance, path_list, &algo_runtime);
4041

4142
//
4243

4344
for (int i=0; i < path_list.size(); i++) {
4445
path_list[i]++;
4546
}
4647

47-
return Rcpp::List::create(Rcpp::Named("min_dist") = min_distance,Rcpp::Named("path_list") = path_list);
48+
double runtime_out = algo_runtime.count();
49+
50+
return Rcpp::List::create(Rcpp::Named("min_dist") = min_distance,Rcpp::Named("path_list") = path_list,
51+
Rcpp::Named("elapsed_time") = runtime_out);
4852
} catch( std::exception &ex ) {
4953
forward_exception_to_r( ex );
5054
} catch(...) {
@@ -65,11 +69,15 @@ SEXP dijkstra_simp_R(SEXP n_R, SEXP source_ind_R, SEXP dest_ind_R, SEXP arcs_R)
6569

6670
std::vector<double> min_distance;
6771
std::vector<int> path_list;
72+
sp::comptime_t algo_runtime;
6873

69-
sp::dijkstra::compute_path(as<int>(source_ind_R), dest_ind, arc_list, min_distance, path_list);
74+
sp::dijkstra::compute_path(as<int>(source_ind_R), dest_ind, arc_list, min_distance, path_list, &algo_runtime);
7075
std::list<int> opt_path = sp::get_shortest_path(dest_ind, path_list);
7176

72-
return Rcpp::List::create(Rcpp::Named("min_dist") = min_distance[dest_ind],Rcpp::Named("path_list") = opt_path);
77+
double runtime_out = algo_runtime.count();
78+
79+
return Rcpp::List::create(Rcpp::Named("min_dist") = min_distance[dest_ind],Rcpp::Named("path_list") = opt_path,
80+
Rcpp::Named("elapsed_time") = runtime_out);
7381
} catch( std::exception &ex ) {
7482
forward_exception_to_r( ex );
7583
} catch(...) {

src/splib/include/bellman_ford.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
namespace bellman_ford {
2020

21-
void compute_paths(const int source_ind, const graph_t& node_list, std::vector<double>& min_distance, std::vector<int>& path_list);
21+
void compute_paths(const int source_ind, const graph_t& node_list,
22+
std::vector<double>& min_distance, std::vector<int>& path_list,
23+
comptime_t* time_spent);
2224

2325
}

src/splib/include/dijkstra.hpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@
1818

1919
namespace dijkstra {
2020

21-
void compute_path(const int source_ind, const int dest_ind, const graph_t& node_list, std::vector<double>& min_distance, std::vector<int>& path_list);
21+
void compute_path(const int source_ind, const int dest_ind, const graph_t& node_list,
22+
std::vector<double>& min_distance, std::vector<int>& path_list,
23+
comptime_t* time_spent);
2224

23-
void compute_paths(const int source_ind, const graph_t& node_list, std::vector<double>& min_distance, std::vector<int>& path_list);
24-
void compute_paths_robust(const int source_ind, const graph_t& node_list, std::vector<double>& min_distance, std::vector<int>& path_list);
25+
void compute_paths(const int source_ind, const graph_t& node_list,
26+
std::vector<double>& min_distance, std::vector<int>& path_list,
27+
comptime_t* time_spent);
28+
29+
void compute_paths_robust(const int source_ind, const graph_t& node_list,
30+
std::vector<double>& min_distance, std::vector<int>& path_list);
2531

2632
}

src/splib/include/sp.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ namespace sp {
4343

4444
#include "sp_structs.hpp"
4545
#include "sp_misc.hpp"
46+
#include "tictoc.hpp"
4647

4748
#include "bellman_ford.hpp"
4849
#include "dijkstra.hpp"

src/splib/include/tictoc.hpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*################################################################################
2+
##
3+
## Copyright (C) 2017-2018 Keith O'Hara
4+
##
5+
## This file is part of the Shortest Path C++ library (SPLib).
6+
##
7+
## SPLib is free software: you can redistribute it and/or modify
8+
## it under the terms of the GNU General Public License as published by
9+
## the Free Software Foundation, either version 2 of the License, or
10+
## (at your option) any later version.
11+
##
12+
## SPLib is distributed in the hope that it will be useful,
13+
## but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
## GNU General Public License for more details.
16+
##
17+
################################################################################*/
18+
19+
/*
20+
* simple tictoc functionality
21+
*/
22+
23+
using clocktime_t = std::chrono::time_point<std::chrono::system_clock>;
24+
using comptime_t = std::chrono::duration<double>;
25+
26+
inline
27+
clocktime_t
28+
tic()
29+
{
30+
return std::chrono::system_clock::now();
31+
}
32+
33+
inline
34+
void
35+
tictoc(clocktime_t time_inp)
36+
{
37+
clocktime_t time_now = std::chrono::system_clock::now();
38+
39+
comptime_t run_time = time_now - time_inp;
40+
41+
//
42+
43+
std::time_t end_time = std::chrono::system_clock::to_time_t(time_now);
44+
45+
std::cout << "finished computation at " << std::ctime(&end_time)
46+
<< "total runtime: " << run_time.count() << "s\n";
47+
}

0 commit comments

Comments
 (0)