problemscpp
A collection of my answers to algorithm problems in c++.
载入中...
搜索中...
未找到
comp526::stable_matching 命名空间参考

class  Company
 
class  Solution
 
class  Student
 

函数

vector< vector< int > > convert_prefs_to_matrix (const vector< vector< int > > &prefs)
 
 TEST (StableMatchingTest, MultiRound3x3)
 
 TEST (StableMatchingTest, Simple2x2)
 
 TEST (StableMatchingTest, Wikipedia3x3Example)
 
 TEST (StableMatchingTest, WorstCaseForStudents)
 

函数说明

◆ convert_prefs_to_matrix()

vector< vector< int > > comp526::stable_matching::convert_prefs_to_matrix ( const vector< vector< int > > & prefs)

在文件 comp526_test.cpp14 行定义.

14 {
15 int n = prefs.size();
16 vector<vector<int>> matrix(n, vector<int>(n));
17 for(int i = 0; i < n; ++i) {
18 for(int rank = 0; rank < n; ++rank) {
19 int partner_id = prefs[i][rank];
20 matrix[i][partner_id] = rank;
21 }
22 }
23 return matrix;
24 }

被这些函数引用 TEST(), TEST(), TEST() , 以及 TEST().

◆ TEST() [1/4]

comp526::stable_matching::TEST ( StableMatchingTest ,
MultiRound3x3  )

在文件 comp526_test.cpp71 行定义.

71 {
72 // A case designed to require multiple rounds of proposals and rejections.
73 // Students:
74 // S0: C0 > C1 > C2
75 // S1: C1 > C0 > C2
76 // S2: C0 > C1 > C2
77 // Companies:
78 // C0: S1 > S2 > S0
79 // C1: S0 > S1 > S2
80 // C2: S0 > S1 > S2
81 // Expected Student-Optimal Matching: (S0, C1), (S1, C0), (S2, C2)
82 // Trace:
83 // 1. S0->C0, S1->C1, S2->C0. C0 keeps S2 (better rank), rejects S0. Matches: (S2,C0), (S1,C1).
84 // 2. S0 (free) -> C1. C1 has S1, prefers S0. C1 keeps S0, rejects S1. Matches: (S2,C0), (S0,C1).
85 // 3. S1 (free) -> C0. C0 has S2, prefers S1. C0 keeps S1, rejects S2. Matches: (S1,C0), (S0,C1).
86 // 4. S2 (free) -> C1. C1 has S0, prefers S0. Rejects S2.
87 // 5. S2 (free) -> C2. C2 is free, accepts S2.
88 // Final: (S0,C1), (S1,C0), (S2,C2)
89 vector<vector<int>> s_prefs = {{0, 1, 2}, {1, 0, 2}, {0, 1, 2}};
90 vector<vector<int>> c_prefs = {{1, 2, 0}, {0, 1, 2}, {0, 1, 2}};
91
92 vector<vector<int>> s2c = convert_prefs_to_matrix(s_prefs);
93 vector<vector<int>> c2s = convert_prefs_to_matrix(c_prefs);
94
95 Solution sol;
96 vector<int> expected_matches = {1, 0, 2};// ans[0]=C1, ans[1]=C0, ans[2]=C2
97 ASSERT_EQ(sol.match(s2c, c2s), expected_matches);
98 }
vector< vector< int > > convert_prefs_to_matrix(const vector< vector< int > > &prefs)

引用了 convert_prefs_to_matrix() , 以及 comp526::stable_matching::Solution::match().

◆ TEST() [2/4]

comp526::stable_matching::TEST ( StableMatchingTest ,
Simple2x2  )

在文件 comp526_test.cpp26 行定义.

