Presentation is loading. Please wait.

Presentation is loading. Please wait.

BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS CHAPTER 1 FUNDAMENTALS: ALGORITHMS, INTEGERS AND MATRICES SITI ZANARIAH SATARI FIST/FSKKP UMP I0910.

Similar presentations


Presentation on theme: "BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS CHAPTER 1 FUNDAMENTALS: ALGORITHMS, INTEGERS AND MATRICES SITI ZANARIAH SATARI FIST/FSKKP UMP I0910."— Presentation transcript:

1 BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS CHAPTER 1 FUNDAMENTALS: ALGORITHMS, INTEGERS AND MATRICES SITI ZANARIAH SATARI FIST/FSKKP UMP I0910

2 CHAPTER 1 FUNDAMENTALS: ALGORITHMS, INTEGERS AND MATRICES CONTENT CONTENT 1.1Algorithm 1.2Integers and Algorithms 1.3Matrices

3 1.1ALGORITHM Describe an algorithm Define and categorized the properties of an algorithm CHAPTER 1 FUND AMENTALS: ALGORITHMS, INTEGERS AND MATRICES

4 1.1 ALGORITHM ► Is a complete list of the steps (finite sequence of instructions) necessary to perform a task/computation or for solving a problem. ► The steps maybe a general descriptions (not very detail) or may be totally precise description (very detail). ► Often used in Mathematics, computing, linguistics, and related subjects for calculation and data processing. ► The term is a corruption of the name al-Khowarizmi (9 th century mathematician) – Algorism. ► Algorism was used for the rules for performing arithmetic using decimal notation. ► Algorism evolved into the word algorithm by 18 th century. What is Algorithm? What is Algorithm?

5 1.1 ALGORITHM We often used algorithm in our life, but when and how? → Your cooking recipe → Your daily routine as a student → When you buy something → When you go outing → My class routine Example of Simple Algorithm Example of Simple Algorithm

6 1.1 ALGORITHM We can specify a procedure for solving a problem in term of the following ways: 1. Natural languages – used any language – rarely used for complex or technical algorithms. 2. Pseudocode & Flowcharts – structured ways to express algorithms that avoid many of the ambiguities common in natural language statements. 3. Programming language – primarily intended for expressing algorithms in a form that can be executed by a computer. – often used as a way to define or document algorithms. Expressing Algorithms (Describe) Expressing Algorithms (Describe)

7 1.1 ALGORITHM EXAMPLE 1: A recipe for baking a cake A) general - without subroutines 1. MIX MARGERINE AND SUGAR 2. ADD EGG TO THE MIXTURE 3. BEAT THE MIXTURE 4. ADD THE FLOUR TO THE MIXTURE 5. POUR MIXTURE INTO PAN 6. BAKE IN OVEN FOR 40 MINUTES AT 350°F Describe an algorithm by Natural Languages Describe an algorithm by Natural Languages

8 1.1 ALGORITHM EXAMPLE 1: A recipe for baking a cake B) precise - with subroutines and the details steps given for each subroutines. 1. CALL MIX 2. CALL ADDEGG 3. CALL BEAT 4. CALL ADDFLOUR 5. CALL PAN 6. CALL BAKE (OVEN,40,350) Describe an algorithm by Natural Languages Describe an algorithm by Natural Languages 1.Remove egg from the carton 2.Break egg on edge of bowl 3.Drop egg, without shell into bowl

9 1.1 ALGORITHM EXAMPLE 2: Describe an algorithm for finding the maximum (largest) value in a finite sequence of integers 1. Assume the first integer in the sequence is the largest (temporary maximum). 2. Compare the next integer in the sequence to the temporary maximum. If it is larger than the temporary maximum, set the temporary maximum equal to this integer. 3. Repeat STEP 2 for the next integer in the sequence. 4. Stop when there are no integers left in the sequence. The temporary maximum at this point is the largest integer in the sequence. Describe an algorithm by Natural Languages Describe an algorithm by Natural Languages

