Algorithms - 09                                                          CSC1001 Discrete Mathematics             1

 CHAPTER
                                                          อัลกอริทึม
      9                                                  (Algorithms)

  1      Introduction to Algorithms
1. Algorithm Deffinitions
 Definition 1

 An algorithm is a finite sequence of precise instructions or steps for performing a computation or for solving
 a problem (In computer science usually represent the algorithm by using pseudocode).

Example 1 (5 points) Describe an algorithm or write a pseudocode for finding the maximum (largest) value in
a finite sequence of integers.
 procedure maximum({a1, a2, … , an}: integers) {
   max = a1
   for i = 2 to n
     if max < ai then max = ai
   return max
 }


Example 2 (5 points) Describe an algorithm or write a pseudocode for finding the minimum value in a finite se-
quence of real number.




Example 3 (5 points) Describe an algorithm to calculate the average of a finite sequence of integers.




มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                                เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
2       CSC1001 Discrete Mathematics                                                               09 - Algorithms


Example 4 (5 points) Describe an algorithm to find the absolute value of integers.




Example 5 (5 points) Describe an algorithm to find the factorial value of integers.




Example 6 (5 points) Describe an algorithm to find the Fibonacci value of integers (a0 = 0 and a1 = 1).




Example 7 (5 points) Describe an algorithm to find the multiplication of two matrices.




มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                                  เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
Algorithms - 09                                                        CSC1001 Discrete Mathematics           3
2. Searching Algorithms
 Definition 2

 The Linear Search Algorithm

 procedure linearSearch({a1, a2, … , an}: integers, x: integer) {
   i = 1
   while i ≤ n {
     if ai = x then return i
     else i = i + 1
   }
   return -1
 }


 Definition 3
 The Binary Search Algorithm

 procedure binarySearch({a1, a2, … , an}: integers, x: integer) {
   l = 1 //i is left endpoint of search interval
   r = n //j is right endpoint of search interval
   while l < r {
     m = ⎣ + r) / 2 ⎦
         (l
     if x = am then return m
     else if x > am then l = m + 1
     else r = m - 1
   }
   return -1
 }


Example 8 (20 points) Consider the iteration of linear search and binary search for searching some value from
the input sequence.
1) Search 26 using linear search
        2          3          6         8         11        15        21         26        30          39

2) Search 26 using binary search
        2          3          6         8         11        15        21         26        30          39




3) Search 3 using linear search
        2          3          6         8         11        15        21         26        30          39


มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                             เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
4       CSC1001 Discrete Mathematics                                                      09 - Algorithms


4) Search 3 using binary search
        2          3          6         8         11    15        21        26         30          39




5) Search 2 using linear search
        2          3          6         8         11    15        21        26         30          39

6) Search 2 using binary search
        2          3          6         8         11    15        21        26         30          39




7) Search 17 using linear search
        2          3          6         8         11    15        21        26         30          39

8) Search 17 using binary search
        2          3          6         8         11    15        21        26         30          39




Example 9 (4 points) From an Example 4, can you summarize the different functions or features of linear
search and binary search algorithms?




มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                         เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
Algorithms - 09                                                         CSC1001 Discrete Mathematics            5
3. Sorting Algorithms
 Definition 4

 The Bubble Sort Algorithm

 procedure bubbleSort({a1, a2, … , an}: real number) {
   for i = n to 2 {
     for j = 1 to i - 1 {
       if aj > aj + 1 then {
          temp = aj
          aj = aj + 1
          aj + 1 = temp
       }
     }
   }
 }


 Definition 5
 The Selection Sort Algorithm

 procedure selectionSort({a1, a2, … , an}: real number) {
   for i = n to 2 {
     maxIndex = 1
     for j = 1 to i {
        if aj > amaxIndex then maxIndex = j
     }
     temp = ai
     ai = amaxIndex
     amaxIndex = temp
   }
 }


Example 10 (20 points) Write the steps of bubble sort and selection sort of this sequence.
1) Using bubble sort
       15         30          2         26        21         6         39         3          11           8




มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                               เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
6         CSC1001 Discrete Mathematics                                                                   09 - Algorithms