26 {
27 // A simple case where both students prefer Company 0.
28 // Students:
29 // S0: C0 > C1
30 // S1: C0 > C1
31 // Companies:
32 // C0: S1 > S0
33 // C1: S0 > S1
34 // Expected Student-Optimal Matching: (S0, C1), (S1, C0)
35 // S0 proposes to C0, S1 proposes to C0. C0 prefers S1 and rejects S0.
36 // S0 then proposes to C1, who is free and accepts.
37 vector<vector<int>> s_prefs = {{0, 1}, {0, 1}};
38 vector<vector<int>> c_prefs = {{1, 0}, {0, 1}};
39
40 vector<vector<int>> s2c = convert_prefs_to_matrix(s_prefs);
41 vector<vector<int>> c2s = convert_prefs_to_matrix(c_prefs);
42
43 Solution sol;
44 vector<int> expected_matches = {1, 0};// ans[0]=C1, ans[1]=C0
45 ASSERT_EQ(sol.match(s2c, c2s), expected_matches);
46 }

引用了 convert_prefs_to_matrix() , 以及 comp526::stable_matching::Solution::match().

◆ TEST() [3/4]

comp526::stable_matching::TEST ( StableMatchingTest ,
Wikipedia3x3Example  )

在文件 comp526_test.cpp48 行定义.

48 {
49 // This is the classic example from the Stable Marriage Problem Wikipedia page.
50 // Students (Men): A=0, B=1, C=2
51 // Companies (Women): X=0, Y=1, Z=2
52 // Prefs:
53 // A: Y > X > Z -> {1, 0, 2}
54 // B: Z > Y > X -> {2, 1, 0}
55 // C: X > Z > Y -> {0, 2, 1}
56 // X: B > A > C -> {1, 0, 2}
57 // Y: C > B > A -> {2, 1, 0}
58 // Z: A > C > B -> {0, 2, 1}
59 // Expected Student-Optimal (Men-Optimal) Matching: (A,Y), (B,Z), (C,X)
60 vector<vector<int>> s_prefs = {{1, 0, 2}, {2, 1, 0}, {0, 2, 1}};
61 vector<vector<int>> c_prefs = {{1, 0, 2}, {2, 1, 0}, {0, 2, 1}};
62
63 vector<vector<int>> s2c = convert_prefs_to_matrix(s_prefs);
64 vector<vector<int>> c2s = convert_prefs_to_matrix(c_prefs);
65
66 Solution sol;
67 vector<int> expected_matches = {1, 2, 0};// ans[0]=C1, ans[1]=C2, ans[2]=C0
68 ASSERT_EQ(sol.match(s2c, c2s), expected_matches);
69 }

引用了 convert_prefs_to_matrix() , 以及 comp526::stable_matching::Solution::match().

◆ TEST() [4/4]

comp526::stable_matching::TEST ( StableMatchingTest ,
WorstCaseForStudents  )

在文件 comp526_test.cpp100 行定义.

100 {
101 // A case where every student gets their last choice.
102 // Students:
103 // S0: C0 > C1
104 // S1: C0 > C1
105 // Companies:
106 // C0: S1 > S0
107 // C1: S1 > S0
108 // Expected Student-Optimal Matching: (S0, C1), (S1, C0)
109 // S0 proposes to C0, S1 proposes to C0. C0 takes S1, rejects S0.
110 // S0 proposes to C1. C1 takes S0. Final: (S0,C1), (S1,C0).
111 // S0 gets 2nd choice. S1 gets 1st choice.
112 vector<vector<int>> s_prefs = {{0, 1}, {0, 1}};
113 vector<vector<int>> c_prefs = {{1, 0}, {1, 0}};
114
115 vector<vector<int>> s2c = convert_prefs_to_matrix(s_prefs);
116 vector<vector<int>> c2s = convert_prefs_to_matrix(c_prefs);
117
118 Solution sol;
119 vector<int> expected_matches = {1, 0};
120 ASSERT_EQ(sol.match(s2c, c2s), expected_matches);
121 }

引用了 convert_prefs_to_matrix() , 以及 comp526::stable_matching::Solution::match().