10 1.1 ALGORITHM Describe an algorithm by Pseudocode Describe an algorithm by Pseudocode  Pseudocode is an informal programming language  Pseudocode provides an intermediate step between Language description of an algorithm and an implementation of this algorithm in an Programming Language.  The instruction used can included any well defined operation or statements.  A computer program can be produced in any computer language using the pseudocode description as a starting point.  Advantage: simple, it can be easily written and understood

11 1.1 ALGORITHM Describe an algorithm by Pseudocode Describe an algorithm by Pseudocode Characteristics in Pseudocode used in this class 1.Procedure Statements – procedure maximum (L: list of integers) gives the name of an algorithm 2.Assignment Statements – symbol := is used; example, (max := a) Used to assign values to variables 3.Blocks of Statements Group into blocks using begin and end statements 4.Comments – ex, {x is the largest element in L} 5.Conditional Constructions – if condition then statement, else if then 6.Loop Constructions – for, while 7.Loops within Loops - 8.Using Procedures in Other Procedures Refer Appendix 3 (Rosen K.H., Discrete Mathematics & Its Applications, (Seventh Edition), McGraw-Hill, 2007) / Pseudocode,,

12 1.1 ALGORITHM Describe an algorithm by Pseudocode Describe an algorithm by Pseudocode EXAMPLE : Describe an algorithm for finding the maximum (largest) value in a finite sequence of integers procedure max(a 1,a 2,…,a n : integers) max := a 1 for i := 2 to n if max < a i then max := a i {max is the largest element} - Comment - Procedure statement - Loop construction - Conditional construction - Assignment statement

13 1.1 ALGORITHM A diagrammatic representation of an algorithm Describe an algorithm by Flowchart Describe an algorithm by Flowchart EXAMPLE : Describe an algorithm for finding the maximum (largest) value in a finite sequence of integers max = a i, i = 1,…,n; a i integers max = a i+1 start max < a i+1 i = i + 1 stop i + 1 = n max = a i yes no

14 1.1 ALGORITHM A programming language is a language used to write computer programs computer programs Some common programming language –C, C++, C#, Java, etc Describe an algorithm by Programming Languages EXAMPLE : Describe an algorithm for finding the maximum (largest) value in a finite sequence of integers Write down your own code and test your answer.

15 1.1 ALGORITHM EXERCISE 1.1 (ASSIGNMENT 1) EXERCISE 1.1 (ASSIGNMENT 1) 1.Describe an algorithm that takes a list of n integers a 1,a 2,…,a n and finds the number of integers each greater than five in the list. 2. Describe an algorithm that takes a list of integers a 1,a 2,…,a n (n ≥ 2) and finds the second-largest integer in the sequence. 3. Describe an algorithm that takes a list of n integers (n ≥ 1) and finds the location of the last even integer in the list, or returns 0 if there are no even integers in the list. 4. Describe an algorithm that takes a list of n integers (n ≥ 1) and finds the average of the largest and smallest integers in the list.

16 1.1 ALGORITHM Properties of an Algorithm Properties of an Algorithm 1. Input: a specific set of values 2. Output: the produces values when input values is tested to an algorithm (the solution to the problem) 3. Definiteness: The steps must be defined precisely 4. Correctness: The output must be true for all input values 5. Finiteness: An algorithm should produce the desired output after a finite number of steps for any input in the set 6. Effectiveness: It must be possible to perform each step exactly and in a finite amount of time 7. Generality: The procedure should be applicable for all problems

17 1.1 ALGORITHM Example: Show that the algorithm for finding maximum Example: Show that the algorithm for finding maximum values has all the properties values has all the properties 1. Input: a sequence of integers. 2. Output: the largest value in the sequence. 3. Definiteness: Each step is precisely defined because only assignments, a finite loop and conditional statements occur. 4. Correctness: We can show that when the algorithm terminates, the value of the variable max equals to the maximum value in the sequence. 5. Finiteness: A finite number of steps is used because it terminates after all the integers in the sequence have been test. 6. Effectiveness: The algorithm can be carried out in a finite amount of time. 7. Generality: The algorithm can be used to find the maximum of any finite sequence of integers.

