Title: Data Structures-CSE207
1Data Structures-CSE207
2Course Detail
- BASIC CONCEPTS OF ALGORITHM
- (1.2.1,1.4,1.4.1,1.4.2,1.4.3 of Text3)
- POINTERS AND POINTER APPLICATIONS
- (9-3 to 9-7, 10.1 to 10.6 of Text1)
- DERIVED TYPES (12.1,12.2,12.4-12.7 of Text1)
- RECURSION (3.1-3.5 of Text2)
- STACKS AND QUEUES (3.1,3.2,3.4,3.5 of Text3)
- LINKED LIST (4.2-4.4,4.8 of Text3)
- TREES (5.1,5.2,5.3,5.7,10.2 of Text3)
- GRAPHS AND THEIR APPLICATIONS (6.1-6.4 of Text3)
- SEARCHING AND SORTING (7.1,7.3,7.4 of Text3)
- HASHING (8.2 of Text3)
3Text Books
- Behrouz A. Forouzan, Richard F. Gilberg A
Structured Programming Approach Using C,Thomson,
2nd Edition, 2003. - Aaron M. Tenenbaum,Yedidyah Langsam,Moshe J.
Augeustein,Data Structures using C, PEARSON
Education , 2006. - Ellis Horowitz, Sartaj Sahni , Anderson,
Fundamentals of Data Structures in C, Silicon
Press, 2nd Edition, 2007.
4REFERENCES
- Richard F. Gilberg, Behrouz A. Forouzan, Data
structures, A Pseudocode Approach with C,
Thomson, 2005. - Robert Kruc Bruce Lening, Data structures
Program Design in C, Pearson Education, 2007. - Mark Allen Weiss, Data Structures and Algorithm
Analysis in C, Prentice Hall
5DataStructure
- Definition
- A data structure is a particular way of storing
and organizing data in a computer so that it can
be used efficiently.
6Basic concepts of Algorithm
- An algorithm is a finite set of instructions that
, if followed , accomplishes a particular task. - Characteristics of an algorithm
- Input These are 0 or more quantities that are
externally supplied. - Output At least one quantity is produced
- Definiteness Each instruction is clear and
unambiguous. - Finiteness If we trace out instructions of an
algorithm,then for all cases ,the algorithm
terminates after a finite number of steps - Effectiveness Every instruction must be basic
enough to be carried out, in principle by a
person using a pencil and paper.i.e instructions
must be feasible.
7Sorting-Ascending
- Selection sort algorithm
- Find the minimum value in the list
- Swap it with the value in the first position
- Repeat the steps 1 and 2 for the remainder of the
list (starting at the second position and
advancing each time) - Example
- 64 25 12 22 11
- 11 25 12 22 64
- 11 12 25 22 64
- 11 12 22 25 64
- 11 12 22 25 64
8Selection sort algorithm bit revised
- for(i0iltni)
- Examine listi to listn-1 and suppose that the
smallest integer is at listmin - Interchange /swap listi with listmin.
-
9(No Transcript)
10(No Transcript)
11(No Transcript)
12Performance Analysis
- Space Complexity The space complexity of a
program is the amount of memory it needs to run
to completion. - Time Complexity The time complexity of a program
is the amount of computer time it needs to run to
completion
13Space complexity
- A fixed part that is independent of the
characteristics of the inputs and outputs. This
part typically includes the instruction space,
space for simple varialbes and fixed-size
component variables, space for constants, etc. - A variable part that consists of the space needed
by component variables whose size is dependent on
the particular problem instance I being solved,
the space needed by referenced variables, and the
recursion stack space. - The space requirement S(P)of any program P is
written as S(P) c Sp(I) where c is a constant
and Sp(I) is variable part.
14Examples
- float abc(float a,float b, float c)
- return abcbc
-
- Sabc(I)0
- float sum(float list,int n)
- float tempsum0 int i
- for (i0iltni) tempsumlisti
- return(tempsum)
- in C Ssum(I)0
15- float rsum(float list,int n)
-
- if(n) return rsum(list,n-1)listn-1)
- return 0
- Srsum(I)6n
16Time complexity
- The time, T(P), taken by a program P is the sum
of the compile time and the run (or execution)
time. The compile time does not depend on the
instance characteristics. We focus on the run
time of a program, denoted by Tp (instance
characteristics).
17- Determining Tp requires detailed knowledge of
Compiler translation procedure. - Obtaining such a detailed estimate is rarely
worth the effort. - We count no.of operations in a program
- This gives us a m/c independent measure
- But we should know how to divide the program into
distinct steps.
18- Definition A program step is a syntactically or
semantically meaningful program segment whose
execution time is independent of instance
characteristics. - A2 and A2ab/cd both are treated as steps one
each though their actual time differs. What we
need is they should be independent of instance
characteristics
19Computation of timecomplexity using global count
method
- float sum(float list,int n)
- float tempsum0 count//assignment
- int i
- for (i0iltni) count//whole for loop as
one // step - tempsumlisti count//assignment
- count //last execution of for
- count //for return
- return(tempsum)
- // count2n3
20- float rsum(float list,int n)
-
- count //for if condition
- if(n)count //for return and rsum //invocation
- return rsum(list,n-1)listn-1)
- count// for return0
- return 0
- //count2n2
21Calculate step count for
- void add(int amaxsize,int bmaxsize,int
cmaxsize,int rows,int cols) - int i,j
- for(i0iltrowsi)
- for(j0jltcolsj)cijaijbij
-
22Finding stepcount using tabular method
- We first find steps/execution or s/e .
- Next we find number of times that each statement
is executed.This is called frequency. - s/efrequency gives us step count for each
statement - Summing these for every statement gives total
step count for whole function.
23Total steps
frequency
s/e
Statement
0 0 1 0 n1 n 1 0
0 0 1 0 n1 n 1 0
0 0 1 0 1 1 1 0
float sum(float list,int n) float
tempsum0 int i for (i0iltni)
tempsumlisti return(tempsum)
2n3
Total
24Total steps
frequency
s/e
Statement
0 0 n1 n 1 0
0 0 n1 n 1 0
0 0 1 1 1 0
float rsum(float list,int n) if(n) return
rsum(list,n-1)listn-1) return 0
2n2
Total
25Total steps
frequency
s/e
Statement
0 0 0 r1 rcr rc 0
0 0 0 r1 r(c 1) r.c 0
0 0 0 1 1 1 0
void add(int amaxsize,int bmaxsize,int
cmaxsize,int rows,int cols) int
i,j for(i0iltri) for(j0jltcj) cijai
jbij
2rc2r1
Total
26Asymptotic Notation
- Determining step counts help us to compare the
time complexities of two programs and to predict
the growth in run time as the instance
characteristics change. - But determining exact step counts could be very
difficult. Since the notion of a step count is
itself inexact, it may be worth the effort to
compute the exact step counts. - Definition Big oh f(n) O(g(n)) iff there
exist positive constants c and n0 such that f(n)
cg(n) for all n, n n0
27Examples of Asymptotic Notation
- 3n 2 O(n)
- 3n 2 4n for all n 3
- 100n 6 O(n)
- 100n 6 101n for all n 10
- 10n2 4n 2 O(n2)
- 10n2 4n 2 11n2 for all n 5
- Note O(2)O(1)constant computing time
28Asymptotic Notation (Cont.)
- Theorem 1.2 If f(n) amnm a1n a0, then
f(n) O(nm). - Proof
- for n 1
- So, f(n) O(nm)
29Asymptotic Notation
- f(n)O(g(n)) only states that g(n) is an
upperbound on the value of f(n) for all values of
ngtn0. nO(n2),nO(n3). - Inorder for this statement to be informative g(n)
must be as small function of n one can come up
with for which f(n)O(g(n)).So we say 3n2O(n)
rather than O(n2). - Lastly f(n)O(g(n)) does not imply O(g(n))f(n).
Here should be read as is
30Asymptotic Notation (Cont.)
- Definition Omega f(n) O(g(n)) iff there
exist positive constants c and n0 such that f(n)
cg(n) for all n, n n0. (n0gt0) - Example
- 3n 2 O(n) as 3n2gt3n ngt1
- 100n 6 O(n) as 100n6gt100n ,ngt1
- 10n2 4n 2 O(n2) as 10n2 4n 2 gtn2 ,ngt1.
- Like O(n) here g(n) is only a lower bound on
f(n)i.e g(n) should be as large a function of n
as possible for which the statement f(n) O(g(n))
is true. - Hence we never say 3n2 O(1) though it is true.
31Asymptotic Notation (Cont.)
- Definition f(n) T(g(n)) iff there exist
positive constants c1, c2, and n0 such that
c1g(n) f(n) c2g(n) for all n, n n0. - 3n2 T(n) as 3n2lt4n,ngt2 and 3n2gt3n ngt2
- f(n) T(g(n)) iff g(n) is both upper and lower
bound of f(n). - Note In all these we neglect coefficients i.e we
do not say 3n2O(3n) though it is correct
32Asymptotic Notation (Cont.)
- Theorem 1.3 If f(n) amnm a1n a0 and am
gt 0, then f(n) O(nm). - Theorem 1.4 If f(n) amnm a1n a0 and am
gt 0, then f(n) T(nm).
33Use of Asymptotic notations
- The asymptotic complexity can be determined quite
easily without determining the exact step count. - We determine asymptotic complexity of each or a
group of statements in a program and then add
these to get total asymptotic complexity
34Asymptotic complexity
Statement
0 0 0 T(r) T(r.c) T(r.c) 0
void add(int amaxsize,int bmaxsize,int
cmaxsize,int rows,int cols) int
i,j for(i0iltri) for(j0jltcj) cijai
jbij
T(r.c)
Total
35Practical Complexities
- If a program P has complexities T(n) and program
Q has complexities T(n2), then, in general, we
can assume program P is faster than Q for a
sufficient large n. - However, caution needs to be used on the
assertion of sufficiently large.
36Function Values
log n
n
n log n
n2
n3
2n
0
1
0
1
1
2
1
2
2
4
8
4
2
4
8
16
64
16
3
8
24
64
512
256
4
16
64
256
4096
65536
5
32
160
1024
32768
4294967296
37S.T.The following statements are correct/incorrect
- 5n2-6n T(n2).
- 2n2nlogn T(n2)
- 10n29O(n)
- N2logn T(n2).