+ All Categories
Home > Documents > Csci20 Prelim Lecture

Csci20 Prelim Lecture

Date post: 07-Apr-2018
Category:
Upload: ronald-cordova
View: 222 times
Download: 0 times
Share this document with a friend

of 37

Transcript
  • 8/6/2019 Csci20 Prelim Lecture

    1/37

    CSCI20Prelim

    References:

    Introduction to Algorithms, Thomas H. Cormen, 2nd edition,

    MIT Press

    Instructor: Priyanka S

  • 8/6/2019 Csci20 Prelim Lecture

    2/37

    What are the Algorithms?

    Algorithm:

    Is any well-defined computational procedure that takes somevalue, or set of values, as input and produces some value, or set

    of values, as output.

    It is a tool forsolving a well-specified computational problem.

    They are written in apseudo code which can be implemented inthe language of programmers choice.

  • 8/6/2019 Csci20 Prelim Lecture

    3/37

  • 8/6/2019 Csci20 Prelim Lecture

    4/37

  • 8/6/2019 Csci20 Prelim Lecture

    5/37

    Example: sorting numbers.

    Sorting is a fundamental operation.

    Many algorithms existed for that purpose.

    The best algorithm to use depends on: The number of items to be sorted.

    possible restrictions on the item values

    kind of storage device to be used: main memory, disks, or tapes.

  • 8/6/2019 Csci20 Prelim Lecture

    6/37

    Correct and incorrect algorithms

    Algorithm is correct if, for every input instance, itends with the correct output. We say that a correct

    algorithm solves the given computational problem.

    An incorrect algorithm might not end at all on some

    input instances, or it might end with an answer other

    than the desired one.

    We shall be concerned only with correct algorithms.

  • 8/6/2019 Csci20 Prelim Lecture

    7/37

    Practical Examples

    Internet and Networks.

    The need to access large amount of information with theshortest time.

    Problems of finding thebest routs for the data to travel. Algorithms forsearching this large amount of data toquickly find the pages on which particular informationresides.

    Electronic Commerce. The ability ofkeeping the information (credit card

    numbers, passwords, bank statements) private, safe, andsecure.

    Algorithms involves encryption/decryption techniques.

  • 8/6/2019 Csci20 Prelim Lecture

    8/37

    These lists are far from ex

    haustive, but ex

    hibittow characteristics that are common to many

    interesting algorithms.

    (1)There are many candidate solutions, most of

    which are not we want. Finding one that we do

    want can present quite a challenge.

    (2)There are practical applications.

  • 8/6/2019 Csci20 Prelim Lecture

    9/37

    Hard problems

    We can identify the Efficiency of analgorithm from its speed (how long does the

    algorithm take toproduce the result).

    Some problems have unknown efficient

    solution.

    These problems are called NP-complete

    problems.

  • 8/6/2019 Csci20 Prelim Lecture

    10/37

    Hard problems

    Nobody has proven that an efficientalgorithm for one cannot exist.

    Several NP-complete problems are similar, but not

    identical.

    If we can show that the problem is NP-complete, we

    can spend our time developing an efficient algorithm

    that gives a good, but not the best possible solution.

    Example of NP-complete problem: traveling

    salesman.

  • 8/6/2019 Csci20 Prelim Lecture

    11/37

    Why do we need the algorithms?

    One always need to proofthat his solution method terminateand does the correct answer.

    Computers may be fastbut they are not infinitely fast, andmemory may be cheap but it is not free. This resources should

    be used wisely.

    As an example:

    There is two algorithms for sorting; merge sort algorithm,

    and insertion sort algorithm

    Insertion sort takes an execution time equal c1*n2 to sort nitems.

    Merge sort takes an execution time equal c2*n log2n to sort n

    items.

  • 8/6/2019 Csci20 Prelim Lecture

    12/37

    Algorithms as a technology

    Suppose computers were infinitely fast and computermemory was free. Would you have any reason to studyalgorithms?

    The answer is yes, if for no other reason than that you

    would still like to demonstrate that your solution methodterminates and does so with the correct answer.

    If computers were infinitely fast, any correct method forsolving a problem would do. You would probably wantyour implementation to be within the bounds of goodsoftware engineering practice (i.e., well designed anddocumented), but you would most often use whichevermethod was the easiest to implement

  • 8/6/2019 Csci20 Prelim Lecture

    13/37

    Why do we need algorithms?- cont.

    c1 and c2 are constants.

    Insertion sort usually has a smallerconstant factor thanmerge sort, so that c1< c2

    Two computers; computer A running insertion sort, andcomputer B running merge sort.

    Assuming Computer A executes one billion instructions per

    second and computer B executes only ten million instructionsper second, so that computer A is 100 times faster thancomputer B.

  • 8/6/2019 Csci20 Prelim Lecture

    14/37

    Why do we need algorithms?- cont.

    Suppose (c1 =2, and c2 =50).

    To sort one million number, computer A takes,

    2 x (106 ) 2 instruction /109 instruction/sec = 2000 sec.

    While computer B takes, 50 x 106x log2 106 instruction /107

    instruction/sec = 100 sec.

  • 8/6/2019 Csci20 Prelim Lecture

    15/37

    Algorithms and other technologies

    Algorithms are truly that important oncontemporary computers in light of otheradvanced technologies, such as

    Hardware with high clock rates, pipelining, andsuperscalar architectures,

    Easy-to-use, intuitive graphical user interfaces

    (GUIs),Object-oriented systems, and

    Local-area and wide-area networking.

  • 8/6/2019 Csci20 Prelim Lecture

    16/37

    Expressing algorithms

    Algorithms can be expressed in many

    kinds of notation, including

    natural languages, pseudocode,

    flowcharts,

    programming languages or

    control tables (processed by interpreters).

  • 8/6/2019 Csci20 Prelim Lecture

    17/37

  • 8/6/2019 Csci20 Prelim Lecture

    18/37

    Algorithm vs Pseudo-code vs

    Program-code Algorithm:

    Step 1: Declare two integers and variable that

    computes the sum. (num1, num2, sum)

    Step 2: Accept the first number.

    Step 3: Accept the second number.

    Step 4: Computes for the sum of the first and

    second numbers.Step 5: Display the computed sum of the two

    numbers.

  • 8/6/2019 Csci20 Prelim Lecture

    19/37

    Pseudo-code:

    declare num1 as int

    declare num2 as intdeclare sum as double

    accept num1

    accept num2

    sum = num1 + num2

    display sum

  • 8/6/2019 Csci20 Prelim Lecture

    20/37

    Flowchart: Is an algorithm that uses symbols

    to describe a method or a program. Used

    usually instead ofpseudo-code.

  • 8/6/2019 Csci20 Prelim Lecture

    21/37

    Program

    Import java.util.Scanner;

    Public class sample{

    public static void main(String[] args)

    {

    int num1, num2, sum;

    Scanner sc = new Scanner(System.in);

    num1 = sc.nextInt();

    num2 = sc.nextInt();

    sum = num1 + num2;

    System.out.println(The sum is +sum);

    }

    }

  • 8/6/2019 Csci20 Prelim Lecture

    22/37

  • 8/6/2019 Csci20 Prelim Lecture

    23/37

    Comparing algorithms

    Time complexity

    The amount of time that an algorithm needs torun to completion.

    Space complexity The amount of memory an algorithm needs to

    run

    The better algorithm is the one whichruns faster (has smaller timecomplexity)

  • 8/6/2019 Csci20 Prelim Lecture

    24/37

    Coding an Algorithm

    Coding an algorithm consists of

    implementing the algorithm using a

    computerprogramming language.

  • 8/6/2019 Csci20 Prelim Lecture

    25/37

    Algorithm examples

    To find the sum of two numbers

    1. declare n1,n2 as integers

    2. declare sum as integers3. input values for n1,n2

    4. sum n1+n2

    5. print Sum is, sum6. halt

  • 8/6/2019 Csci20 Prelim Lecture

    26/37

  • 8/6/2019 Csci20 Prelim Lecture

    27/37

    Write an algorithm to calculate the final price

    as the product of itemprice and tax.

    declare itemprice,tax as double

    declare finalprice as double

    input values for itemprice, tax

    finalprice = itemprice + tax

    print final price is , finalpricehalt

  • 8/6/2019 Csci20 Prelim Lecture

    28/37

    To find the largest of two numbers

    1. Declare a, b as integers

    2. Input values for a,b3. if a> b then

    3.1 print a is larger than b

    4. else

    4.1 print b is larger than a

    5. halt

  • 8/6/2019 Csci20 Prelim Lecture

    29/37

    Loops

    Write an algorithm to display the first

    20 natural numbers

    declare I as integerfor I=1;I

  • 8/6/2019 Csci20 Prelim Lecture

    30/37

    To find the sum of first n natural numbers.

    Declare i, n,sum as integers

    Input value for n

    Assign sum =0

    for i=1;i

  • 8/6/2019 Csci20 Prelim Lecture

    31/37

    Euclids Algorithms

    The Euclidean algorithm (also called Euclid's

    algorithm) is an algorithm to determine the

    greatest common divisor(gcd) of two integers.

    In mathematics, the Euclidean algorithm(alsocalled Euclid's algorithm) is an efficient method

    for computing the greatest common divisor

    (GCD), also known as the greatest common

    factor (GCF) or highest common factor (HCF). Itis named after the Greek mathematician Euclid.

  • 8/6/2019 Csci20 Prelim Lecture

    32/37

    GCD

    3 divides 15 (15/3 is a whole number), and is called adivisor of 15. 1, 3, 5, and 15 are all of the divisors of15.

    Prime numbers have only themselves and 1 asdivisors.

    Example 7 has 1 and 7 as divisors Example: Take number 36 and 15

    Divisors of 15 are 1,3,5,15

    Divisors of 36 are 1,2,3,4,6,9,12,18,36

    36 and 15 have some "common divisors," 1, and 3. You canlist the divisors of each, and you will find that 1 and 3 are onboth lists.

    Sometimes you need to know the greatest common divisor(gcd), the greatest number that is a common divisor of two ormore whole numbers.

  • 8/6/2019 Csci20 Prelim Lecture

    33/37

    Euclids Algorithm

    In Euclid's Elements we find a way ofcalculating the gcd of two numbers, withoutlisting the divisors of either number. It is nowcalled Euclid's Algorithm.

    [An algorithm is a step by stepprocess (orrecipe) for doing something.]

    We will find the gcd of 36 and 15. Divide 36 by 15 (the greater by the smaller), getting 2

    with a remainder of 6. Then we divide 15 by 6 (the previous remainder) andwe get 2 and a remainder of 3.

    Then we divide 6 by 3 (the previous remainder) andwe get 2 with no remainder.

    The last non-zero remainder (3) is our gcd.

  • 8/6/2019 Csci20 Prelim Lecture

    34/37

    Euclids Algorithm for computing

    gcd(m,n)

    Step1: if n=0, return the value of m as the

    answer and stop;otherwise,proceed to

    Step2.

    Step2: Divide m by n and assign the value

    of the remainder to r.

    Step3: Assign the value of n to m and the

    value of r to n. Go to Step1.

  • 8/6/2019 Csci20 Prelim Lecture

    35/37

    //Computes gcd(m,n)

    //Input: Two nonnegative, not-both-zero integersm and n

    //output: Greatest common divisor of m and n While n 0 do

    rm mod n

    mn

    nr

    return m

  • 8/6/2019 Csci20 Prelim Lecture

    36/37

    Example

    Using Euclids algorithm find the gcd of 24, 33

    Euclid's algorithm comes in handy withcomputers, because listing divisors is moredifficult than the above algorithm. Largenumbers are difficult to factor, while they arerelatively easy to divide.

    Euclids algorithm is considered to be one of thebest examples of an efficient algorithm.

    33/24 =1 remainder 9 24/9 = 2 remainder 6

    9/6=1 remainder 3

    6/3=2 remainder 0

    The last non-zero remainder is 3

  • 8/6/2019 Csci20 Prelim Lecture

    37/37

    1. Write an algorithm to check if a given number is even orodd

    2. Write an algorithm to find the smallest of 3 numbers.

    3. To find the smallest of 3 numbers

    4. To check if a given number is a positive or negativenumber

    5. To check if a number is divisible by 5

    6. Write an algorithm to print the sum and count of non-

    negative numbers out of a list of 50 numbers7. Write an algorithm to read the gender and age of 50

    voters and count how many male voters of age above18 are present in the list

    8. Write an algorithm to find the largest number out of a list

    of 100 numbers.


Recommended