Escolar Documentos
Profissional Documentos
Cultura Documentos
11
Data + Structure
z
Data
Data is collection of numbers, alphabets and symbols combine to represent information. The value 545, A, JVP etc. are example of the data. Way of organization. Possible ways to define logically related 2/2/13 Dr. JIGAR PATEL 22 data item or atom called the Data
Structure
Data Structure
33
// Insert A[j] into the sort Sequences A[1j-1] i1 while i > 0 and A[i-1] > key do A[i+1] a[i] i i -1
Dr. JIGAR PATEL
2/2/13
44
Algorithm
z
The word Algorithm comes from the name of Persian author Abu Jafar Mohammad ibn Musba al Khowarizmi. It is method for solving problem in Computer Science and Applications
2/2/13
55
Algorithm
Finiten ess
Effective ness
Algorithm
Definite ness
Output
Input
2/2/13
66
More on Algorithm
z
Definiteness
Every steps of algorithm should be clear and not have any ambiguity Every algorithm have a proper end. (Avoid Infinite condition) Easy to understand and easy for the implementation for any PL.
Dr. JIGAR PATEL
2/2/13
Finiteness
Effectiveness
77
Algorithmic Strategies
z
Problem divide into subproblems to solve the entire problem The problems is divided into smaller instances and result of reoccurring instances are obtained to solve the problem Choose the best possible solution to obtain the final solution. 2/2/13 Dr. JIGAR PATEL
Dynamic Programming
Greedy Technique
88
Analysis of Algorithms
z
Efficiency measure
how long the program runs time complexity how much memory it uses space complexity
99
Analysis of Algorithms
z
Analyzing an algorithm has come to mean predicting the resources that the algorithm requires. we shall assume a generic oneprocessor, random-access machine (RAM) model of computation as our implementation technology and understand that our algorithms will be implemented as computer programs. 2/2/13
Dr. JIGAR PATEL
10
The running time of an algorithm on a particular input is the number of primitive operations or "steps executed. For each j = 2, 3, . . . , n, where n = length[A], we let tj be the number of times the while loop test in line 5 is executed for that value of j.
Dr. JIGAR PATEL
2/2/13
11
2/2/13
12
And
We find that in the worst case, the running time of INSERTION-SORT is.
Dr. JIGAR PATEL
2/2/13
14
This worst-case running time can be expressed as an2 + bn+ c for constants a, b, and c that again depend on the statement costs ci; it is thus a quadratic function of n
Dr. JIGAR PATEL
2/2/13
15
Designing algorithms
z
Many useful algorithms are recursive in structure: The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.
Dr. JIGAR PATEL
2/2/13
16
Merge Sort
z
The merge sort algorithm closely follows the divide-and-conquer paradigm. Divide: Divide the n-element sequence to be sorted into two subsequences of n/2 elements each. Conquer: Sort the two subsequences recursively using merge sort. Combine: Merge the two sorted subsequences to produce the sorted 2/2/13 17 answer. Dr. JIGAR PATEL
Merge Sort
z
MERGE-SORT(A,p,r)
2/2/13
18
Merge Sort
2/2/13
19
Other Functions
z
20
Time difference too small to be noticeable External things dominate (OS, disk I/O, )
21
Exercise - Searching
2 3 5 16 37 50 73 75 126
22
Solution - I
boolean LinearArrayFind(int array[],int n, int key ) { for( int i = 0; i < n; i++ ) { if( array[i] == key ) // Found it! return true; }
Dr. JIGAR PATEL
2/2/13
23
Solution - II
boolean BinArrayFind( int array[], int low, int high, int key ) { // The subarray is empty if( low > high ) return false; int mid = (high + low) / 2; if( key == array[mid] ) { return true; } Dr. JIGAR PATEL
2/2/13
24
Asymptotic Notation
z
Asymptotic notations are convenient for describing the worst-case running time function T(n), which is defined only on integer input size. Let n be a non-negative integer representing the size of the input to an algorithm
Dr. JIGAR PATEL
2/2/13 25 25
Asymptotic Notation
z
Let f(n) and g(n) be two positive functions, representing the number of basic calculations (operations, instructions) that an algorithm takes (or the number of memory words an algorithm needs). - Big Theta O - Big O - Big Omega PATEL Dr. JIGAR
2/2/13 26 26
z z z
- Notation
For a given function g(n), we denote by (g(n)) the set of functions (g(n)) = {f(n) : there exist positive constants c1 , c2 , and n0 such that 0 c1 g(n) f(n) c2 g(n) for all n n0
Dr. JIGAR PATEL
2/2/13
27 27
Example
z
(g(n)) = {f(n) : positive constants c1, c2, and n0, such that n n0, 0 c1g(n) f(n) c2g(n) }
1/2n2 - 3n = (n2) Determine the positive constant n0, c1, and c2.
Dr. JIGAR PATEL
2/2/13 28 28
c2 =
n0 = 7
2/2/13 29 29
1/2n2 - 3n = (n2)
O-Notation
z
O(g(n)) = {f(n) : there exist positive constants c and n0 such that 0 f(n) c g(n) for all n n0 }
z
Set of all functions whose rate of growth is the same as or lower than that of c. g(n)
Dr. JIGAR PATEL
2/2/13 30 30
O-Notation
2/2/13
31 31
f(n) 6n, for n 3 (C=6, n0=3) f(n) 0.5 n for n 0 (C=0.5, n0=0)
f(n)=n/2 3 = O(n)
n2-n = O(n2)
// g(n) = n2
n(n+1)/2 = O(n2)
- Notation
z For a given function g(n) z (g(n)) = {f(n) : there exist
positive constants c and n0 such that 0 c g(n) f(n) for all n n0}
Intuitively: Set of all functions whose rate of growth is the same as or higher than that of g(n). Dr. JIGAR PATEL
2/2/13 33 33
- Notation
2/2/13
34 34
2/2/13
35 35
Relations Between , O,
z
For any two function f(n) and g(n), we have f(n) = (g(n)) if and only if f(n) = O(g(n)) and f(n) = (g(n)) That is (g(n)) = O(g(n)) (g(n))
z z
2/2/13
36 36
1 log n n n log n n2 n3 2n n! T( n)
2 n
n 2
n log2 n
log 2n
2/2/13
37 37
Assignment
z
What is Data Structure? What are the important elements of Data Structure? Define the term: Algorithm. What should be the properties of Algorithm. How can you analyze the Algorithm using Asymptotic Notation? Explain 2/2/13 38 Dr. with example. JIGAR PATEL