d l hData Structures and Algorithms 
Lab(MC501)Lab(MC501) 
Dr. Sudhan Majhi
fAssistant Professor
smajhi@iitp.ac.in
Why Data Structures?Why Data Structures?
P bl l i i l t d tProblem solving is more related to
understanding the problem, designing a
solution and implementing the solution.
What exactly is a solution?What exactly is a solution?
In a very crisp way, it can be demonstrate as a
solution which is equal to a program and it issolution which is equal to a program and it is
also approximately equal to algorithm.
2
House  structure of data 
Furniture  DataFurniture  Data 
Data StructureData Structure 
A data structure is a scheme for organizing data in theA data structure is a scheme for organizing data in the 
memory of a computer. 
Some of the more commonly used data structures 
include lists, arrays, stacks, queues, heaps, trees, and 
graphsgraphs.
The way in which the data is organized affects the y g
performance of a program for different tasks.
Computer programmers decide which data structuresComputer programmers decide which data structures 
to use based on the nature of the data and the processes 
that need to be performed on that data.that need to be performed on that data.
AlgorithmAlgorithm
An algorithm is a sequence of steps that take us from theAn algorithm is a sequence of steps that take us from the
input to the output.
An algorithm must be Correct. It should provide a correct
solution according to the specificationssolution according to the specifications.
Finite It should terminate and generalFinite, It should terminate and general.
It should work for every instance of a problem is efficient.
5
It should use few resources (such as time or memory).
List & ArraysList & Arrays
SyllabusSyllabus
 List&Arrays  Stacks ShortingList&Arrays
