2. Why Data Structures?Why Data Structures?
P bl l i i l t d tProblem 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
4. Data StructureData Structure
A data structure is a scheme for organizing data in theA 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 structuresComputer 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 is a sequence of steps that take us from theAn 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 generalFinite, 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).
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 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
10. 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
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 vs Lists
You can see the difference between arrays andYou 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
Without arrays, we cannot think implement anything inWithout arrays, we cannot think implement anything in
computer.
To represent discrete mathematics, we required array.
Information theory bioinformatics coding theoryInformation theory, bioinformatics, coding theory.
l d lImage 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;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)
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 AarryList 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
}
}