SlideShare a Scribd company logo
MATRIX With Arrays
&
Polynomial Representation
using Linked List
Department of CSE
Row Major Order
• When it comes to organizing and accessing elements in a multi-dimensional array, two prevalent methods are Row Major Order and Column
Major Order.
• These approaches define how elements are stored in memory and impact the efficiency of data access in computing.
• Row Major Order
• Row major ordering assigns successive elements, moving across the rows and then down the next row, to successive memory locations.
In simple language, the elements of an array are stored in a Row-Wise fashion.
• To find the address of the element using row-major order uses the following formula:
Address of A [I][J] = B + W * ((I – LR) * N + (J – LC))
I = Row Subset of an element whose address to be found,
J = Column Subset of an element whose address to be found,
B = Base address,
W = Storage size of one element store in an array(in byte),
LR = Lower Limit of row/start row index of the matrix(If not given assume it
as zero),
LC = Lower Limit of column/start column index of the matrix(If not given
assume it as zero),
N = Number of column given in the matrix.
How to find address using Row Major Order?
Given an array, arr[1………10][1………15] with base value 100 and the size
of each element is 1 Byte in memory. Find the address of arr[8][6] with the
help of row-major order.
Solution:
Given:
Base address B = 100
Storage size of one element store in any array W = 1 Byte
Row Subset of an element whose address to be found I = 8
Column Subset of an element whose address to be found J = 6
Lower Limit of row/start row index of matrix LR = 1
Lower Limit of column/start column index of matrix = 1
Number of column given in the matrix N = Upper Bound – Lower Bound + 1
= 15 – 1 + 1
= 15
Formula:
Address of A[I][J] = B + W * ((I – LR) * N + (J – LC))
Solution:
Address of A[8][6] = 100 + 1 * ((8 – 1) * 15 + (6 – 1))
= 100 + 1 * ((7) * 15 + (5))
= 100 + 1 * (110)
Column Major Order
• If elements of an array are stored in a column-major fashion means moving across the column and then to the next column then it’s in column-
major order. To find the address of the element using column-major order use the following formula:
Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))
I = Row Subset of an element whose address to be found,
J = Column Subset of an element whose address to be found,
B = Base address,
W = Storage size of one element store in any array(in byte),
LR = Lower Limit of row/start row index of matrix(If not given
assume it as zero),
LC = Lower Limit of column/start column index of matrix(If
not given assume it as zero),
M = Number of rows given in the matrix.
How to find address using Column Major Order?
Given an array arr[1………10][1………15] with a base value of 100 and the size of each
element is 1 Byte in memory find the address of arr[8][6] with the help of column-major
order.
Solution:
Given:
Base address B = 100
Storage size of one element store in any array W = 1 Bytes
Row Subset of an element whose address to be found I = 8
Column Subset of an element whose address to be found J = 6
Lower Limit of row/start row index of matrix LR = 1
Lower Limit of column/start column index of matrix = 1
Number of Rows given in the matrix M = Upper Bound – Lower Bound + 1
= 10 – 1 + 1
= 10
Formula: used
Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))
Address of A[8][6] = 100 + 1 * ((6 – 1) * 10 + (8 – 1))
= 100 + 1 * ((5) * 10 + (7))
= 100 + 1 * (57)
Address of A[I][J] = 157
From the above examples, it can be observed that for
the same position two different address locations are
obtained that’s because in row-major order movement is
done across the rows and then down to the next row,
and in column-major order, first move down to the first
column and then next column. So both the answers are
right.
Sparse Matrix
• A matrix is a two-dimensional data object made of m rows and n columns, therefore having total m x n values. If most of the
elements of the matrix have 0 value, then it is called a sparse matrix.
• Why to use Sparse Matrix instead of simple matrix ?
• Storage: There are lesser non-zero elements than zeros and thus lesser memory can be used to store only those elements.
• Computing time: Computing time can be saved by logically designing a data structure traversing only non-zero elements..
Example:
0 0 3 0 4
0 0 5 7 0
0 0 0 0 0
0 2 6 0 0
Representing a sparse matrix by a 2D array leads to wastage of lots of memory as zeroes in the matrix are of no use in most of the
cases. So, instead of storing zeroes with non-zero elements, we only store non-zero elements. This means storing non-zero
elements with triples- (Row, Column, value).
Sparse Matrix Representations can be done in many ways following are two common representations:
Array representation
Linked list representation
Why is a sparse matrix required if we can use the simple matrix to store
elements?
There are the following benefits of using the sparse matrix -
Storage - We know that a sparse matrix contains lesser non-zero elements than zero,
so less memory can be used to store elements. It evaluates only the non-zero
elements.
Computing time: In the case of searching in sparse matrix, we need to traverse only
the non-zero elements rather than traversing all the sparse matrix elements. It saves
computing time by logically designing a data structure traversing non-zero elements.
Sparse Matrix Representation (ARRAY)
• Array representation of the sparse matrix
• Representing a sparse matrix by a 2D array leads to the wastage of lots of memory. This is because zeroes in the matrix are of no use, so storing
zeroes with non-zero elements is wastage of memory. To avoid such wastage, we can store only non-zero elements. If we store only non-zero
elements, it reduces the traversal time and the storage space.
• In 2D array representation of sparse matrix, there are three fields used that are named as -
• Row - It is the index of a row where a non-zero element is located in the matrix.
• Column - It is the index of the column where a non-zero element is located in the matrix.
• Value - It is the value of the non-zero element that is located at the index (row, column).
• Time Complexity: O(NM), where N is the number of rows in the sparse matrix, and M is the number of columns in the
sparse matrix.
• Auxiliary Space: O(NM), where N is the number of rows in the sparse matrix, and M is the number of columns in the sparse
matrix.
Sparse Matrix Representation (ARRAY)
• Let's understand the array representation of sparse matrix with the help of the example given below -
• Consider the sparse matrix –
• In the above figure, we can observe a 5x4 sparse matrix containing 7 non-zero elements and 13 zero elements. The above matrix occupies 5x4 =
20 memory space. Increasing the size of matrix will increase the wastage space.
• The tabular representation of the above matrix given in Fig: 1 is called Triplet Representation
• In the above structure, in Fig: 1 first column represents the rows, the second column represents the columns, and the third column represents the
non-zero value.
• The first row of the table represents the triplets. The first triplet represents that the value 4 is stored at 0th row and 1st column. Similarly, the
second triplet represents that the value 5 is stored at the 0th row and 3rd column. In a similar manner, all triplets represent the stored location of
the non-zero elements in the matrix.
• The size of the table depends upon the total number of non-zero elements in the given sparse matrix.
• Above table occupies 8x3 = 24 memory space which is more than the space occupied by the sparse matrix. So, what's the benefit of using the
sparse matrix? Consider the case if the matrix is 8*8 and there are only 8 non-zero elements in the matrix, then the space occupied by the sparse
matrix would be 8*8 = 64, whereas the space occupied by the table represented using triplets would be 8*3 = 24.
Fig: 1
Sparse Matrix Representation (Linked List)
• In linked list, each node has four fields. These four fields are defined as:
• Row: Index of row, where non-zero element is located
• Column: Index of column, where non-zero element is located
• Value: Value of the non zero element located at index – (row,column)
• Next node: Address of the next node
• Time Complexity: O(N*M), where N is the number of rows in the sparse matrix, and M is the number of columns in the sparse matrix.
Auxiliary Space: O(K), where K is the number of non-zero elements in the array.
Addition of Two Sparse Matrices
7 0 0 0 0 6
0 4 0 0 3 0
0 1 0 1 0 0
3 0 0 4 8 7
Representation
of the two
sparse matrices
using singly
linked list
Addition of Two Sparse Matrices
BEGIN:
List3 = NULL
P = List1 Q = List2
WHILE P!= NULLAnd Q! = NULL DO
IF P->Row = = Q->Row THEN
IF P->Col = = Q->Col THEN
InsEnd(List3, P->Row, P->Col, P->Data+Q-
>Data)
P = P->Next
Q = Q->Next
ELSE
IF P->Col < Q->Col THEN
InsEnd(List3, P->Row, P->Col, P->Data)
ELSE
InsEnd(List3, Q->Row, Q->Col, Q->Data)
ELSE
IF P->Row < Q->Row THEN
InsEnd(List3, P->Row, P->Col, P->Data)
ELSE
InsEnd(List3, Q->Row, Q->Col, Q->Data)
WHILE P! = NULL DO
InsEnd(List3, P->Row, P->Col, P->Data)
P = P->Next
WHILE Q! = NULL DO
InsEnd(List3, Q->Row, Q->Col, Q->Data)
Q = Q->Next
RETURN List3
END;
Polynomial Representation using Linked List
• Polynomial representation using linked lists is a critical concept in computer science and mathematics.
• Let us explore how linked lists can effectively represent polynomials, especially in situations where polynomials are sparse (i.e., have many zero
coefficients).
• Unlike arrays, linked lists provide a dynamic and memory-efficient way of representing polynomials, making operations like addition,
subtraction, and multiplication more manageable and efficient.
• Understanding this representation is crucial for students and professionals dealing with complex mathematical computations and algorithm
design.
• An ordered list of non-zero terms can be thought of as a polynomial. Each non-zero term consists of three sections namely coefficient part,
exponent part, and then a pointer pointing to the node containing the next term of the polynomial.
• Representing a Polynomial Term
• In the linked list representation of a polynomial, each node corresponds to a term of the polynomial. The node structure usually contains
three components:
• Coefficient: The numerical multiplier of the term.
• Exponent: The power to which the variable is raised in the term.
• Next: A pointer to the next term (node) in the polynomial.
Representation of the Polynomial as a Linked List
1. First Node (7x^3):
 Coefficient: 7
 Exponent: 3
 Next: Pointer to the second node