2) Using selection sort
          15          30          2          26         21           6         39           3          11          8




     2      Growth of Functions and Complexity of Algorithms
1. Big-O, Big-Ω and Big-Θ Notation
    Definition 1

    Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We
    say that f (x) is O(g(x)) if there are constants C and k such that
    |f (x)| ≤ C|g(x)| whenever x > k. This is read as “f (x) is big-oh of g(x).”

    Definition 2

    Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We
    say that f (x) is Ω(g(x)) if there are positive constants C and k such that
    |f (x)| ≥ C|g(x)| whenever x > k. This is read as “f (x) is big-Omega of g(x).”

    Definition 3

    Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We
    say that f (x) is Θ(g(x)) if there are real numbers C1 and C2 and a positive real number k such that
    C1|g(x)| ≤ |f (x)| ≤ C2|g(x)| whenever x > k. We say that f (x) is Θ(g(x)) if f (x) is O(g(x)) and f (x) is Ω(g(x)).
    This is read as “f (x) is big-Omega of g(x).”

มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                                        เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
Algorithms - 09                                                           CSC1001 Discrete Mathematics           7
Example 11 (4 points) Show that f(x) = x2 + 2x + 1 is O(x2).




Example 12 (4 points) Show that f(x) = 3x4 + 5x2 + 15 is O(x4).




Example 13 (4 points) Show that f(x) = 7x2 is O(x3) by replace x into f(x).




Example 14 (24 points) Estimate the growth of functions.




Figure: A Display of the Commonly Used in Big-O Estimates

มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                                เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
8       CSC1001 Discrete Mathematics                                                      09 - Algorithms


1) (12 points) Ranking the speed rate of functions by descending
      No        Functions            Ranking                     No    Functions           Ranking
      1.             n                                           7.        n2
      2.           0.5n                                          8.      log6 n
      3.         n log n                                         9.        n0.5
      4.             1                                           10.        n!
      5.         n2 log n                                        11.       2n
      6.          log n                                          12.       n3

2) (12 points) Ranking the growth rate of functions by descending
      No        Functions            Ranking                     No    Functions           Ranking
      1.             n                                           7.        n2
      2.           0.5n                                          8.      log6 n
      3.         n log n                                         9.        n0.5
      4.             1                                           10.        n!
      5.         n2 log n                                        11.       2n
      6.          log n                                          12.       n3

Example 15 (4 points) Show that f(x) = 5x3 + 2x2 - 4x + 1 is Ω(x4).




Example 16 (4 points) Show that 3x2 + 8x log x is Θ(x2).




มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                         เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
Algorithms - 09                                                           CSC1001 Discrete Mathematics            9
Example 17 (4 points) Find Big-O of f(x) + g(x) if f(x) = 4x5 + 2x – 10 and g(x) = x3 log x + 10x




2. Time Complexity of Algorithms
    The time complexity of an algorithm can be expressed in terms of the number of operations used by the
algorithm when the input has a particular size. The operations used to measure time complexity can be the
comparison of integers, the addition of integers, the multiplication of integers, the division of integers, or any
other basic operation.
Example 18 (5 points) Analyze the time complexity of Finding maximum value algorithm.
 procedure maximum({a1, a2, … , an}: integers) {
   max = a1
   for i = 2 to n
     if max < ai then max = ai
   return max
 }




Example 19 (5 points) Analyze the time complexity of an algorithm in Example 3.




มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                                 เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
10      CSC1001 Discrete Mathematics                                                           09 - Algorithms


Example 20 (5 points) Analyze the time complexity of an algorithm in Example 4.




Example 21 (5 points) Analyze the time complexity of an algorithm in Example 5.




Example 22 (5 points) Analyze the time complexity of an algorithm in Example 6.




Example 23 (5 points) Analyze the time complexity of an algorithm in Example 7.




มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                              เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
Algorithms - 09                                                       CSC1001 Discrete Mathematics        11
Example 24 (13 points) Find the Big-O notation of a part of Java program.
 No.                       A Part of Java Program                                     Big-O
        int temp = a[i];
  1.    a[i] = a[a.length - i - 1];
        a[a.length - i - 1] = temp;

        for (int i = a.length - 1; i >= 0; i--) {
          if (a[i] == x) {
  2.        System.out.println(i);
          }
        }

        for (int i = 0; i <= n; i = i + 4) {
  3.      System.out.println(a[i]);
        }

        for (int i = 0; i < a.length; i++) {
          for (int j = 0; j < a[i].length; j++) {
  4.        a[i][j] = 13;
          }
        }

        for (int i = 10000000; i >= 2; i--) {
          System.out.println(a[i]);
  5.      System.out.println(a[i - 1]);
          System.out.println(a[i - 2]);
        }

        for (int i = 0; i < n; i++) {
          for (int j = i; j >= 0; j--) {
            System.out.print(a[i][j] + " ");
  6.      }
          System.out.println();
        }

        for (int i = 0; i < n; i++) {
          for (int j = 100; j >= 0; j--) {
            System.out.print(a[i][j] + " ");
  7.      }
          System.out.print("----------------");
          System.out.println();
        }

        for (int i = 0; i <= n; i = i * 2) {
  8.      System.out.println(a[i]);
        }

        for (int i = 0; i < n; i++) {
          for (int j = n; j >= 0; j = j / 5) {
            System.out.print(a[i][j]);
  9.        System.out.println();
          }
        }

        for (int i = 0; i < n; i += 100) {
          for (int j = 0; j <= 200; j++) {
            System.out.print(a[i][j] + " ");
            sum = sum + a[i][j];
  10.     }
        }
        for (int i = 0; i <= n; i = i * 2) {
          System.out.println(b[i]);
        }



มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)                            เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
12      CSC1001 Discrete Mathematics                                09 - Algorithms


  No.                       A Part of Java Program           Big-O
        for (int i = 0; i < mul.length; i++) {
          for (int j = 0; j < mul[i].length; j++) {
            for (int k = 0; k < y.length; k++) {
  11.         mul[i][j] += x[i][k] * y[k][j];
            }
          }
        }

        for (int i = 0; i < n; i++) {
          for (int j = i; j >= 0; j -= 2) {
            for (int k = 0; k < n; k *= 10) {
  12.         mul[i][j] += x[i][k] * y[k][j];
            }
          }
        }

        int left = 0, right = n, index = -1;
        while (left <= right) {
          int mid = (left + right) / 2;
  13.     if (key == a[mid]) index = mid;
          else if (key < a[mid]) right = mid - 1;
          else left = mid + 1;
        }




มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555)   เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี

