Download presentation

Presentation is loading. Please wait.

1
**BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS**

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

2
**CHAPTER 1 FUNDAMENTALS: ALGORITHMS, INTEGERS AND MATRICES**

BCT2083 DISCRETE STRUCTURE & APPLICATIONS CONTENT CHAPTER 1 FUNDAMENTALS: ALGORITHMS, INTEGERS AND MATRICES 1.1 Algorithm 1.2 Integers and Algorithms 1.3 Matrices CHAPTER 1 2

3
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

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

4
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

What is Algorithm? 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 (9th century mathematician) – Algorism. Algorism was used for the rules for performing arithmetic using decimal notation. Algorism evolved into the word algorithm by 18th century. CHAPTER 1

5
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

Example of Simple Algorithm 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 CHAPTER 1

6
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

Expressing Algorithms (Describe) 1.1 ALGORITHM We can specify a procedure for solving a problem in term of the following ways: Natural languages – used any language – rarely used for complex or technical algorithms. Pseudocode & Flowcharts – structured ways to express algorithms that avoid many of the ambiguities common in natural language statements. 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. CHAPTER 1

7
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

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

8
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

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

9
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

Describe an algorithm by Natural Languages 1.1 ALGORITHM EXAMPLE 2: Describe an algorithm for finding the maximum (largest) value in a finite sequence of integers Assume the first integer in the sequence is the largest (temporary maximum). 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. Repeat STEP 2 for the next integer in the sequence. Stop when there are no integers left in the sequence. The temporary maximum at this point is the largest integer in the sequence. CHAPTER 1

10
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

Describe an algorithm by Pseudocode 1.1 ALGORITHM 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 CHAPTER 1

11
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

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

12
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

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

13
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

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

14
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

Describe an algorithm by Programming Languages 1.1 ALGORITHM A programming language is a language used to write computer programs Some common programming language C, C++, C#, Java, etc 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. CHAPTER 1

15
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

EXERCISE 1.1 (ASSIGNMENT 1) 1.1 ALGORITHM Describe an algorithm that takes a list of n integers a1,a2,…,an and finds the number of integers each greater than five in the list. 2. Describe an algorithm that takes a list of integers a1,a2,…,an (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. CHAPTER 1

16
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

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

17
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

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

18
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

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

19
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

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

20
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

SEARCHING ALGORITHM 1.1 ALGORITHM Solve problem of locating an element in an ordered lists. Example: checks the spelling of words in dictionary GENERAL DESCRIPTION OF SEARCHING ALGORITHM Locate an element x in a list of distinct elements a1, a2, …, an, 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 = ai) and is 0 if x is not in the list. CHAPTER 1

21
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

SEARCHING ALGORITHM – Linear Search 1.1 ALGORITHM Begins by comparing x and ai If x = a1, the solution is the location a1, namely 1 When x ≠ a1, compare x with a2 If x = a2, the solution is the location a2, 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. Pseudocode: The Linear (Sequential) Search Algorithm procedure linear search(x: integer, a1,a2,…,an: distinct integers) i := 1 while (i ≤ n and x ≠ ai) i := i + 1 if x = ai then location := i else location := 0 {location is the subscript of the term that equals x, or is 0 if x is not found } CHAPTER 1

22
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

SEARCHING ALGORITHM – Binary Search 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 Pseudocode: The Binary Search Algorithm procedure binary search(x: integer, a1,a2,…,an: 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 > am then i := m + 1 else j := m end if x = ai then location := i else location := 0 {location is the subscript of the term that equals x, or is 0 if x is not found } CHAPTER 1

23
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

EXAMPLE – Binary Search 1.1 ALGORITHM Problem: Search 19 in the list of 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 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 14th term in the list CHAPTER 1

24
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

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

25
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

SORTING ALGORITHM 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 CHAPTER 1

26
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

SORTING ALGORITHM – Bubble Sort 1.1 ALGORITHM 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 Pseudocode: The Bubble Sort Algorithm € procedure bubblesort(a1,a2,…,an: real numbers with n ≥ 2) for i := 1 to n - 1 for j:= 1 to n - i if aj > aj+1 then interchange aj and aj+1 {a1,a2,…,an is in increasing order} CHAPTER 1

27
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

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

28
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

EXAMPLE – Bubble Sort (Description) 1.1 ALGORITHM 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 CHAPTER 1

29
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

EXAMPLE – Bubble Sort (Description) 1.1 ALGORITHM 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 CHAPTER 1

30
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

SORTING ALGORITHM – Insertion Sort 1.1 ALGORITHM 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. Pseudocode: The Insertion Sort Algorithm procedure insertion sort(a1,a2,…,an: real numbers with n ≥ 2) for j:= 2 to n begin i:= 1 while aj > ai i:= i + 1 m:=aj for k:= 0 to j – i - 1 aj-k:= aj-k-1 ai:=m end {a1,a2,…,an are sorted} CHAPTER 1

31
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

EXAMPLE – Insertion Sort 1.1 ALGORITHM 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 CHAPTER 1

32
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

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

33
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

GREEDY ALGORITHM – Optimization Problems 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 CHAPTER 1

34
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

GREEDY ALGORITHM – EXAMPLE 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, 2007. Pseudocode: The Greedy Change-Making Algorithm procedure change(c1,c2,…,cr: values of denomination of coins where c1 > c2 >…> cr ; n: a positive integer) for i := 1 to r while n ≥ ci begin add a coin with value ci to the change n := n - ci end CHAPTER 1

35
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

GREEDY ALGORITHM – EXAMPLE 1.1 ALGORITHM 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) CHAPTER 1