2. Second Node (5x^2):
 Coefficient: 5
 Exponent: 2
 Next: Pointer to the third node
3. Third Node (-2x):
 Coefficient: -2
 Exponent: 1
 Next: Pointer to the fourth node
4. Fourth Node (4):
 Coefficient: 4
 Exponent: 0 (since it’s a constant term)
 Next: NULL (as it is the last term)
Each box represents a node, with the first
number being the coefficient and the second
number being the exponent. The arrows
indicate the ‘next’ pointers linking each term
of the polynomial.
Polynomial Representation using Linked List
• We want to represent this in our applications. So, for representation, we have to store the data about that polynomial using a linked list.
• It is sufficient to store the coefficient and exponent of each term. We can represent these terms in the form of a linked list. So, we can define
each term as a node and we will have a set of nodes for a single polynomial. So let us define a node for a single term.
• Here is the node that is having a coefficient, an exponent, and a pointer (next) to the next node.
• Let us define the structure for this.
• This structure has a coefficient and exponent of type int and a pointer to the next node of type Node*.
• If the coefficient is in decimal, then we can take float also.
• Now one more thing we can observe, this is the node of the linked list and it is having 3 members.
• So, the linked list that we have studied was taking only one value but now we are using a linked list that are to take 2 values. Based on the
requirements a node can have any number of data members.
• Now, let us represent the polynomial as a linked list.
• Here we have a linked type Node which we have created above.
• Each node has 3 sections: coefficient, exponent, and a pointer to the next node.
• There are 4 terms in the (P(x) = 4x3
+ 9x2
+ 6x + 7) polynomial so we have taken 4 nodes here.
• Let us fill up the value from the above polynomial
• This is how a polynomial is represented in the form of a linked list.
struct Node { int coefficient;
int exponent; struct Node *next;
}
Polynomial Representation using Linked List
• While representing a polynomial using a linked list, each polynomial term represents a node in the linked list.
• To get better efficiency in processing, we assume that the term of every polynomial is stored within the linked list in the order of decreasing exponents.
• Also, no two terms have the same exponent, and no term has a zero coefficient and without coefficients. The coefficient takes a value of 1.
• Each node of a linked list representing polynomial constitute three parts:
 The first part contains the value of the coefficient of the term.
 The second part contains the value of the exponent.
 The third part, LINK points to the next term (next node).
