+ All Categories
Home > Documents > BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

Date post: 29-Jan-2016
Category:
Upload: pules
View: 41 times
Download: 0 times
Share this document with a friend
Description:
BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS. CHAPTER 1 FUNDAMENTALS: ALGORITHMS, INTEGERS AND MATRICES. SITI ZANARIAH SATARI FIST/FSKKP UMP I0910. CONTENT. 1.1Algorithm 1.2Integers and Algorithms 1.3Matrices. CHAPTER 1 FUNDAMENTALS: ALGORITHMS, INTEGERS AND MATRICES. - PowerPoint PPT Presentation
Popular Tags:
65
BCT 2083 DISCRETE STRUCTURE AND BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS APPLICATIONS CHAPTER 1 CHAPTER 1 FUNDAMENTALS: FUNDAMENTALS: ALGORITHMS, INTEGERS AND ALGORITHMS, INTEGERS AND MATRICES MATRICES SITI ZANARIAH SATARI SITI ZANARIAH SATARI FIST/FSKKP UMP I0910 FIST/FSKKP UMP I0910
Transcript
Page 1: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

BCT 2083 DISCRETE STRUCTURE AND APPLICATIONSBCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

CHAPTER 1CHAPTER 1FUNDAMENTALS: ALGORITHMS, FUNDAMENTALS: ALGORITHMS, INTEGERS AND MATRICESINTEGERS AND MATRICES

SITI ZANARIAH SATARISITI ZANARIAH SATARI

FIST/FSKKP UMP I0910FIST/FSKKP UMP I0910

Page 2: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

CHAPTER 1 FU

ND

AMEN

TALS: ALGO

RITHM

S, INTEG

ERS AND

MATRICES

CONTENTCONTENT

1.1 Algorithm1.2 Integers and Algorithms1.3 Matrices

Page 3: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.11.1 ALGORITHMALGORITHM

• Describe an algorithm

• Define and categorized the properties of an

algorithm

CHAPTER 1 FU

ND

AMEN

TALS: ALGO

RITHM

S, INTEG

ERS AND

M

ATRICES

Page 4: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

► 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.

What is Algorithm? What is Algorithm?

Page 5: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

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 AlgorithmExample of Simple Algorithm

Page 6: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

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)

Page 7: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

EXAMPLE 1: A recipe for baking a cake A) general - without subroutines

1. MIX MARGERINE AND SUGAR2. ADD EGG TO THE MIXTURE3. BEAT THE MIXTURE4. ADD THE FLOUR TO THE MIXTURE5. POUR MIXTURE INTO PAN6. BAKE IN OVEN FOR 40 MINUTES AT 350°F

Describe an algorithm by Natural LanguagesDescribe an algorithm by Natural Languages

Page 8: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

EXAMPLE 1: A recipe for baking a cake B) precise - with subroutines and the details steps

given for each subroutines.

1. CALL MIX2. CALL ADDEGG3. CALL BEAT4. CALL ADDFLOUR5. CALL PAN6. CALL BAKE (OVEN,40,350)

Describe an algorithm by Natural LanguagesDescribe 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

Page 9: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

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 LanguagesDescribe an algorithm by Natural Languages

Page 10: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

Describe an algorithm by PseudocodeDescribe 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

Page 11: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

Describe an algorithm by PseudocodeDescribe 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 then6. Loop Constructions – for, while7. 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,,

Page 12: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

Describe an algorithm by PseudocodeDescribe an algorithm by Pseudocode

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 nif max < ai then max := ai

{max is the largest element} - Comment

- Procedure statement

- Loop construction

- Conditional construction

- Assignment statement

Page 13: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

• A diagrammatic representation of an algorithm

Describe an algorithm by FlowchartDescribe an algorithm by Flowchart

EXAMPLE : Describe an algorithm for finding the maximum (largest) value in a finite sequence of integers

max = ai, i = 1,…,n; ai integers

max = ai+1

start

max < ai+1

i = i + 1

stop

i + 1 = n

max = ai

yes

yes

no

no

Page 14: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

• A programming language is a language used to write computer programs

• Some common programming language– C, C++, C#, Java, etc

Describe an algorithm by Programming LanguagesDescribe an algorithm by Programming Languages