36
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

EXERCISE 1.1 1.1 ALGORITHM Use greedy algorithm to make change using 50 cents, 20 cents, 10 cents, 5 cents and 1 cents for 54 cents b) RM c) 86 cents 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. CHAPTER 1

37
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

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

38
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

CHAPTER 1 FUNDAMENTALS: ALGORITHMS, INTEGERS AND MATRICES 1.2 INTEGERS 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

39
**1.2 INTEGERS AND ALGORITHMS**

BCT2083 DISCRETE STRUCTURE & APPLICATIONS Introduction 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) Base 16 (hexadecimal) notation – characters (letters or digits) Basis of computer arithmetic – adapted algorithm with binary representations Generally, an integers can be expressed in the form of: CHAPTER 1

40
**1.2 INTEGERS AND ALGORITHMS**

BCT2083 DISCRETE STRUCTURE & APPLICATIONS EXAMPLE: Algorithm and Integers 1.2 INTEGERS AND ALGORITHMS Constructing Base b Expansions Addition of Integers Multiplying Integers Computing div and mod Modular Exponentiation Euclidean Algorithm CHAPTER 1

41
**1.2 INTEGERS AND ALGORITHMS**

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

42
**1.2 INTEGERS AND ALGORITHMS**

BCT2083 DISCRETE STRUCTURE & APPLICATIONS EXERCISE 1.2.1 1.2 INTEGERS AND ALGORITHMS Find the binary expansion of (241)10 Find the base 8 or octal expansion of (12345)10 Find the hexadecimal expansion of (177130)10 Convert ( )2 to hexadecimal expansion CHAPTER 1

43
**1.2 INTEGERS AND ALGORITHMS**

BCT2083 DISCRETE STRUCTURE & APPLICATIONS 2. Addition of Integers 1.2 INTEGERS AND ALGORITHMS Describe the Addition Algorithm for two integers expressed in binary notation Let a= (an-1…a1a0)2 and b = (bn-1…b1b0)2 a and b each have n bits (putting bits equal to 0 at the beginning of one these expansion if necessary) Pseudocode: Addition of Integers procedure add (a,b: positive integer) {the binary expansions of a and b are (an-1…a1a0)2 and (bn-1…b1b0)2} c := 0 for j := 0 to n - 1 begin d := sj := aj + bj + c - 2d c := d end sn := c {the binary expansion of the sum is a + b = (snsn-1…s1s0)2} CHAPTER 1

44
**1.2 INTEGERS AND ALGORITHMS**

BCT2083 DISCRETE STRUCTURE & APPLICATIONS EXERCISE 1.2.2 1.2 INTEGERS AND ALGORITHMS Add (110)2 and (101)2 Add (1110)2 and (1011)2 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? CHAPTER 1

45
**1.2 INTEGERS AND ALGORITHMS**

BCT2083 DISCRETE STRUCTURE & APPLICATIONS 3. Multiplying Integers 1.2 INTEGERS AND ALGORITHMS Describe the multiplication of two n-bit integers a and b Let a= (an-1…a1a0)2 and b = (bn-1…b1b0)2 Pseudocode: Multiplying Integers procedure multiply (a, b: positive integer) {the binary expansions of a and b are (an-1…a1a0)2 and (bn-1…b1b0)2} for j := 0 to n - 1 begin if bj := 1 then cj := a shift j places else cj := 0 end {c0, c1, …,cn-1 are the partial product} p := 0 for j := 0 to n – 1 p := p + cj {p is the value of ab} CHAPTER 1

46
**1.2 INTEGERS AND ALGORITHMS**

BCT2083 DISCRETE STRUCTURE & APPLICATIONS EXERCISE 1.2.3 1.2 INTEGERS AND ALGORITHMS Find the product of (110)2 and (101)2 Find the product of (1110)2 and (1011)2 Find the product of (110)2 , (0000)2 and (11000)2 How many additions of bits and shifts of bits are used to multiply a and b using the multiplying algorithm? CHAPTER 1

47
**1.2 INTEGERS AND ALGORITHMS**

