File: Mongoose_EdgeCutProblem.hpp

package info (click to toggle)
suitesparse 1%3A7.10.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 254,920 kB
  • sloc: ansic: 1,134,743; cpp: 46,133; makefile: 4,875; fortran: 2,087; java: 1,826; sh: 996; ruby: 725; python: 495; asm: 371; sed: 166; awk: 44
file content (213 lines) | stat: -rw-r--r-- 7,156 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* ========================================================================== */
/* === Include/Mongoose_EdgeCutProblem.hpp ================================== */
/* ========================================================================== */

/* -----------------------------------------------------------------------------
 * Mongoose Graph Partitioning Library, Copyright (C) 2017-2023,
 * Scott P. Kolodziej, Nuri S. Yeralan, Timothy A. Davis, William W. Hager
 * Mongoose is licensed under Version 3 of the GNU General Public License.
 * Mongoose is also available under other licenses; contact authors for details.
 * SPDX-License-Identifier: GPL-3.0-only
 * -------------------------------------------------------------------------- */

/**
 * Graph data structure.
 *
 * Stores graph adjacency and weight information. Also used as a container for
 * storing information about matching, coarsening, and partitioning.
 */

// #pragma once
#ifndef MONGOOSE_EDGECUTPROBLEM_HPP
#define MONGOOSE_EDGECUTPROBLEM_HPP

#include "Mongoose_CSparse.hpp"
#include "Mongoose_Graph.hpp"
#include "Mongoose_Internal.hpp"
#include "Mongoose_EdgeCutOptions.hpp"

namespace Mongoose
{

class EdgeCutProblem
{
public:
    /** Graph Data ***********************************************************/
    Int n;     /** # vertices                      */
    Int nz;    /** # edges                         */
    Int *p;    /** Column pointers                 */
    Int *i;    /** Row indices                     */
    double *x; /** Edge weight                     */
    double *w; /** Node weight                     */
    double X;  /** Sum of edge weights             */
    double W;  /** Sum of vertex weights           */

    double H; /** Heuristic max penalty to assess */
    double worstCaseRatio;

    /** Partition Data *******************************************************/
    bool *partition;     /** T/F denoting partition side     */
    double *vertexGains; /** Gains for each vertex           */
    Int *externalDegree; /** # edges lying across the cut    */
    Int *bhIndex;        /** Index+1 of a vertex in the heap */
    Int *bhHeap[2];      /** Heap data structure organized by
                            boundaryGains descending         */
    Int bhSize[2];       /** Size of the boundary heap       */

    /** Cut Cost Metrics *****************************************************/
    double heuCost;   /** cutCost + balance penalty         */
    double cutCost;   /** Sum of edge weights in cut set    */
    Int cutSize;      /** Number of edges in cut set        */
    double W0;        /** Sum of partition 0 vertex weights */
    double W1;        /** Sum of partition 1 vertex weights */
    double imbalance; /** Degree to which the partitioning
                          is imbalanced, and this is
                          computed as (0.5 - W0/W).         */

    /** Matching Data ********************************************************/
    EdgeCutProblem *parent;    /** Link to the parent graph        */
    Int clevel;       /** Coarsening level for this graph */
    Int cn;           /** # vertices in coarse graph      */
    Int *matching;    /** Linked List of matched vertices */
    Int *matchmap;    /** Map from fine to coarse vertices */
    Int *invmatchmap; /** Map from coarse to fine vertices */
    Int *matchtype;   /** Vertex's match classification
                           0: Orphan
                           1: Standard (random, hem, shem)
                           2: Brotherly
                           3: Community                   */
    Int singleton;

    /* Constructor & Destructor */
    static EdgeCutProblem *create(const Int _n, const Int _nz, Int *_p = NULL,
                                  Int *_i = NULL, double *_x = NULL, double *_w = NULL);
    static EdgeCutProblem *create(const Graph *_graph);
    static EdgeCutProblem *create(EdgeCutProblem *_parent);
    ~EdgeCutProblem();
    void initialize(const EdgeCut_Options *options);

    /** Matching Functions ****************************************************/
    inline bool isMatched(Int vertex)
    {
        return (matching[vertex] > 0);
    }

    inline Int getMatch(Int vertex)
    {
        return (matching[vertex] - 1);
    }

    inline void createMatch(Int vertexA, Int vertexB, MatchType matchType)
    {
        matching[vertexA]  = (vertexB) + 1;
        matching[vertexB]  = (vertexA) + 1;
        invmatchmap[cn]    = vertexA;
        matchtype[vertexA] = matchType;
        matchtype[vertexB] = matchType;
        matchmap[vertexA]  = cn;
        matchmap[vertexB]  = cn;
        cn++;
    }

    inline void createCommunityMatch(Int vertexA, Int vertexB,
                                     MatchType matchType)
    {
        Int vm[4] = { -1, -1, -1, -1 };
        vm[0]     = vertexA;
        vm[1]     = getMatch(vm[0]);
        vm[2]     = getMatch(vm[1]);
        vm[3]     = getMatch(vm[2]);

        bool is3Way = (vm[0] == vm[3]);
        if (is3Way)
        {
            matching[vm[1]] = vertexA + 1;
            createMatch(vm[2], vertexB, matchType);
        }
        else
        {
            matching[vertexB]  = matching[vertexA];
            matching[vertexA]  = vertexB + 1;
            matchmap[vertexB]  = matchmap[vertexA];
            matchtype[vertexB] = matchType;
        }
    }

    /** Boundary Heap Functions ***********************************************/
    inline Int BH_getParent(Int a)
    {
        return ((a - 1) / 2);
    }

    inline Int BH_getLeftChild(Int a)
    {
        return (2 * a + 1);
    }

    inline Int BH_getRightChild(Int a)
    {
        return (2 * a + 2);
    }

    inline bool BH_inBoundary(Int v)
    {
        return (bhIndex[v] > 0);
    }

    inline void BH_putIndex(Int v, Int pos)
    {
        bhIndex[v] = (pos + 1);
    }

    inline Int BH_getIndex(Int v)
    {
        return (bhIndex[v] - 1);
    }

    /** Mark Array Functions **************************************************/
    inline void mark(Int index)
    {
        markArray[index] = markValue;
    }

    inline void unmark(Int index)
    {
        markArray[index] = 0;
    }

    inline bool isMarked(Int index)
    {
        return markArray[index] == markValue;
    }

    inline Int getMarkValue()
    {
        return markValue;
    }

    void clearMarkArray();
    void clearMarkArray(Int incrementBy);

private:
    EdgeCutProblem();

    /** Memory Management Flags ***********************************************/
    bool shallow_p;
    bool shallow_i;
    bool shallow_x;
    bool shallow_w;

    /** Mark Data *************************************************************/
    Int *markArray; /** O(n) mark array                 */
    Int markValue;  /** Mark array can be cleared in O(k)
                        by incrementing markValue.
                        Implicitly, a mark value less than
                        markValue is unmarked.          */
    void resetMarkArray();
    bool initialized; // Used to mark if the graph has been initialized
                      // previously.
};

} // end namespace Mongoose

#endif