EXAMPLE : Describe an algorithm for finding the maximum (largest) value in a finite sequence of integersWrite down your own code and test your answer.

Page 15: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

EXERCISE 1.1 (ASSIGNMENT 1)EXERCISE 1.1 (ASSIGNMENT 1)

1. 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.

Page 16: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

Properties of an AlgorithmProperties 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

Page 17: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

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.

Page 18: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

EXERCISE 1.1EXERCISE 1.1

5. 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

Page 19: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

• Searching Algorithm– The Linear Search / Sequential Search– The Binary Search

• Sorting Algorithm– The Bubble Sort– The Insertion Sort

DISCUSSION: MOST POPULAR ALGORITHMDISCUSSION: MOST POPULAR ALGORITHM

Page 20: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

• Solve problem of locating an element in an ordered lists.• Example: checks the spelling of words in dictionary

SEARCHING ALGORITHMSEARCHING ALGORITHM

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.

Page 21: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

• 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.

SEARCHING ALGORITHM – Linear SearchSEARCHING ALGORITHM – Linear Search

Pseudocode: The Linear (Sequential) Search Algorithm

procedure linear search(x: integer, a1,a2,…,an: distinct integers)i := 1while (i ≤ n and x ≠ ai)

i := i + 1if x = ai then location := ielse location := 0{location is the subscript of the term that equals x, or is 0 if x is not found }

Page 22: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

• 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 SearchSEARCHING ALGORITHM – Binary Search

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 + 1else j := m

endif x = ai then location := ielse location := 0{location is the subscript of the term that equals x, or is 0 if x is not found }

2i j

Page 23: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

• Problem: Search 19 in the list of – 1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22

EXAMPLE – Binary SearchEXAMPLE – Binary Search

• Solution:– Split into two smaller lists with 8 terms each – 1 2 3 5 6 7 8 10 and 12 13 15 16 18 19 20 22 – 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– 12 13 15 16 and 18 19 20 22– Since 16 < 19, Split the second list into another subsplits with 2 term

each– 18 19 and 20 22– 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

Page 24: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

EXERCISE 1.1EXERCISE 1.1

6. 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

Page 25: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

• 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 ALGORITHMSORTING ALGORITHM

Page 26: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

SORTING ALGORITHM – Bubble SortSORTING ALGORITHM – Bubble Sort

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 - iif aj > aj+1 then interchange aj and

aj+1

{a1,a2,…,an 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

Page 27: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

EXAMPLE – Bubble Sort (Illustration)EXAMPLE – Bubble Sort (Illustration)

3 2 2 2

4 3 3 3

4 4 4 1

1 1 1 4

5 5 5 5

2 2 2

3 3 1

1 1 3

4 4 4

5 5 5

• Problem: Use bubble sort to put 3, 2, 4, 1, 5 into increasing order

2 1

1 2

3 3

4 4

5 5

1

2

3

4

5

First pass Fourth passThird passSecond pass

An interchange

Pair in correct order

Number that guaranteed to be in correct order

Page 28: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

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

Page 29: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

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

Page 30: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

SORTING ALGORITHM – Insertion SortSORTING ALGORITHM – Insertion Sort

Pseudocode: The Insertion Sort Algorithm

procedure insertion sort(a1,a2,…,an: real numbers with n ≥ 2)for j:= 2 to nbegin

i:= 1while aj > ai

i:= i + 1m:=aj

for k:= 0 to j – i - 1aj-k:= aj-k-1

ai:=mend {a1,a2,…,an 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.

Page 31: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

EXAMPLE – Insertion SortEXAMPLE – 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

Page 32: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

EXERCISE 1.1EXERCISE 1.1

10. 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

Page 33: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

• 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 ProblemsGREEDY ALGORITHM – Optimization Problems

Page 34: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

• 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.

GREEDY ALGORITHM – EXAMPLEGREEDY ALGORITHM – EXAMPLE

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 changen := n - ci

end

Page 35: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

GREEDY ALGORITHM – EXAMPLEGREEDY 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)

Page 36: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

EXERCISE 1.1EXERCISE 1.1

14. Use greedy algorithm to make change using 50 cents, 20 cents, 10 cents, 5 cents and 1 cents for

a) 54 cents b) 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.

Page 37: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.1 ALGO

