From 6f06be83ea38da4440d022e2a4be3a25e685dfde Mon Sep 17 00:00:00 2001 From: gianlucarossi15 <gianluca.rossi2000@live.com> Date: Mon, 11 Mar 2024 16:07:37 +0100 Subject: [PATCH] minor changes --- (p,t)_sparsification/main.cpp | 76 ++++++++++++++++++++++------------- README.md | 2 +- 2 files changed, 48 insertions(+), 30 deletions(-) diff --git a/(p,t)_sparsification/main.cpp b/(p,t)_sparsification/main.cpp index ce3bc9d..b80c575 100644 --- a/(p,t)_sparsification/main.cpp +++ b/(p,t)_sparsification/main.cpp @@ -1,23 +1,35 @@ #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 <pthread.h> + + +#define NUM_TRIALS 10 using namespace std; namespace po = boost::program_options; +static void *handler(void *args){ + string mode = *(string *) args; + if(mode=="Random"){ + g2 = compress_graph_basic(g, var["depth"].as<int>(), var["proportions"].as<vector<double>>(), + var["directed"].as<bool>()); + }else if(mode=="LP"){ + g2 = compress_graph_LP(g, e_s, var["depth"].as<int>(), var["proportions"].as<vector<double>>(), + var["directed"].as<bool>()); + } +} int main(int argc, char *argv[]) { srand((unsigned)time(NULL)); + pthread_t threads[NUM_TRIALS]; po::options_description desc("Allowed options"); desc.add_options() @@ -51,45 +63,51 @@ 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") { + 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>(); + for (int i=0; i<NUM_TRIALS; i++) { + pthread_create(&threads[i], NULL, handler, NULL); + if (execMode == "Random") { g2 = compress_graph_basic(g, var["depth"].as<int>(), var["proportions"].as<vector<double>>(), var["directed"].as<bool>()); - } else if (var["algorithm"].as<string>() == "LP") { + } else if (execMode == "LP") { g2 = compress_graph_LP(g, e_s, var["depth"].as<int>(), var["proportions"].as<vector<double>>(), - var["directed"].as<bool>()); + 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") { + }else if (execMode == "Greedy") { g2 = compress_graph_greedy(g, var["depth"].as<int>(), var["proportions"].as<vector<double>>(), - var["directed"].as<bool>()); + var["directed"].as<bool>()); } 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; + edges_compressed += get_edges(g2, var["directed"].as<bool>()).size(); + + 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); + } - 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 b33d853..751af02 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:** -- GitLab