18 1.1 ALGORITHM EXERCISE 1.1 EXERCISE Determine which characteristics of an algorithm the following procedures have and which they lack a) Procedure double (n: positive integer) while n > 0 n:= 2n b) Procedure sum (n: positive integer) sum := 0 while i < 10 sum:= sum + i

19 1.1 ALGORITHM Searching Algorithm –The Linear Search / Sequential Search –The Binary Search Sorting Algorithm –The Bubble Sort –The Insertion Sort DISCUSSION: MOST POPULAR ALGORITHM DISCUSSION: MOST POPULAR ALGORITHM

20 1.1 ALGORITHM Solve problem of locating an element in an ordered lists. Example: checks the spelling of words in dictionary SEARCHING ALGORITHM SEARCHING ALGORITHM GENERAL DESCRIPTION OF SEARCHING ALGORITHM Locate an element x in a list of distinct elements a 1, a 2, …, a n, or determine that it is not in the list. The solution –The location of the term in the list that equals to x (that is, i is the solution if x = a i ) and is 0 if x is not in the list.

21 1.1 ALGORITHM Begins by comparing x and a i If x = a 1, the solution is the location a 1, namely 1 When x ≠ a 1, compare x with a 2 If x = a 2, the solution is the location a 2, namely 2 Continue and repeat the process until a match is found If the entire list has been searched without locating x, the solution is 0. SEARCHING ALGORITHM – Linear Search SEARCHING ALGORITHM – Linear Search Pseudocode: The Linear (Sequential) Search Algorithm procedure linear search(x: integer, a 1,a 2,…,a n : distinct integers) i := 1 while (i ≤ n and x ≠ a i ) i := i + 1 if x = a i then location := i else location := 0 {location is the subscript of the term that equals x, or is 0 if x is not found }

22 1.1 ALGORITHM The list has terms occurring in increasing order size The list is split into two same size sublists (or one list has one fewer term) The search continues by restricting the search to the appropriate sublist based on the comparison of the element to located and the middle term SEARCHING ALGORITHM – Binary Search SEARCHING ALGORITHM – Binary Search Pseudocode: The Binary Search Algorithm procedure binary search(x: integer, a 1,a 2,…,a n : increasing integers) i := 1 {i is the left endpoint of search interval} j := n {j is the right endpoint of search interval} while i < j begin m := if x > a m then i := m + 1 else j := m end if x = a i then location := i else location := 0 {location is the subscript of the term that equals x, or is 0 if x is not found }

23 1.1 ALGORITHM Problem: Search 19 in the list of – EXAMPLE – Binary Search EXAMPLE – Binary Search Solution: – Split into two smaller lists with 8 terms each – and –Compare 19 and the largest term in the first list –10 < 19, the search for 19 can be restricted to the second list –Split the second list into another subsplits with 4 term each – and –Since 16 < 19, Split the second list into another subsplits with 2 term each –18 19 and –19 = 19, so split the first list into another subsplits with 1 term each –18 and 19 –18 < 19, so chose 19 which is located in the 14 th term in the list

24 1.1 ALGORITHM EXERCISE 1.1 EXERCISE Show how the linear search algorithm searches for 27 in the following list: 5, 28, 15, 31, 27, 8, 6, 12 7.Show how the binary search algorithm searches for 27 in the following list: 5, 6, 8, 12, 15, 27, 28, 31 8.Show how the linear search algorithm searches for 11 in the following list: 4, 13, 15, 11, 2, 8, 5, 18 9.Show how the binary search algorithm searches for 11 in the following list: 1, 5, 7, 11, 19, 21, 25, 26, 30, 33

