Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures-CSE207.

Similar presentations


Presentation on theme: "Data Structures-CSE207."— Presentation transcript:

1 Data Structures-CSE207

2 Course 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, of Text1) RECURSION ( of Text2) STACKS AND QUEUES (3.1,3.2,3.4,3.5 of Text3) LINKED LIST ( ,4.8 of Text3) TREES (5.1,5.2,5.3,5.7,10.2 of Text3) GRAPHS AND THEIR APPLICATIONS ( of Text3) SEARCHING AND SORTING (7.1,7.3,7.4 of Text3) HASHING (8.2 of Text3)

3 Text 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.

4 REFERENCES 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

5 DataStructure Definition
A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

6 Basic 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.

7 Sorting-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:

8 Selection sort algorithm bit revised
for(i=0;i<n;i++){ Examine list[i] to list[n-1] and suppose that the smallest integer is at list[min]; Interchange /swap list[i] with list[min]. }

9

10

11

12 Performance 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

13 Space 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.

14 Examples float abc(float a,float b, float c){ return a+b+c+b*c; }
Sabc(I)=0 float sum(float list[],int n){ float tempsum=0; int i; for (i=0;i<n;i++) tempsum+=list[i]; return(tempsum); } in C Ssum(I)=0

15 float rsum(float list[],int n)
{ if(n) return rsum(list,n-1)+list[n-1]); return 0; } Srsum(I)=6*n

16 Time 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. A=2 and A=2*a+b/c+d both are treated as steps one each though their actual time differs. What we need is they should be independent of instance characteristics

19 Computation of timecomplexity using global count method
float sum(float list[],int n){ float tempsum=0; count++;//assignment int i; for (i=0;i<n;i++) {count++;//whole for loop as one // step tempsum+=list[i]; count++;//assignment } count++; //last execution of for count++; //for return return(tempsum); } // count=2n+3

20 float rsum(float list[],int n)
{ count++; //for if condition if(n){count++; //for return and rsum //invocation return rsum(list,n-1)+list[n-1]); } count++;// for return0 return 0; } //count=2n+2

21 Calculate step count for
void add(int a[][maxsize],int b[][maxsize],int c[][maxsize],int rows,int cols){ int i,j; for(i=0;i<rows;i++) for(j=0;j<cols;j++){c[i][j]=a[i][j]+b[i][j];} }

22 Finding 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/e*frequency gives us step count for each statement Summing these for every statement gives total step count for whole function.

23 Statement s/e frequency Total steps float sum(float list[],int n) { float tempsum=0; int i; for (i=0;i<n;i++) tempsum+=list[i]; return(tempsum); } 1 1 n+1 n 1 n+1 n Total 2n+3

24 Statement s/e frequency Total steps float rsum(float list[],int n) { if(n) return rsum(list,n-1)+list[n-1]); return 0; } 1 n+1 n 1 n+1 n 1 Total 2n+2

25 Statement s/e frequency Total steps void add(int a[][maxsize],int b[][maxsize],int c[][maxsize],int rows,int cols) { int i,j; for(i=0;i<r;i++) for(j=0;j<c;j++) c[i][j]=a[i][j]+b[i][j]; } 1 r+1 r(c +1) r.c r+1 rc+r rc Total 2rc+2r+1

26 Asymptotic 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

27 Examples 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

28 Asymptotic 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)

29 Asymptotic Notation f(n)=O(g(n)) only states that g(n) is an upperbound on the value of f(n) for all values of n>=n0. n=O(n2),n=O(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 3n+2=O(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”

30 Asymptotic Notation (Cont.)
Definition: [Omega] f(n) = Ω(g(n)) iff there exist positive constants c and n0 such that f(n) ≥ cg(n) for all n, n ≥ n0. (n0>0) Example: 3n + 2 = Ω(n) as 3n+2>=3n n>=1 100n + 6 = Ω(n) as 100n+6>=100n ,n>=1 10n2 + 4n + 2 =Ω(n2) as 10n2 + 4n + 2 >=n2 ,n>=1. 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)= Ω(g(n)) is true. Hence we never say 3n+2= Ω(1) though it is true.

31 Asymptotic Notation (Cont.)
Definition: f(n) = Θ(g(n)) iff there exist positive constants c1, c2, and n0 such that c1g(n) ≤ f(n) ≤ c2g(n) for all n, n ≥ n0. 3n+2= Θ(n) as 3n+2<=4n,n>=2 and 3n+2>=3n n>=2 f(n)= Θ(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 3n+2=O(3n) though it is correct

32 Asymptotic Notation (Cont.)
Theorem 1.3: If f(n) = amnm + … + a1n + a0 and am > 0, then f(n) = Ω(nm). Theorem 1.4: If f(n) = amnm + … + a1n + a0 and am > 0, then f(n) = Θ(nm).

33 Use 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

34 Statement Asymptotic complexity void add(int a[][maxsize],int b[][maxsize],int c[][maxsize],int rows,int cols) { int i,j; for(i=0;i<r;i++) for(j=0;j<c;j++) c[i][j]=a[i][j]+b[i][j]; } Θ(r) Θ(r.c) Total Θ(r.c)

35 Practical Complexities
If a program P has complexities Θ(n) and program Q has complexities Θ(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”.

36 Function Values log n n n log n n2 n3 2n 1 1 1 2 1 2 2 4 8 4 2 4 8 16
1 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

37 S.T.The following statements are correct/incorrect
5n2-6n= Θ(n2). 2n2+nlogn= Θ(n2) 10n2+9=O(n) N2logn= Θ(n2).


Download ppt "Data Structures-CSE207."

Similar presentations


Ads by Google