SlideShare a Scribd company logo
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)   เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี

More Related Content

PDF
ติวสบายคณิต (เพิ่มเติม) บทที่ 15 ความสัมพันธ์เชิงฟังก์ชันของข้อมูล สรุปเข้ม
PDF
Discrete-Chapter 03 Matrices
PDF
Java-Chapter 13 Advanced Classes and Objects
PDF
ความสัมพันธ์เชิงฟังก์ชัน
PDF
Java-Answer Chapter 10-11
PDF
Java-Answer Chapter 08-09 (For Print)
PDF
Java-Answer Chapter 10-11 (For Print)
PDF
Java-Chapter 08 Methods
ติวสบายคณิต (เพิ่มเติม) บทที่ 15 ความสัมพันธ์เชิงฟังก์ชันของข้อมูล สรุปเข้ม
Discrete-Chapter 03 Matrices
Java-Chapter 13 Advanced Classes and Objects
ความสัมพันธ์เชิงฟังก์ชัน
Java-Answer Chapter 10-11
Java-Answer Chapter 08-09 (For Print)
Java-Answer Chapter 10-11 (For Print)
Java-Chapter 08 Methods

What's hot (20)

PDF
Java-Answer Chapter 12-13
PDF
เอกสารความสัมพันธ์เชิงฟังก์ชัน
PDF
Calculus1
PDF
Java-Answer Chapter 07 (For Print)
PDF
Java-Answer Chapter 07
DOC
ใบงานเลขยกกำลังม.5
PDF
Java-Answer Chapter 01-04 (For Print)
PDF
Java-Answer Chapter 08-09
PPTX
ฟังก์ชัน
PDF
Java-Answer Chapter 01-04
PDF
Java-Answer Chapter 12-13 (For Print)
PDF
Expolog clipvidva
PDF
ฟังก์ชันเชิงเส้น
DOCX
กฎของเลขยกกำลัง
PDF
Java-Chapter 12 Classes and Objects
PDF
เมทริกซ์...
PDF
Pat1 expo&log
PPT
สมการและอสมการ
PDF
แนวข้อสอบ
PDF
linear function
Java-Answer Chapter 12-13
เอกสารความสัมพันธ์เชิงฟังก์ชัน
Calculus1
Java-Answer Chapter 07 (For Print)
Java-Answer Chapter 07
ใบงานเลขยกกำลังม.5
Java-Answer Chapter 01-04 (For Print)
Java-Answer Chapter 08-09
ฟังก์ชัน
Java-Answer Chapter 01-04
Java-Answer Chapter 12-13 (For Print)
Expolog clipvidva
ฟังก์ชันเชิงเส้น
กฎของเลขยกกำลัง
Java-Chapter 12 Classes and Objects
เมทริกซ์...
Pat1 expo&log
สมการและอสมการ
แนวข้อสอบ
linear function
Ad

Viewers also liked (20)

PDF
Discrete-Chapter 07 Probability
PPT
Chapter 06
PPT
Chapter 11
PPT
Chapter 08
PDF
Discrete-Chapter 04 Logic Part I
PPTX
Introduction to management
PDF
Discrete-Chapter 02 Functions and Sequences
PDF
Discrete-Chapter 01 Sets
PDF
Discrete-Chapter 04 Logic Part II
PDF
Discrete-Chapter 08 Relations
PPT
Chapter 12
PPT
Chapter 09
PDF
Discrete-Chapter 06 Counting
PPTX
Tenerife airport disaster klm flight 4805 and pan
PDF
Discrete-Chapter 05 Inference and Proofs
PPT
Set Operations
PDF
Chapter 1 Logic of Compound Statements
PPTX
Set Theory Presentation
PPTX
PDF
Discrete Structures. Lecture 1
Discrete-Chapter 07 Probability
Chapter 06
Chapter 11
Chapter 08
Discrete-Chapter 04 Logic Part I
Introduction to management
Discrete-Chapter 02 Functions and Sequences
Discrete-Chapter 01 Sets
Discrete-Chapter 04 Logic Part II
Discrete-Chapter 08 Relations
Chapter 12
Chapter 09
Discrete-Chapter 06 Counting
Tenerife airport disaster klm flight 4805 and pan
Discrete-Chapter 05 Inference and Proofs
Set Operations
Chapter 1 Logic of Compound Statements
Set Theory Presentation
Discrete Structures. Lecture 1
Ad