‐Consisting of a 
collection 
of elements
‐Allow insertions and 
removals only at 
top of stack(LIFO)
Shorting
‐Arranging data in 
some given order
of elements top of stack(LIFO)
Queue
All i ti t
•Searching
‐Finding the
Recursion
‐Allow insertions at 
the back and removals 
from the front  (FIFO)
Finding the 
location of a given 
item
‐Is the process of 
repeating items in 
a self‐similar way
Linked Lists Trees
Graphs and their 
Application Linked Lists 
‐Allow insertions and removals 
anywhere 
‐ High‐speed searching and 
sorting of data and efficient 
elimination of duplicate data 
items
OutlineOutline
i• List 
• Arraysy
• Arrays vs Lists
• Application of arrays• Application of arrays 
• Sum of arrays 
• Multidimensional arrays  
• Sum of MatrixSum of Matrix 
• Multiplication of Matrix 
ListList
List of students in M.Tech batch 2013
1. Amal
2. Kamal
3. Anil
4. Kartheek ( after withdrew) 
5. Mahesh
Fi l Li t f t d t i M T h b t h 2013Final List of students in M.Tech batch 2013
1. Amal
2 Kamal2. Kamal
3. Anil
4. Mahesh
ArraysArrays
An array is a data structure consisting of a collectiony g
of elements (values or variables), each identified by
at least one array index or key.y y
a= 3         8         9         0       2       6         9         3       7
a[0]     a[1]    a[2]    a[3]  a[4]  a[ 5]   a[6]    a[7]   a[8][ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
Index i=3Index i=3
a[i] equals to 0
If i=4 then a[i] equals to 2 and so on 
Arrays (cont’d)Arrays (cont d)
 A i i d d f i bl h An array is an indexed set of variables, such as  
dancer[1], dancer[2], dancer[3],… It is like a set of [ ] [ ] [ ]
boxes that hold things. 
 A list is a set of items.
 An array is a set of
variables that each 
store an item.
Arrays vs ListsArrays vs Lists
You can see the difference between arrays andYou can see the difference between arrays and 
lists when you delete items.
In a list, the missing spot is filled in when 
something is deleted.something is deleted.
Applications of ArraysApplications of Arrays
Without arrays, we cannot think implement anything inWithout arrays, we cannot think implement anything in 
computer. 
To represent discrete mathematics, we required array. 
Information theory bioinformatics coding theoryInformation theory, bioinformatics, coding theory. 
 l d lImage processing, signal processing and wireless 
communications. 
Sum of arraysSum of arrays
a= 3         8         9         0       2       6         9         3       7
a[0] a[1] a[2] a[3] a[4] a[ 5] a[6] a[7] a[8]a[0]     a[1]    a[2]    a[3]  a[4]  a[ 5]   a[6]    a[7]   a[8]
sum a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]sum=a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]
If the index is very large , let say i=1000000,  then we 
need to write all the elements in a programneed to write all the elements in a program.  
Sum of arraysSum of arrays
sum=0;sum 0;
sum=sum+a[0]
sum=sum+a[1]
sum=sum+a[i], i=0 to 8
sum sum+a[1]
sum=sum+a[2]
Programming Algorithm g
1. Set sum=0;
2. Set i =0;
Sum=0
for (i=0;i<9;i++)2. Set i 0;
3. Sum=sum+a[i];
4. i=i+1;
5 Go to line 3 if i lesser than 9
( )
{
sum=sum+a[i]
}
5. Go to line 3 if i lesser than 9
6. Print sum
scanf(“%d”,sum)
Multidimensional arraysMultidimensional arrays 
Two dimensional arrays
Column
0
Column
2
Column
3
Column
4
Column
1
Column represents by j
Row 0
[1][3]Row 1 a[1][3]
Row 2
Row represents by i Column wise reading Row wise readingp y
for{i=0;i<3;i++}
{ for{j=0;j<5;j++}
{ a[i][j]
Column wise reading  Row wise reading 
for{j=0;j<5;j++}
{for{i=0;i<3;i++}
{a[i][j]{ a[i][j]
}
}
{a[i][j]
}
}
Sum of MatricesSum of Matrices 
a[0][0] a[0][1] a[0][2] a[0][3] a[0][4] b[0][0] b[0][1] b[0][2] b[0][ 3] b[0][4]
B
a[0][0] a[0][1] a[0][2] a[0][3] a[0][4]
a[1][0] a[1][1] a[1][2] a[1][3] a[1][4]
b[0][0] b[0][1] b[0][2] b[0][ 3] b[0][4]
b[1][0] b[1][1] b[1][2] b[1][3] b[1][4]
A
a[2][0] a[2][1] a[2][2] a[2][3] a[2][4] b[2][0] b[2][1] b[2][2] b[2][3] b[2][4]
Algorithm:
i=0, j=0  i=0, j=1  i=0, j=2 i=0, j=3  i=0, j=4 
Int c[3,5]
For(i=0;i<3;i++)
{
g
a[0][0]+
b[0][0]
a[0][1]+
b[0][1]
a[0][2]+
b[0][2]
a[0][3]+ 
b[0][3]
a[0][4]+
b[0][4]
{
For(j=0;j<5;j++)
{
c[i][j]=a[i][j]+b[i][j]
i=1, j=0 i=1, j=1  i=1, j=2 i=1, j=3  i=1, j=4 
a[1][0]+ a[1][1]+ a[1][2]+ [1][3] [1][4]
c[i][j] a[i][j] b[i][j]
}
}
a[1][0]+
b[1][0]
a[1][1]+
b[1][1]
a[1][2]+
b[1][2]
a[1][3]+
b[1][3]
a[1][4]+
b[1][4]
Multiplication of MatricesMultiplication of Matrices 
a[0][0] a[0][1] a[0][2] a[0][3] a[0][4] b[0][0] b[0][1] b[0][2]
a[1][0] a[1][1] a[1][2] a[1][3] a[1][4] b[1][0] b[1][1] b[1][2]A
B
a[2][0] a[2][1] a[2][2] a[2][3] a[2][4] b[2][0] b[2][1] b[2][2]
b[3][0] B[3][1] b[3][2]
Sum=0
Code for multiplication
b[4][0] B[4][1] B[4][2]For(k=0;k<3;k++)
For(l=0;l<3;l++)
For(i=0;i<5;i++)
sum=sum+a[l][i]*b[i][l]
}
c[k][l]=sum
0sum=0
}
}
Multiplication of MatricesMultiplication of Matrices 
k=0, l=0, i=0 to 4
c[0][0] a[0][0] a[0][1] a[0][2] a[0][3] a[0][4] b[0][0]
b[1][0]
Al orithm
C
b[2][0]
b[3][0]
Algorithm
Sum=0
For(k=0;k<3;k++)
C b[3][0]
b[4][0]
For(k 0;k<3;k++)
For(l=0;l<3;l++)
For(i=0;i<5;i++)
sum=sum+a[l][i]*b[i][l][ ][ ] [ ][ ]
}
c[k][l]=sum
sum=0
}
}
Multiplication of MatricesMultiplication of Matrices 
k=0, l=1, i=0 to 4
c[0][0] c[0][1] a[1][0] a[0][1] a[0][2] a[0][3] a[0][4] b[0][1]
b[1][1]
b[2][1]
l i h
C
b[2][1]
b[3][1]
Algorithm
Sum=0
For(k=0;k<3;k++)
C
b[4][1]
For(k=0;k<3;k++)
For(l=0;l<3;l++)
For(i=0;i<5;i++)
sum=sum+a[l][i]*b[i][l]sum sum+a[l][i] b[i][l]
}
c[k][l]=sum
sum=0
}
}
Multiplication of MatricesMultiplication of Matrices 
K=1, l=0, i=0 to 4
[0][0] [0][1] [0][2]
a[1][0] a[1][1] a[1][2] a[1][3] a[1][4] b[0][0]
c[0][0] c[0][1] c[0][2]
c[1][0]
b[1][0]
Al i h b[2][0]
b[3][0]
Algorithm
Sum=0
For(k=0;k<3;k++)
C
b[3][0]
b[4][0]
For(k 0;k<3;k++)
For(l=0;l<3;l++)
For(i=0;i<5;i++)
sum=sum+a[l][i]*b[i][l][ ][ ] [ ][ ]
}
c[k][l]=sum
sum=0
}
}
SummarySummary
List vs AarryList  vs Aarry
Sum=0
Sum of array  Sum of Matrices Product of Matrices
Int c[3,5] Sum=0
F (k 0 k 3 k )for (i=0;i<9;i++)
{
sum=sum+a[i]
[ ]
For(i=0;i<3;i++)
{
For(j=0;j<5;j++)
For(k=0;k<3;k++)
For(l=0;l<3;l++)
For(i=0;i<5;i++)
sum sum+a[l][i]*b[i][l]}
scanf(“%d”,sum)
{
c[i][j]=a[i][j]+b[i][j]
}
sum=sum+a[l][i]*b[i][l]
}
c[k][l]=sum
sum=0
22
}
sum=0
}
}
h kThanks