BCT2083 DISCRETE STRUCTURE & APPLICATIONS 4. Computing div and mod 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 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 and r > 0 then r := d - r q := - (q + 1) end {q = a div d is the quotient and r = a mod d is the remainder} CHAPTER 1

48
**1.2 INTEGERS AND ALGORITHMS**

BCT2083 DISCRETE STRUCTURE & APPLICATIONS 5. Modular Exponentiation 1.2 INTEGERS AND ALGORITHMS Describe the algorithm use to find Let n = (ak-1…a1a0)2 Pseudocode: Modular Exponentiation procedure modular exponentiation (b: integer, n = (ak-1…a1a0)2 , m: positive integer) x := 1 power := b mod m for i := 0 to k - 1 begin if ai := 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 and r > 0 then power := m - power {power := b mod m is the remainder} CHAPTER 1

49
**1.2 INTEGERS AND ALGORITHMS**

BCT2083 DISCRETE STRUCTURE & APPLICATIONS EXERCISE 1.2.4 1.2 INTEGERS AND ALGORITHMS Find CHAPTER 1

50
**1.2 INTEGERS AND ALGORITHMS**

BCT2083 DISCRETE STRUCTURE & APPLICATIONS 6. Euclidean Algorithm 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) 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 and r > 0 then r := b - r {r := x mod y is the remainder} CHAPTER 1

51
**1.2 INTEGERS AND ALGORITHMS**

BCT2083 DISCRETE STRUCTURE & APPLICATIONS EXERCISE 1.2.5 1.2 INTEGERS AND ALGORITHMS Find the greatest common divisor of 414 and 662 using the Euclidean Algorithm Use Euclidean Algorithm to find gcd (1, 5) Use Euclidean Algorithm to find gcd (123, 277) Use Euclidean Algorithm to find gcd (1529, 14038) CHAPTER 1

52
**1.2 INTEGERS AND ALGORITHMS**

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

53
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

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

54
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

Introduction 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 CHAPTER 1

55
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

Zero-One Matrices 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 : CHAPTER 1

56
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

Join and Meet of Zero-One Matrices 1.3 MATRICES Let A = [aij] and B = [bij] be m × n zero-one matrices. The join of A and B is the zero-one matrix with (i, j)th entry aij V bij. 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 aij Λ bij. The meet of A and B is denoted by A Λ B. CHAPTER 1

57
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

EXERCISE 1.3.1 1.3 MATRICES Find the join and meet of the zero-one matrices CHAPTER 1

58
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

Boolean Product of Zero-One Matrices 1.3 MATRICES Let A = [aij] be a m × k zero-one matrix and B = [bij] 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 cij where: cij = (ai1 Λ b1j) V (ai2 Λ b2j) V · · · V (aik Λ bkj) CHAPTER 1

59
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

EXERCISE 1.3.2 1.3 MATRICES Find the Boolean Product of A and B where CHAPTER 1

60
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

Boolean Powers of Zero-One Matrices 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 CHAPTER 1

61
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

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

62
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

Algorithm for Boolean Product 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. Pseudocode: Boolean Product procedure Boolean Product (A, B: zero-one matrices) for i := 1 to m for j := 1 to n begin cij := 0 for q := 1 to k cij := cij ∨ (aiq ∧ bqj) end {C = [cij] is the Boolean product of A and B} CHAPTER 1

63
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

Algorithm for Matrix Multiplication 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. Pseudocode: Matrix Multiplication procedure matrix multiplication (A, B: matrices) for i := 1 to m for j := 1 to n begin cij := 0 for q := 1 to k cij := cij + (aiq × bqj) end {C = [cij] is the product of A and B} CHAPTER 1

64
**BCT2083 DISCRETE STRUCTURE & APPLICATIONS**

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

65
**CHAPTER 1 FUNDAMENTALS: ALGORITHMS, INTEGERS AND MATRICES**

BCT2083 DISCRETE STRUCTURE & APPLICATIONS CHAPTER 1 FUNDAMENTALS: ALGORITHMS, INTEGERS AND MATRICES 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. SUMMARY What NEXT? Chapter 2: Advanced Counting Technique THAT’S ALL ; THANK YOU CHAPTER 1

Similar presentations

OK

Discrete Mathematics Chapter 2 The Fundamentals : Algorithms, the Integers, and Matrices. 大葉大學 資訊工程系 黃鈴玲.

Discrete Mathematics Chapter 2 The Fundamentals : Algorithms, the Integers, and Matrices. 大葉大學 資訊工程系 黃鈴玲.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on articles of association sample Ppt on market friendly staten Ppt on miniature circuit breaker Ppt on ram and rom explained Ppt on acid bases and salts for class 10 Ppt on ideal gas law worksheet Ppt on 3d geometry maths class 11 Ppt on field study 5 Ppt on object oriented programming language Ppt on fuel from plastic waste pdf