• The structure of a node of a linked list that represents a polynomial is shown below:
• Consider a polynomial P(x) = 7x4
+ 15x3
- 2x2
+ 9. Here 7, 15, -2, and 9 are the coefficients, and 4,3,2,0 are the exponents of the terms in the polynomial. On
representing this polynomial using a linked list, we have
• Observe that the number of nodes equals the number of terms in the polynomial. So we have 4 nodes. Moreover, the terms are stored to decrease exponents in the
linked list. Such representation of polynomial using linked lists makes the operations like subtraction, addition, multiplication, etc., on polynomial very easy.
Addition of Polynomials
• To add two polynomials, we traverse the list P and Q.
• We take corresponding terms of the list P and Q and compare their exponents. If the two exponents are equal, the coefficients are added to create
a new coefficient.
• If the new coefficient is equal to 0, then the term is dropped, and if it is not zero, it is inserted at the end of the new linked list containing the
resulting polynomial.
• If one of the exponents is larger than the other, the corresponding term is immediately placed into the new linked list, and the term with the
smaller exponent is held to be compared with the next term from the other list.
• If one list ends before the other, the rest of the terms of the longer list is inserted at the end of the new linked list containing the resulting
polynomial.
• Let us consider an example an example to show how the addition of two polynomials is performed,
• P(x) = 3x4
+ 2x3
- 4 x2
+ 7 and Q (x) = 5x3
+ 4 x2
- 5
Addition of Polynomials
• The addition of given polynomials P(x) and Q(x), we perform the following steps,
1. Traverse the two lists P and Q and examine all the nodes.
2. We compare the exponents of the corresponding terms of two polynomials. The first term of polynomials P and Q contain exponents 4 and 3,
respectively. Since the exponent of the first term of the polynomial P is greater than the other polynomial Q, the term having a larger exponent
is inserted into the new list. The new list initially looks as shown in Fig:1
3. We then compare the exponent of the next term of the list P with the exponents of the present term of list Q. Since the two exponents are equal,
so their coefficients are added and appended to the new list as shown in Fig: 2.
4. Then we move to the next term of P and Q lists and compare their exponents. Since exponents of both these terms are equal and after addition
of their coefficients, we get 0, so the term is dropped, and no node is appended to the new list after this. Fig: 3
Fig: 1 Fig: 2
Fig: 3
Moving to the next term of the two lists, P and Q, we find that the corresponding terms
have the same exponents equal to 0. We add their coefficients and append them to the new
list for the resulting polynomial as shown below in Fig: 4
Fig: 4
Reversal Of a Linked List
• A linked list is a data structure composed of nodes, where each node contains a value and a pointer to the next node in the list.
• Reversing a linked list involves changing the direction of pointers, so the first node becomes the last, the second node becomes the second
last, and so on.
• In this article, we will explore a simple algorithm to reverse a linked list and analyze its time and space complexity.
• Algorithm
• The algorithm to reverse a linked list can be implemented iteratively or recursively. Here, we will focus on the iterative approach as it is
more straightforward and efficient in terms of space complexity.
• Time Complexity Analysis
• The time complexity of the iterative approach to reverse a linked list is O(N), where N is the number of nodes in the list. This is because
we traverse the entire list once, performing constant-time operations for each node.
To reverse the linked list, we will maintain three
pointers: `current`, `previous`, and `next`. We will iterate through the
linked list, updating the pointers as follows:
1. Initialize `current` as the head of the linked list
and `previous` and `next` as `None`.
2. Traverse the list using a loop until `current` becomes `None`.
3. Within each iteration, store the next node of `current` in
the `next` pointer.
4. Set the `next` pointer of `current` to `previous`.
5.Move `previous` to `current`.
6. Move `current` to `next`.
7. Repeat steps 3–6 until the end of the linked list is reached.
8. Finally, update the head of the reversed linked list as `previous`.
Pitfalls Encountered In Singly Linked List
• "Disadvantages Of Linked List:
• Memory usage: More memory is required in the linked list as compared to an array. Because in a linked
list, a pointer is also required to store the address of the next element and it requires extra memory for
itself.
• Traversal: In a Linked list traversal is more time-consuming as compared to an array. Direct access to an
element is not possible in a linked list as in an array by index. For example, for accessing a node at
position n, one has to traverse all the nodes before it.
• Reverse Traversing: In a singly linked list reverse traversing is not possible, but in the case of a doubly-
linked list, it can be possible as it contains a pointer to the previously connected nodes with each node.
For performing this extra memory is required for the back pointer hence, there is a wastage of memory.
• Random Access: Random access is not possible in a linked list due to its dynamic memory allocation.
• Lower efficiency at times: For certain operations, such as searching for an element or iterating through
the list, can be slower in a linked list.
• Complex implementation: The linked list implementation is more complex when compared to array. It
requires a complex programming understanding.Difficult to share data: This is because it is not possible
to directly access the memory address of an element in a linked list.
• Not suited for small dataset: Cannot provide any significant benefits on small dataset compare to that of
an array."

