Download Introduction to Data Structure and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!
Lecture # 1
Introduction
to
Data Structure and Algorithms
B y : M r. V i j a y K u m a r S o n i
Data Structure & Algorithms
- (^) Algorithms : Outline ,the essence of computational procedures, step-by-step instructions.
- (^) Program: An implementation of an algorithm in some programming language.
- (^) Data Structure: Organization of data needed to solve the problem.
Algorithm Solution
- (^) Algorithm describes action on the input instance.
- (^) Infinitely many correct algorithms for the same algorithmic problem. Input Instance, adhering to the specification Algorithm Output related to the input as required
What is a Good Algorithm?
- (^) Efficient:
- (^) Running time
- (^) Space used
- (^) Efficiency as a function of input size:
- (^) The number of bits in an input number.
- (^) Number of data element(numbers, etc.).
Limitations of Experimental Studies
- (^) It is necessary to implement and test the algorithm in order to determine its running time.
- (^) Experiment can be done only on a limited set of inputs.
- (^) In order to compare two algorithms, the same hardware and software environment should be used.
Beyond Experimental Studies
- (^) We will develop a general methodology for
analyzing running time of algorithms:
- (^) This approach uses a high-level description of the algorithm instead of testing one of its implementations.
- (^) Takes into account all possible inputs.
- (^) Allows one to evaluate the efficiency of any algorithm in a way that is independent of the hardware and software environment.
P s e u d o - C o d e ( 2 )
- (^) It is more structured then usual prose but less
formal than a programming language.
- (^) Expressions:
- (^) Use standard mathematical symbols to describe numeric and boolean expressions.
- (^) Use ← for assignment (“=“ in java)
- (^) Use = for the equality relationship (“==“ in java)
- (^) Method Declarations:
- (^) Algorithm name(param1,param2)
P s e u d o - C o d e ( 3 )
- (^) Programming Constructs:
- (^) Decision structures: if……..then….[else..]
- (^) While-loops: while…do
- (^) Repeat-loops: repeat….until …
- (^) For-loop: for…do
- (^) Array indexing: A[i],A[i,j]
- (^) Methods
- (^) Calls: object method(args)
- (^) Returns: return value
E x a m p l e : S o r ti n g
INPUT OUTPUT
Sequence of numbers a permutation of the sequence of numbers a1,a2,a3,…..an b1,b2,b3………bn 2 5 4 6 8 2 4 5 6 8 Sort Running Time Depends on
- (^) numbers of elements(n)
- How(partially) sorted they are
- algorithm
E x a m p l e : S o r ti n g
INPUT OUTPUT
Sequence of numbers a permutation of the sequence of numbers a1,a2,a3,…..an b1,b2,b3………bn 2 5 4 6 8 2 4 5 6 8 Running Time Depends on
- (^) numbers of elements(n)
- How(partially) sorted they are
- algorithm
Analysis of Insertion Sort
for j=2 to length (A) do key=A[j] “insert A[j] into the sorted sequence A[1..j-1]” i=j- while i>0 and A[i]>key do A[i+1]=A[i] i-- A[i+1]:=key cost c 1 c 2 0 c 3 c 4 c 5 c 6 c 7 times n n- n- n- n- 2 n j t j 2 ( 1) n j t j 2 ( 1) n j t j
- (^) Time to compute the running time as a function of the input size
Best/Worst/Average Case
- Best case : elements already sorted ® tj=1, running time = f(n), i.e., linear time.
- (^) Worst case : elements are sorted in inverse order ® tj=j , running time = f(n^2 ), i.e., quadratic time
- (^) Average case : tj=j/2, running time = f(n^2 ), i.e., quadratic time
Best/Worst/Average Case (3)
- (^) For inputs of all sizes: 1n 2n 3n 4n 5n 6n Input instance size Running time 1 2 3 4 5 6 7 8 9 10 11 12 ….. best-case average-case worst-case
Best/Worst/Average Case (4)
- (^) Worst case is usually used:
- (^) It is an upper-bound and in certain application domains (e.g., air traffic control, surgery) knowing the worst-case time complexity is of crucial importance
- (^) For some algorithms worst case occurs fairly often
- (^) The average case is often as bad as the worst case
- (^) Finding the average case can be very difficult