A general presentation for helping out students participating in ALGORITHIMIX with the judging parameters.
ALGORITHIMIX The slides that you are about to see contain a general help for participants on the issue as to How are we going to judge the best algorithm among the submitted lot for a particular problem?
What Makes a good Algorithm? Suppose you have two possible algorithms or data structures that basically do the same thing; which is better?The general parameters are: 1)They should be Faster. 2)Should take up and consume Less space. 3)Should be Easier to code. 4)Easier to maintain. 5)Computers are fast BUT not infinitely fast, And memory is cheap BUT its not free ! Computing therefore is a bounded resource ,and so is space in your hard disk. These resources should be used wisely, and therefore ALGORITHMS THAT ARE EFFICIENT IN TERMS OF TIME OR SPACE ARE THE BEST ! Still biggest parameter remains------Padosi ko Samajh aara hai ya nahi?......:P How do we measure the first two?
Sample Problem:Searching Determine if a sorted array doosra solution: Binary Search of integers contains a given integer static boolean find (int[ ] a, int item) { 1st solution: Linear Search int low = 0; (check each element) int high = a.length - 1; static boolean find (int[ ] a, int item) { while (low <= high) { for (int i = 0; i < a.length; i++) { int mid = (low+high)/2; if (a[i] == item) return true; } if (a[mid] < item) return false; low = mid+1; } else if (item < a[mid]) high = mid - 1; else return true; } return false; }
Linear Search vs. Binary Search,kaunsa better hai? Simplifying assumption #1: Linear Search is easier to program Use the size of the input But Binary Search is rather than the input itself faster… isn’t it? For our sample search How do we measure to show that one is faster than problem, the input size the other is n where n-1 is the Experiment? array size Proof? But which inputs do we Simplifying assumption use? #2: Count the number of “ basic steps” rather than computing exact times
One Basic Step = One Time Unit For a conditional, we count Basic Step: *input or output of a scalar number of basic steps on value the branch that is executed *accessing the value of a For a loop, we count scalar variable, array element, or field of an object number of basic steps in *assignment to a variable, loop body times the array element, or field of an number of iterations object For a method, we count *a single arithmetic or logical operation number of basic steps in *method invocation (not method body (including counting argument evaluation steps needed to prepare and execution of the method stack-frame) body)
Runtime vs. Number of Basic Steps But isn’t this cheating? Which is better? *The runtime is not the * n or n2 time?(n is the size of same as the number of input) basic steps *100 n or n2 time? *Time per basic step *10,000 n or n2 time? varies depending on computer, on compiler, As n gets large, on details of code … multiplicative constants Well… yes, it is cheating in become less important a way.. But the number of basic Simplifying assumption steps is proportional to #3: the actual runtime Multiplicative constants aren’t important.
Using Big-O to Hide Constants Roughly, f(n) = O(g(n)) Claim: N 2 means that f(n) grows like + n = O(N 2 ) g(n) or slower We know n ≤ N 2 Definition: O(g(n)) is a set; for n ≥1 f(n) is a member of this set So N 2 + n ≤ 2 N 2 if and only if there exist constants c and N such that for n ≥1 0 ≤ f(n) ≤ c g(n), for all n≥N So by definition, We should write f(n) ∈ O(g(n)) N 2 + n = O(N 2 ) But by convention, we write for c=2 and N=1 f(n) = O(g(n))
Worst-Case/Expected-Case Bounds We can’t determine time 1. Use the size of the input rather than the input bounds for all possible itself inputs of size n Simplifying assumption #4: 2. Count the number of Determine number of steps “ basic steps” rather than for either computing exact times *worst-case or 3. Multiplicative constants *expected-case aren’t important 4. Determine number of steps for either worst-case or expected-case
Problem-Size Examples Suppose we have a computing device that can execute 1000 operations per second; how large aproblem can we solve? COMPLEXITY 1 SECOND 1 MINUTE 1 HOUR n 1000 60,000 3,600,000 nlog n 140 4893 200,000 N 2 31 244 1897 3N 2 18 144 1096 N 3 10 39 153 2 n 9 15 21
How will we judge? Depending upon the type of the time bound we get for your algorithm our perception for ur algo would be as follows[this will not be the ‘only’ parameter]: Type of Time category Our general bound of ur algo perception O(1) constant Excellent O(Log n) logarithmic Excellent O(n) linear Good O(n log n) Good O(N 2) quadratic Ok O(N 3) cubic May be ok O(2n) exponential Too slow
Our Simplifying Assumptions 1. Use the size of the input rather than the input itself 2. Count the number of “ basic steps” rather than computing exact times 3. Multiplicative constants aren’t important 4. Determine number of steps for either worst-case or expected-case
Worst-Case Analysis of Searching Linear Search (check each Binary Search element) static boolean find (int[ ] a, int item) { static boolean find (int[ ] a, int item) { int low = 0; for (int i = 0; i < a.length; i++) { int high = a.length - 1; if (a[i] == item) return true; } while (low <= high) { return false; int mid = (low+high)/2; } if (a[mid] < item) For Linear Search, worst-case low = mid+1; time is O(n) else if (item < a[mid]) For Binary Search, worst-case time is O(log n) high = mid - 1; else return true; } return false; }
Analysis of Matrix Multiplication Code for multiplying n-by-n By convention, matrix matrices A and B: problems are measured in for (i = 0; i<n; i++) terms of n, the number of for (j = 0; j < n; j++) rows and columns for (k = 0; k < n; k++) C[i][j] = C[i][j] + A[i][k] * Note that the input size B[k][j]; is 2N 2 Worst-case time is O(N 3 ) Expected-case time is also O(N 3 )
Further query? In case of any query or doubt please do any one of following: 1)Visit the ALGORITHIMIX Page at www.cossco.wordpress.com and put up ur query as a comment. 2)Mail at hkbiet@gmail.com subject:ALGORITHIMIX QUERY 3)Message or Call at 09455179526
End Of presentation Thank You
Recommend
More recommend