More Related Content

Similar to polynomial_linked list for electrical engineer (20)

PDF
Multiplication of two 3 d sparse matrices using 1d arrays and linked lists
Dr Sandeep Kumar Poonia
 
PPTX
U2.pptx Advanced Data Structures and Algorithms
snehalkulkarni78
 
PPTX
Data structure and algorithm list structures
gangaresearch21
 
PPT
Arrays
Komal Singh
 
PPTX
TMK_DSA_Unit 2 part1(array and stack).pptx
dhruvkareliya1004
 
PDF
2-D array
Swarup Kumar Boro
 
PPT
sparse-matrix.ppt
ArunachalamSelva
 
PDF
03 programming 1
Tareq Qazi
 
PPTX
array.pptx
ssuser651960
 
PDF
cluod.pdf
ssuser92d367
 
PPTX
Address_Calculation_Arrays .pptx
miteshchaudhari4466
 
PPTX
Sparse matrices
Zain Zafar
 
PPTX
1.Array and linklst definition
balavigneshwari
 
PPTX
3 DS-Arrays.pptx 3 DS-Arrays.pptx 3 DS-Arrays.pptx
mohammedansaralima
 
PPTX
SPARSE MATRIX for enginnering students specially
amiyosarkar23
 
PPTX
ADVANCED DATA STRUCTURES AND ALGORITHMS.pptx
brajmohan21nitj
 
PPTX
arrays.pptx
NehaJain919374
 
PPTX
DSA Unit II array.pptx
sayalishivarkar1
 
PPT
02 Arrays And Memory Mapping
Qundeel
 
