COMBINATORIAL_BLAS 1.6
 
Loading...
Searching...
No Matches
MatchingDefs.h
Go to the documentation of this file.
1//
2// MatchingDefs.h
3//
4//
5// Created by Ariful Azad on 8/22/17.
6//
7//
8
9#ifndef MatchingDefs_h
10#define MatchingDefs_h
11
12#include "CombBLAS/CombBLAS.h"
13#include <iostream>
14
15namespace combblas {
16
17// Vertex data structure for maximal cardinality matching
18template <typename T1, typename T2>
19struct VertexTypeML
20{
21public:
22 VertexTypeML(T1 p=-1, T2 com=0){parent=p; comp = com; };
23
24 friend bool operator<(const VertexTypeML & vtx1, const VertexTypeML & vtx2 )
25 {
26 if(vtx1.comp==vtx2.comp) return vtx1.parent<vtx2.parent;
27 else return vtx1.comp<vtx2.comp;
28 };
29 friend bool operator==(const VertexTypeML & vtx1, const VertexTypeML & vtx2 ){return (vtx1.parent==vtx2.parent) & (vtx1.comp==vtx2.comp);};
30 friend std::ostream& operator<<(std::ostream& os, const VertexTypeML & vertex ){os << "(" << vertex.parent << "," << vertex.comp << ")"; return os;};
31 T1 parent;
32 T2 comp; // can be index, probability, degree or an adjacent edge weight
33};
34
35
36
37// Vertex data structure for maximum cardinality matching
38template <typename IT>
39struct VertexTypeMM
40{
41public:
42 VertexTypeMM(IT p=-1, IT r=-1, double w=0){parent=p; root = r; comp = w;};
43 friend bool operator<(const VertexTypeMM & vtx1, const VertexTypeMM & vtx2 )
44 {
45 if(vtx1.comp==vtx2.comp)
46 {
47 if(vtx1.parent==vtx2.parent)
48 return vtx1.root<vtx2.root;
49 else return vtx1.parent<vtx2.parent;
50 }
51 else return vtx1.comp<vtx2.comp;
52
53 };
54 friend bool operator==(const VertexTypeMM & vtx1, const VertexTypeMM & vtx2 ){return vtx1.parent==vtx2.parent;};
55 friend std::ostream& operator<<(std::ostream& os, const VertexTypeMM & vertex ){os << "(" << vertex.parent << "," << vertex.root << ","<< vertex.comp << ")"; return os;};
56 IT parent;
57 IT root;
58 double comp; // probability of selecting an edge or weight of an adjacent edge
59 //making it double so that we can always use edge weights or probability
60 // TODO: this is an overkill for Boolean matrices. Think a better way to cover the Boolean case
61};
62
63// Semiring needed to compute degrees within a subgraph
64template <typename T1, typename T2>
65struct SelectPlusSR
66{
67 static T2 id(){ return 1; };
68 static bool returnedSAID() { return false; }
69 static MPI_Op mpi_op() { return MPI_SUM; };
70
71 static T2 add(const T2 & arg1, const T2 & arg2)
72 {
73 return std::plus<T2>()(arg1, arg2);
74 }
75
76 static T2 multiply(const T1 & arg1, const T2 & arg2)
77 {
78 return static_cast<T2> (1); // note: it is not called on a Boolean matrix
79 }
80
81 static void axpy(const T1 a, const T2 & x, T2 & y)
82 {
83 y = add(y, multiply(a, x));
84 }
85};
86
87
88// Usual semiring used in maximal and maximum matching
89template <typename T1, typename T2>
90struct Select2ndMinSR
91{
92 static T2 id(){ return T2(); };
93 static bool returnedSAID() { return false; }
94 static MPI_Op mpi_op() { return MPI_MIN; };
95
96 static T2 add(const T2 & arg1, const T2 & arg2)
97 {
98 return std::min(arg1, arg2);
99 }
100
101 static T2 multiply(const T1 & arg1, const T2 & arg2)
102 {
103 return arg2;
104 }
105
106 static void axpy(const T1 a, const T2 & x, T2 & y)
107 {
108 y = add(y, multiply(a, x));
109 }
110};
111
112// Designed to pseudo maximize weights on a maximal matching
113template <typename T1, typename T2>
114struct WeightMaxMLSR
115{
116 static T2 id(){ return T2(-1, std::numeric_limits<T1>::lowest()); };
117 static bool returnedSAID() { return false; }
118 static MPI_Op mpi_op() { return MPI_MAX; };
119
120 static T2 add(const T2 & arg1, const T2 & arg2)
121 {
122 return std::max(arg1, arg2);
123 }
124
125 static T2 multiply(const T1 & arg1, const T2 & arg2)
126 {
127 return T2(arg2.parent, arg1);
128 }
129
130 static void axpy(const T1 a, const T2 & x, T2 & y)
131 {
132 y = add(y, multiply(a, x));
133 }
134};
135
136
137
138// Designed to pseudo maximize weights on the augmenting paths
139// for boolean matrix (T1 <=> bool), this semiring converts to Select2ndMax semiring
140template <typename T1, typename T2>
141struct WeightMaxMMSR
142{
143 static T2 id(){ return T2(-1, -1, std::numeric_limits<T1>::lowest()); };
144 static bool returnedSAID() { return false; }
145 static MPI_Op mpi_op() { return MPI_MAX; };
146
147 static T2 add(const T2 & arg1, const T2 & arg2)
148 {
149 return std::max(arg1, arg2);
150 }
151
152 static T2 multiply(const T1 & arg1, const T2 & arg2)
153 {
154 return T2(arg2.parent, arg2.root, arg1);
155 }
156
157
158 static void axpy(const T1 a, const T2 & x, T2 & y)
159 {
160 y = add(y, multiply(a, x));
161 }
162};
163
164}
165
166#endif /* MatchingDefs_h */
int64_t IT
static MPI_Op mpi_op()
static bool returnedSAID()
static void axpy(const T1 a, const T2 &x, T2 &y)
static T2 add(const T2 &arg1, const T2 &arg2)
static T2 multiply(const T1 &arg1, const T2 &arg2)
static void axpy(const T1 a, const T2 &x, T2 &y)
static bool returnedSAID()
static T2 add(const T2 &arg1, const T2 &arg2)
static T2 multiply(const T1 &arg1, const T2 &arg2)
static MPI_Op mpi_op()
friend bool operator==(const VertexTypeML &vtx1, const VertexTypeML &vtx2)
friend bool operator<(const VertexTypeML &vtx1, const VertexTypeML &vtx2)
friend std::ostream & operator<<(std::ostream &os, const VertexTypeML &vertex)
VertexTypeML(T1 p=-1, T2 com=0)
friend std::ostream & operator<<(std::ostream &os, const VertexTypeMM &vertex)
friend bool operator==(const VertexTypeMM &vtx1, const VertexTypeMM &vtx2)
VertexTypeMM(IT p=-1, IT r=-1, double w=0)
friend bool operator<(const VertexTypeMM &vtx1, const VertexTypeMM &vtx2)
static void axpy(const T1 a, const T2 &x, T2 &y)
static T2 add(const T2 &arg1, const T2 &arg2)
static T2 multiply(const T1 &arg1, const T2 &arg2)
static bool returnedSAID()
static MPI_Op mpi_op()
static T2 multiply(const T1 &arg1, const T2 &arg2)
static T2 add(const T2 &arg1, const T2 &arg2)
static MPI_Op mpi_op()
static bool returnedSAID()
static void axpy(const T1 a, const T2 &x, T2 &y)