Introduction to Data Structure

  • 1.
  • 2.
    Why Data Structures?Why Data Structures? Pbl l i i l t d tProblem solving is more related to understanding the problem, designing a solution and implementing the solution. What exactly is a solution?What exactly is a solution? In a very crisp way, it can be demonstrate as a solution which is equal to a program and it issolution which is equal to a program and it is also approximately equal to algorithm. 2
  • 3.
  • 4.
    Data StructureData Structure  A datastructure is a scheme for organizing data in theA data structure is a scheme for organizing data in the  memory of a computer.  Some of the more commonly used data structures  include lists, arrays, stacks, queues, heaps, trees, and  graphsgraphs. The way in which the data is organized affects the y g performance of a program for different tasks. Computer programmers decide which data structuresComputer programmers decide which data structures  to use based on the nature of the data and the processes  that need to be performed on that data.that need to be performed on that data.
  • 5.
    AlgorithmAlgorithm An algorithm isa sequence of steps that take us from theAn algorithm is a sequence of steps that take us from the input to the output. An algorithm must be Correct. It should provide a correct solution according to the specificationssolution according to the specifications. Finite It should terminate and generalFinite, It should terminate and general. It should work for every instance of a problem is efficient. 5 It should use few resources (such as time or memory).
  • 6.
  • 7.
    SyllabusSyllabus  List&Arrays Stacks ShortingList&Arrays ‐Consisting of a  collection  of elements ‐Allow insertions and  removals only at  top of stack(LIFO) Shorting ‐Arranging data in  some given order of elements top of stack(LIFO) Queue All i ti t •Searching ‐Finding the Recursion ‐Allow insertions at  the back and removals  from the front  (FIFO) Finding the  location of a given  item ‐Is the process of  repeating items in  a self‐similar way Linked Lists Trees Graphs and their  Application Linked Lists  ‐Allow insertions and removals  anywhere  ‐ High‐speed searching and  sorting of data and efficient  elimination of duplicate data  items
  • 8.
    OutlineOutline i• List  • Arraysy •Arrays vs Lists • Application of arrays• Application of arrays  • Sum of arrays  • Multidimensional arrays   • Sum of MatrixSum of Matrix  • Multiplication of Matrix 
  • 9.
    ListList List of studentsin M.Tech batch 2013 1. Amal 2. Kamal 3. Anil 4. Kartheek ( after withdrew)  5. Mahesh Fi l Li t f t d t i M T h b t h 2013Final List of students in M.Tech batch 2013 1. Amal 2 Kamal2. Kamal 3. Anil 4. Mahesh
  • 10.
    ArraysArrays An array isa data structure consisting of a collectiony g of elements (values or variables), each identified by at least one array index or key.y y a= 3         8         9         0       2       6         9         3       7 a[0]     a[1]    a[2]    a[3]  a[4]  a[ 5]   a[6]    a[7]   a[8][ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] Index i=3Index i=3 a[i] equals to 0 If i=4 then a[i] equals to 2 and so on 
  • 11.
    Arrays (cont’d)Arrays (cont d) A i i d d f i bl h An array is an indexed set of variables, such as   dancer[1], dancer[2], dancer[3],… It is like a set of [ ] [ ] [ ] boxes that hold things.   A list is a set of items.  An array is a set of variables that each  store an item.
  • 12.
    Arrays vs ListsArrays vsLists You can see the difference between arrays andYou can see the difference between arrays and  lists when you delete items. In a list, the missing spot is filled in when  something is deleted.something is deleted.
  • 13.
    Applications of ArraysApplications of Arrays Withoutarrays, we cannot think implement anything inWithout arrays, we cannot think implement anything in  computer.  To represent discrete mathematics, we required array.  Information theory bioinformatics coding theoryInformation theory, bioinformatics, coding theory.   l d lImage processing, signal processing and wireless  communications. 
  • 14.
    Sum of arraysSum of arrays a= 3         8         9         0       2       6         9         3       7 a[0]a[1] a[2] a[3] a[4] a[ 5] a[6] a[7] a[8]a[0]     a[1]    a[2]    a[3]  a[4]  a[ 5]   a[6]    a[7]   a[8] sum a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]sum=a[0]+a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8] If the index is very large , let say i=1000000,  then we  need to write all the elements in a programneed to write all the elements in a program.  
  • 15.
    Sum of arraysSum of arrays sum=0;sum0; sum=sum+a[0] sum=sum+a[1] sum=sum+a[i], i=0 to 8 sum sum+a[1] sum=sum+a[2] Programming Algorithm g 1. Set sum=0; 2. Set i =0; Sum=0 for (i=0;i<9;i++)2. Set i 0; 3. Sum=sum+a[i]; 4. i=i+1; 5 Go to line 3 if i lesser than 9 ( ) { sum=sum+a[i] } 5. Go to line 3 if i lesser than 9 6. Print sum scanf(“%d”,sum)
  • 16.
    Multidimensional arraysMultidimensional arrays  Two dimensional arrays Column 0 Column 2 Column 3 Column 4 Column 1 Column represents by j Row 0 [1][3]Row 1 a[1][3] Row 2 Row represents by iColumn wise reading Row wise readingp y for{i=0;i<3;i++} { for{j=0;j<5;j++} { a[i][j] Column wise reading  Row wise reading  for{j=0;j<5;j++} {for{i=0;i<3;i++} {a[i][j]{ a[i][j] } } {a[i][j] } }
  • 17.
    Sum of MatricesSum of Matrices  a[0][0]a[0][1] a[0][2] a[0][3] a[0][4] b[0][0] b[0][1] b[0][2] b[0][ 3] b[0][4] B a[0][0] a[0][1] a[0][2] a[0][3] a[0][4] a[1][0] a[1][1] a[1][2] a[1][3] a[1][4] b[0][0] b[0][1] b[0][2] b[0][ 3] b[0][4] b[1][0] b[1][1] b[1][2] b[1][3] b[1][4] A a[2][0] a[2][1] a[2][2] a[2][3] a[2][4] b[2][0] b[2][1] b[2][2] b[2][3] b[2][4] Algorithm: i=0, j=0  i=0, j=1  i=0, j=2 i=0, j=3  i=0, j=4  Int c[3,5] For(i=0;i<3;i++) { g a[0][0]+ b[0][0] a[0][1]+ b[0][1] a[0][2]+ b[0][2] a[0][3]+  b[0][3] a[0][4]+ b[0][4] { For(j=0;j<5;j++) { c[i][j]=a[i][j]+b[i][j] i=1, j=0 i=1, j=1  i=1, j=2 i=1, j=3  i=1, j=4  a[1][0]+ a[1][1]+ a[1][2]+ [1][3] [1][4] c[i][j] a[i][j] b[i][j] } } a[1][0]+ b[1][0] a[1][1]+ b[1][1] a[1][2]+ b[1][2] a[1][3]+ b[1][3] a[1][4]+ b[1][4]
  • 18.
    Multiplication of MatricesMultiplication of Matrices  a[0][0]a[0][1] a[0][2] a[0][3] a[0][4] b[0][0] b[0][1] b[0][2] a[1][0] a[1][1] a[1][2] a[1][3] a[1][4] b[1][0] b[1][1] b[1][2]A B a[2][0] a[2][1] a[2][2] a[2][3] a[2][4] b[2][0] b[2][1] b[2][2] b[3][0] B[3][1] b[3][2] Sum=0 Code for multiplication b[4][0] B[4][1] B[4][2]For(k=0;k<3;k++) For(l=0;l<3;l++) For(i=0;i<5;i++) sum=sum+a[l][i]*b[i][l] } c[k][l]=sum 0sum=0 } }
  • 19.
    Multiplication of MatricesMultiplication of Matrices  k=0, l=0, i=0 to 4 c[0][0]a[0][0] a[0][1] a[0][2] a[0][3] a[0][4] b[0][0] b[1][0] Al orithm C b[2][0] b[3][0] Algorithm Sum=0 For(k=0;k<3;k++) C b[3][0] b[4][0] For(k 0;k<3;k++) For(l=0;l<3;l++) For(i=0;i<5;i++) sum=sum+a[l][i]*b[i][l][ ][ ] [ ][ ] } c[k][l]=sum sum=0 } }
  • 20.
    Multiplication of MatricesMultiplication of Matrices  k=0, l=1, i=0 to 4 c[0][0]c[0][1] a[1][0] a[0][1] a[0][2] a[0][3] a[0][4] b[0][1] b[1][1] b[2][1] l i h C b[2][1] b[3][1] Algorithm Sum=0 For(k=0;k<3;k++) C b[4][1] For(k=0;k<3;k++) For(l=0;l<3;l++) For(i=0;i<5;i++) sum=sum+a[l][i]*b[i][l]sum sum+a[l][i] b[i][l] } c[k][l]=sum sum=0 } }
  • 21.
    Multiplication of MatricesMultiplication of Matrices  K=1, l=0, i=0 to 4 [0][0][0][1] [0][2] a[1][0] a[1][1] a[1][2] a[1][3] a[1][4] b[0][0] c[0][0] c[0][1] c[0][2] c[1][0] b[1][0] Al i h b[2][0] b[3][0] Algorithm Sum=0 For(k=0;k<3;k++) C b[3][0] b[4][0] For(k 0;k<3;k++) For(l=0;l<3;l++) For(i=0;i<5;i++) sum=sum+a[l][i]*b[i][l][ ][ ] [ ][ ] } c[k][l]=sum sum=0 } }
  • 22.
    SummarySummary List vs AarryList  vsAarry Sum=0 Sum of array  Sum of Matrices Product of Matrices Int c[3,5] Sum=0 F (k 0 k 3 k )for (i=0;i<9;i++) { sum=sum+a[i] [ ] For(i=0;i<3;i++) { For(j=0;j<5;j++) For(k=0;k<3;k++) For(l=0;l<3;l++) For(i=0;i<5;i++) sum sum+a[l][i]*b[i][l]} scanf(“%d”,sum) { c[i][j]=a[i][j]+b[i][j] } sum=sum+a[l][i]*b[i][l] } c[k][l]=sum sum=0 22 } sum=0 } }
  • 23.