ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
KaHIP.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------------ File KaHIP.h ------------------------------*/
3/*--------------------------------------------------------------------------*/
41/*--------------------------------------------------------------------------*/
42/*----------------------------- DEFINITIONS --------------------------------*/
43/*--------------------------------------------------------------------------*/
44
45#ifndef __ADS_interop_kahip_KaHIP
46 #define __ADS_interop_kahip_KaHIP
47/* self-identification: #endif constant */
48
49/*--------------------------------------------------------------------------*/
50/*------------------------------ INCLUDES ----------------------------------*/
51/*--------------------------------------------------------------------------*/
52
53#if ! __has_include( <kaHIP_interface.h> )
54 #error "the interoperation with KaHIP needs the header of KaHIP"
55#endif
56
57#include "ADS/Contract.h"
59#include "ADS/interop/Csr.h"
60
61#include <kaHIP_interface.h>
62
63#include <cstddef>
64#include <vector>
65
66/*--------------------------------------------------------------------------*/
67/*--------------------------- NAMESPACE ------------------------------------*/
68/*--------------------------------------------------------------------------*/
69
70namespace ADS::kahip
71{
72
73/*--------------------------------------------------------------------------*/
74/*------------------------------ THE OPTIONS -------------------------------*/
75/*--------------------------------------------------------------------------*/
76
78struct Options
79{
80 int mode = FAST;
81 bool weights = true;
82 double imbalance = 0.03;
83 int seed = 0;
84 };
85
86/*--------------------------------------------------------------------------*/
87/*----------------------------- THE PARTITION ------------------------------*/
88/*--------------------------------------------------------------------------*/
89
92
97template< class G >
98[[nodiscard]] Partition partition( const G & g , const Input & I , int k ,
99 const Options & o = Options() )
100{
101 ADS_PRE( ( ! g.directed() ) && ( k >= 1 ) && ( k <= g.n() ) &&
102 ( kahip_sizeof_idx() == int( sizeof( kahip_idx ) ) ) );
103 Partition P;
104 P.k = k;
105 P.part.assign( std::size_t( g.n() ) , 0 );
106 if( ( g.n() == 0 ) || ( k == 1 ) )
107 return( P );
108 int n = g.n() , parts = k;
109 double eps = o.imbalance;
110 kahip_idx cut = 0;
111 // kaffpa() builds its graph from the arrays and writes none of them
112 auto * xadj = const_cast< kahip_idx * >( I.offsets() );
113 auto * adjncy = const_cast< kahip_idx * >( I.heads() );
114 auto * adjcwgt =
115 o.weights ? const_cast< kahip_idx * >( I.weights() ) : nullptr;
116 kaffpa( &n , nullptr , xadj , adjcwgt , adjncy , &parts , &eps , true ,
117 o.seed ,
118 o.mode , &cut , P.part.data() );
119 P.cut = static_cast< long long >( cut );
120 return( P );
121 }
122
127template< class G >
128[[nodiscard]] Partition partition( const G & g , int k ,
129 const Options & o = Options() )
130{
131 const Input I( g , o.weights );
132 return( partition( g , I , k , o ) );
133 }
134
135/*--------------------------------------------------------------------------*/
136
137} // end( namespace ADS::kahip )
138
139/*--------------------------------------------------------------------------*/
140
141#endif /* KaHIP.h included */
142
143/*--------------------------------------------------------------------------*/
144/*---------------------------- End File KaHIP.h ----------------------------*/
145/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The compressed rows of a StaticGraph handed to a library that takes them as plain arrays,...
A static GRAPH for the engineered algorithms, beyond the book [see Graph.h]: the compressed sparse ro...
the offsets and the heads of a graph as arrays of X, and its weights as an array of Y,...
Definition Csr.h:72
const X * offsets(void) const
the offsets of the rows
Definition Csr.h:125
const X * heads(void) const
the heads of the arcs, row by row
Definition Csr.h:127
const Y * weights(void) const
the weights of the arcs in the places of the heads, null if the graph is unweighted or they were not ...
Definition Csr.h:130
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
Partition partition(const G &g, const Input &I, int k, const Options &o=Options())
the partition of the nodes of into parts by kaffpa(), on the input of (whose arrays it reads in p...
Definition KaHIP.h:98
CsrInput< kahip_idx, kahip_idx > Input
the arrays of a graph for KaHIP: in place where the layouts agree
Definition KaHIP.h:91
Definition KaHIP.h:71
a partition of the nodes of a graph into parts
Definition Csr.h:150
int k
the number of the parts
Definition Csr.h:153
long long cut
the weight of the edges between two parts
Definition Csr.h:152
std::vector< int > part
the part of each node, in
Definition Csr.h:151
the options of a partition by KaHIP
Definition KaHIP.h:79
int seed
the seed of KaHIP
Definition KaHIP.h:83
int mode
FAST, ECO, STRONG or their *SOCIAL
Definition KaHIP.h:80
bool weights
the weights of the arcs, if any, as adjcwgt
Definition KaHIP.h:81
double imbalance
the largest part over the mean, less 1
Definition KaHIP.h:82