RITHM

EXERCISE 1.1 : EXTRAEXERCISE 1.1 : EXTRA

PAGE : 177, 178, and 179

Rosen K.H., Discrete Mathematics & Its Applications, (Seventh Edition), McGraw-Hill, 2007.

Page 38: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.21.2 INTEGERS AND ALGORITHMSINTEGERS 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 FU

ND

AMEN

TALS: ALGO

RITHM

S, INTEG

ERS AND

M

ATRICES

Page 39: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.2 INTEG

ERS AND

ALGO

RITHM

S

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: 1 1

1 1 0 0; , , , 0;k kk k kn a b a b a b a k a a a b

IntroductionIntroduction

4 3 2 1

162AE0B 2 16 10 16 14 16 0 16 11 175627

4 3 2 1 0

210101 1 2 0 2 1 2 0 2 1 2 21

2

8245 2 8 4 8 5 165

Page 40: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.2 INTEG

ERS AND

ALGO

RITHM

S

1. Constructing Base b Expansions2. Addition of Integers3. Multiplying Integers4. Computing div and mod5. Modular Exponentiation6. Euclidean Algorithm

EXAMPLE: Algorithm and Integers EXAMPLE: Algorithm and Integers

Page 41: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.2 INTEG

ERS AND

ALGO

RITHM

S

• 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

1. Constructing Base 1. Constructing Base bb Expansions Expansions

Pseudocode: Constructing Base Pseudocode: Constructing Base bb Expansions Expansions

procedure base b expansion (n: positive integer)q := n k := 0 while q ≠ 0begin

ak := q mod bq := k := k + 1

end{the base b expansion of n is (ak-1…a1a0)b}

q 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}

Page 42: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.2 INTEG

ERS AND

ALGO

RITHM

S

1. Find the binary expansion of (241)10

2. Find the base 8 or octal expansion of (12345)10

3. Find the hexadecimal expansion of (177130)10

4. Convert (1 1000 0110 0011)2 to hexadecimal expansion

EXERCISE 1.2.1EXERCISE 1.2.1

Page 43: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.2 INTEG

ERS AND

ALGO

RITHM

S

• 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)

2. Addition of Integers2. Addition of Integers

Pseudocode: Addition of IntegersPseudocode: 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 - 1begin

d := sj := aj + bj + c - 2d c := d

endsn := c{the binary expansion of the sum is a + b = (snsn-1…s1s0)2}

2j ja b c

Page 44: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.2 INTEG

ERS AND

ALGO

RITHM

S

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.2EXERCISE 1.2.2

Page 45: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.2 INTEG

ERS AND

ALGO

RITHM

S

• Describe the multiplication of two n-bit integers a and b• Let a= (an-1…a1a0)2 and b = (bn-1…b1b0)2

3. Multiplying Integers3. Multiplying Integers

Pseudocode: Multiplying IntegersPseudocode: 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 - 1begin

if bj := 1 then cj := a shift j places else cj := 0

end{c0, c1, …,cn-1 are the partial product}p := 0for j := 0 to n – 1

p := p + cj

{p is the value of ab}

0 1 1 0 1 10 1 1 0 1 12 2 2 2 2 2n n

n nab a b b b a b a b a b

Page 46: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.2 INTEG

ERS AND

ALGO

RITHM

S

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.3EXERCISE 1.2.3

Page 47: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.2 INTEG

ERS AND

ALGO

RITHM

S

• 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 mod4. Computing div and mod

Pseudocode: Computing div and modPseudocode: Computing div and mod

procedure division algorithm (a: integer, b: positive integer)q := 0r := |a| while r ≥ dbegin

r := r - d q := q + 1

endif a < 0 and r > 0 thenbegin

r := d - rq := - (q + 1)

end {q = a div d is the quotient and r = a mod d is the remainder}

Page 48: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.2 INTEG

ERS AND

ALGO

RITHM

S

• Describe the algorithm use to find • Let n = (ak-1…a1a0)2

• 1 11 1 0 1 01.2 ... .2 .2 .2k k

k ka a a a aanb b b b b

5. Modular Exponentiation5. Modular Exponentiation

Pseudocode: Modular ExponentiationPseudocode: Modular Exponentiation

