Discrete-Chapter 09 Algorithms
-
Upload
wongyos-keardsri -
Category
Documents
-
view
224 -
download
0
Transcript of Discrete-Chapter 09 Algorithms
1
มหาวทยาลยราชภฏสวนสนนทา (ภาคการศกษาท 2/2555) เรยบเรยงโดย อ.วงศยศ เกดศร
CSC1001 Discrete Mathematics Algorithms - 09
1. Algorithm Deffinitions
Example 1 (5 points) Describe an algorithm or write a pseudocode for finding the maximum (largest) value in a finite sequence of integers.
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.
CHAPTER
9
อลกอรทม(Algorithms)
Introduction to Algorithms 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).
Definition 1
procedure maximum(a1, a2, … , an: integers) max = a1 for i = 2 to n if max < ai then max = ai return max
2
มหาวทยาลยราชภฏสวนสนนทา (ภาคการศกษาท 2/2555) เรยบเรยงโดย อ.วงศยศ เกดศร
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.
3
มหาวทยาลยราชภฏสวนสนนทา (ภาคการศกษาท 2/2555) เรยบเรยงโดย อ.วงศยศ เกดศร
CSC1001 Discrete Mathematics Algorithms - 09
2. Searching Algorithms
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
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 2
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 = ⎣ ⎦2 / r) (l + if x = am then return m else if x > am then l = m + 1 else r = m - 1 return -1
Definition 3
4
มหาวทยาลยราชภฏสวนสนนทา (ภาคการศกษาท 2/2555) เรยบเรยงโดย อ.วงศยศ เกดศร
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?
5
มหาวทยาลยราชภฏสวนสนนทา (ภาคการศกษาท 2/2555) เรยบเรยงโดย อ.วงศยศ เกดศร
CSC1001 Discrete Mathematics Algorithms - 09
3. Sorting Algorithms
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
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 4
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
Definition 5
6
มหาวทยาลยราชภฏสวนสนนทา (ภาคการศกษาท 2/2555) เรยบเรยงโดย อ.วงศยศ เกดศร
CSC1001 Discrete Mathematics 09 - Algorithms
2) Using selection sort
15 30 2 26 21 6 39 3 11 8
1. Big-O, Big-Ω and Big-Θ Notation
Growth of Functions and Complexity of Algorithms 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 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 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 Ω(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 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 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).”
Definition 3
7
มหาวทยาลยราชภฏสวนสนนทา (ภาคการศกษาท 2/2555) เรยบเรยงโดย อ.วงศยศ เกดศร
CSC1001 Discrete Mathematics Algorithms - 09
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
8
มหาวทยาลยราชภฏสวนสนนทา (ภาคการศกษาท 2/2555) เรยบเรยงโดย อ.วงศยศ เกดศร
CSC1001 Discrete Mathematics 09 - Algorithms
1) (12 points) Ranking the speed rate of functions by descending
No Functions Ranking
1. n
2. 0.5n
3. n log n
4. 1
5. n2 log n
6. log n
No Functions Ranking
7. n2
8. log6 n
9. n0.5
10. n!
11. 2n
12. n3
2) (12 points) Ranking the growth rate of functions by descending
No Functions Ranking
1. n
2. 0.5n
3. n log n
4. 1
5. n2 log n
6. log n
No Functions Ranking
7. n2
8. log6 n
9. n0.5
10. n!
11. 2n
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).
9
มหาวทยาลยราชภฏสวนสนนทา (ภาคการศกษาท 2/2555) เรยบเรยงโดย อ.วงศยศ เกดศร
CSC1001 Discrete Mathematics Algorithms - 09
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.
Example 19 (5 points) Analyze the time complexity of an algorithm in Example 3.
procedure maximum(a1, a2, … , an: integers) max = a1 for i = 2 to n if max < ai then max = ai return max
10
มหาวทยาลยราชภฏสวนสนนทา (ภาคการศกษาท 2/2555) เรยบเรยงโดย อ.วงศยศ เกดศร
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.
11
มหาวทยาลยราชภฏสวนสนนทา (ภาคการศกษาท 2/2555) เรยบเรยงโดย อ.วงศยศ เกดศร
CSC1001 Discrete Mathematics Algorithms - 09
Example 24 (13 points) Find the Big-O notation of a part of Java program.
No. A Part of Java Program Big-O
1. int temp = a[i]; a[i] = a[a.length - i - 1]; a[a.length - i - 1] = temp;
2.
for (int i = a.length - 1; i >= 0; i--) if (a[i] == x) System.out.println(i);
3. for (int i = 0; i <= n; i = i + 4) System.out.println(a[i]);
4.
for (int i = 0; i < a.length; i++) for (int j = 0; j < a[i].length; j++) a[i][j] = 13;
5.
for (int i = 10000000; i >= 2; i--) System.out.println(a[i]); System.out.println(a[i - 1]);
System.out.println(a[i - 2]);
6.
for (int i = 0; i < n; i++) for (int j = i; j >= 0; j--) System.out.print(a[i][j] + " "); System.out.println();
7.
for (int i = 0; i < n; i++) for (int j = 100; j >= 0; j--) System.out.print(a[i][j] + " "); System.out.print("----------------"); System.out.println();
8. for (int i = 0; i <= n; i = i * 2) System.out.println(a[i]);
9.
for (int i = 0; i < n; i++) for (int j = n; j >= 0; j = j / 5) System.out.print(a[i][j]); System.out.println();
10.
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]; for (int i = 0; i <= n; i = i * 2) System.out.println(b[i]);
12
มหาวทยาลยราชภฏสวนสนนทา (ภาคการศกษาท 2/2555) เรยบเรยงโดย อ.วงศยศ เกดศร
CSC1001 Discrete Mathematics 09 - Algorithms
No. A Part of Java Program Big-O
11.
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++) mul[i][j] += x[i][k] * y[k][j];
12.
for (int i = 0; i < n; i++) for (int j = i; j >= 0; j -= 2) for (int k = 0; k < n; k *= 10) mul[i][j] += x[i][k] * y[k][j];
13.
int left = 0, right = n, index = -1; while (left <= right) int mid = (left + right) / 2; if (key == a[mid]) index = mid; else if (key < a[mid]) right = mid - 1; else left = mid + 1;