Discrete-Chapter 09 Algorithms

  • 1.
    Algorithms - 09 CSC1001 Discrete Mathematics 1 CHAPTER อัลกอริทึม 9 (Algorithms) 1 Introduction to Algorithms 1. Algorithm Deffinitions Definition 1 An algorithm is a finite sequence of precise instructions or steps for performing a computation or for solving a problem (In computer science usually represent the algorithm by using pseudocode). Example 1 (5 points) Describe an algorithm or write a pseudocode for finding the maximum (largest) value in a finite sequence of integers. procedure maximum({a1, a2, … , an}: integers) { max = a1 for i = 2 to n if max < ai then max = ai return max } Example 2 (5 points) Describe an algorithm or write a pseudocode for finding the minimum value in a finite se- quence of real number. Example 3 (5 points) Describe an algorithm to calculate the average of a finite sequence of integers. มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 2.
    2 CSC1001 Discrete Mathematics 09 - Algorithms Example 4 (5 points) Describe an algorithm to find the absolute value of integers. Example 5 (5 points) Describe an algorithm to find the factorial value of integers. Example 6 (5 points) Describe an algorithm to find the Fibonacci value of integers (a0 = 0 and a1 = 1). Example 7 (5 points) Describe an algorithm to find the multiplication of two matrices. มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 3.
    Algorithms - 09 CSC1001 Discrete Mathematics 3 2. Searching Algorithms Definition 2 The Linear Search Algorithm procedure linearSearch({a1, a2, … , an}: integers, x: integer) { i = 1 while i ≤ n { if ai = x then return i else i = i + 1 } return -1 } Definition 3 The Binary Search Algorithm procedure binarySearch({a1, a2, … , an}: integers, x: integer) { l = 1 //i is left endpoint of search interval r = n //j is right endpoint of search interval while l < r { m = ⎣ + r) / 2 ⎦ (l if x = am then return m else if x > am then l = m + 1 else r = m - 1 } return -1 } Example 8 (20 points) Consider the iteration of linear search and binary search for searching some value from the input sequence. 1) Search 26 using linear search 2 3 6 8 11 15 21 26 30 39 2) Search 26 using binary search 2 3 6 8 11 15 21 26 30 39 3) Search 3 using linear search 2 3 6 8 11 15 21 26 30 39 มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 4.
    4 CSC1001 Discrete Mathematics 09 - Algorithms 4) Search 3 using binary search 2 3 6 8 11 15 21 26 30 39 5) Search 2 using linear search 2 3 6 8 11 15 21 26 30 39 6) Search 2 using binary search 2 3 6 8 11 15 21 26 30 39 7) Search 17 using linear search 2 3 6 8 11 15 21 26 30 39 8) Search 17 using binary search 2 3 6 8 11 15 21 26 30 39 Example 9 (4 points) From an Example 4, can you summarize the different functions or features of linear search and binary search algorithms? มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 5.
    Algorithms - 09 CSC1001 Discrete Mathematics 5 3. Sorting Algorithms Definition 4 The Bubble Sort Algorithm procedure bubbleSort({a1, a2, … , an}: real number) { for i = n to 2 { for j = 1 to i - 1 { if aj > aj + 1 then { temp = aj aj = aj + 1 aj + 1 = temp } } } } Definition 5 The Selection Sort Algorithm procedure selectionSort({a1, a2, … , an}: real number) { for i = n to 2 { maxIndex = 1 for j = 1 to i { if aj > amaxIndex then maxIndex = j } temp = ai ai = amaxIndex amaxIndex = temp } } Example 10 (20 points) Write the steps of bubble sort and selection sort of this sequence. 1) Using bubble sort 15 30 2 26 21 6 39 3 11 8 มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 6.
    6 CSC1001 Discrete Mathematics 09 - Algorithms 2) Using selection sort 15 30 2 26 21 6 39 3 11 8 2 Growth of Functions and Complexity of Algorithms 1. Big-O, Big-Ω and Big-Θ Notation Definition 1 Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We say that f (x) is O(g(x)) if there are constants C and k such that |f (x)| ≤ C|g(x)| whenever x > k. This is read as “f (x) is big-oh of g(x).” Definition 2 Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We say that f (x) is Ω(g(x)) if there are positive constants C and k such that |f (x)| ≥ C|g(x)| whenever x > k. This is read as “f (x) is big-Omega of g(x).” Definition 3 Let f and g be functions from the set of integers or the set of real numbers to the set of real numbers. We say that f (x) is Θ(g(x)) if there are real numbers C1 and C2 and a positive real number k such that C1|g(x)| ≤ |f (x)| ≤ C2|g(x)| whenever x > k. We say that f (x) is Θ(g(x)) if f (x) is O(g(x)) and f (x) is Ω(g(x)). This is read as “f (x) is big-Omega of g(x).” มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 7.
    Algorithms - 09 CSC1001 Discrete Mathematics 7 Example 11 (4 points) Show that f(x) = x2 + 2x + 1 is O(x2). Example 12 (4 points) Show that f(x) = 3x4 + 5x2 + 15 is O(x4). Example 13 (4 points) Show that f(x) = 7x2 is O(x3) by replace x into f(x). Example 14 (24 points) Estimate the growth of functions. Figure: A Display of the Commonly Used in Big-O Estimates มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 8.
    8 CSC1001 Discrete Mathematics 09 - Algorithms 1) (12 points) Ranking the speed rate of functions by descending No Functions Ranking No Functions Ranking 1. n 7. n2 2. 0.5n 8. log6 n 3. n log n 9. n0.5 4. 1 10. n! 5. n2 log n 11. 2n 6. log n 12. n3 2) (12 points) Ranking the growth rate of functions by descending No Functions Ranking No Functions Ranking 1. n 7. n2 2. 0.5n 8. log6 n 3. n log n 9. n0.5 4. 1 10. n! 5. n2 log n 11. 2n 6. log n 12. n3 Example 15 (4 points) Show that f(x) = 5x3 + 2x2 - 4x + 1 is Ω(x4). Example 16 (4 points) Show that 3x2 + 8x log x is Θ(x2). มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 9.
    Algorithms - 09 CSC1001 Discrete Mathematics 9 Example 17 (4 points) Find Big-O of f(x) + g(x) if f(x) = 4x5 + 2x – 10 and g(x) = x3 log x + 10x 2. Time Complexity of Algorithms The time complexity of an algorithm can be expressed in terms of the number of operations used by the algorithm when the input has a particular size. The operations used to measure time complexity can be the comparison of integers, the addition of integers, the multiplication of integers, the division of integers, or any other basic operation. Example 18 (5 points) Analyze the time complexity of Finding maximum value algorithm. procedure maximum({a1, a2, … , an}: integers) { max = a1 for i = 2 to n if max < ai then max = ai return max } Example 19 (5 points) Analyze the time complexity of an algorithm in Example 3. มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 10.
    10 CSC1001 Discrete Mathematics 09 - Algorithms Example 20 (5 points) Analyze the time complexity of an algorithm in Example 4. Example 21 (5 points) Analyze the time complexity of an algorithm in Example 5. Example 22 (5 points) Analyze the time complexity of an algorithm in Example 6. Example 23 (5 points) Analyze the time complexity of an algorithm in Example 7. มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 11.
    Algorithms - 09 CSC1001 Discrete Mathematics 11 Example 24 (13 points) Find the Big-O notation of a part of Java program. No. A Part of Java Program Big-O int temp = a[i]; 1. a[i] = a[a.length - i - 1]; a[a.length - i - 1] = temp; for (int i = a.length - 1; i >= 0; i--) { if (a[i] == x) { 2. System.out.println(i); } } for (int i = 0; i <= n; i = i + 4) { 3. System.out.println(a[i]); } for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { 4. a[i][j] = 13; } } for (int i = 10000000; i >= 2; i--) { System.out.println(a[i]); 5. System.out.println(a[i - 1]); System.out.println(a[i - 2]); } for (int i = 0; i < n; i++) { for (int j = i; j >= 0; j--) { System.out.print(a[i][j] + " "); 6. } System.out.println(); } for (int i = 0; i < n; i++) { for (int j = 100; j >= 0; j--) { System.out.print(a[i][j] + " "); 7. } System.out.print("----------------"); System.out.println(); } for (int i = 0; i <= n; i = i * 2) { 8. System.out.println(a[i]); } for (int i = 0; i < n; i++) { for (int j = n; j >= 0; j = j / 5) { System.out.print(a[i][j]); 9. System.out.println(); } } for (int i = 0; i < n; i += 100) { for (int j = 0; j <= 200; j++) { System.out.print(a[i][j] + " "); sum = sum + a[i][j]; 10. } } for (int i = 0; i <= n; i = i * 2) { System.out.println(b[i]); } มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี
  • 12.
    12 CSC1001 Discrete Mathematics 09 - Algorithms No. A Part of Java Program Big-O for (int i = 0; i < mul.length; i++) { for (int j = 0; j < mul[i].length; j++) { for (int k = 0; k < y.length; k++) { 11. mul[i][j] += x[i][k] * y[k][j]; } } } for (int i = 0; i < n; i++) { for (int j = i; j >= 0; j -= 2) { for (int k = 0; k < n; k *= 10) { 12. mul[i][j] += x[i][k] * y[k][j]; } } } int left = 0, right = n, index = -1; while (left <= right) { int mid = (left + right) / 2; 13. if (key == a[mid]) index = mid; else if (key < a[mid]) right = mid - 1; else left = mid + 1; } มหาวิทยาลัยราชภัฏสวนส ุนันทา (ภาคการศึกษาที่ 2/2555) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี