MCPcopy Create free account
hub / github.com/OpenDriveLab/OpenLane / pipartiteGraph

Class pipartiteGraph

eval/LANE_evaluation/lane2d/include/hungarianGraph.hpp:29–91  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

27using namespace std;
28
29struct pipartiteGraph {
30 vector<vector<double> > mat;
31 vector<bool> leftUsed, rightUsed;
32 vector<double> leftWeight, rightWeight;
33 vector<int>rightMatch, leftMatch;
34 int leftNum, rightNum;
35 bool matchDfs(int u) {
36 leftUsed[u] = true;
37 for (int v = 0; v < rightNum; v++) {
38 if (!rightUsed[v] && fabs(leftWeight[u] + rightWeight[v] - mat[u][v]) < 1e-2) {
39 rightUsed[v] = true;
40 if (rightMatch[v] == -1 || matchDfs(rightMatch[v])) {
41 rightMatch[v] = u;
42 leftMatch[u] = v;
43 return true;
44 }
45 }
46 }
47 return false;
48 }
49 void resize(int leftNum, int rightNum) {
50 this->leftNum = leftNum;
51 this->rightNum = rightNum;
52 leftMatch.resize(leftNum);
53 rightMatch.resize(rightNum);
54 leftUsed.resize(leftNum);
55 rightUsed.resize(rightNum);
56 leftWeight.resize(leftNum);
57 rightWeight.resize(rightNum);
58 mat.resize(leftNum);
59 for (int i = 0; i < leftNum; i++) mat[i].resize(rightNum);
60 }
61 void match() {
62 for (int i = 0; i < leftNum; i++) leftMatch[i] = -1;
63 for (int i = 0; i < rightNum; i++) rightMatch[i] = -1;
64 for (int i = 0; i < rightNum; i++) rightWeight[i] = 0;
65 for (int i = 0; i < leftNum; i++) {
66 leftWeight[i] = -1e5;
67 for (int j = 0; j < rightNum; j++) {
68 if (leftWeight[i] < mat[i][j]) leftWeight[i] = mat[i][j];
69 }
70 }
71
72 for (int u = 0; u < leftNum; u++) {
73 while (1) {
74 for (int i = 0; i < leftNum; i++) leftUsed[i] = false;
75 for (int i = 0; i < rightNum; i++) rightUsed[i] = false;
76 if (matchDfs(u)) break;
77 double d = 1e10;
78 for (int i = 0; i < leftNum; i++) {
79 if (leftUsed[i] ) {
80 for (int j = 0; j < rightNum; j++) {
81 if (!rightUsed[j]) d = min(d, leftWeight[i] + rightWeight[j] - mat[i][j]);
82 }
83 }
84 }
85 if (d == 1e10) return ;
86 for (int i = 0; i < leftNum; i++) if (leftUsed[i]) leftWeight[i] -= d;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected