用链表表示矩阵
问题是:
稀疏矩阵的另一种链接表示使用具有向下、向右字段的节点, 鱼子、颜色和价值。稀疏矩阵的每个非零条目都被表示 通过一个节点。零项没有显式存储。节点已链接 一起形成两个循环列表。第一个列表,即行列表,已制作完成 通过使用右侧按行链接节点以及按列在行内链接节点来向上 场。第二个列表,即列列表,由通过以下链接链接节点组成 下场。在此列表中,节点按列链接并在列内链接 按行。这两个列表共享一个公共头节点。此外,还有一个 节点被添加到矩阵的维度。
我希望重载运算符>>还添加和转置方法:
istream & operator>>(istream & in, sparseMatrixLinked<T> x);
//The input format is as follows.
4 4 3 // # of rows, # of cols, # of nonzero entries
0 0 2 // row #, col #, item value #
0 3 1
1 1 7
void sparseMatrixLinked<T>::add(sparseMatrixLinked<T> &b,sparseMatrixLinked<T> &c);
// c = (*this) + b
void sparseMatrixLinked<T>::transpose(sparseMatrixLinked<T> &b) ;
// b is transpose of *this.
我无法找出解决方案。有人可以提供一些建议吗?非常感谢!
The problem is:
An alternative linked representation for sparse matrices uses nodes that have the fields down, right,
roe, col, and value. Each nonzero entry of the sparse matrix is represented
by a node. The zero terms are not explicitly stored. The nodes are linked
together to form two circular lists. The first list, the row list, is made
up by linking nodes by rows and within rows by columns using the right
eld. The second,list, the column list, is made up by linking nodes via the
down field. In this list, nodes are linked by columns and within columns
by rows. These two lists share a common header node. In addition, a
node is added to the dimensions of the matrix.
I hope to overload operator >> and also add and transpose methods:
istream & operator>>(istream & in, sparseMatrixLinked<T> x);
//The input format is as follows.
4 4 3 // # of rows, # of cols, # of nonzero entries
0 0 2 // row #, col #, item value #
0 3 1
1 1 7
void sparseMatrixLinked<T>::add(sparseMatrixLinked<T> &b,sparseMatrixLinked<T> &c);
// c = (*this) + b
void sparseMatrixLinked<T>::transpose(sparseMatrixLinked<T> &b) ;
// b is transpose of *this.
I cannot figure out a solution. Could anyone provide some advice? Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于转置,您可以遍历一个列表,交换所有链接和行/列指针。在伪代码中:
这里是
addNode
,一种(低效)解决方案是通过遍历两个列表来添加单个节点,直到找到节点的插入点,然后将其添加到那里。它可以用在第二个矩阵中的每个节点上,以实现类似+=
; 的功能。首先添加当前矩阵的副本给出add
。有很多更有效的方法来实现
add
,但这些方法留给读者作为练习。operator>>
应该看起来或多或少像这样:}
您可以使用上面这样的算法来实现
addNode
。不过,这还是很慢。这里有一个提示:如果输入以任何方式排序,您可以利用它来更快地构建矩阵。即使没有,执行任意节点插入的更有效方法也会使事情变得更好。For
transpose
, you could traverse one list, swapping all the links and row/col pointers. In pseudocode:Here's
addNode
, one (inefficient) solution is to add individual nodes by traversing both lists until you find the insertion point for your node, and then add it there. It can be used on every node in a second matrix to implement something like+=
; adding a copy of the current matrix first givesadd
.There are much more efficient ways to implement
add
but those are left as an exercise to the reader.operator>>
should look more or less like this:}
You could use an algorithm like the above to implement
addNode
. Again, that's very slow, though. Here's a hint: if the input is sorted in any way, you can take advantage of that to make building the matrix much faster. Even if not, a more efficient way of doing arbitrary node inserts will make things better.