HIGH-LEVEL PROGRAMMING I
Intro to Algorithms   by Prasanna Ghali
    Outline
2
       What is an Algorithm?
       Algorithm characteristics
       Elements of an algorithm
       Expressing algorithms
       Pseudocode
       Flowcharting
       Examples
    What is an Algorithm?
3
     Step-by-step method for solving a problem
     Similar in concept to a cook’s recipe
    Algorithm Example: Recipe for
4
    Brownies
           ½ cup butter             1 tsp vanilla extract
           1 cup sugar              ½ cup cocoa
           2 eggs                   ½ cup flour
    1.   If butter not soft, then melt butter
    2.   Blend melted buffer and sugar until mixture has
         creamy consistency
    3.   Add eggs and vanilla; stir
    4.   Add cocoa and flour; mix until well blended
    5.   Pour into greased round glass cake pan
    6.   Microwave for 9-10 minutes
    Examples of Algorithms
5
     Putting together IKEA furniture
     Going from airport to hotel in new city
     Solving Rubik’s cube
     Playing board games such as Chess, Go, …
     Solving a maze
     Choosing a TV cable plan
     Choosing a cell phone subscription plan
    Oldest Algorithm?
6
       First documented algorithm was presented by
        Greek mathematician Euclid in 300 B.C. to
        compute Greatest Common Divisor (GCD)
        1.   Let 𝐴 and 𝐵 be integers such that 𝐴 > 𝐵 ≥ 0
        2.   If 𝐴 = 0, then GCD is 𝐵 and algorithm ends
        3.   If 𝐵 = 0, then GCD is 𝐴 and algorithm ends
        4.   Write quotient remainder format: 𝐴 ← 𝐵 ∙ 𝑄 + 𝑅
        5.   Set 𝐴 ← 𝐵 and 𝐵 ← 𝑅
        6.   Go to 2
    Formal Definition
7
     Procedure to solve well-specified problem
     Must have following characteristics
         Input
         Output
         Precision
         Finiteness
         Uniqueness
         Generality
    Algorithm Elements (1/4)
8
       Mid-60s, mathematicians proved that any logic
        problem, no matter how complicated, can be
        constructed using one or more of only three
        structures or elements
         Think   of an element as basic unit of logic
       What are these three elements?
         Sequence
         Selection
         Iteration
    Algorithm Elements (2/4)
9
       Euclid’s Greatest Common Divisor (GCD)
        algorithm
        1.   Let 𝐴 and 𝐵 be integers such that 𝐴 > 𝐵 ≥ 0
        2.   If 𝐴 = 0, then GCD is 𝐵 and algorithm ends
        3.   If 𝐵 = 0, then GCD is 𝐴 and algorithm ends
        4.   Write quotient remainder format: 𝐴 ← 𝐵 ∙
             𝑄+𝑅
        5.   Set 𝐴 ← 𝐵 and 𝐵 ← 𝑅
        6.   Go to 2
     Algorithm Elements (3/4)
10
        Elements an be combined in infinite ways
          Every element has single entry and single exit point
          Elements can be stacked or connected to one
           another only at their entry or exit points
          Any element can be nested within another element
     Algorithm Elements (3/3)
11
        We’ll add two additional elements
          Input
          Sequence
          Selection
          Iteration
          Output
     Expressing Algorithms
12
      Need notation for expressing algorithms
      Options range from natural to programming
       languages
      Two tools commonly used
          Pseudocode
          Flowchart
     Pseudocode
13
      Pseudo means false
      Code    a problem means to put it into
       programming language
      Pseudocode means false code
      Sentences that appear to be written in
       programming language but don’t necessarily
       follow syntax rules of any specific language
     Flowcharting
14
        Combination of graphical
                                      input/output
         symbols     that represent
         logical flow of data thro’
         solution                       sequence
                                         decision
                                          terminal
                                          flowline
     Pseudocode: Input
15
        Can write reading of input from user as:
                  1: read x          input x
                  1: read a, b, c   input a, b, c
     Pseudocode: Output
16
        Writing of output to user represented as:
                              1: print x         output x
                                                 output
     1: print “your speed is: “, x
                                           “your speed is: “, x
     Sequence (1/2)
17
      Computational statements executed
       in order, one after another
      Once you start series of steps in
       sequence, you must continue step by   step 1
       step until sequence ends
                               1: step 1     step 2
                               2: step 2
                               3: …
                               ⋮             step n
                               n: step n
     Sequence (2/2)
18
                            x = 10
        1: x = 10
        2: y = ax2+bx+c   y=ax2+bx+c
        3: z = y
                             z=y
     Statements, Expressions, … (1/4)
19
      Statement made up one or more expressions
      Expression consists of one or more operands
       and zero or more operators
      Operand can be constant or variable
                      operator
         variable       x = 10      constant
                       operand
     Statements, Expressions, … (2/4)
20
        How many expressions in following statements?
                3     1: x = 10
                5     2: y = x*x
                7     3: z = x + y * y
     Statements, Expressions, … (3/4)
