Download presentation
Presentation is loading. Please wait.
1
Lecture – 2 on Data structures
Preliminaries
2
Algorithmic Notation The format for the formal presentation of an algorithm consists of two parts. The first part is a paragraph which tells the purpose of the algorithm, identifies the variables which occur in the algorithm and lists the input data. The second part of the algorithm consists of the lists of steps that is to be executed. Example : A nonempty array DATA with N numerical values is given. Find the location LOC and the value MAX of the largest element of DATA. Algorithm 2.3: Given a nonempty array DATA with N numerical values, this algorithm finds the location LOC and the value MAX of the largest element of DATA. Set K : = 1, LOC : =1 and MAX : =DATA[1]. Repeat steps 3 and 4 while K<=N: If MAX<DATA[K], then : Set LOC : =K and MAX : =DATA[K]. [End of if structure] Set K : = K+1. Write: LOC, MAX. Exit.
3
Algorithmic Notation Steps, Control, Exit :
The steps of the algorithm are executed one after the other, beginning with step 1. Control may be transferred to step n by Go to step n If several statements appear in the same step, e. g. Set K : = 1, LOC : =1 and MAX : =DATA[1]. Then they are executed from left to right. The algorithm is completed when the statement Exit. Is encountered. Comments : Step may contain a comment in brackets which indicates the main purpose of the step.
4
Algorithmic Notation Variable names :
Will use capital letters, as in MAX and DATA Counters and subscripts will also be capitalized (K, N) Assignment Statement : Will use the dots-equal notation : = MAX : =DATA[1] Assigns the value in DATA[1] in MAX. Input and Output : Data may be input and assigned to variables by means of a read statement Read : Variable names. Messages placed in quotation marks and Data in variables may be output by Write or print statement. Write : Messages and/ or Variable names.
5
Algorithmic Notation Procedures :
Independent algorithmic module which solves a particular problem. Certain type of sub algorithm.
6
Complexity of Algorithm
The complexity of an algorithm is a function describing the efficiency of the algorithm in terms of the amount of data the algorithm must process. There are two main complexity measures of the efficiency of an algorithm: Time complexity is a function describing the amount of time an algorithm takes in terms of the amount of input to the algorithm. "Time" can mean the number of memory accesses performed, the number of comparisons between integers, the number of times some inner loop is executed, or some other natural unit related to the amount of real time the algorithm will take. Space complexity is a function describing the amount of memory (space) an algorithm takes in terms of the amount of input to the algorithm. We often speak of "extra" memory needed, not counting the memory needed to store the input itself. Again, we use natural (but fixed-length) units to measure this. We can use bytes, but it's easier to use, say, number of integers used, number of fixed-sized structures, etc. In the end, the function we come up with will be independent of the actual number of bytes needed to represent the unit. Space complexity is sometimes ignored because the space used is minimal and/or obvious, but sometimes it becomes as important an issue as time.
7
Complexity of Algorithm
Suppose M is an algorithm, n size of the input data. The time and space used by the algorithm M are the two main measures for the efficiency of M. The time is measured by counting the number of key operations – in sorting and searching algorithms, for example the number of comparisons. The space is measured by counting the maximum of memory needed by the algorithm. The complexity of an algorithm M is the function f(n) which gives the running time and or storage space requirement of the algorithm in terms of the size n of the input data. Frequently, the storage space required by an algorithm is simply a multiple of the data size n.
8
Complexity of Algorithm
The two cases one usually investigates in complexity theory are as follows : Worst case : The maximum value of f(n) for any possible input. Average case : The expected value of f(n). Sometimes we also consider the minimum possible value of f(n), called the best case. Average case assumes a certain probabilistic distribution for the input data. Suppose the numbers n1, n2,………,nk occur with respective probabilities p1, p2, Pk .Then the average value E is given by E = n1p1 +n2p nkpk.
9
Complexity of Algorithm
Example : Linear Search : Algorithm 2.4 : A linear array DATA with N elements and a specific ITEM of information are given. This algorithm finds the location LOC of ITEM in the array DATA or sets LOC = 0. Set K : = 1, LOC : =0. Repeat steps 3 and 4 while LOC = 0 and K<=N: If ITEM = DATA[K], then : Set LOC : =K . Set K : = K+1. [End of step 2 loop] [Successful?] If LOC = 0, then : Write : ITEM is not in the array DATA. Else : Write : LOC is the location of ITEM. [End of if structure] Exit.
10
Complexity of Algorithm
The complexity of the search algorithm is given by the number C comparisons between ITEM and DATA[k]. We seek C(n) for the worst case and the average case. Worst case : The worst case occurs when ITEM is the last element in the array DATA is not there at all. In other situation, we have C(n) = n Accordingly C(n) is the worst case complexity of the linear search algorithm. Average case : The number of comparisons can be any of the numbers 1, 2, 3, , n and each number occurs with probability p = 1/n. Then C(n) = 1 . 1/n /n /n =( n) .1/n =(n(n+1)/2).1/n = (n+1)/2 The average number of comparisons needed to find the location of ITEM is approximately equal to half the number of elements in the DATA list.
11
Solved problem 2.6, 2.7, 2.8, 2.9(p2.9A, p2.9B)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.