programming-theories and algorithms
TRANSCRIPT
-
8/12/2019 Programming-Theories and Algorithms
1/13
Nicholas 07-015
Memory representation of two dimensional arrays:
In memory, the elements of 2 dimensional arrays are stored as single dimensional array because the
C++ programming language actually does not support true multi-dimensional array. There are two
methods of storing elements into the memory:
Row major order:
In row-major order, the elements of the two dimensional array are stored as single dimensional array
in contiguous memory locations. The elements are organized in such a way that the rows are stored
one after the other. For example, consider this, a [3] [3] as a two dimensional array:
In memory, the array would look like:
To find the physical location of any element for a row-major order array:
Loc (a[J][K])= Base(a)+ { (J-LB)*N+ (K-LB)}* W
For example:
Loc (a[2][1])= 142+ { (2-0)* 3+ (1-0) }* 2
=142+ 14 = 156
It means the physical location of the element a[2][1], 8is 156
Lets consider, the first element of this array 1,a[o][0] is stored in memory location 142.
Therefore, the next element will be 2, a[o][1]
which will be stored in location 144; 3rdelement
3, a[o][2] will be stored in 146 and so on. Itmeans, all the elements are stored in contiguous
memory locations as row order (row 0, 1, 2).
Where,
Base= starting physical address/ memory address
J= row; K= column
LB= Lower Bound (lowest logical address of an array)
N= Number of rowsW= size of each element
-
8/12/2019 Programming-Theories and Algorithms
2/13
-
8/12/2019 Programming-Theories and Algorithms
3/13
Nicholas 07-015
Two dimensional array:
A two-dimensional array is a collection of same type data elements placed in both rows and columns.
A two-dimensional array can be think as a table which will have x number of rows and ynumber of
columns. A 2-dimensional array a[3][4], which contains 3 rows and 4 columns can be shown as below:
Syntax:
type array_name [row_size][column_size]
Where typecan be any valid C data type and array_namecan be a valid C identifier.
For example: int student [5] [4]
Sparse Matrix:
There are specially two types of sparse matrix:
1. Triangular Sparse Matrix: Triangular sparse matrices have the same number of rows as they havecolumns; that is, they have n rows and n columns. In triangular sparse matrix, all elements above or below themain diagonal are zero. They are of two types:
Upper Triangular: In upper triangular sparse matrix, all the elements below the main diagonalare zero, or we can say that the nonzero elements are found only in the upper triangle of the
matrix, including the main diagonal.
Lower Triangular: In lower triangular sparse matrix, all the elements above the main diagonalare zero, or we can say that the nonzero elements are found only in the lower triangle of the
matrix, including the main diagonal.
2.Tri-diagonal Sparse Matrix:A tri-diagonal matrix is a matrix that has nonzero elements only in the main
diagonal, the first diagonal below this, and the first diagonal above the main diagonal.
Thus, every element in array a is identified
by an element name of the form a[ i ][ j ],where a is the name of the array, and i and j
are the subscripts that uniquely identify
each element in a.
Matrices with a relatively high proportion of zero entities are called sparse
matrices. Sparse matrix is a two-dimensional array in which most of the
elements have null value or zero 0 . Consider the diagram as an example ofsparse matrixA:
-
8/12/2019 Programming-Theories and Algorithms
4/13
Nicholas 07-015
Memory representation of lower triangular sparse matrix:
Most of the elements of a sparse matrix are zero. Thats why, to save memory storage only the non -
zero elements are stored in a linear array.
In linear memory the elements may appear like this:
Memory requirement:
In an n X n lower triangular matrix, row 1 has one non zero element, row 2 has 2,........,and row n has n.
whereas, in an n X n upper triangular matrix , row 1 has n non zero elements, row 2 has (n-1) ,.... , and
row n has 1. In both cases, the total number of non-zero elements= n (n+1)/2
In our diagram, the total number of rows is n=4. Therefore, total non-zero elements= 4 (4+1)/2=10.
To find the total number of non-zero items before a specific item= {j (j-1)/2} + (k-1) where j and k is
its corresponding row and column. In our diagram, lets find the number of non-zero items before the
item 8 where a[4][2]= 8 (item 8 is located in j=4throw & k=2ndcolumn). Therefore, {4 (4-1)/2} + (2-1)
= 7 item lies before the item 8.
Stack
Stack overflow: If the stack is full and does not contain enough space to accept a new element to be
pushed, the stack is then considered to be in an overflow state. The top of the stack is -1 when the
stack is empty. Top increases its value by 1 for each push operation. For example, when the firstelement is pushed, the top= -1+1=0, for 2nd element 0+1=1, for 3rd element 1+1=2 and so on. So
when the top is at its maximum position and then a push command arises, stack overflow occurs.
In lower triangular sparse matrix, storing the nonzero elements into a
linear array is done by walking down each column (column-major
format) or each row (row-major format) sequentially, and writing
them to a linear array in the order they appear in the walk.
The diagram is a lower triangular sparse matrix where each element
is stored sequentially in row-major order (elements in 1st row then
elements in 2nd row then elements in last row)
Lets consider the 1st
element [1][1]- 1 is stored
in location 525 in linear
array. So the 2nd element
[2][1]-2 will be stored in
contiguous memory
location 527 and so on.
Stack is a list of elements in which elements are inserted or deleted
only at one end, called the top of the stack. Stack is a Last-In-First-Out(LIFO) data structure. In a LIFO data structure, the last element added
to the structure must be the first one to be removed. Two special
terminologies are used for two basic operations associated with stack:
Push-the term used to insert an element into a stack
Pop-the term used to delete an element from a stack
-
8/12/2019 Programming-Theories and Algorithms
5/13
Nicholas 07-015
Stack underflow: The pop operation removes an element from the top of the stack. A pop either
reveals previously concealed elements or results in an empty stack. But if the stack is empty, it goes
into underflow state, which means no elements are present in stack to be removed. When the stack is
empty, the top is -1. If a pop command arises when the top is -1, stack underflow occurs.
Stackpushalgorithm: Stackpopalgorithm:
Push (stack[ ], max, top, item ){
1. if (top= = max-1)
2. print: stack overflow;
3. else
{
4. top++;
5. stack [top] = item;
}
}
Pop (stack [ ], top, item){
1. if (top= = -1)
2. print: stack underflow;
3. else
{
4. item = stack [top];
5. top --;
}
}
Insertion sort:
Insertion sort does exactly what we would expect: it inserts each element of the array into its proper
position, leaving progressively larger stretches of the array sorted. Suppose we want to sort in
ascending order.
The comparison always starts from the 2nd element. The second element is compared with theelement that appears before it (only first element in this case). If the second element is smaller
-
8/12/2019 Programming-Theories and Algorithms
6/13
Nicholas 07-015
than first element, second element is inserted in the position of first element. After first step,
first two elements will be sorted.
Now, the third element is compared with the elements that appears before it (first and secondelement). If third element is smaller than first element, it is inserted in the position of first
element. If third element is larger than first element but, smaller than second element, it is
inserted in the position of second element. If third element is larger than both the elements, it
is kept in the position as it is. After second step, first three elements of an array will be sorted.
Similarly, the fourth element of an array is compared with the elements that appears before it(first, second and third element) and the same procedure is applied and that element is
inserted in the proper position. After third step, first four elements of an array will be sorted.
If there are n elements to be sorted. Then, this procedure is repeated (n-1) times to get sortedlist of array.
Algorithm Coding
Insertion (a[ ] , n)
{
// this algorithm will sort n data item.
//using insertion sort technique.
1. for (i=1; i-1; k--)
{4. if (a[k] > temp)5. a[k+1] = a[k];6. else7. break;
}
8. a[k+1] = temp;}
}
#include
main( )
{ int a[100], n, i, k, temp;
scanf(%d, &n);
for(i=0,i temp)
a[k+1] = a[k];else
break;
}
a[k+1] = temp;
}
for(i=0,i
-
8/12/2019 Programming-Theories and Algorithms
7/13
Nicholas 07-015
Selection sort:
This type of sorting is called "Selection Sort" because it works by repeatedly element. Suppose we
need to sort the list in ascending order. So it works as follows:
The comparison always starts with the 1st element. The 1st element compares with thesmallest element in the list and swaps its position. If the 1st element does not find any smaller
element, it does not need to be swapped because it is already in its correct position.
Now the 2nd element compares with the rest of the elements (excluding the 1st element) andswaps its position with the smaller element if necessary.
Now the 3rd element compares with the rest of the elements (excluding the 1st and 2ndelement) and swaps its position.
This procedure is repeated until the last element has been swapped in its correct position. Ifthere are n elements to be sorted, the procedure is repeated (n-1) times to get sorted list of
array.
Complexity of selection sort:(Best, Average & Worst case)
The complexity of selection sort = number of comparisons needed to sort n data items
=
=
= O () [big O of n square]
-
8/12/2019 Programming-Theories and Algorithms
8/13
Nicholas 07-015
Algorithm of Selection sort
Selection (a[ ] ,n)
{
// This algorithm will sort n data item using selection sort technique.
1. for(i=0; i
-
8/12/2019 Programming-Theories and Algorithms
9/13
Nicholas 07-015
Algorithm for postfix value evaluation:
Postfix_value (p)
{
// This algorithm will find the value of an arithmetic expression P written in postfix notation
1. push ( onto the stack and add ) at the end of P.2. scan P from left to right until ) is encountered and repeat step 3 to 9.3. if (an operand A is found)4. push A onto the stack.5. if ( an operator is encountered)6. pop stack into A.7. pop stack into B.8. perform B A.9. push B A onto the stack.10.pop the stack into value and print the value
}
How to evaluate a postfix expression:
P: 5, 6, 2, +, *, 12, 4, /, -) step 1
Scand Symbol Stack operation (need not to be mentioned in exam)
1) 5 ( step 1,5 ( is pushed onto the stack; operand is pushed onto stack2) 6 ( , 5 , 6 step 2, 3 operand is pushed onto stack3) 2 ( , 5 , 6 , 2 operand is pushed onto stack4) + (, 5 , 8 A=2, B=6 ; B+A = 6+2 = 8 step 55) * (, 40 A=8, B= 5; B*A= 8*5 = 40 step 56) 12 (, 40, 12 operand is pushed onto stack7)
4 (, 40, 12, 4
operand is pushed onto stack
8) / (,40, 3 A=4, B=12; B/A= 12/4= 3 step 59) - (, 37 A=3, B=40; B-A= 40-3= 37 step 510) ) ( step 10 ) is encountered, so 37 is popped and printed
Algorithm to find postfix notation
Postfix (p)
{
//This algorithm finds the equivalent postfix expression P of an arithmetic expression Q.
1.
push ( onto stack and add ) to the end of Q.2. scan Q from left to right and repeat steps 3 to 10 for each element of Q until the stack is empty.3. If an operand is encountered, add it to P.4. If a ( is encountered, push it onto stack.5. If an operator is encountered6. repeatedly pop from stack and add to P each operator which has the same precedence
as or higher precedence than .
7. add to stack.8. If a ) is encountered9. repeatedly pop from stack and add to P each operator until a ( is encountered.10. remove (11. print: Q
}
-
8/12/2019 Programming-Theories and Algorithms
10/13
Nicholas 07-015
Q. A+ (B*C-(D/E^F)*G)*H) step 1
Scand symbol stack P
( step 1
1. A ( A step 32. + (+ step 4 A3. ( (+( A4. B (+( AB5. * (+(* AB6. C (+(* ABC7. - (+(- ABC*8. ( (+(-( ABC*9. D (+(-( ABC*D10./ (+(-(/ ABC*D11.E (+(-(/ ABC*DE12. (+(-(/^ ABC*DE13.F (+(-(/^ ABC*DEF14.) (+(- ABC*DEF^/15.* (+(-* ABC*DEF^/16.G (+(-* ABC*DEF^/G17.) (+ ABC*DEF^/G*-18.* (+* ABC*DEF^/G*-19.H (+* ABC*DEF^/G*-H20.) ABC*DEF^/G*-H*+
Find maximum value from an array
Coding Algorithm flowchart
#include
main( )
{
int a[100],i,n,max;
printf(enter the value of n:);
scanf(%d, &n);
for(i=0;i
-
8/12/2019 Programming-Theories and Algorithms
11/13
Nicholas 07-015
Find minimum value from an array
Coding Algorithm flowchart
#include
main( )
{
int a[100],i,n,min;
printf(enter the value of n:);
scanf(%d, &n);for(i=0;i
-
8/12/2019 Programming-Theories and Algorithms
12/13
Nicholas 07-015
Matrix Multiplication Coding:
#include
input(int a[10][10],int m,int n)
{ int i,j;
for(i=0;i
-
8/12/2019 Programming-Theories and Algorithms
13/13
Matrix Addition Coding:
#include
#define max 5
matrix_add(int a[max][max],int b[max][max],int c[max][max],int m,int n)
{ int i,j;
for(i=0;i