查看: 2299| 回复: 0
跳转到指定楼层
上一主题 下一主题
收起左侧

[数组] 稀疏矩阵乘法 - 微软题

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
题目 : 实现三元组表示的稀疏矩阵乘法
思路 : 基础方法,将两个稀疏矩阵分别存储到两个哈希表中,若两个矩阵的size分别为(m, n)(n, r),进行m->r->n三重循环,若该点在哈希表中,则进行累加,若累加结果不为0,则将其加入返回结果中。
代码 :
  1. #include <iostream>
  2. #include <vector>
  3. #include <unordered_map>

  4. using namespace std;

  5. struct Point {
  6.   int first_point;
  7.   int second_point;
  8.   Point (int x, int y) : first_point(x), second_point(y) {}
  9. };

  10. struct HashFunc {
  11.   size_t operator()(const Point& p) const {
  12.     return ((hash<int>()(p.first_point) ^ (hash<int>()(p.second_point) << 1)) >> 1);
  13.   }
  14. };

  15. struct EqualPoint {
  16.   bool operator ()(const Point& p1, const Point& p2) const {
  17.     return p1.first_point == p2.first_point && p1.second_point == p2.second_point;
  18.   }
  19. };

  20. class SparseMatrixOperator {
  21. public:
  22.   vector<vector<int>> SparseMatrixMultiply(vector<vector<int>>& matrix1, vector<vector<int>>& matrix2, int m, int n, int r) {
  23.     vector<vector<int>> matrix_multiply;
  24.     unordered_map<Point, int, HashFunc, EqualPoint> matrix_store1;
  25.     for (vector<int> iter : matrix1) {
  26.       Point p(iter[0], iter[1]);
  27.       matrix_store1[p] = iter[2];
  28.     }
  29.     unordered_map<Point, int, HashFunc, EqualPoint> matrix_store2;
  30.     for (vector<int> iter : matrix2) {
  31.       Point p(iter[0], iter[1]);
  32.       matrix_store2[p] = iter[2];
  33.     }
  34.     for (int i = 0; i < m; i++) {
  35.       for (int j = 0; j < r; j++) {
  36.         int element_multiply = 0;
  37.         for (int k = 0; k < n; k++) {
  38.           Point p1(i, k);
  39.           Point p2(k, j);
  40.           if (matrix_store1.find(p1) != matrix_store1.end() && matrix_store2.find(p2) != matrix_store2.end()) {
  41.             element_multiply += matrix_store1[p1] * matrix_store2[p2];
  42.           }
  43.         }
  44.         if (element_multiply != 0) {
  45.           matrix_multiply.push_back({i, j, element_multiply});
  46.         }
  47.       }
  48.     }
  49.     return matrix_multiply;
  50.   }

  51.   void print(vector<vector<int>>& matrix, int m, int n) {
  52.     unordered_map<Point, int, HashFunc, EqualPoint> element_store;
  53.     for (vector<int> iter : matrix) {
  54.       Point p(iter[0], iter[1]);
  55.       element_store[p] = iter[2];
  56.     }
  57.     for (int i = 0; i < m; i++) {
  58.       for (int j = 0; j < n; j++) {
  59.         Point p(i, j);
  60.         if (element_store.find(p) != element_store.end()) {
  61.           cout << element_store[p] << " ";
  62.         } else {
  63.           cout << "0" << " ";
  64.         }
  65.       }
  66.       cout << endl;
  67.     }
  68.   }
  69. };

  70. int main() {
  71.   // matrix1
  72.   // 1 0
  73.   // 0 2
  74.   // 0 0
  75.   vector<vector<int>> matrix1 = {{0, 0, 1}, {1, 1, 2}};
  76.   // matrix2
  77.   // 3 0 0 4
  78.   // 0 0 5 0
  79.   vector<vector<int>> matrix2 = {{0, 0, 3}, {0, 3, 4}, {1, 2, 5}};
  80.   SparseMatrixOperator sparse_matrix_operator;
  81.   vector<vector<int>> matrix_multiply =  sparse_matrix_operator.SparseMatrixMultiply(matrix1, matrix2, 3, 2, 4);
  82.   cout << "matrix1:" << endl;
  83.   sparse_matrix_operator.print(matrix1, 3, 2);
  84.   cout << "matrix2:" << endl;
  85.   sparse_matrix_operator.print(matrix2, 2, 4);
  86.   cout << "matrix_multiply:" << endl;
  87.   sparse_matrix_operator.print(matrix_multiply, 3, 4);
  88.   return 0;
  89. }
复制代码

上一篇:寻找mock interview的童鞋们
下一篇:约小伙伴一起刷题,坐标东湾Fremont
您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表