21
      Every expression evaluates to a value of
       certain type
      Type represents set of values and set of
       operations that can be applied on these values
     1: z is integer                 assignment
     2: z = 10             =         operator
                                        type is
                       z        10      integer
     Statements, Expressions, … (4/4)
22
      Numbers without fractions (such as 10, 33, …)
       are called integers
      Digital systems cannot represent infinite set of
       fractional values on real number-line
      Instead, they represent a finite set of floating-
       point values with each floating-point value
       being an approximation of real values
     Assignment Operator
23
     variable = expression
      Copy value of expression into variable, or
      Replace current value of variable by value of
       expression
                variable       expression
     Arithmetic Operators
24
     variable = operand1 operator operand2
        +, -, *, /, %
                             =
                                        op
                  variable
                             operand1        operand2
     Relational Operators
25
      Used to compare two operands or values or
       expressions
      ==, !=, >, >=, <, <=
      Expression involving relational operators
       always evaluates to either 1 (true) or 0 (false)
          Value 0 means false
          Any non-zero value means true
     Logical Operators
26
      Used to control sequencing of steps; combine
       simple relational expressions into more
       complex expressions
      &&, ||, !
      Expression involving logical operators always
       evaluates to either 1 (true) or 0 (false)
      a = x < y || x > z
      b = x >= y && x <= z
     Selection Element (1/4)
27
        Alters sequential flow of control
        One of two courses of action is taken based on
         result of evaluation of condition
        How to assign letter grades?
        Should I take an umbrella?
        Overtime pay?                     condition
                                 true                   false
        Cable billing
                              action1                action 2
       Selection Structure (2/4)
28
            condition                      condition
     true                         true                   false
                        false
action                          action 1               action 2
      if (condition) then           if (condition) then
        action                        action 1
      endif                         else
                                      action 2
                                    endif
     Selection Structure (3/4)
29
                                         𝒂%      Letter Grade
                                      𝑎 ≥ 93          A
            𝑎 ≥ 93                   90 ≤ 𝑎 < 93       A−
 true                       false    87 ≤ 𝑎 < 90       B+
                                     83 ≤ 𝑎 < 87       B
print ‘A’            print not ‘A’   80 ≤ 𝑎 < 83       B−
                                     77 ≤ 𝑎 < 80       C+
                                     73 ≤ 𝑎 < 77       C
                                     70 ≤ 𝑎 < 73       C−
                                     60 ≤ 𝑎 < 70       D
                                      𝑎 < 60           F
     Selection Structure (4/4)
30
            𝑎 ≥ 93
     true                false
print ‘A’                𝑎 ≥ 90
                 true                   false
            print ‘A-’                  𝑎 ≥ 87
                                 true                  false
                         print ‘B+’                    𝑎 ≥ 83
                                                true                  false
                                         print ‘B’                            ⋮
                                                               true           false
     Iteration Element
31
        Repeat actions while a condition remains true
                                 condition       true    action
     while (condition)
       action                       false
     endwhile
                                         start
     First Algorithm                    input 𝑁
32
        Compute   𝑥 2 ∀ 𝑥 ∈ 1, 𝑁       𝑠𝑢𝑚 = 0
                                         𝑥=1
          1: read 𝑁
          2: 𝑠𝑢𝑚 = 0
                                                    𝑓𝑎𝑙𝑠𝑒
                                        𝑥≤𝑁
          3: 𝑥 = 1                          𝑡𝑟𝑢𝑒
          4: while (𝑥 <= 𝑁)
                                    𝑠𝑢𝑚 = 𝑠𝑢𝑚 + 𝑥 2
          5: 𝑠𝑢𝑚 = 𝑠𝑢𝑚 + 𝑥 ∗ 𝑥        𝑥 =𝑥+1
          6: 𝑥 = 𝑥 + 1
          7: endwhile
                                      output 𝑠𝑢𝑚
          8: print 𝑠𝑢𝑚                   end
     Traveling on a Subway
33
     Algorithm: Subway
34
        Input: Source station 𝐴 and destination station 𝐵
        Output: Directions to go from 𝐴 to 𝐵 using maximum of single transfer
        if 𝐴 and 𝐵 are on same line 𝐿 then
           travel on line 𝐿 from 𝐴 to 𝐵
        else [𝐴 and 𝐵 are on different lines]
           find all pairs of lines (𝐿𝑖 , 𝐿𝑗 ) s.t 𝐴 is on 𝐿𝑖 & 𝐵 is on 𝐿𝑗
                             and two lines have common station
           if there is only one such pair then
             take 𝐿𝑖 to a station also on 𝐿𝑗 , get off, and then take 𝐿𝑗 to station 𝐵
           else [more than one possible pair of them]
             use pair (𝐿𝑖 , 𝐿𝑗 ) which requires fewest stops
           endif
        endif
     Summary
35
        What is an Algorithm?
        Algorithm characteristics: inputs, outputs, precision,
         finiteness, uniqueness, generality
        Elements of an algorithm: sequence, selection, iteration
        Expressing algorithms: Pseudocode, flowcharting
        Sequence: operator, operand, expression, variable,
         constant, type, integer and floating-point values,
         assignment operator, arithmetic operator, relational
         operators
        Examples
        Things to do: Read handout, complete assignment