25 1.1 ALGORITHM Solve problem of ordering the elements of a list. Example: telephone directory, staff directory Sorting will put these elements into a lists in which the elements are in increasing orders – number: 1, 2, 3,… – alphabet: a, b, c,… Many algorithm have been developed, but we will discussed only two algorithm –Bubble sort –Insertion sort SORTING ALGORITHM SORTING ALGORITHM

26 1.1 ALGORITHM SORTING ALGORITHM – Bubble Sort SORTING ALGORITHM – Bubble Sort Pseudocode: The Bubble Sort Algorithm procedure bubblesort(a 1,a 2,…,a n : real numbers with n ≥ 2) for i := 1 to n - 1 for j:= 1 to n - i if a j > a j+1 then interchange a j and a j+1 {a 1,a 2,…,a n is in increasing order} Put a list into increasing order by successively comparing adjacent elements, interchanging them they are in the wrong order. Interchange a larger element with a smaller one Iterate the procedure until the sort is complete The smaller elements “bubble” to the top as they are interchanged with larger elements The larger elements “sink” to the bottom

27 1.1 ALGORITHM EXAMPLE – Bubble Sort (Illustration) EXAMPLE – Bubble Sort (Illustration) Problem: Use bubble sort to put 3, 2, 4, 1, 5 into increasing order First passFourth passThird passSecond pass An interchange Pair in correct order Number that guaranteed to be in correct order

28 1.1 ALGORITHM EXAMPLE – Bubble Sort (Description) EXAMPLE – Bubble Sort (Description) Problem: Use bubble sort to put 3, 2, 4, 1, 5 into increasing order Solution: –First Pass: 3, 2, 4, 1, 5 Compare first two elements 3 and 2 Since 3>2, interchange 3 and 2 : 2, 3, 4, 1, 5 Since 3<4, continue by comparing 4 and 1 Since 4>1, interchange 4 and 1 : 2, 3, 1,4, 5 Since 4<5, first pass is complete. It guarantees that the largest value, 5 in the correct position –Second Pass: 2, 3, 1,4, 5 Compare first two elements 2 and 3 Since 2 and 3 are in the correct order, compare 3 and 1 Since 3>1, interchange 3 and 1 : 2, 1, 3, 4, 5 Since 3<4, then these number are in correct order Since 5 in the correct position, it guarantees that 4 and 5 also in the correct position

29 1.1 ALGORITHM EXAMPLE – Bubble Sort (Description) EXAMPLE – Bubble Sort (Description) Problem: Use bubble sort to put 3, 2, 4, 1, 5 into increasing order Solution continue: –Third Pass: 2, 1, 3, 4, 5 Compare first two elements 2 and 1 Since 2>1, interchange 2 and 1 : 1, 2, 3, 4, 5 Since 2<3, these two elements are in the correct order Since 4 and 5 in the correct position, it guarantees that 3, 4 and 5 also in the correct position –Fourth Pass: 1, 2, 3, 4, 5 Compare first two elements 1 and 2 Since 1<2, these two elements are in the correct order Since 3, 4 and 5 in the correct position, it guarantees that 2, 3, 4 and 5 also in the correct position. This completes the bubble sort

30 1.1 ALGORITHM SORTING ALGORITHM – Insertion Sort SORTING ALGORITHM – Insertion Sort Pseudocode: The Insertion Sort Algorithm procedure insertion sort(a 1,a 2,…,a n : real numbers with n ≥ 2) for j:= 2 to n begin i:= 1 while a j > a i i:= i + 1 m:=a j for k:= 0 to j – i - 1 a j-k := a j-k-1 a i :=m end {a 1,a 2,…,a n are sorted} Compares the second element with the first element –inserts it before the first element if it does not exceed the first element –inserts it after the first element if it exceeds the first element Continue until the last element is placed in the correct position relative to the already sorted list of the first n – 1 elements.

