Sparse matrices

1,143 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,143
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
27
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Sparse matrices

  1. 1. Sparse Matrices Steve Paks
  2. 2. Sparse Matrices • Definition – head node • • • • using the down field to link into a column list using the right field to link into a row list the next field links the head nodes the total number of head nods = max{number of rows, number of columns} entry – entry node • • the down field links to the next nonzero term in the same column the right field links to the next nonzero term in the same row
  3. 3. Sparse Matrices(Cont’d) • Declarations class SparseMatrices{ int MAX_SIZE = 50; MatrixNode hdnode[MAX_SIZE]; enum TagField{ head, entry } class EntryNode{ int row; int col; int value; } class MatrixNode{ MatrixNode down; MatrixNode right; TagField tag; MatrixNode next; EntryNode entry; } }
  4. 4. Sparse Matrices(Cont’d) • mread() Matrix Sparse Matrix Linked Representation Of The Sparse Matrix
  5. 5. Sparse Matrices(Cont’d) entry • Algorithm node temp e h hdnode[0] last temp … h temp h hdnode[4] currentRow = 0 h e h e last temp 2 0 2 11 temp h 0 11 temp hdnode[0] hdnode[0] 4 hdnode[1] hdnode[0] last 4 e 0 11 2
  6. 6. Sparse Matrices(Cont’d) entry • Algorithm(Cont’d) h h hdnode[0] currentRow = 0 hdnode[2] e last 0 2 11 h h hdnode[0] hdnode[2] last temp e 0 11 2 currentRow = 1
  7. 7. Sparse Matrices(Cont’d) entry • Algorithm(Cont’d) h h hdnode[0] last hdnode[1] hdnode[2] h e 0 11 h h hdnode[0] last hdnode[1] 2 hdnode[2] h e 0 2 11 e temp 1 12 0 currentRow = 1
  8. 8. Sparse Matrices(Cont’d) entry • Algorithm(Cont’d) currentRow = 1 h h hdnode[0] hdnode[2] h e 0 2 11 hdnode[1] e last temp 1 12 0
  9. 9. Sparse Matrices(Cont’d) entry • Algorithm(Cont’d) currentRow = 1 h h hdnode[0] hdnode[2] h e 0 2 11 hdnode[1] e last temp 1 12 0
  10. 10. Sparse Matrices(Cont’d) entry • Algorithm(Cont’d) currentRow = 1 h h hdnode[0] hdnode[2] h e 0 2 11 hdnode[1] e last temp 1 12 0
  11. 11. Sparse Matrices(Cont’d) entry • Algorithm(Cont’d) currentRow = 2 h h last hdnode[2] hdnode[0] h e 0 2 11 hdnode[1] e temp 1 12 0
  12. 12. Sparse Matrices(Cont’d) entry • Algorithm(Cont’d) currentRow = 2 h h hdnode[0] hdnode[2] h e 0 2 11 hdnode[1] e 1 12 temp last e 2 -4 1 0
  13. 13. Sparse Matrices(Cont’d) entry • Algorithm(Cont’d) currentRow = 2 h h hdnode[0] hdnode[2] h e 0 2 11 hdnode[1] e 1 12 temp e last 2 -4 1 0
  14. 14. Sparse Matrices(Cont’d) entry • Algorithm(Cont’d) currentRow = 2 h h hdnode[0] hdnode[2] h e 0 2 11 hdnode[1] e 1 12 temp e last 2 -4 1 0
  15. 15. Sparse Matrices(Cont’d) entry • Algorithm(Cont’d) currentRow = 3 h last h hdnode[0] hdnode[2] h h hdnode[3] e 0 2 11 hdnode[1] e 1 0 12 e 2 1 -4 e temp 3 -15 3
  16. 16. Sparse Matrices(Cont’d) entry • Algorithm(Cont’d) currentRow = 3 h last h hdnode[0] hdnode[2] h h hdnode[3] e 0 2 11 hdnode[1] e 1 0 12 e 2 1 -4 e temp 3 -15 3
  17. 17. Sparse Matrices(Cont’d) entry • Algorithm(Cont’d) currentRow = 3 h h hdnode[0] h hdnode[2] h hdnode[3] e 0 2 11 hdnode[1] e 1 0 12 e 2 1 -4 temp last e 3 -15 3
  18. 18. Sparse Matrices(Cont’d) entry • Algorithm(Cont’d) currentRow = 3 h h hdnode[0] h hdnode[2] h hdnode[3] e 0 2 11 hdnode[1] e 1 0 12 e 2 1 -4 temp last e 3 -15 3
  19. 19. Sparse Matrices(Cont’d) • e Algorithm(Cont’d) 4 4 node h h hdnode[0] h hdnode[2] h hdnode[3] e 0 2 11 hdnode[1] e 1 0 12 e 2 1 -4 temp last e 3 -15 3
  20. 20. Sparse Matrices(Cont’d) • Analysis of mread – – – – – O(max{numRows, numCols}) = new keyword works in a constant amount of time. O(numTerms) = in a constant amount of time to set up each none zero O(max{numRows, numCols}, numTerms) = O(numRows + numCols + numTerms) Sparse Matrix using two-dimensional array • O(numRows∙numCols) linked list is better, but it is slightly worse than sequential method
  21. 21. Sparse Matrices(Cont’d) • mwrite() – – – – • using two for loop outer for loop = the number of rows inner for loop = the number of terms O(numRows + numTerms) merase() – – – – – – resembles the structure found in mwrite() erasing the entry nodes resembles the structure found in mwrite() O(numRows + numTerms) requiring extra time to erase the head nodes O(numRows + numCols) finally, O(numRows + numCols + numTerms)
  22. 22. Sparse Matrices(Cont’d) • Union in c vs Inheritance in java AbstractNode down : AbstractNode right : AbstractNode tag : TagField getEntry() getNext() setNext(AbstractNode) VS HeadNode next : AbstractNode getEntry() getNext() setNext(AbstractNode) EntryNode entry : Entry entry EntryNode() getEntry()

×