c++ - 递归矩阵乘法算法计算失败

标签 c++ recursion dynamic-programming matrix-multiplication

我们的任务是使用动态规划用 C++ 编写程序以实现矩阵乘法。他告诉我们使用递归并给了我们一个自定义的书面矩阵类。我编写了以下递归算法,但是当我运行时出现错误

Object& Matrix<Object>::at(uint, uint) [with Object = unsigned int, uint = unsigned int]: Assertions 'row < rows && col < cols' failed.

关于为什么会发生这种情况的任何想法?我在下面包括了他的矩阵类和我的递归矩阵乘法。

#ifndef MATRIX_H
#define MATRIX_H

#include <cassert>
typedef unsigned int uint;

template <class Object>
class Matrix
{
public:
    Matrix( uint rows, uint cols );
    Object & at( uint row, uint col );
    const Object & at( uint row, uint col ) const;
    ~Matrix();
    Matrix( const Matrix<Object> & m ); // Copy constructor
    Matrix & operator= ( const Matrix<Object> & m );   // Assignment operator
    uint numrows() const;
    uint numcols() const;

private:
    uint rows;
    uint cols;
    Object* data;
};

template <class Object>
Matrix<Object>::Matrix( uint rows, uint cols )
: rows( rows ), cols( cols )
{
    assert( rows > 0 && cols > 0 );
    data = new Object[ rows * cols ];
}

template <class Object>
Matrix<Object>::~Matrix()
{
    delete[] data;
}

template <class Object>
Object & Matrix<Object>::at( uint row, uint col )
{
    assert( row < rows && col < cols );
    return data[ cols * row + col ];
}

template <class Object>
const Object & Matrix<Object>::at( uint row, uint col ) const
{
    assert( row < rows && col < cols );
    return data[ cols * row + col ];
}

template <class Object>
uint Matrix<Object>::numrows() const
{
    return rows;
}

template <class Object>
uint Matrix<Object>::numcols() const
{
    return cols;
}

int minmult( Matrix<uint> & P,
         Matrix<uint> & M,
         const vector<uint> & d,
         uint i,
         uint j )
{


if( M.at(i,j) != INF )
{
    return M.at(i,j);               //already has been defined
}
else if( i == j )
{
    M.at(i,j) = 0;                  //base case
}
else
{
    //M.at(i,j) = UINT_MAX;         //initialize to infinity
    for( uint k = i; k <= j-1; k++)
    {
        uint ops = minmult(P, M, d, i, k)
            + minmult(P, M, d, k+1, j)
            + d.at(i-1)*d.at(k)*d.at(j);
        if( ops < M.at(i,j))
        {
            M.at(i,j) = ops;        
            P.at(i,j) = k;          
        }
    }
}
return M.at(i,j);                  //returns the final cost
}

最佳答案

错误似乎很明显,您正在调用 at 方法并传递不小于 rowcol 的值行数和列数......这在代码中很明显:

uint i = M.numcols();
uint j = M.numrows();
if(i == j) {
    M.at(i,j) = 0;    // i == numcols() thus !(i<numcols()) 
                      // j == numrows() thus !(j<numrows())

假设您打算调用 M.at(j,i),也就是说,因为 at 的参数是 rows 并且cols 而不是相反...

除此之外,您的递归步骤是错误的,因为递归的下一步没有比原始问题更小的问题(它实际上具有完全相同的大小,因为 minmult(M,P,d ) 调用 minmult(M,P,d)。一旦你修复了断言,这将以堆栈溢出的形式踢你。

最后,不清楚代码的意图是什么,你应该花时间用笔和纸来解决问题,然后将解决方案映射到你选择的编程语言上。

关于c++ - 递归矩阵乘法算法计算失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15215235/

相关文章:

c++ - cin.ignore()为什么忽略getline输入的第一个字符?

algorithm - 这是NP完全的吗?如果是,背包、MIS、设置填充或调度?

c++ - C++ fmt::print 与 fmt::format_to 命名的技术背景?

c++ - C++ 中的图像 bmp 中的 Alpha 混合水印 bmp

c++ - C/C++ 编译器是否会对可交换运算符(例如 : +, *)进行重新排序以优化常量

c++ - 递归函数中的for循环在递归结束后继续

java - 如何从linkedList中递归删除一个项目?

java - "non-static variable this cannot be referenced from a static context"?

javascript - 禁用 Reactjs 中的依赖下拉选项

python - LeetCode "Maximum Points You Can Obtain from Cards"python 代码无法正常工作,我需要显示原因