31 1.1 ALGORITHM EXAMPLE – Insertion Sort EXAMPLE – Insertion Sort Problem: Use insertion sort to put 3, 2, 4, 1, 5 into increasing order Solution: –Compare 2 and 3 –Since 3>2, put 2 in the first position : 2, 3, 4, 1, 5 –Since 2 and 3 are in the correct order, compare third element, 4 with 2 and 3 –Since 4>2 and 4>3, put 4 in the third position : 2, 3, 4, 1, 5 –Since 2, 3 and 4 are in the correct order, compare fourth element, 1 with 2, 3 and 4 –Since 1<2 then put 1 in the first position : 1, 2, 3, 4, 5 –Since 1, 2, 3 and 4 are in the correct order, compare fifth element, 5 with 1, 2, 3 and 4 –Because 5>4, it goes at the end of list –The correct order is given by : 1, 2, 3, 4, 5

32 1.1 ALGORITHM EXERCISE 1.1 EXERCISE Use the bubble sort to sort 13, 15, 11, 2, 8, 5 and show the list obtained at each step 11. Use the insertion sort to sort 13, 15, 11, 2, 8, 5 and show the list obtained at each step 12. Use the bubble sort to sort e, a, g, m, h and show the list obtained at each step 13. Use the insertion sort to sort e, a, g, m, h and show the list obtained at each step

33 1.1 ALGORITHM The goal of optimization problems: to find a solution to the given problem that either minimizes or maximizes the value of some parameter Greedy algorithm – the simplest approaches which leads to a solution of an optimization problem –Selects the best choice at each step that may lead to optimal solution –Once we know that the greedy algorithm finds a feasible solution, we need to determine whether it has found an optimal solution –How? Prove that the solution is optimal Show that there is a counterexample where the algorithm yields a nonoptimal solution GREEDY ALGORITHM – Optimization Problems GREEDY ALGORITHM – Optimization Problems

34 1.1 ALGORITHM Consider the problems of making n cents change with 50 cents, 20 cents, 10 cents, 5 cents and 1 cent using the least total number of coins. At each step we choose the coin of the largest denomination possible to add to the group of change without exceeding n cents Proving: Page 175/176 Rosen K.H., Discrete Mathematics & Its Applications, (Seventh Edition), McGraw-Hill, GREEDY ALGORITHM – EXAMPLE GREEDY ALGORITHM – EXAMPLE Pseudocode: The Greedy Change-Making Algorithm procedure change(c 1,c 2,…,c r : values of denomination of coins where c 1 > c 2 >…> c r ; n: a positive integer) for i := 1 to r while n ≥ c i begin add a coin with value c i to the change n := n - c i end

35 1.1 ALGORITHM GREEDY ALGORITHM – EXAMPLE GREEDY ALGORITHM – EXAMPLE Problem: Use greedy algorithm to make change for 67 cents Solution: –Since 67>50, select a 50 cents 67 – 50 = 17 –Since 17<20, do not select a 20 cents –Since 17>10, select a 10 cents 17 – 10 = 7 –Since 7>5, select a 5 cents 7 – 5 = 2 –Since 2>1, select a 1 cents 2 –1 = 1 –Since 1=1, select a 1 cents 1-1=0 (stop) –We have one 50 cents, one 10 cents, one 5 cents and two 1 cent from 67 cents (the least number of coins are 5)

36 1.1 ALGORITHM EXERCISE 1.1 EXERCISE Use greedy algorithm to make change using 50 cents, 20 cents, 10 cents, 5 cents and 1 cents for a) 54 centsb) RM 1.33 c) 86 cents 15. You have supplies of boards that are one foot, five feet, seven feet, and twelve feet long. You need to lay pieces end-to-end to make a molding 15 feet long and wish to do this using the fewest number of pieces possible. Explain why the greedy algorithm of taking boards of the longest length at each stage (so long as the total length of the boards selected does not exceed 15 feet) does not give the fewest number of boards possible.

