diff --git a/(p,t)_sparsification/main.cpp b/(p,t)_sparsification/main.cpp index ce3bc9d20300afda0857d880878610a693c7c174..b4b342b53125528fa344fa4d37473f1d63deb596 100644 --- a/(p,t)_sparsification/main.cpp +++ b/(p,t)_sparsification/main.cpp @@ -1,15 +1,15 @@ #include <iostream> -#include <chrono> #include "p_k_compression.h" #include <boost/program_options.hpp> #include "graph.h" #include "io.h" -#include <sstream> #include <string> -#include <fstream> #include "hash.h" +#include <omp.h> +#define NUM_TRIALS 10 + using namespace std; namespace po = boost::program_options; @@ -51,45 +51,59 @@ int main(int argc, char *argv[]) { vector<int> s ; vector<double> c ; - // perform 30 experiments - for (int i =0; i<30;i++) { - cout << i << endl; - string file_name = var["input"].as<string>() + to_string(i) + ".txt"; - double compression_rate = 0; - auto[g, e_s] = read_graph_from_file(file_name, var["directed"].as<bool>()); - auto start = chrono::steady_clock::now(); - - if (var["algorithm"].as<string>() == "Random") { - g2 = compress_graph_basic(g, var["depth"].as<int>(), var["proportions"].as<vector<double>>(), + int edges_compressed = 0; + + string file_name = var["input"].as<string>(); + + double compression_rate = 0; + double elapsed_time = 0; + + auto[g, e_s] = read_graph_from_file(file_name, var["directed"].as<bool>()); + auto start = chrono::steady_clock::now(); + + string execMode=var["algorithm"].as<string>(); + + + + + #pragma omp parallel for num_threads(NUM_TRIALS) + for (int i = 0; i < NUM_TRIALS; i++) { + + auto start = std::chrono::steady_clock::now(); + + if (execMode == "Random") { + g2 = compress_graph_basic(g, var["depth"].as<int>(), var["proportions"].as<std::vector<double>>(), var["directed"].as<bool>()); - } else if (var["algorithm"].as<string>() == "LP") { - g2 = compress_graph_LP(g, e_s, var["depth"].as<int>(), var["proportions"].as<vector<double>>(), - var["directed"].as<bool>()); + } else if (execMode == "LP") { + g2 = compress_graph_LP(g, e_s, var["depth"].as<int>(), var["proportions"].as<std::vector<double>>(), + var["directed"].as<bool>()); - } else if (var["algorithm"].as<string>() == "SA") { + } else if (execMode == "SA") { g2 = Simulated_annealing(1000, 10, 0.99, g, var["directed"].as<bool>(), var["depth"].as<int>(), - var["proportions"].as<vector<double>>()); - }else if (var["algorithm"].as<string>() == "Greedy") { - g2 = compress_graph_greedy(g, var["depth"].as<int>(), var["proportions"].as<vector<double>>(), - var["directed"].as<bool>()); + var["proportions"].as<std::vector<double>>()); + } else if (execMode == "Greedy") { + g2 = compress_graph_greedy(g, var["depth"].as<int>(), var["proportions"].as<std::vector<double>>(), + var["directed"].as<bool>()); + + } + + auto finish = std::chrono::steady_clock::now(); + // Update edges_compressed and elapsed_time in a thread-safe manner + #pragma omp critical + { + vector<edge> edges_original = get_edges(g, var["directed"].as<bool>()); + edges_compressed += get_edges(g2, var["directed"].as<bool>()).size(); + elapsed_time += std::chrono::duration_cast<std::chrono::duration<double>>(finish - start).count(); } - auto finish = chrono::steady_clock::now(); - vector<edge> edges_original = get_edges(g, var["directed"].as<bool>()); - vector<edge> edges_compressed = get_edges(g2, var["directed"].as<bool>()); - - double elapsed_time = chrono::duration_cast<chrono::duration<double>>(finish - start).count(); - compression_rate = ((double) (edges_original.size() - edges_compressed.size()) / - edges_original.size()); - c.push_back(compression_rate); - s.push_back(edges_compressed.size()); - //graph_to_file(var, elapsed_time, compression_rate, edges_compressed); - cout << "compression time " << elapsed_time << endl; - cout << "compression rate " << compression_rate << endl; } - for (int i = 0 ; i<30;i++) cout << c.at(i) << '\t' << s.at(i) << endl; - return 0; + +// graph_to_file(var, elapsed_time, compression_rate, edges_compressed); +// cout << "compression rate: " << compression_rate << endl; + cout <<endl << "compression time: " << elapsed_time/NUM_TRIALS << endl; + cout << "Average of compressed edges: " << edges_compressed/NUM_TRIALS; + } diff --git a/README.md b/README.md index b33d853617035d2c1e8363dd1b0f1d9c4ceda13c..751af029527bab671ca841d41cfa56133593256f 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ ptSpar is a C++ program implementing a Neighborhood-Preserving Graph Sparsificat ## Compilation 1. **Install Boost Library:** Ensure the Boost Program Options library is installed on your system. -2. **Compilation Command:** In the source directory, compile using `g++ -std=c++11 main.c++ -o ptSpar -lboost_program_options`. +2. **Compilation Command:** In the source directory, compile using `g++ -std=c++11 main.cpp -o ptSpar -lboost_program_options`. ## Usage - **Basic Command Structure:**