algorithm analysis (dastal)

Upload: teztdummy

Post on 30-May-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 Algorithm Analysis (DASTAL)

    1/20

  • 8/14/2019 Algorithm Analysis (DASTAL)

    2/20

    Algorithm Efficiency

    is used to describe properties of an

    algorithm relating to how much ofvarious types of resources it consumes

  • 8/14/2019 Algorithm Analysis (DASTAL)

    3/20

    Criteria of Algorithm Efficiency

    Space Utilization the amount of memory requiredto store the data

    Time Efficiency the amount of time required toprocess the data

  • 8/14/2019 Algorithm Analysis (DASTAL)

    4/20

    Algorithm Execution Time Factors

    Size of the input the number of input items affects the timerequired for the process to be completed

    - Example: the time it takes to sort a list of itemsdepends on the number of items in the list. Thus, theexecution time T of an algorithm must be expressed as afunction T(n) of the size n of the input.

    The kinds of intruction & the speed with which the machinecan execute the intructions

    - the value of T(n) cannot be express in real time units,instead by the approximate count of the intructionsexecuted

    Quality of the source code that implements the algorithm &the quality of the source code generated by the compilerfrom the source code

  • 8/14/2019 Algorithm Analysis (DASTAL)

    5/20

    Algorithm to Calculate a Mean

    /* Algorithm to find the mean of x[0],...,x[n-1] */

    1. Initialize sum = 0

    2. Initialize index variable i = 0

    3. While i < n do the following:

    4. A. Add x[i] to the sum

    5. B. Increment i by 1

    end while

    6. Calculate and return mean = sum / n

    *note: statements 1 and 2 are each executed 1 time, statement 4

    & 5 which comprise the body of the while loop are eachexecuted n times, and statement 3 which controls therepetition is executed n + 1 times, since 1 additional check isrequired to detemine that the control variable i is no longerless than the value of n. Statement 6 is executed 1 time.

  • 8/14/2019 Algorithm Analysis (DASTAL)

    6/20

    Summarized Analysis

    Statement # of time executed

    1 1

    2 1

    3 N + 1

    4 N

    5 N

    6 1

    Total 3n + 4

  • 8/14/2019 Algorithm Analysis (DASTAL)

    7/20

    Conclusion

    The computing time for the algorithm to calculate a mean isgiven by T(n) = 3n + 4

    So as the number of input increases, the value of T(n) at arate proportional to n, so we say the T(n) has order ofmagnitude n

    T(n) is O(n)

  • 8/14/2019 Algorithm Analysis (DASTAL)

    8/20

    Proving ALgorithms Correct

    It is a deductive proof of a programs correctness

    - consist of:

    Precondition Pre(the Given)

    Postcondition Post(the To show:)

  • 8/14/2019 Algorithm Analysis (DASTAL)

    9/20

    Example

    /* Algorithm to find the mean of x[0],...,x[n-1] */

    1. Initialize sum = 02. Initialize index variable i = 0

    3. While i < n do the following:

    4. A. Add x[i] to the sum

    5. B. Increment i by 1

    end while

    6. Calculate and return mean = sum / n

    Pre: Input consist of an integer n 1 and an array x of n realnumbers.

    Post: Execution of the algorithm will terminate, and when itdoes, the value of the variable mean is the mean(average)of x[0],..., x[n-1]

  • 8/14/2019 Algorithm Analysis (DASTAL)

    10/20

    Note:

    To demonstrate that the postcondition Post follows from theprecondition Pre and the execution of the algorithm,one

    usually introduces, at several points in the algorithm,intermediate assertions about the state of processing whenexecution reaches these points. For the precedingalgorithm we might use an additional intermediateassertion at the bottom of the while loop that will be true

    each time execution reaches this point. Such assertion iscalled loop invariant.

  • 8/14/2019 Algorithm Analysis (DASTAL)

    11/20

  • 8/14/2019 Algorithm Analysis (DASTAL)

    12/20

    Standard Algorithm in Java

    In java it is know as standard library or java class library(JCL)

    the library that is conventionally made available in everyimplementation of that language. In some cases, thelibrary is described directly in the programming languagespecification; in other cases, the contents of the standard

    library are determined by more informal social practices inthe programming community.

  • 8/14/2019 Algorithm Analysis (DASTAL)

    13/20

    Standard Library Includes

    Subroutines

    Macro definitions

    Global variables

    Class definitions

    Templates

    Most standard libraries include definitions for at least the followingcommonly used facilities:

    Algorithms (such as sorting algorithms)

    Data structures (such lists, trees and hash tables)

    Interaction with the host platform, including input/output andoperating system calls

  • 8/14/2019 Algorithm Analysis (DASTAL)

    14/20

    Java Class Library (JCL)

    is a set of dynamically loadable libraries

    that Java applications can call at runtime.

    The Java Class Library is almost entirelywritten in Java itself, except the parts that

    need to have direct access to thehardware and operating system (as forI/O, or Graphic Rasterisation). The Javaclasses that give access to these functions

    commonly use java native interface (JNI)wrappers to access the native API of theoperating system.

  • 8/14/2019 Algorithm Analysis (DASTAL)

    15/20

    Purposes of JCL within the Java Platform

    Like other standard code libraries, they provide theprogrammer a well-known set of functions to perform

    common tasks, such as maintaining lists of items orperforming complex string parsing.

    In addition, the class libraries provide an abstract interface totasks that would normally depend heavily on the hardware

    and operating system. Tasks such as network access andfile access are often heavily dependent on the nativecapabilities of the platform.

    Finally, some underlying platforms may not support all of the

    features a Java application expects. In these cases, theclass libraries can either emulate those features usingwhatever is available, or provide a consistent way to checkfor the presence of a specific feature.

  • 8/14/2019 Algorithm Analysis (DASTAL)

    16/20

    Java Platform

  • 8/14/2019 Algorithm Analysis (DASTAL)

    17/20

    Main Feature of JCL

    Features of the Class Library are accessed through classesgrouped by packages.

    java.lang contains fundamental classes and interfaces closelytied to the language and runtime system.

    I/O and networking: access to the platform file system, and

    more generally to networks, is provided through thejava.io, java.nio, and java.net packages.

    Mathematics package: java.math provides regularmathematical expressions, as well as arbitrary-precisiondecimals and integers numbers.

    Collections and Utilities : provide built-in Collection datastructures, and various utility classes, for Regularexpressions, Concurrency, logging and Data compression.

  • 8/14/2019 Algorithm Analysis (DASTAL)

    18/20

    Main Feature of JCL (cont)

    GUI and 2D Graphics: the java.awt package supports basic GUI operationsand binds to the underlying native system. It also contains the 2DGraphics API. The javax.swing package is built on AWT and provides a

    platform independent widget toolkit, as well as a Pluggable look and feel.It also deals with editable and non-editable text components.

    Sound: provides interfaces and classes for reading, writing, sequencing, andsynthesizing of sound data.

    Text: the java.text package deals with text, dates, numbers, and messages.

    Image package: java.awt.image and javax.imageio provide APIs to write,read, and modify images.

    XML: built-in classes handle SAX, DOM, StAX, XSLT transforms, XPath, andvarious APIs for Web services, as SOAP protocol and JAX-WS.

    CORBA and RMI APIs, including a built-in ORB

    C 2

  • 8/14/2019 Algorithm Analysis (DASTAL)

    19/20

    Main Feature of JCL (cont2)

    Security and Cryptography

    Databases: access to SQL databases is provided through thejava.sql package.

    Access to Scripting engines: the javax.script package givesaccess any Scripting language that conforms to this API.

    Applets: java.applet allows applications to be downloadedover a network and run within a guarded sandbox

    Java Beans: java.beans provides ways to manipulate reusablecomponents.

  • 8/14/2019 Algorithm Analysis (DASTAL)

    20/20

    END