Similar to Discrete-Chapter 09 Algorithms (20)

PPT
1 test
PDF
Fibonacci for print
PDF
(Big One) C Language - 10 เทคนิคอัลกอริทึมแบบ divide-and-conquer
PDF
ความรู้เบื้องต้นเกี่ยวกับจำนวนจริง
PDF
Operation
PDF
04 data representation
PDF
อสมการ
PDF
ความน่าจะเป็น
PDF
2 1 แบบรูป
PDF
Function
PDF
โปรแกรมย่อยและฟังก์ชันมาตรฐาน
PDF
Onet math
PDF
แบบฝึกหัดสำหรับทบทวนเนื้อหาเซตและการให้เหตุผล
PDF
การประยุกต์ของสมการเชิงเส้นตัวแปรเดียว
PDF
PDF
Mo 5
1 test
Fibonacci for print
(Big One) C Language - 10 เทคนิคอัลกอริทึมแบบ divide-and-conquer
ความรู้เบื้องต้นเกี่ยวกับจำนวนจริง
Operation
04 data representation
อสมการ
ความน่าจะเป็น
2 1 แบบรูป
Function
โปรแกรมย่อยและฟังก์ชันมาตรฐาน
Onet math
แบบฝึกหัดสำหรับทบทวนเนื้อหาเซตและการให้เหตุผล
การประยุกต์ของสมการเชิงเส้นตัวแปรเดียว
Mo 5

More from Wongyos Keardsri (19)

PDF
How to Study and Research in Computer-related Master Program
PPT
The next generation intelligent transport systems: standards and applications
PPT
IP address anonymization
PDF
SysProg-Tutor 03 Unix Shell Script Programming
PDF
SysProg-Tutor 02 Introduction to Unix Operating System
PDF
SysProg-Tutor 01 Introduction to C Programming Language
PDF
Discrete-Chapter 11 Graphs Part III
PDF
Discrete-Chapter 11 Graphs Part II
PDF
Discrete-Chapter 11 Graphs Part I
PDF
Discrete-Chapter 10 Trees
PDF
Discrete-Chapter 12 Modeling Computation
PDF
Java-Chapter 14 Creating Graphics with DWindow
PDF
Java-Chapter 11 Recursions
PDF
Java-Chapter 10 Two Dimensional Arrays
PDF
Java-Chapter 09 Advanced Statements and Applications
PDF
Java-Chapter 07 One Dimensional Arrays
PDF
Java-Chapter 06 File Operations
PDF
Java-Chapter 05 String Operations
PDF
Java-Chapter 04 Iteration Statements
How to Study and Research in Computer-related Master Program
The next generation intelligent transport systems: standards and applications
IP address anonymization
SysProg-Tutor 03 Unix Shell Script Programming
SysProg-Tutor 02 Introduction to Unix Operating System
SysProg-Tutor 01 Introduction to C Programming Language
Discrete-Chapter 11 Graphs Part III
Discrete-Chapter 11 Graphs Part II
Discrete-Chapter 11 Graphs Part I
Discrete-Chapter 10 Trees
Discrete-Chapter 12 Modeling Computation
Java-Chapter 14 Creating Graphics with DWindow
Java-Chapter 11 Recursions
Java-Chapter 10 Two Dimensional Arrays
Java-Chapter 09 Advanced Statements and Applications
Java-Chapter 07 One Dimensional Arrays
Java-Chapter 06 File Operations
Java-Chapter 05 String Operations
Java-Chapter 04 Iteration Statements

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) เรียบเรียงโดย อ.วงศ์ยศ เกิดศรี