Multiplication of two 3 d sparse matrices using 1d arrays and linked lists
Dr Sandeep Kumar Poonia
 
U2.pptx Advanced Data Structures and Algorithms
snehalkulkarni78
 
Data structure and algorithm list structures
gangaresearch21
 
Arrays
Komal Singh
 
TMK_DSA_Unit 2 part1(array and stack).pptx
dhruvkareliya1004
 
sparse-matrix.ppt
ArunachalamSelva
 
03 programming 1
Tareq Qazi
 
array.pptx
ssuser651960
 
cluod.pdf
ssuser92d367
 
Address_Calculation_Arrays .pptx
miteshchaudhari4466
 
Sparse matrices
Zain Zafar
 
1.Array and linklst definition
balavigneshwari
 
3 DS-Arrays.pptx 3 DS-Arrays.pptx 3 DS-Arrays.pptx
mohammedansaralima
 
SPARSE MATRIX for enginnering students specially
amiyosarkar23
 
ADVANCED DATA STRUCTURES AND ALGORITHMS.pptx
brajmohan21nitj
 
arrays.pptx
NehaJain919374
 
DSA Unit II array.pptx
sayalishivarkar1
 
02 Arrays And Memory Mapping
Qundeel
 

Recently uploaded (20)

PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
DOCX
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PDF
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PDF
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
PPTX
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PPTX
Thermal runway and thermal stability.pptx
godow93766
 
PPTX
Green Building & Energy Conservation ppt
Sagar Sarangi
 
PPTX
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PPTX
Day2 B2 Best.pptx
helenjenefa1
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
Design Thinking basics for Engineers.pdf
CMR University
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
MRRS Strength and Durability of Concrete
CivilMythili
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
Thermal runway and thermal stability.pptx
godow93766
 
Green Building & Energy Conservation ppt
Sagar Sarangi
 
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Day2 B2 Best.pptx
helenjenefa1
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
Ad

polynomial_linked list for electrical engineer

  • 1. MATRIX With Arrays & Polynomial Representation using Linked List Department of CSE
  • 2. Row Major Order • When it comes to organizing and accessing elements in a multi-dimensional array, two prevalent methods are Row Major Order and Column Major Order. • These approaches define how elements are stored in memory and impact the efficiency of data access in computing. • Row Major Order • Row major ordering assigns successive elements, moving across the rows and then down the next row, to successive memory locations. In simple language, the elements of an array are stored in a Row-Wise fashion. • To find the address of the element using row-major order uses the following formula: Address of A [I][J] = B + W * ((I – LR) * N + (J – LC)) I = Row Subset of an element whose address to be found, J = Column Subset of an element whose address to be found, B = Base address, W = Storage size of one element store in an array(in byte), LR = Lower Limit of row/start row index of the matrix(If not given assume it as zero), LC = Lower Limit of column/start column index of the matrix(If not given assume it as zero), N = Number of column given in the matrix. How to find address using Row Major Order? Given an array, arr[1………10][1………15] with base value 100 and the size of each element is 1 Byte in memory. Find the address of arr[8][6] with the help of row-major order. Solution: Given: Base address B = 100 Storage size of one element store in any array W = 1 Byte Row Subset of an element whose address to be found I = 8 Column Subset of an element whose address to be found J = 6 Lower Limit of row/start row index of matrix LR = 1 Lower Limit of column/start column index of matrix = 1 Number of column given in the matrix N = Upper Bound – Lower Bound + 1 = 15 – 1 + 1 = 15 Formula: Address of A[I][J] = B + W * ((I – LR) * N + (J – LC)) Solution: Address of A[8][6] = 100 + 1 * ((8 – 1) * 15 + (6 – 1)) = 100 + 1 * ((7) * 15 + (5)) = 100 + 1 * (110)
  • 3. Column Major Order • If elements of an array are stored in a column-major fashion means moving across the column and then to the next column then it’s in column- major order. To find the address of the element using column-major order use the following formula: Address of A[I][J] = B + W * ((J – LC) * M + (I – LR)) I = Row Subset of an element whose address to be found, J = Column Subset of an element whose address to be found, B = Base address, W = Storage size of one element store in any array(in byte), LR = Lower Limit of row/start row index of matrix(If not given assume it as zero), LC = Lower Limit of column/start column index of matrix(If not given assume it as zero), M = Number of rows given in the matrix. How to find address using Column Major Order? Given an array arr[1………10][1………15] with a base value of 100 and the size of each element is 1 Byte in memory find the address of arr[8][6] with the help of column-major order. Solution: Given: Base address B = 100 Storage size of one element store in any array W = 1 Bytes Row Subset of an element whose address to be found I = 8 Column Subset of an element whose address to be found J = 6 Lower Limit of row/start row index of matrix LR = 1 Lower Limit of column/start column index of matrix = 1 Number of Rows given in the matrix M = Upper Bound – Lower Bound + 1 = 10 – 1 + 1 = 10 Formula: used Address of A[I][J] = B + W * ((J – LC) * M + (I – LR)) Address of A[8][6] = 100 + 1 * ((6 – 1) * 10 + (8 – 1)) = 100 + 1 * ((5) * 10 + (7)) = 100 + 1 * (57) Address of A[I][J] = 157 From the above examples, it can be observed that for the same position two different address locations are obtained that’s because in row-major order movement is done across the rows and then down to the next row, and in column-major order, first move down to the first column and then next column. So both the answers are right.
  • 4. Sparse Matrix • A matrix is a two-dimensional data object made of m rows and n columns, therefore having total m x n values. If most of the elements of the matrix have 0 value, then it is called a sparse matrix. • Why to use Sparse Matrix instead of simple matrix ? • Storage: There are lesser non-zero elements than zeros and thus lesser memory can be used to store only those elements. • Computing time: Computing time can be saved by logically designing a data structure traversing only non-zero elements.. Example: 0 0 3 0 4 0 0 5 7 0 0 0 0 0 0 0 2 6 0 0 Representing a sparse matrix by a 2D array leads to wastage of lots of memory as zeroes in the matrix are of no use in most of the cases. So, instead of storing zeroes with non-zero elements, we only store non-zero elements. This means storing non-zero elements with triples- (Row, Column, value). Sparse Matrix Representations can be done in many ways following are two common representations: Array representation Linked list representation Why is a sparse matrix required if we can use the simple matrix to store elements? There are the following benefits of using the sparse matrix - Storage - We know that a sparse matrix contains lesser non-zero elements than zero, so less memory can be used to store elements. It evaluates only the non-zero elements. Computing time: In the case of searching in sparse matrix, we need to traverse only the non-zero elements rather than traversing all the sparse matrix elements. It saves computing time by logically designing a data structure traversing non-zero elements.
  • 5. Sparse Matrix Representation (ARRAY) • Array representation of the sparse matrix • Representing a sparse matrix by a 2D array leads to the wastage of lots of memory. This is because zeroes in the matrix are of no use, so storing zeroes with non-zero elements is wastage of memory. To avoid such wastage, we can store only non-zero elements. If we store only non-zero elements, it reduces the traversal time and the storage space. • In 2D array representation of sparse matrix, there are three fields used that are named as - • Row - It is the index of a row where a non-zero element is located in the matrix. • Column - It is the index of the column where a non-zero element is located in the matrix. • Value - It is the value of the non-zero element that is located at the index (row, column). • Time Complexity: O(NM), where N is the number of rows in the sparse matrix, and M is the number of columns in the sparse matrix. • Auxiliary Space: O(NM), where N is the number of rows in the sparse matrix, and M is the number of columns in the sparse matrix.
  • 6. Sparse Matrix Representation (ARRAY) • Let's understand the array representation of sparse matrix with the help of the example given below - • Consider the sparse matrix – • In the above figure, we can observe a 5x4 sparse matrix containing 7 non-zero elements and 13 zero elements. The above matrix occupies 5x4 = 20 memory space. Increasing the size of matrix will increase the wastage space. • The tabular representation of the above matrix given in Fig: 1 is called Triplet Representation • In the above structure, in Fig: 1 first column represents the rows, the second column represents the columns, and the third column represents the non-zero value. • The first row of the table represents the triplets. The first triplet represents that the value 4 is stored at 0th row and 1st column. Similarly, the second triplet represents that the value 5 is stored at the 0th row and 3rd column. In a similar manner, all triplets represent the stored location of the non-zero elements in the matrix. • The size of the table depends upon the total number of non-zero elements in the given sparse matrix. • Above table occupies 8x3 = 24 memory space which is more than the space occupied by the sparse matrix. So, what's the benefit of using the sparse matrix? Consider the case if the matrix is 8*8 and there are only 8 non-zero elements in the matrix, then the space occupied by the sparse matrix would be 8*8 = 64, whereas the space occupied by the table represented using triplets would be 8*3 = 24. Fig: 1
  • 7. Sparse Matrix Representation (Linked List) • In linked list, each node has four fields. These four fields are defined as: • Row: Index of row, where non-zero element is located • Column: Index of column, where non-zero element is located • Value: Value of the non zero element located at index – (row,column) • Next node: Address of the next node • Time Complexity: O(N*M), where N is the number of rows in the sparse matrix, and M is the number of columns in the sparse matrix. Auxiliary Space: O(K), where K is the number of non-zero elements in the array.
  • 8. Addition of Two Sparse Matrices 7 0 0 0 0 6 0 4 0 0 3 0 0 1 0 1 0 0 3 0 0 4 8 7 Representation of the two sparse matrices using singly linked list
  • 9. Addition of Two Sparse Matrices BEGIN: List3 = NULL P = List1 Q = List2 WHILE P!= NULLAnd Q! = NULL DO IF P->Row = = Q->Row THEN IF P->Col = = Q->Col THEN InsEnd(List3, P->Row, P->Col, P->Data+Q- >Data) P = P->Next Q = Q->Next ELSE IF P->Col < Q->Col THEN InsEnd(List3, P->Row, P->Col, P->Data) ELSE InsEnd(List3, Q->Row, Q->Col, Q->Data) ELSE IF P->Row < Q->Row THEN InsEnd(List3, P->Row, P->Col, P->Data) ELSE InsEnd(List3, Q->Row, Q->Col, Q->Data) WHILE P! = NULL DO InsEnd(List3, P->Row, P->Col, P->Data) P = P->Next WHILE Q! = NULL DO InsEnd(List3, Q->Row, Q->Col, Q->Data) Q = Q->Next RETURN List3 END;
  • 10. Polynomial Representation using Linked List • Polynomial representation using linked lists is a critical concept in computer science and mathematics. • Let us explore how linked lists can effectively represent polynomials, especially in situations where polynomials are sparse (i.e., have many zero coefficients). • Unlike arrays, linked lists provide a dynamic and memory-efficient way of representing polynomials, making operations like addition, subtraction, and multiplication more manageable and efficient. • Understanding this representation is crucial for students and professionals dealing with complex mathematical computations and algorithm design. • An ordered list of non-zero terms can be thought of as a polynomial. Each non-zero term consists of three sections namely coefficient part, exponent part, and then a pointer pointing to the node containing the next term of the polynomial. • Representing a Polynomial Term • In the linked list representation of a polynomial, each node corresponds to a term of the polynomial. The node structure usually contains three components: • Coefficient: The numerical multiplier of the term. • Exponent: The power to which the variable is raised in the term. • Next: A pointer to the next term (node) in the polynomial. Representation of the Polynomial as a Linked List 1. First Node (7x^3):  Coefficient: 7  Exponent: 3  Next: Pointer to the second node 2. Second Node (5x^2):  Coefficient: 5  Exponent: 2  Next: Pointer to the third node 3. Third Node (-2x):  Coefficient: -2  Exponent: 1  Next: Pointer to the fourth node 4. Fourth Node (4):  Coefficient: 4  Exponent: 0 (since it’s a constant term)  Next: NULL (as it is the last term) Each box represents a node, with the first number being the coefficient and the second number being the exponent. The arrows indicate the ‘next’ pointers linking each term of the polynomial.
  • 11. Polynomial Representation using Linked List • We want to represent this in our applications. So, for representation, we have to store the data about that polynomial using a linked list. • It is sufficient to store the coefficient and exponent of each term. We can represent these terms in the form of a linked list. So, we can define each term as a node and we will have a set of nodes for a single polynomial. So let us define a node for a single term. • Here is the node that is having a coefficient, an exponent, and a pointer (next) to the next node. • Let us define the structure for this. • This structure has a coefficient and exponent of type int and a pointer to the next node of type Node*. • If the coefficient is in decimal, then we can take float also. • Now one more thing we can observe, this is the node of the linked list and it is having 3 members. • So, the linked list that we have studied was taking only one value but now we are using a linked list that are to take 2 values. Based on the requirements a node can have any number of data members. • Now, let us represent the polynomial as a linked list. • Here we have a linked type Node which we have created above. • Each node has 3 sections: coefficient, exponent, and a pointer to the next node. • There are 4 terms in the (P(x) = 4x3 + 9x2 + 6x + 7) polynomial so we have taken 4 nodes here. • Let us fill up the value from the above polynomial • This is how a polynomial is represented in the form of a linked list. struct Node { int coefficient; int exponent; struct Node *next; }
  • 12. Polynomial Representation using Linked List • While representing a polynomial using a linked list, each polynomial term represents a node in the linked list. • To get better efficiency in processing, we assume that the term of every polynomial is stored within the linked list in the order of decreasing exponents. • Also, no two terms have the same exponent, and no term has a zero coefficient and without coefficients. The coefficient takes a value of 1. • Each node of a linked list representing polynomial constitute three parts:  The first part contains the value of the coefficient of the term.  The second part contains the value of the exponent.  The third part, LINK points to the next term (next node). • The structure of a node of a linked list that represents a polynomial is shown below: • Consider a polynomial P(x) = 7x4 + 15x3 - 2x2 + 9. Here 7, 15, -2, and 9 are the coefficients, and 4,3,2,0 are the exponents of the terms in the polynomial. On representing this polynomial using a linked list, we have • Observe that the number of nodes equals the number of terms in the polynomial. So we have 4 nodes. Moreover, the terms are stored to decrease exponents in the linked list. Such representation of polynomial using linked lists makes the operations like subtraction, addition, multiplication, etc., on polynomial very easy.
  • 13. Addition of Polynomials • To add two polynomials, we traverse the list P and Q. • We take corresponding terms of the list P and Q and compare their exponents. If the two exponents are equal, the coefficients are added to create a new coefficient. • If the new coefficient is equal to 0, then the term is dropped, and if it is not zero, it is inserted at the end of the new linked list containing the resulting polynomial. • If one of the exponents is larger than the other, the corresponding term is immediately placed into the new linked list, and the term with the smaller exponent is held to be compared with the next term from the other list. • If one list ends before the other, the rest of the terms of the longer list is inserted at the end of the new linked list containing the resulting polynomial. • Let us consider an example an example to show how the addition of two polynomials is performed, • P(x) = 3x4 + 2x3 - 4 x2 + 7 and Q (x) = 5x3 + 4 x2 - 5
  • 14. Addition of Polynomials • The addition of given polynomials P(x) and Q(x), we perform the following steps, 1. Traverse the two lists P and Q and examine all the nodes. 2. We compare the exponents of the corresponding terms of two polynomials. The first term of polynomials P and Q contain exponents 4 and 3, respectively. Since the exponent of the first term of the polynomial P is greater than the other polynomial Q, the term having a larger exponent is inserted into the new list. The new list initially looks as shown in Fig:1 3. We then compare the exponent of the next term of the list P with the exponents of the present term of list Q. Since the two exponents are equal, so their coefficients are added and appended to the new list as shown in Fig: 2. 4. Then we move to the next term of P and Q lists and compare their exponents. Since exponents of both these terms are equal and after addition of their coefficients, we get 0, so the term is dropped, and no node is appended to the new list after this. Fig: 3 Fig: 1 Fig: 2 Fig: 3 Moving to the next term of the two lists, P and Q, we find that the corresponding terms have the same exponents equal to 0. We add their coefficients and append them to the new list for the resulting polynomial as shown below in Fig: 4 Fig: 4
  • 15. Reversal Of a Linked List • A linked list is a data structure composed of nodes, where each node contains a value and a pointer to the next node in the list. • Reversing a linked list involves changing the direction of pointers, so the first node becomes the last, the second node becomes the second last, and so on. • In this article, we will explore a simple algorithm to reverse a linked list and analyze its time and space complexity. • Algorithm • The algorithm to reverse a linked list can be implemented iteratively or recursively. Here, we will focus on the iterative approach as it is more straightforward and efficient in terms of space complexity. • Time Complexity Analysis • The time complexity of the iterative approach to reverse a linked list is O(N), where N is the number of nodes in the list. This is because we traverse the entire list once, performing constant-time operations for each node. To reverse the linked list, we will maintain three pointers: `current`, `previous`, and `next`. We will iterate through the linked list, updating the pointers as follows: 1. Initialize `current` as the head of the linked list and `previous` and `next` as `None`. 2. Traverse the list using a loop until `current` becomes `None`. 3. Within each iteration, store the next node of `current` in the `next` pointer. 4. Set the `next` pointer of `current` to `previous`. 5.Move `previous` to `current`. 6. Move `current` to `next`. 7. Repeat steps 3–6 until the end of the linked list is reached. 8. Finally, update the head of the reversed linked list as `previous`.
  • 16. Pitfalls Encountered In Singly Linked List • "Disadvantages Of Linked List: • Memory usage: More memory is required in the linked list as compared to an array. Because in a linked list, a pointer is also required to store the address of the next element and it requires extra memory for itself. • Traversal: In a Linked list traversal is more time-consuming as compared to an array. Direct access to an element is not possible in a linked list as in an array by index. For example, for accessing a node at position n, one has to traverse all the nodes before it. • Reverse Traversing: In a singly linked list reverse traversing is not possible, but in the case of a doubly- linked list, it can be possible as it contains a pointer to the previously connected nodes with each node. For performing this extra memory is required for the back pointer hence, there is a wastage of memory. • Random Access: Random access is not possible in a linked list due to its dynamic memory allocation. • Lower efficiency at times: For certain operations, such as searching for an element or iterating through the list, can be slower in a linked list. • Complex implementation: The linked list implementation is more complex when compared to array. It requires a complex programming understanding.Difficult to share data: This is because it is not possible to directly access the memory address of an element in a linked list. • Not suited for small dataset: Cannot provide any significant benefits on small dataset compare to that of an array."