procedure modular exponentiation (b: integer, n = (ak-1…a1a0)2 , m: positive integer)x := 1power := b mod mfor i := 0 to k - 1begin if ai := 1 then x := (x · power) mod m power := (power · power) mod mend{ x equals }nb mmod

nb mmod

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}

Page 49: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.2 INTEG

ERS AND

ALGO

RITHM

S

1. Find

2. Find

3. Find

6443 645mod

EXERCISE 1.2.4EXERCISE 1.2.4

64411 645mod

1001123 101mod

Page 50: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.2 INTEG

ERS AND

ALGO

RITHM

S

• 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 Algorithm6. Euclidean Algorithm

Pseudocode: Euclidean AlgorithmPseudocode: Euclidean Algorithm

procedure gcd (a, b : positive integer)x := ay := b while y ≠ 0 begin

r := x mod y x := yy := 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}

Page 51: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.2 INTEG

ERS AND

ALGO

RITHM

S

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.5EXERCISE 1.2.5

Page 52: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.2 INTEG

ERS AND

ALGO

RITHM

S

EXERCISE 1.2 : EXTRAEXERCISE 1.2 : EXTRA

PAGE : 229, 230, and 231

Rosen K.H., Discrete Mathematics & Its Applications, (Seventh Edition), McGraw-Hill, 2007.

Page 53: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.31.3 MATRICESMATRICES

• Review on matrix arithmetic and types

• Introduce zero-one matrices

CHAPTER 1 FU

ND

AMEN

TALS: ALGO

RITHM

S, INTEG

ERS AND

M

ATRICES

Page 54: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

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

IntroductionIntroduction

Page 55: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

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 MatricesZero-One Matrices

1 2 1 21 2 1 2

1 if 1 1 if 1 or 1

0 otherwise 0 otherwise

b b b bb b b b

Page 56: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

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.

Join and Meet of Zero-One MatricesJoin and Meet of Zero-One Matrices

Page 57: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.3 MATRICES1. Find the join and meet of the zero-one matrices

2. Find the join and meet of the zero-one matrices

1 0 1 0 1 0,

0 1 0 1 1 0

A B

EXERCISE 1.3.1EXERCISE 1.3.1

1 0 1 0

0 1 , 0 1 ,

1 0 0 1

A B

Page 58: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

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)

Boolean Product of Zero-One MatricesBoolean Product of Zero-One Matrices

A B

Page 59: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.3 MATRICES1. Find the Boolean Product of A and B where

2. Find the Boolean Product of A and B where

1 0 1 0,

1 1 0 1

A B

EXERCISE 1.3.2EXERCISE 1.3.2

1 01 1 0

0 1 ,0 1 1

1 0

A B

Page 60: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

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 MatricesBoolean Powers of Zero-One Matrices

times

0

r

r

n

A A A A A

A I

Page 61: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.3 MATRICES1. 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?

0 0 11 0

, 1 0 01 1

1 1 0

A B

EXERCISE 1.3.3EXERCISE 1.3.3

A B

Page 62: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

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 ProductAlgorithm for Boolean Product

Pseudocode: Boolean ProductPseudocode: Boolean Product

procedure Boolean Product (A, B: zero-one matrices)for i := 1 to m

for j := 1 to nbegin

cij := 0for q := 1 to k

cij := cij ∨ (aiq ∧ bqj)end

{C = [cij] is the Boolean product of A and B}

Page 63: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

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 MultiplicationAlgorithm for Matrix Multiplication

Pseudocode: Matrix MultiplicationPseudocode: Matrix Multiplication

procedure matrix multiplication (A, B: matrices)for i := 1 to m

for j := 1 to nbegin

cij := 0for q := 1 to k

cij := cij + (aiq × bqj)end

{C = [cij] is the product of A and B}

Page 64: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

1.3 MATRICES

EXERCISE 1.3 : EXTRAEXERCISE 1.3 : EXTRA

PAGE : 254, 255, and 256

Rosen K.H., Discrete Mathematics & Its Applications, (Seventh Edition), McGraw-Hill, 2007.

Page 65: BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS

CHAPTER 1 FU

NDAM

ENTALS: ALG

ORITH

MS, IN

TEGERS AN

D M

ATRICES

SUMMARYSUMMARY

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


Recommended