37 1.1 ALGORITHM EXERCISE 1.1 : EXTRA EXERCISE 1.1 : EXTRA PAGE : 177, 178, and 179 Rosen K.H., Discrete Mathematics & Its Applications, (Seventh Edition), McGraw-Hill, 2007.

38 1.2INTEGERS AND ALGORITHMS Describe important algorithms involving integers → Constructing Base b Expansions → Addition of Integers → Multiplying Integers → Computing div and mod → Modular Exponentiation → Euclidean Algorithm CHAPTER 1 FUND AMENTALS: ALGORITHMS, INTEGERS AND MATRICES

39 1.2 INTEGERS AND ALGORITHMS  Algorithm – procedures for performing arithmetic operations using the decimal representations of integers  Integers representations;  Decimal notation – Base 10 (ex: 965=9·10²+6·10+5)  Binary (Base 2) notation – arithmetic  ex:  Base 8 (octal) notation – characters (letters or digits)  ex:  Base 16 (hexadecimal) notation – characters (letters or digits)  ex: Basis of computer arithmetic – adapted algorithm with binary representations  Generally, an integers can be expressed in the form of: Introduction

40 1.2 INTEGERS AND ALGORITHMS 1. Constructing Base b Expansions 2. Addition of Integers 3. Multiplying Integers 4. Computing div and mod 5. Modular Exponentiation 6. Euclidean Algorithm EXAMPLE: Algorithm and Integers

41 1.2 INTEGERS AND ALGORITHMS Base b expansion of integer n is given by (a k-1 …a 1 a 0 ) b First step –divide n by b to obtain a the first quotient (q 0 ) and remainder (a 0 ) n = bq + a, 0 ≤ a < b, a = q mod b 2nd step –divide q 0 by b to obtain a the 2nd quotient (q 1 ) and remainder (a 1 ) Continue the process until we obtain a quotient equal to zero 1. Constructing Base b Expansions 1. Constructing Base b Expansions Pseudocode: Constructing Base b Expansions procedure base b expansion (n: positive integer) q := n k := 0 while q ≠ 0 begin a k := q mod b q := k := k + 1 end {the base b expansion of n is (a k-1 …a 1 a 0 ) b } procedure mod (q: integer, b: positive integer) a k := |q| while a k ≥ b a k := a k - b if q 0 then a k := b - a k {ak := q mod b is the remainder}

42 1.2 INTEGERS AND ALGORITHMS 1. Find the binary expansion of (241) Find the base 8 or octal expansion of (12345) Find the hexadecimal expansion of (177130) Convert ( ) 2 to hexadecimal expansion EXERCISE 1.2.1

43 1.2 INTEGERS AND ALGORITHMS Describe the Addition Algorithm for two integers expressed in binary notation Let a= (a n-1 …a 1 a 0 ) 2 and b = (b n-1 …b 1 b 0 ) 2 a and b each have n bits (putting bits equal to 0 at the beginning of one these expansion if necessary) 2. Addition of Integers 2. Addition of Integers Pseudocode: Addition of Integers procedure add (a,b: positive integer) {the binary expansions of a and b are ( a n-1 … a 1 a 0 ) 2 and ( b n-1 … b 1 b 0 ) 2 } c := 0 for j := 0 to n - 1 begin d := s j := a j + b j + c - 2d c := d end s n := c {the binary expansion of the sum is a + b = (s n s n-1 …s 1 s 0 ) 2 }

44 1.2 INTEGERS AND ALGORITHMS 1. Add (110) 2 and (101) 2 2. Add (1110) 2 and (1011) 2 3. How many additions of bits are required to use the addition algorithm to add two integers with n bits (or less) in their binary representations? EXERCISE 1.2.2

