0% found this document useful (0 votes)
14 views24 pages

Question

Long question computer science data structure 2nd semester

Uploaded by

srutismita43
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
14 views24 pages

Question

Long question computer science data structure 2nd semester

Uploaded by

srutismita43
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 24
Question Define sparse matrix ? What are its types ? Explain it with a cuitable example, 20:28 "al 8 Q define sparse matrix bo A sparse matrix is one with a large number of zero entries. A more practical definition is that a matrix is sparse If the number or distribution of the zero entries makes it worthwhile to avoid storing or operating on the zero entries. @ Sept 2020 @ pssnhignam.com What is a Sparse Matrix? - Nick Higham People also ask i What is a sparse matrix? v What do you mean by sparse array? v * Q al Discover Search Collections 20:29 "sal St Be aea Sparse Matrix (Dat... ce) * Javatpvinticvm Nuw, let's see the representativn vf te sparse matrix. Thie nvn-zerv elements in the sparse matrix can be stvred using triplets that are rvws, cvlumns, and values. There are twv ways tv represent the sparse matrix that are listed as fvllvws - © Array representativn © Linked list representativn Array representativn vf tlie sparse matrix Representing a sparse matrix by a 2D array leads tv thie wastage vf Ivts vf memvry. This is because zerves in the matrix are vf nv use, sv stvring zerves wit nvn-zerv elements i¢ wastage vf memvry. Ty avvid suck wastage, we can stvre vnly nvn-zerv elements. If we etre vnly nvn-zerv elements, it reduces the traversal time and te stvrage space. ADVERTISEMENT 20:29" $38 Me ae0m In 2D array representativn vf sparse matrix, tere are tiree fields used that are named as ~ ROW loo) s VALUE © Rew - It is the index vf a rvw wlhiere a nvn- zerv element is Ivcated in thie matrix. © Cvlumn - It is the index vf the cvlumn where a nvn-zerv element is lvcated in thie matrix. © Value - It is the value vf the nvn-zerv element that is lweated at the index (rvw, cvlumn). Example - ADVERTISEMENT 20:29" £38 fe acom Let's understand the array representativn vf sparse matrix wit thie lielp vf thie example given belww - Cynsider the sparse matrix - o.12 3 ‘i o}0 405 S| Mat parse Matrix —> 51 Os 6 2}0 020 3}2 000 441000 In te abvve figure, we can vbserve a Sxit sparse matrix evntaining 7 nvn-zerv elements and 13 zerv elements. The abvve matrix vecupies Sx = 20 memvry space. Increasing the size vf matrix will increase tlie wastage space. ADVERTISEMENT 20:29 "sa 1, Be acom Table Structure | Row | Column Value al In the abvve structure, first cvlumn represents the Oo Oo 1 1 2 3 4 5 rvws, te secvnd evlumn represents tlie evlumns, and tie third evlumn represents the nvn-zerv value. The first rvw vf thie table represents the triplets. The first triplet represents tliat the value 4 is stvred at Ot rvw and ‘Ast evlumn, Similarly, the seevnd triplet represents tat the value 5 is stvred at the Otfi rw and 3rd evlumn, In a similar manner, all triplets represent tlie stvred Weativn vf the nvn-zerv elements in the matrix. 20:33 "nl 38M 2008 Linked List representativn vf the sparse matrix In a linked list representativn, the linked list data structure is used tv represent the sparse matrix. The aiivantage vf using a linked list tv represent the sparse matrix is that the evmplexity vf inserting wr deleting a nvie in a linked lst is lesser thian tlie array. Unlike the array representativn, a nvde in the linked list representativn evnsists vf fur fields. The fyur fields vf the linked list are given as fvllvws ~ ‘Rew - It represents the index vF the rw whiere the nvn-zerv element is Ivcated. Cvlurmn - It represents the index vf the cevlumn whiere the nvn-zerv element is lvcated. Value - It is the value vf the nvn-zerv element that is weated at the index (rvw, cvlumn). Next nvde - It stvres the address vf the next nvde. The nvde structure vf thie linked list representativn 20:34 sal BS Me seo Tlie nvde structure vF the linked list representativn vf tie sparse matrix is stivemn in the betvw image ~ Node Structure Example - Let's understand the linked list representativn vf sparse matrix with the Help vf the example given belvw - Cunsider thie sparse matrix - Sparse Matrix ——> wrnao oowol|o aROO| ouom|ny 2000, In the abwve figure, we can vbserve a Uxtt sparse matrix evntaining 5 nvn-zerv elements and 11 zerv elements. Abvve matrix vecupies Ux = 16 memvry space. Inereasing the size vf matrix will increase the wastage space. 20:34" 188 Me aeow ‘The linked list representativn vf tle abvue matrix given belww - In tie abvve figure, tHe sparse matrix ie represented in the linked list fvrm. In the nvide, the first field represents thie index vf the rvw, the secvnd field field represents tie index vf the evlumn, the represents the value, and te fvurtl field cvntains the aiddress vf tie next nvde. In the abvve figure, the first field vf the first nvde vf tlie linked list cvntains 0, which means Ot rw, the secvnd field cvntains 2, whiicl means 2" cvlumn, and ‘thie third field cvntains 1 that is the nvn-zerv element. Sy, the first nvile represents that clement 1 is stvred at thie Ot ryw-2"4 cylumn in the given sparse matrix. In a simitar manner, all vf the nvies represent the nvn-zerv elements vF thie sparse matrix. Implementativn vf linked list representativn vf sparse matrix Nvw, let's see the implementativn vf linked list 20:34 al #38 Me 2008 Nvw, let's see the implementativn vf array representativn vf sparse matrix in C language. In the prvgram belvw, we will stivw the tabular representativn vf thie nvn-zerv elements vf the sparse matrix stvred in array. ‘include int main() { 1/ Sparse matrix Kaving size 4*5 int sparse_matrix(4)[5) = { {0,0,6,0,9}, {0,0,4,6,0}, {0,0,0,0,0}, {0,1,2,0,0} i 11 size vt matrix int size=0; fur(int 0; 1

You might also like