Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
lecfab authored Feb 14, 2022
1 parent 7eeeb9b commit e17b918
Show file tree
Hide file tree
Showing 7 changed files with 471 additions and 0 deletions.
98 changes: 98 additions & 0 deletions order/order_deg.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include "order_deg.h"
#include "../utils/adjlist.h"
#include "../utils/edgelist.h"
#include <algorithm>

using namespace std;

inline bool compare_nodedeg_desc (Keyvalue &a, Keyvalue &b) { // sort degree DESC, node ASC
return ( a.val > b.val or (a.val == b.val and a.key < b.key) );
}
inline bool compare_nodedeg_asc (Keyvalue &a, Keyvalue &b) { // sort degree ASC, node ASC
return ( a.val < b.val or (a.val == b.val and a.key < b.key) );
}

// --------------------------------------------------
// ----------------- From edgelist ------------------
// --------------------------------------------------
vector<ul> order_from_deg(const vector<ul> &deg, const ul &n, bool desc) { return rank_from_deg(deg, n, desc, true); }
vector<ul> rank_from_deg(const vector<ul> &deg, const ul &n, bool desc, bool order) {
vector<Keyvalue> toSort; toSort.reserve(n);
for (ul u = 0; u < n; ++u)
toSort.push_back( Keyvalue(u, deg[u]) );

if(desc) sort(toSort.begin(), toSort.end(), compare_nodedeg_desc);
else sort(toSort.begin(), toSort.end(), compare_nodedeg_asc);

vector<ul> rank; rank.reserve(n);
if(order)
for (ul i = 0; i < n; ++i) rank[i] = toSort[i].key;
else
for (ul u = 0; u < n; ++u) rank[ toSort[u].key ] = u;

return rank;
}

vector<ul> order_deg(Edgelist &g, bool desc) {
Debug("Order deg from edgelist")
g.compute_degrees();
return rank_from_deg(g.deg, g.n, desc);
}
vector<ul> order_degOut(Edgelist &g, bool desc) {
Debug("Order degOut from edgelist")
g.compute_degrees();
return rank_from_deg(g.degOut, g.n, desc);
}

vector<ul> order_degIn(Edgelist &g, bool desc) {
Debug("Order degIn from edgelist")
g.compute_degrees();
return rank_from_deg(g.degIn, g.n, desc);
}

vector<ul> order_deg_split(Edgelist &g, bool desc) {
Debug("Order deg Split from edgelist")
g.compute_degrees();
vector<ul> order = order_from_deg(g.deg, g.n, desc);
vector<ul> rank; rank.reserve(g.n);
for (ul i = 0; i < g.n; ++i)
rank[ order[i] ] = (i % 2 == 0) ? (i / 2) : (g.n - (i+1)/2);
return rank;
}

// --------------------------------------------------
// ----------------- From adjlist -------------------
// --------------------------------------------------
vector<ul> order_degOut(Adjlist &g) {
Debug("Order degOut")
vector<Keyvalue> toSort; toSort.reserve(g.n);
for (ul u = 0; u < g.n; ++u)
toSort.push_back( Keyvalue(u, g.get_degree(u)) );

// cout << "before sort: "<< endl; for (ul i = 0; i < 10; ++i) cout << "\t" << toSort[i].node << " \t " << toSort[i].deg << endl;
sort(toSort.begin(), toSort.end(), compare_nodedeg_desc);

vector<ul> rank; rank.reserve(g.n);
for (ul u = 0; u < g.n; ++u)
rank[ toSort[u].key ] = u;

// cout << "after sort: " << endl; for (ul i = 0; i < 10; ++i) cout << "\t" << toSort[i].node << " \t " << toSort[i].deg << " test:" << rank[i] << endl;
return rank;
}

vector<ul> order_degIn(Adjlist &g) {
Debug("Order degIn")
vector<Keyvalue> toSort; toSort.reserve(g.n);
for (ul u = 0; u < g.n; ++u)
toSort.push_back( Keyvalue(u, 0) );

for (intEdge i = 0; i < g.e; ++i)
toSort[g.adj[i]].val ++;

sort(toSort.begin(), toSort.end(), compare_nodedeg_desc);

vector<ul> rank; rank.reserve(g.n);
for (ul u = 0; u < g.n; ++u)
rank[ toSort[u].key ] = u;
return rank;
}
35 changes: 35 additions & 0 deletions order/order_deg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef ORDER_DEG_H
#define ORDER_DEG_H

#include "../utils/tools.h"

struct Keyvalue;
struct Edgelist;
struct Adjlist;

inline bool compare_nodedeg_desc (Keyvalue &a, Keyvalue &b);
inline bool compare_nodedeg_asc (Keyvalue &a, Keyvalue &b);

// ----------------- From edgelist ------------------
std::vector<ul> rank_from_deg(const std::vector<ul> &deg, const ul &n, bool desc=true, bool order=false);
std::vector<ul> order_from_deg(const std::vector<ul> &deg, const ul &n, bool desc=true);
std::vector<ul> order_deg(Edgelist &g, bool desc=true);
std::vector<ul> order_degOut(Edgelist &g, bool desc=true);
std::vector<ul> order_degIn(Edgelist &g, bool desc=true);
std::vector<ul> order_deg_split(Edgelist &g, bool desc=true);

// ----------------- From adjlist -------------------
std::vector<ul> order_degOut(Adjlist &g);
std::vector<ul> order_degIn(Adjlist &g);

#endif