45 1.2 INTEGERS AND ALGORITHMS Describe the multiplication of two n-bit integers a and b Let a= (a n-1 …a 1 a 0 ) 2 and b = (b n-1 …b 1 b 0 ) 2 3. Multiplying Integers 3. Multiplying Integers Pseudocode: Multiplying Integers procedure multiply (a, b: positive integer) {the binary expansions of a and b are ( a n-1 … a 1 a 0 ) 2 and ( b n-1 … b 1 b 0 ) 2 } for j := 0 to n - 1 begin if b j := 1 then c j := a shift j places else c j := 0 end {c 0, c 1, …,c n-1 are the partial product} p := 0 for j := 0 to n – 1 p := p + c j {p is the value of ab}

46 1.2 INTEGERS AND ALGORITHMS 1. Find the product of (110) 2 and (101) 2 2. Find the product of (1110) 2 and (1011) 2 3. Find the product of (110) 2, (0000) 2 and (11000) 2 4. How many additions of bits and shifts of bits are used to multiply a and b using the multiplying algorithm? EXERCISE 1.2.3

47 1.2 INTEGERS AND ALGORITHMS describe division algorithm : a = dq + r where: q = a div d is the quotient and r = a mod d is the remainder when a divided by d we need O(n²) bit operations to find q and r 4. Computing div and mod 4. Computing div and mod Pseudocode: Computing div and mod procedure division algorithm (a: integer, b: positive integer) q := 0 r := |a| while r ≥ d begin r := r - d q := q + 1 end if a 0 then begin r := d - r q := - (q + 1) end {q = a div d is the quotient and r = a mod d is the remainder}

48 1.2 INTEGERS AND ALGORITHMS Describe the algorithm use to find Let n = (a k-1 …a 1 a 0 ) 2 5. Modular Exponentiation 5. Modular Exponentiation Pseudocode: Modular Exponentiation procedure modular exponentiation (b: integer, n = ( a k-1 … a 1 a 0 ) 2, m: positive integer) x := 1 power := b mod m for i := 0 to k - 1 begin if a i := 1 then x := (x · power) mod m power := (power · power) mod m end { x equals } procedure mod (x: integer, y: positive integer) power := |b| while power ≥ m power := power - m if x 0 then power := m - power {power := b mod m is the remainder}

49 1.2 INTEGERS AND ALGORITHMS 1. Find 2. Find 3. Find EXERCISE 1.2.4

50 1.2 INTEGERS AND ALGORITHMS Describe the algorithm use to find greatest common divisor, gcd (a, b) Let a = bq + r, where a, b, q and r are integers gcd (a, b) = gcd (b, r) 6. Euclidean Algorithm 6. Euclidean Algorithm Pseudocode: Euclidean Algorithm procedure gcd (a, b : positive integer) x := a y := b while y ≠ 0 begin r := x mod y x := y y := r end { gcd(a, b) is x} procedure mod (x: integer, y: positive integer) r := |x| while r ≥ y r := r - y if x 0 then r := b - r {r := x mod y is the remainder}

51 1.2 INTEGERS AND ALGORITHMS 1. Find the greatest common divisor of 414 and 662 using the Euclidean Algorithm 2. Use Euclidean Algorithm to find gcd (1, 5) 3. Use Euclidean Algorithm to find gcd (123, 277) 4.Use Euclidean Algorithm to find gcd (1529, 14038) EXERCISE 1.2.5

52 1.2 INTEGERS AND ALGORITHMS EXERCISE 1.2 : EXTRA EXERCISE 1.2 : EXTRA PAGE : 229, 230, and 231 Rosen K.H., Discrete Mathematics & Its Applications, (Seventh Edition), McGraw-Hill, 2007.

53 1.3MATRICES Review on matrix arithmetic and types Introduce zero-one matrices CHAPTER 1 FUND AMENTALS: ALGORITHMS, INTEGERS AND MATRICES

54 1.3 MATRICES In Discrete Structure, matrix is used to express relationship between elements in sets. Example of application: –Models the communications networks and transportation systems Review on matrix arithmetic –Addition, multiplication, transpose and power Review on type of matrix –Row, column, square, identity, symmetric Introduces Zero-One Matrices Introduction

