680 likes | 946 Vues
BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS. CHAPTER 1 FUNDAMENTALS: ALGORITHMS, INTEGERS AND MATRICES. SITI ZANARIAH SATARI FIST/FSKKP UMP I0910. CONTENT. 1.1 Algorithm 1.2 Integers and Algorithms 1.3 Matrices. CHAPTER 1 FUNDAMENTALS: ALGORITHMS, INTEGERS AND MATRICES.
E N D
BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS CHAPTER 1 FUNDAMENTALS: ALGORITHMS, INTEGERS AND MATRICES SITI ZANARIAH SATARI FIST/FSKKP UMP I0910
CONTENT 1.1 Algorithm 1.2 Integers and Algorithms 1.3 Matrices CHAPTER 1 FUNDAMENTALS: ALGORITHMS, INTEGERS AND MATRICES
CHAPTER 1 FUNDAMENTALS: ALGORITHMS, INTEGERS AND MATRICES • Describe an algorithm • Define and categorized the properties of an • algorithm 1.1 ALGORITHM
What is 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. 1.1 ALGORITHM
Example of Simple 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 1.1 ALGORITHM
Expressing Algorithms (Describe) 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. 1.1 ALGORITHM
Describe an algorithm by Natural Languages 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 1.1 ALGORITHM
Describe an algorithm by Natural Languages 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) 1.1 ALGORITHM • Remove egg from the carton • Break egg on edge of bowl • Drop egg, without shell into bowl
Describe an algorithm by Natural Languages 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. 1.1 ALGORITHM
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
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,,
Describe an algorithm by Pseudocode 1.1 ALGORITHM • EXAMPLE : Describe an algorithm for finding the maximum (largest) value in a finite sequence of integers • proceduremax(a1,a2,…,an: integers) • max := a1 • fori := 2 ton • ifmax < aithenmax := ai • {max is the largest element} - Procedure statement - Assignment statement - Loop construction - Conditional construction - Comment
Describe an algorithm by Flowchart • A diagrammatic representation of an algorithm 1.1 ALGORITHM EXAMPLE : Describe an algorithm for finding the maximum (largest) value in a finite sequence of integers start max = ai, i = 1,…,n; aiintegers no max < ai+1 max = ai yes max = ai+1 no i = i + 1 i + 1 = n yes stop
Describe an algorithm by Programming Languages • A programming language is a language used to write computer programs • Some common programming language • C, C++, C#, Java, etc 1.1 ALGORITHM 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.
EXERCISE 1.1 (ASSIGNMENT 1) • Describe an algorithm that takes a list of n integers a1,a2,…,anand 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. 1.1 ALGORITHM
Properties of an 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 1.1 ALGORITHM
Example: Show that the algorithm for finding maximum values has all the properties • 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. 1.1 ALGORITHM
EXERCISE 1.1 • Determine which characteristics of an algorithm the following procedures have and which they lack • Proceduredouble (n: positive integer) whilen > 0 n:= 2n • Proceduresum (n: positive integer) sum := 0 whilei < 10 sum:= sum + i 1.1 ALGORITHM
DISCUSSION: MOST POPULAR ALGORITHM • Searching Algorithm • The Linear Search / Sequential Search • The Binary Search • Sorting Algorithm • The Bubble Sort • The Insertion Sort 1.1 ALGORITHM
SEARCHING ALGORITHM • Solve problem of locating an element in an ordered lists. • Example: checks the spelling of words in dictionary 1.1 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.
SEARCHING ALGORITHM – Linear Search • 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. 1.1 ALGORITHM Pseudocode: The Linear (Sequential) Search Algorithm • procedurelinear search(x: integer, a1,a2,…,an: distinct integers) • i := 1 • while (i ≤ n and x ≠ ai) • i := i + 1 • ifx = aithenlocation := i • else location := 0 • {location is the subscript of the term that equals x, or is 0 if x is not found }
SEARCHING ALGORITHM – Binary Search • 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 1.1 ALGORITHM Pseudocode: The Binary Search Algorithm • procedurebinary 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} • whilei < j • begin • m := • ifx > am theni := m + 1 • else j := m • end • ifx = ai thenlocation := i • else location := 0 • {location is the subscript of the term that equals x, or is 0 if x is not found }
EXAMPLE – Binary Search • Problem: Search 19 in the list of • 1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22 1.1 ALGORITHM • 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
EXERCISE 1.1 • 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 1.1 ALGORITHM
SORTING 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 1.1 ALGORITHM
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 • procedurebubblesort(a1,a2,…,an: real numbers with n ≥ 2) • for i := 1 ton - 1 • for j:= 1 ton - i • ifaj > aj+1 then interchange ajand aj+1 • {a1,a2,…,an is in increasing order}
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 • 2 2 2 • 3 3 3 • 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 2 1 1 2 3 3 4 4 5 5 1 2 3 4 5 An interchange Pair in correct order Number that guaranteed to be in correct order
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
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
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 • procedureinsertion sort(a1,a2,…,an: real numbers with n ≥ 2) • for j:= 2 ton • begin • i:= 1 • whileaj > ai • i:= i + 1 • m:=aj • for k:= 0 toj – i - 1 • aj-k:= aj-k-1 • ai:=m • end {a1,a2,…,anare sorted}
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
EXERCISE 1.1 • 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 1.1 ALGORITHM
GREEDY ALGORITHM – Optimization Problems • 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 1.1 ALGORITHM
GREEDY ALGORITHM – EXAMPLE • 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. 1.1 ALGORITHM Pseudocode: The Greedy Change-Making Algorithm • procedurechange(c1,c2,…,cr: values of denomination of coins where c1 > c2 >…> cr ; n:a positive integer) • for i := 1 tor • whilen ≥ ci • begin • add a coin with value cito the change • n := n - ci • end
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)
EXERCISE 1.1 • Use greedy algorithm to make change using 50 cents, 20 cents, 10 cents, 5 cents and 1 cents for • 54 cents b) RM 1.33 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. 1.1 ALGORITHM
EXERCISE 1.1 : EXTRA PAGE : 177, 178, and 179 Rosen K.H., Discrete Mathematics & Its Applications, (Seventh Edition), McGraw-Hill, 2007. 1.1 ALGORITHM
1.2 INTEGERS AND ALGORITHMS CHAPTER 1 FUNDAMENTALS: ALGORITHMS, INTEGERS AND MATRICES • Describe important algorithms involving integers • → Constructing Base b Expansions • →Addition of Integers • →Multiplying Integers • →Computing div and mod • → Modular Exponentiation • →Euclidean Algorithm
Introduction • 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.2 INTEGERS AND ALGORITHMS
EXAMPLE: Algorithm and Integers • Constructing Base b Expansions • Addition of Integers • Multiplying Integers • Computing div and mod • Modular Exponentiation • Euclidean Algorithm 1.2 INTEGERS AND ALGORITHMS
1. Constructing Base b Expansions • 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 = qmodb • 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.2 INTEGERS AND ALGORITHMS Pseudocode: Constructing Base b Expansions • procedurebase b expansion (n: positive integer) • q := n • k := 0 • whileq ≠ 0 • begin • ak:= qmodb • q := • k := k + 1 • end • {the base b expansion of n is (ak-1…a1a0)b} • proceduremod (q: integer, b: positive integer) • ak := |q| • whileak ≥ b • ak:= ak - b • ifq < 0 and ak > 0 then ak:= b - ak • {ak := qmodbis the remainder}
EXERCISE 1.2.1 • 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 (1 1000 0110 0011)2 to hexadecimal expansion 1.2 INTEGERS AND ALGORITHMS
2. Addition of Integers • 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) 1.2 INTEGERS AND ALGORITHMS Pseudocode: Addition of Integers • procedureadd (a,b: positive integer) • {the binary expansions of a and b are (an-1…a1a0)2and(bn-1…b1b0)2} • c := 0 • forj := 0 ton - 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}
EXERCISE 1.2.2 • 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? 1.2 INTEGERS AND ALGORITHMS
3. Multiplying Integers • Describe the multiplication of two n-bit integers a and b • Let a= (an-1…a1a0)2 and b = (bn-1…b1b0)2 1.2 INTEGERS AND ALGORITHMS Pseudocode: Multiplying Integers • proceduremultiply (a, b: positive integer) • {the binary expansions of a and b are (an-1…a1a0)2and(bn-1…b1b0)2} • forj := 0 ton - 1 • begin • ifbj:= 1 thencj := a shift j places • else cj:= 0 • end • {c0, c1, …,cn-1 are the partial product} • p := 0 • forj := 0 ton – 1 • p := p + cj • {p is the value of ab}
EXERCISE 1.2.3 • 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? 1.2 INTEGERS AND ALGORITHMS
4. Computing div and mod • describe division algorithm : a = dq + r • where: q = adivd is the quotient and r = amodd is the remainder • when a divided by d we need O(n²) bit operations to find q and r 1.2 INTEGERS AND ALGORITHMS Pseudocode: Computing div and mod • proceduredivision algorithm (a: integer, b: positive integer) • q := 0 • r := |a| • whiler ≥ d • begin • r := r - d • q := q + 1 • end • ifa < 0 and r > 0 then • begin • r := d - r • q := - (q + 1) • end {q = adivd is the quotient and r = amodd is the remainder}
5. Modular Exponentiation • Describe the algorithm use to find • Let n = (ak-1…a1a0)2 1.2 INTEGERS AND ALGORITHMS Pseudocode: Modular Exponentiation • proceduremodular exponentiation (b: integer, n =(ak-1…a1a0)2 , m: positive integer) • x := 1 • power := bmodm • fori := 0 tok - 1 • begin • ifai:= 1 thenx := (x · power) modm • power := (power · power) modm • end • { x equals } • proceduremod (x: integer, y: positive integer) • power := |b| • while power ≥ m • power := power - m • ifx < 0 and r > 0 then • power := m - power • {power := bmodmis the remainder}
EXERCISE 1.2.4 • Find • Find • Find 1.2 INTEGERS AND ALGORITHMS
6. Euclidean Algorithm • 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) 1.2 INTEGERS AND ALGORITHMS Pseudocode: Euclidean Algorithm • proceduregcd (a, b : positive integer) • x := a • y := b • whiley ≠ 0 • begin • r := xmody • x := y • y := r • end • { gcd(a, b) is x} • proceduremod (x: integer, y: positive integer) • r := |x| • whiler ≥ y • r := r - y • ifx < 0 and r > 0 then r := b - r • {r := xmodyis the remainder}