// g++ -std=c++14 benchmark.o utils/tools.o utils/inout.o utils/edgelist.o utils/adjlist.o utils/heap.o utils/continuousrank.o utils/unitheap.o algo/algo_nq.o algo/algo_bfs.o algo/algo_dfs.o algo/algo_bellman.o algo/algo_diameter.o algo/algo_kcore.o algo/algo_tarjan.o algo/algo_pagerank.o algo/algo_dominatingset.o algo/algo_minuncut.o algo/algo_triangles.o order/order_deg.o order/order_rand.o order/order_rcm.o order/order_gorder.o order/order_ldg.o order/order_minla.o order/order_triangles.o order/order_slashburn.o -Ofast -g -fopenmp -o benchmark
// /usr/bin/ld : algo/algo_minuncut.o : dans la fonction « order_from_deg(std::vector<unsigned long, std::allocator<unsigned long> > const&, unsigned long const&, bool) » :
// /home/fab/Documents/these/work/src/algo/../order/order_deg.h:15 : définitions multiples de « order_from_deg(std::vector<unsigned long, std::allocator<unsigned long> > const&, unsigned long const&, bool) »; benchmark.o:/home/fab/Documents/these/work/src/order/order_deg.h:15 : défini pour la première fois ici
// /usr/bin/ld : order/order_deg.o : dans la fonction « order_from_deg(std::vector<unsigned long, std::allocator<unsigned long> > const&, unsigned long const&, bool) » :
// /home/fab/Documents/these/work/src/order/order_deg.h:15 : définitions multiples de « order_from_deg(std::vector<unsigned long, std::allocator<unsigned long> > const&, unsigned long const&, bool) »; benchmark.o:/home/fab/Documents/these/work/src/order/order_deg.h:15 : défini pour la première fois ici
// /usr/bin/ld : order/order_rcm.o : dans la fonction « __gnu_cxx::new_allocator<unsigned long>::deallocate(unsigned long*, unsigned long) » :
// /home/fab/Documents/these/work/src/order/order_deg.h:15 : définitions multiples de « order_from_deg(std::vector<unsigned long, std::allocator<unsigned long> > const&, unsigned long const&, bool) »; benchmark.o:/home/fab/Documents/these/work/src/order/order_deg.h:15 : défini pour la première fois ici
// collect2: error: ld returned 1 exit status
// make: *** [makefile:52 : benchmark] Erreur 1
44 changes: 44 additions & 0 deletions order/order_kcore.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// See header for general documentation

#include "order_kcore.h"
#include "../utils/adjlist.h"
#include "../utils/heap.h"

using namespace std;

Kdegeneracies algo_kcore(const Adjlist &g) {
Bheap h(g.n);
if(g.directed) {
Debug("Directed in-kcore")
vector<ul> degIn(g.n, 0);
for (ul u = 0; u < g.n; ++u)
for (auto &v : g.neigh_iter(u))
degIn[v] ++;
for (ul u = 0; u < g.n; ++u)
h.insert(Keyvalue(u, degIn[u]));
}
else {
Debug("Undirected kcore")
for (ul u = 0; u < g.n; ++u)
h.insert(Keyvalue(u, g.get_degree(u)));
}

Kdegeneracies kd(g.n);
ul degeneracy = 0;
for (ul i = 1; i <= g.n; ++i) {
Keyvalue kv = h.popmin();

ul u = kv.key;
kd.rank[u] = i; // g.n - i;
if(kv.val > degeneracy) degeneracy = kv.val;
kd.degeneracies[u] = degeneracy;
int count = 0;
for (auto &v : g.neigh_iter(u)) {
if(h.contains(v)) count ++;
h.update_decrement(v);
}
}
Info("Core value: " << degeneracy)

return kd;
}
40 changes: 40 additions & 0 deletions order/order_kcore.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef ALGO_KCORE_H
#define ALGO_KCORE_H

// K-core decomposition (https://en.wikipedia.org/wiki/Degeneracy_(graph_theory)#Algorithms)

// Remove the node of lowest degree, then update the graph. Repeat.
// Gives a ranking of nodes according to their coreness or degeneracy.

// Method: use a binary heap to keep track of nodes' updated degrees.

// Variants:
// * apply it on a directed graph
// * count only in- or out-going edges
// * remove hubs (high-degree nodes) instead of low-degree nodes

// Space optimisation:
// * remove rank if you only need degeneracy
// * remove degeneracy if you only need rank
// * compact the degeneracy array by remembering only the indices where degeneracy increases

#include "../utils/tools.h"
#include <vector>
struct Adjlist;
struct Bheap;


// Structure used to return both rank and degeneracy arrays
struct Kdegeneracies {
ul n;
std::vector<ul> degeneracies;
std::vector<ul> rank;
Kdegeneracies(ul n) : n(n) {
degeneracies.reserve(n);
rank.reserve(n);
}
};

Kdegeneracies algo_kcore(const Adjlist &g); // standard version

#endif
22 changes: 22 additions & 0 deletions order/order_rand.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "order_rand.h"
#include "../utils/tools.h"
#include "../utils/adjlist.h"

#include <algorithm> // std::random_shuffle
#include <ctime> // std::time
#include <cstdlib> // std::rand, std::srand

using namespace std;


vector<ul> order_rand(Adjlist &g) {
Debug("Order rand")
vector<ul> rank = order_identity(g.n);
srand ( unsigned ( time(0) ) );
random_shuffle( rank.begin(), rank.end() );
// auto rd = random_device {};
// auto rng = default_random_engine { rd() };
// shuffle(rank.begin(), rank.end(), rng);

return rank;
}
10 changes: 10 additions & 0 deletions order/order_rand.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef ORDER_RAND_H
#define ORDER_RAND_H

#include "../utils/tools.h"

struct Adjlist;

std::vector<ul> order_rand(Adjlist &g);

#endif
Loading

0 comments on commit e17b918

Please sign in to comment.