55 1.3 MATRICES A matrix with entries that are either 0 or 1 Often used to represent Discrete Structure (Graph) Algorithms using these structures are based on Boolean arithmetic with zero-one matrices This arithmetic based on the Boolean operations Λ (meet) and V (join), which operate on pairs of bits, defined by : Zero-One Matrices

56 1.3 MATRICES Let A = [a ij ] and B = [b ij ] be m × n zero-one matrices. The join of A and B is the zero-one matrix with (i, j)th entry a ij V b ij. The join of A and B is denoted by A V B. The meet of A and B is the zero-one matrix with (i, j)th entry a ij Λ b ij. The meet of A and B is denoted by A Λ B. Join and Meet of Zero-One Matrices

57 1.3 MATRICES 1. Find the join and meet of the zero-one matrices 2. Find the join and meet of the zero-one matrices EXERCISE 1.3.1

58 1.3 MATRICES Let A = [a ij ] be a m × k zero-one matrix and B = [b ij ] be a k × n zero-one matrix. The Boolean product of A and B, denoted by is the m × n matrix with (i, j)th entry c ij where: c ij = (a i1 Λ b 1j ) V (a i2 Λ b 2j ) V · · · V (a ik Λ b kj ) Boolean Product of Zero-One Matrices Boolean Product of Zero-One Matrices

59 1.3 MATRICES 1. Find the Boolean Product of A and B where 2. Find the Boolean Product of A and B where EXERCISE 1.3.2

60 1.3 MATRICES Let A be a square zero-one matrix and r be a positive integer. The rth Boolean power of A is the Boolean product of r factors of A. The rth Boolean product of A is denoted by Boolean Powers of Zero-One Matrices Boolean Powers of Zero-One Matrices

61 1.3 MATRICES 1. Find the Boolean powers of the following matrices 2. How many bit operations are used to find where A and B are n × n zero-one matrices? EXERCISE 1.3.3

62 1.3 MATRICES If A and B are n × n matrices, there are n² entries in the Boolean product A total of n Ors and n ANDs are used to find each entry 2n bit operations are used to find each entry Therefore O(2n³) bit operations are required to find the Boolean product. Algorithm for Boolean Product Pseudocode: Boolean Product procedure Boolean Product (A, B: zero-one matrices) for i := 1 to m for j := 1 to n begin c ij := 0 for q := 1 to k c ij := c ij ∨ (a iq ∧ b qj ) end {C = [c ij ] is the Boolean product of A and B}

63 1.3 MATRICES If A and B are n × n matrices, there are n² entries in the product A total of n multiplications and n - 1 additions are used to find each entry A total of n³ multiplications and n²(n – 1) additions are used Therefore O(n³) operations are required to find the product. Algorithm for Matrix Multiplication Pseudocode: Matrix Multiplication procedure matrix multiplication (A, B: matrices) for i := 1 to m for j := 1 to n begin c ij := 0 for q := 1 to k c ij := c ij + (a iq × b qj ) end {C = [c ij ] is the product of A and B}

64 1.3 MATRICES EXERCISE 1.3 : EXTRA EXERCISE 1.3 : EXTRA PAGE : 254, 255, and 256 Rosen K.H., Discrete Mathematics & Its Applications, (Seventh Edition), McGraw-Hill, 2007.

65 CHAPTER 1 FUNDAMENTALS: ALGORITHMS, INTEGERS AND MATRICES SUMMARY THAT’S ALL ; THANK YOU Algorithm is a finite set of precise instructions for performing a computation or solving a problem. The sets of integers plays a fundamental role in Discrete Structure. Matrices are uses to represent a variety of Discrete Structure. What NEXT? Chapter 2: Advanced Counting Technique


Download ppt "BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS CHAPTER 1 FUNDAMENTALS: ALGORITHMS, INTEGERS AND MATRICES SITI ZANARIAH SATARI FIST/FSKKP UMP I0910."

Similar presentations


Ads by Google