Professional Documents
Culture Documents
CHAPTER-1
DEPARTMENT OF MCA
BASIC CONCEPTS in Data Structures: What do you mean by Data structure and why do we need to use Data structure?
To understand what Data structure is the following analogy will explain the concept in a better way. Say for example I have a cupboard with me in which I keep my clothes. And say I have 10 shirts with me, without folding it I just throw one by one all the shirts in the cupboard as shown in Fig-1. The next day if I want the pink shirt, then I have to check with each and every shirt whether it is pink or not, Say if I have 100 shirts with me then it is really hard for me to find the pink shirt from that huge collection of shirts. Now instead of throwing it directly without fold, lets keep the shirts in a folded manner neatly, as shown in the fig-2. now can you see the color of every shirt, is it not easy for you to select the particulate shirt what you want? Now just pay attention for this statement, the shirts what I am having with me is the data and arranging this data in a logical manner is called as Data structure.
Fig-1
Fig-2
Contact-Email: khutub27@gmail.com
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
NON-PRIMITIVE DS
INTEGER
LINEAR
NONLINEAR
FLOAT
ARRAYS
TREES
DOUBLE
CHARACTER
STACKS
GRAPHS
QUEUES
POINTERS
STRINGS
LINKED LIST
Overview of Chapter-1: 1. System Life Cycle 2. Pointers and Dynamic Memory Allocation 3. Algorithm Specification 4. Data Abstraction 5. Performance Analysis 6. Performance Measurement
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
Good programmers regard large-scale computer programs as systems that contain many complex interacting parts. As systems, these programs undergo a development process called the System life cycle. We consider this cycle as consisting of five phases. 1. 2. 3. 4. 5. Requirements Analysis. Design Refinement and Coding Verification
1) Requirements All large programming projects begin with a set of specifications that define the purpose of the projects. These requirements describe the information required to develop the project. 2) Analysis: bottom-up vs. top-down In this phase we begin to break the problem into two manageable phases. There are two approaches to analysis: Bottom-Up Top-Down Bottom-Up Approach: o The Bottom-up approach is an older approach which gives early emphasis on coding. o Since the programmer does not have a master plan for the project, the resulting program may have many error ridden segments. Top-Down Approach: o This approach divides the problem in to manageable segments. o This technique generates diagrams that are used to design the system. o Frequently several alternate solutions to the programming problem are developed and compared during this phase.
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
o o o o
Data objects and operations This phase continues the work done in analysis phase. The First perspective leads to the creation of abstract data types, Second requires the specification of algorithms and a consideration of algorithm design strategies. Ex: suppose that we are designing a scheduling algorithm system for university. The typical operations might include students, courses, and professors. Typical operations might include inserting, removing, and searching within each object or between them.
4) Refinement and Coding o Choose representation for our data objects and write algorithms for each operation on them. o The order in which we do this is crucial because a data objects representation can determine the efficiency of the algorithm related to it. o Typically this means that we should write those algorithms that are independent of the data objects first. o If our original design is good, it can absorb changes easily. 5) Verification This phase consists of developing correctness proofs for the program, testing the program with a variety of input data, and removing errors. Program Proving: Proving the correctness of program on sample outputs. Testing: Giving the Good test data and to verify every piece of code runs correctly. Debugging: identifying the error if the behavior of the code is not according to the requirements. Debugging will be easier for a well documented code.
Contact-Email: khutub27@gmail.com
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
2. Pointers in C:
Pointer is a special variable which is used to store the address of other variables.
* the dereferencing (indirection) operator referred as value at the address & Address operator gives the address of a memory location. Points to be remembered: Address id a non-negative number Pointer allows arithmetic operations Size of pointer can be different on different computers the null pointer which contains a null address, points to no object or function. NULL is represented by the integer 0 Always set pointers to NULL when not used.
Contact-Email: khutub27@gmail.com
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
1. malloc(): This function is used to allocate a block of memory. If the memory is not available then it returns null. Syntax: pointer ptr; ptr = (data type *) malloc(size); Example: int *p; p=(int *) malloc(10*size of(int)); Ex-2: cptr = (char *) malloc (10);
#include<stdio.h> #include<stdlib.h> #define NULL 0 void main() { int *p, *table; int size; printf("what is the size of table\n"); scanf("%d",&size); if((table = (int *) malloc (size * sizeof(int))) == NULL) { printf("No space available \n"); exit(1); } printf("\n input table values \n"); for(p=table; p<table+size; p++) { scanf("%d",p); printf("%d is stored in address %u \n", *p, p); } }
Contact-Email: khutub27@gmail.com
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
2. calloc(): This function is used to allocate multiple block of memory. Usually calloc() is used to allocate memory to array during run time. If the memory is not available it returns null. Syntax: pointer *ptr; ptr = (data type *) calloc(n,size); n=size required
program
allocates
space
for
structure
strcut student { char name[25]; float age; long int id_num; }; typedef struct student record; record *st_ptr; int class_size = 30; st_ptr = (record *) calloc (class_size, sizeof(record)); . . . . . .
Contact-Email: khutub27@gmail.com
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
3.
realloc():
This function is used to reallocate the memory either by increasing or by decreasing. If the memory is not available then it returns null. Syntax: pointer ptr; ptr = realloc(new_size);
Example: int p*; p=realloc(p,10*size of(int)); 4. free(): This function is used to free the memory allocated during run time. while realising the memory the pointer must be dynamically allocated. Syntax: pointer ptr; free(ptr); Example: int P*; free(p);
Contact-Email: khutub27@gmail.com
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
Write a C program to implement Dynamic Memory allocation. #include<stdio.h> #include<conio.h> void main() { int i,p*,size; printf(\n Enter the size of the array); scanf(%d,&size); p=(int *) calloc(5,size of(int)); if(p==NULL) { printf(\n The Memory Is Not available); exit(0); } printf(\n Input The Array Elements); for(i=p;i<p+size;i++) scanf(%d,i); printf(\n The Contents of array ); for(i=p;i<p+size;i++) printf(%d\n,i); }
3. Algorithm Specification
Defn: an Algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate input in a finite amount time. Criteria or Characteristics of algorithms: Input: zero or more quantities that are externally supplied Output: at least one quantity is produced Definiteness: clear and unambiguous Finiteness: terminate after a finite number of steps Effectiveness: the algorithm must terminate after a finite sequence of instructions.
Representation of Algorithm:
A natural language, like English or Chinese. A graphic, like flowcharts. A computer language, like C. Lecturer: Syed Khutubuddin Ahmed Contact-Email: khutub27@gmail.com 9
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
Example 1.2 [Binary search]: [0] 8 [1] 14 [2] 26 [3] 30 [4] 43 [5] 50 [6] 52
left right middle list[middle] : searchnum Lecturer: Syed Khutubuddin Ahmed Contact-Email: khutub27@gmail.com 10
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
Search No = 18
Low High Mid Compare
0 0 2 2
6 2 2 1
3 1 2
Searching a sorted list while (there are more integers to check) { middle = (left + right) / 2; if (searchnum < list[middle]) right = middle - 1; else if (searchnum == list[middle]) return middle; else left = middle + 1; }
Contact-Email: khutub27@gmail.com
11
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
Recursive algorithms
Beginning programmer view a function as something that is invoked (called) by another function It executes its code and then returns control to the calling function. This perspective ignores the fact that functions can call themselves (direct recursion). They may call other functions that invoke the calling function again (indirect recursion). Recursive function is Extremely powerful Frequently allow us to express an otherwise complex process in very clear term Note: We should express a recursive algorithm when the problem itself is defined recursively.
Contact-Email: khutub27@gmail.com
12
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
Contact-Email: khutub27@gmail.com
13
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
5. Performance analysis
Performance Analysis (machine independent) space complexity: storage requirement time complexity: computing time Defn... of Analysis of algorithm or performance analysis or efficiency algo. Algorithm analysis refers to the task of finding the computing time and storage space of an algorithms, and it is also known as performance analysis or efficiency of an algorithm, which enables us to select an efficient algorithm We can analyze the algorithm in two ways By checking the correctness of an algorithm By measuring time and space and complexity of an algorithms
Contact-Email: khutub27@gmail.com
14
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
Space complexity:
Specifies the amount of temporary storage required for running the algorithm. We do not count storage required for input and output when we are working with the same type of problem and with different algorithms. Space needed by algorithm consists of the following component. a) The fixed static part: it is independent of characteristics ex: number of input/output. This part includes the instruction space (i.e. space for code). Space for simple variables, space for constants, and fixed size component variables. Let Cp be the space required for the code segment (i.e. static part) b) The variable dynamic part: that consists of the space needed by component variables whose size is dependent on the particular problem instance at runtime. i.e. space needed by reference variables, and the recursion stack space. Let Sp be the space for the dynamic part. c) Overall space required by program: S(p)= Cp + Sp
Contact-Email: khutub27@gmail.com
15
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
Space Analysis:
The Depth of recursion = 5 There fore The depth of recursion = n Fact(5) 5*Fact(4) 120 4*Fact(3) 24 3*Fact(2)6 2*Fact(1)2 Space occupied by n and constant 1 = 4 bytes Total space = 4 * depth of recursion Total space = 4 * n
Contact-Email: khutub27@gmail.com
16
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
Time Complexity:
1) Operation Count 2) Step Count 3) Asymptotic Notation
Algorithm Efficiency:
Linear Loops: For(i=0;i<1000;i++) Application code For(i=0;i<1000;i+=2) Application code Logarithmic Loops: For(i=0;i<1000;i*=2) Application code Or For(i=0;i<1000;i/=2) Application code Nested loops: For(i=0;i<1000;i++) For(j=0;j<1000;j++) Application code For(i=0;i<10;i++) For(j=0;j<10;j*=2) Application code For(i=0;i<10;i++) For(j=0;j<i;j++) Application code f(n)= n f(n)= n/2
f(n)= n2
f(n)=
nlogn
f(n)=
n((n+1)/2)
Internal loop : 1+2+3+.+9+10=55 Avg = 55/10 = 5.5 Can be written as (10+1)/2 so we can write as (n+1)/2
Contact-Email: khutub27@gmail.com
17
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
1) Operation Count:
Find the basic operation a= a * b; this code takes 1 unit of time For(i=0;i<n;i++) { for(j=0;j<n;j++) if(j==5) { printf(Internal Instruction); } } Basic operation is comparison So the Time Complexity N*n = n square
2) Step Count:
Ex: i) x = a + b; step count =1
ii) for(i =1; i <= n; i++) step count = n x= a + b; iii) for(i=1; i <= n; i++) step count = n2 for(j=1; j <= n; j++) x = a + b; Ex: int sum (int a[], int n) { int i , sum=0; sum-count ++; for (i=0; i<n; i++) { for-count ++; sum = sum + a[i]; } for-count++; return-count ++; return sum; }
Contact-Email: khutub27@gmail.com
18
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
The Step count for sum = The step count for for statement The step count for assignment = The step count for return total steps =
1 = n + 1 n = 1 2n + 3
Asymptotic Notations:
The asymptotic behavior of a function is the study of how the value of a function varies for larger values of n where n is the size of the input. Using asymptotic behavior we can easily find the time efficiency of an algorithm.
Different asymptotic notations are: Big (big oh) it is the formal method of expressing the upper bound of an algorithms running time. Its a measure of longest amount of time it could possibly take for the algorithm to complete. Let f(n) is the time efficiency of an algorithm. The function f(n) is said to be Big-oh of g(n), denoted by
f(n) O(g(n)) OR f(n) O(g(n))
Contact-Email: khutub27@gmail.com
19
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
Such that there exist a +ve constant c and +ve integer n0 satisfying the constraint.
E.g.: Consider the following assertion: Let f(n)=100n + 5 Express f(n) using big-oh
Solution:
It is given that f(n) =100n + 5. Replacing 5 with n (so that next higher order term is obtained), we get 100n + n and call it c* g(n). i.e. c* g(n) = 100n + n for n = 5 = 101n for n = 5 Now, the following constraint is satisfied f(n) c * g(n) for all n n0 i.e. 100n + 5 <= 101* n for n>= 5 it is clear from the above relation that c=101, g(n)=n and n0 = 5, so by definition f(n) O(g(n)) i.e. f(n) O(n)
2. notation (Big-Omega)
Let f(n) be the time complexity of an algorithm. The function f(n) is said to be Big-Omega of g(n) which is denoted by f(n) (g(n)) OR f(n) (g(n)) such that there exist a +ve constant c and non negative integer n0 satisfying the constraint f(n) c * g(n) for all n n0 ex: Let f(n) = 100n + 5 express f(n) using big-omega Lecturer: Syed Khutubuddin Ahmed Contact-Email: khutub27@gmail.com 20
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
it is clear from the above relation that c = 100, g(n) = n and n0 ,So by definition f(n) (g(n)) i.e. f(n) (n)
3. notation (Theta)
Let f(n) be the time complexity of an algorithm. The function f(n) is said to be Big-Theta of g(n) which is denoted by f(n) (g(n)) OR f(n) (g(n)) such that there exist a +ve constant c1, c2 and non negative integer n0 satisfying the constraint c2g(n) f(n) c1g(n) for all n n0
Contact-Email: khutub27@gmail.com
21
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
+ 5
c2g(n) f(n) c1g(n) for all n n0 100 * n 100n + 5 100n+n for n 5 It is clear from the above relation that c1=100, c2=105, n0= 5 , g(n)= n So by definition f(n) (g(n)) f(n) (n)
Contact-Email: khutub27@gmail.com
22
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
Analysis of the above Program Steps: Input parameter size : n Basic Operation required : if A [ i ] > Max Reason for basic operation to be made throughout the array, for loop is to be used starting from second element until last element is reached. Relation for finding the number of times the basic operation is executed Basic formula involved in solving the sum/ relation Therefore the time complexity in finding the largest element is the array = O (n)
Contact-Email: khutub27@gmail.com
23
SEM-II
CHAPTER-1
DEPARTMENT OF MCA
The Basic Steps involved are: Input parameter size: n Basic operation required : n * Fact (n-1) Reason if the operation be called more number of times: As n is non-negative integer and it starts reducing by 1 for each recursion, the algorithm should stop when n becomes 0, so the operation is performed until n = 0. Recurrence relation and initial condition: T (n) = T (n-1) + 1 for n > 0 // Relation T (0) = 1 // initial condition Solution for the recurrence relation: T (n) = T (n-1) + 1 = T (n -2) + 1 + 1 = T (n -3) + 1 + 1 + 1 = . = T (0) + 1 + 1 + ..n Times. There fore the number of multiplication required = n.
Contact-Email: khutub27@gmail.com
24