of 49 /49
CS104 : Discrete Structures 06/14/22 Prepared by Dr. Zakir H. Ahmed 1 Chapter III Proof Techniques

CS104 : Discrete Structures

  • Author
    efrem

  • View
    40

  • Download
    2

Embed Size (px)

DESCRIPTION

CS104 : Discrete Structures. Chapter III Proof Techniques. Rules of Inference – Valid Arguments in Propositional Logic. Argument: An argument is a sequence of statements that end with a conclusion. - PowerPoint PPT Presentation

Text of CS104 : Discrete Structures

  • CS104 : Discrete Structures*Prepared by Dr. Zakir H. Ahmed*Chapter IIIProof Techniques

    Prepared by Dr. Zakir H. Ahmed

  • Rules of Inference Valid Arguments in Propositional Logic*Prepared by Dr. Zakir H. Ahmed*Argument: An argument is a sequence of statements that end with a conclusion.Valid: An argument is valid if and only if it is impossible for all premises (preceding statements) to be true and the conclusion to be false. By valid, we mean that the conclusion of the argument must follow from the truth of the premises of the argument.Consider the arguments:If you have a current password, then you can log onto the network.You have a current password.Therefore,You can log onto the network.

    Prepared by Dr. Zakir H. Ahmed

  • Rules of Inference Valid Arguments in Propositional Logic*Prepared by Dr. Zakir H. Ahmed*The conclusion You can log onto the network must be true when the premises If you have a current password, then you can log onto the network and You have a current password are true.Let p= You have a current password.and q=You can log onto the network. Then the argument has the form

    where is the symbol that denotes therefore.The statement ((p q) p) q is a tautology. p q p q

    Prepared by Dr. Zakir H. Ahmed

  • Rules of Inference Rule for Propositional Logic*Prepared by Dr. Zakir H. Ahmed*The argument form with premises p1, p2, ., pn and conclusion q is valid, when (p1p2.pn) q is a tautology.To show that an argument is valid, instead of showing by truth table, we can establish the validity of some relatively simple argument forms, called rules of inference, which can be used as building blocks to construct more complicated valid argument forms.The tautology ((p q) p) q is the basis of the rule of inference called modus ponens or law of detachment.

    Prepared by Dr. Zakir H. Ahmed

  • Rules of Inference Rules for Propositional Logic*Prepared by Dr. Zakir H. Ahmed*Example: Suppose that the conditional statement If it snows today, then we will go skiing and its hypothesis It is snowing today, are true. Then by modus ponens, it follows that the conclusion of the conditional statement, We will go skiing is true.Q1: Determine whether the argument given here is valid and determine whether its conclusion must be true because of the validity of the argument.

    Prepared by Dr. Zakir H. Ahmed

  • Rules of Inference Rules for Propositional Logic*Prepared by Dr. Zakir H. Ahmed*

    Prepared by Dr. Zakir H. Ahmed

  • Rules of Inference Rule for Propositional Logic*Prepared by Dr. Zakir H. Ahmed*Example: State which rule of inference is the basis of the following argument: It is below freezing now. Therefore, it is either below freezing or raining now.Sol: Let p= It is below freezing now. and q = It is raining now. Then this argument is of the form:p p v qThis is an argument that uses the addition rule. Q2: State which rule of inference is the basis of the following argument: It is below freezing and raining now. Therefore, it is below freezing.

    Prepared by Dr. Zakir H. Ahmed

  • Introduction to ProofsA theorem is a statement that can be shown to be true (usually important statement)Less important theorem sometimes are called propositionsA proof is a sequence of statements (valid argument) to show that a theorem is trueThe statements to be used in proofs include:Axioms (statement assumed to be true without proof)Ex: If x is positive integer then x+1 is positive integer.Hypothesis (premises) of the theoremPreviously proven theorems Rules of inference used to draw conclusions and to move from one step to another*Prepared by Dr. Zakir H. Ahmed*

    Prepared by Dr. Zakir H. Ahmed

  • Introduction to ProofsA less important theorem that is helpful in the proof of other results is called a lemmaA corollary is a theorem that can be established directly from a theorem that has been provedA conjuncture is a statement that is being proposed to be a true statement, usually on the basis of some partial evidenceWhen a proof of a conjuncture is found, the conjuncture becomes a theorem*Prepared by Dr. Zakir H. Ahmed*Rules of inferenceAxiomsHypothesisproven theoremsNew theorem

    Prepared by Dr. Zakir H. Ahmed

  • Introduction to ProofsExample 1: If I have a car (C) I will drive to Makkah (M). My boss gave me 60,000 (G) or Fired me (F). If I have SR60,000 (H) then I have a car (C). My boss did not fire me. Therefore I will drive to Makkah (M).GF Hypothesis F HypothesisG Disjunctive syllogism rule using 1 and 2GH Axiom H C HypothesisG C Hypo. syllogism using 4,5C Modus ponens using 3 and 6C M HypothesisM Modus ponens using 7 and 8*Prepared by Dr. Zakir H. Ahmed*

    Prepared by Dr. Zakir H. Ahmed

  • Methods of proving theorems:Direct proofsA direct proof of a conditional statement pq is constructed when the first step is the assumption that p is true; subsequent steps are constructed using rules of inference, with the final step showing that q must also be trueIn a direct proof, we assume that p is true and use axioms, definitions, and previously proven theorems, together with rules of inference, to show that q must also be trueDef: The integer n is even if there exists an integer k such that n = 2k, and n is odd if there exists an integer k such that n = 2k + 1.*Prepared by Dr. Zakir H. Ahmed*

    Prepared by Dr. Zakir H. Ahmed

  • Methods of proving theorems:Direct proofsExample 2: Use a direct proof to show that if n is even then n2 is evenProof: Assume that n is even (hypothesis)=> n = 2k where k is integer (definition of even number)=> n2 = (2k)2 = 4k2 = 2(2k2) (By squaring)Since r = 2K2 is integer (Axiom) => n2 = 2r is evenQ 3: Use a direct proof to show that if n is odd then n2 is odd*Prepared by Dr. Zakir H. Ahmed*

    Prepared by Dr. Zakir H. Ahmed

  • Methods of proving theorems:Proof by contrapositionAn indirect proof of a conditional statement p q is a direct proof of its contraposition q p.Example 3: Use an indirect proof to show that if a and b are integers, and (a + b) 15, then a 8 or b 8.Proof: The contraposition of (a + b 15) (a 8) v (b 8) is (a < 8) (b < 8) (a + b < 15) Suppose (a < 8) (b < 8) (hypothesis).=> (a 7) (b 7),=>(a + b) 14,=>(a + b) < 15.*Prepared by Dr. Zakir H. Ahmed*

    Prepared by Dr. Zakir H. Ahmed

  • Methods of proving theorems:Proof by contrapositionExample 4: Use an indirect proof to show that if n2 is even then n is evenProof: The contraposition is if n is not even then n2 is not evenAssume that n is not even i.e., n is odd (hypothesis)=> n = 2k+1 where k is integer (definition of odd number)=> n2 = (2k + 1)2 = 4k2 + 4k + 1 = 2(2k2 + 2k) + 1 = 2r + 1, where r = 2k2 + 2kSince r is integer (Axiom) => n2 is not evenQ 4: Use an indirect proof to show that if n is odd then n2 is odd*Prepared by Dr. Zakir H. Ahmed*

    Prepared by Dr. Zakir H. Ahmed

  • Methods of proving theorems:Proof by contrapositionExample 5: Prove that if n = ab then a n or b n where a and b are positive integers Proof: Let p=an, q=bn and r=n=abWe want to prove that r pq=> The contraposition is (pq ) r (By definition)=> p q r (De Morgans law)Now, assume that an and bn (p q)=> a.b n.n = n (by multiplying above twos)=> ab n => r*Prepared by Dr. Zakir H. Ahmed*

    Prepared by Dr. Zakir H. Ahmed

  • Methods of proving theorems:Vacuous ProofsVacuous Proofs: A conditional statement p q is TRUE if p is FALSE. If we can show that p is False, then we have a proof, called vacuous proof, of the conditional statement p q Example 6: Prove that if x2 0 then 1=2 where x is a real numberProof: Since x2 0 for every real number then the implication is vacuously trueExample 7: Prove that if he is alive and he is dead then the sun is ice cold. Proof: Since the hypothesis is always false the implication is vacuously true.*Prepared by Dr. Zakir H. Ahmed*

    pqp qFFTTFTFTTTFT

    Prepared by Dr. Zakir H. Ahmed

  • Methods of proving theorems:Trivial ProofsTrivial Proofs: A conditional statement p q is TRUE if qis TRUE. If we can show thatq is TRUE, then we have aproof, called trivial proof, ofthe conditional statement p q Example 8: Prove that if x=2 then x2 0 for all real numbersProof: Since x2 0 is true then the implication is trivially true. (we didnt use the fact x=2)Q 5: Use a trivial proof to show that if n > 1 then n2 n for all integers*Prepared by Dr. Zakir H. Ahmed*

    pqp qFFTTFTFTTTFT

    Prepared by Dr. Zakir H. Ahmed

  • Methods of proving theorems:Proofs by ContradictionProof by Contradiction: To prove a proposition p, assume not p and show a contradiction.Example 9: Use a proof by contradiction to show that 2 is irrationalProof: Let 2 is rational=> 2 = a/b for some integers a and b (b0) (relatively prime). (Definition of rational numbers)=> 2 = a2/b2 (Squaring both sides)=> 2b2 = a2=> a2 is even (Definition of even numbers)=> a is even (a = 2k for some k)=> 2b2 = a2 = (2k)2 = 4k2=> b is even (Definition of even numbers)But if a and b are both even, then they are not relatively prime!Hence, 2 is irrational*Prepared by Dr. Zakir H. Ahmed*

    Prepared by Dr. Zakir H. Ahmed

  • Methods of proving theorems:Proofs by ContradictionWhy is this method valid ?The contradiction forces us to reject our assumption because our other steps based on that assumption are logical and justified. The only mistake that we could have made was the assumption itself. Be careful!Sometimes the contradiction comes from a mistake in the steps of the proof and not from the assumption. This makes the proof invalid.Example 10: Prove that 1=2Proof: Suppose that 21 and a=b for some a.=> 2b b [multiply by b]=> a+b b [2b=b+b=a+b by hypothesis]=> (a-b)(a+b) b(a-b) [multiply by a-b]=> a2-b2 ab-b2 => a2 ab [subtract b2 from both sides]=> a b which contradicts our assumption that a=bHence it follows that 1=2 Can you find the error *Prepared by Dr. Zakir H. Ahmed*

    Prepared by Dr. Zakir H. Ahmed

  • Methods of proving theorems:Proofs by ContradictionTo prove a conditional statement p q by contradiction we prove that p q F is true which is equivalent to p q .Example 11: Use a proof by contradiction to show that If 3n+2 is odd then n is oddProof: Suppose that 3n+2 is odd and n is even [p q] => n = 2r [hypothesis q, definition of even numbers]=> 3n = 6r [multiply 1 by 3]=> 3n+2=2+6r [add 2 to both sides] => 3n+2=2(1+3r)=> 3n+2=2k [let k=1+3r]=> Thus 3n+2 is even which is false (a contradiction !) Therefore the implication is true.*Prepared by Dr. Zakir H. Ahmed*

    Prepared by Dr. Zakir H. Ahmed

  • Methods of proving theorems:Proofs of EquivalenceProofs of Equivalence: To prove p q we have to prove p q and q pExample 12: prove n is even if and only if n2 is evenif n is even then n2 is even proved in example 2n2 is even then n is even proved in example 4Therefore, n is even if and only if n2 is evenProving equivalence of several propositions: If we want to prove that p1 p2 p3 pnThen it is sufficient to prove p1 p2,, p2 p3 pn p1Disproof by CounterexampleExample 13: Prove that For all real numbers x2 > x is falseProof: X=0.5 is a counterexample since 0.52 > 0.5 is not trueQ 6: Prove that If n is not positive, then n2 is not positive is false*Prepared by Dr. Zakir H. Ahmed*

    Prepared by Dr. Zakir H. Ahmed

  • Methods of proving theorems: Proof by CasesProof by Cases: A proof by cases must cover all possible cases that arise in a theorem. Each case may cover an infinite number of instancesExample 15: Prove "if n is an integer then n2 n".Proof: We can prove that n2 n for every integer by considering three cases, when n = 0, when n 1, and when n -1.Case 1: When n = 0, since 02 = 0, 02 >= 0. So, n2 n is true.Case 2: When n 1, From n 1, we get n2 n (by multiplying with n). Case 3: When n -1, since n is negative, n2 is positive, so n2 n.Hence, if n is an integer then n2 n.Q 7: Prove that |xy| = |x||y|, where x and y real numbers.Hint: Consider all 4 cases: x and y positive or negativeCommon errors with exhaustive proof and proof by cases:Draw conclusion from non-exhaustive examplesNot covering all possible cases*Prepared by Dr. Zakir H. Ahmed*

    Prepared by Dr. Zakir H. Ahmed

  • Methods of proving theorems: Existence ProofsMany theorems state that an object with certain properties exists, i.e., xP(x) where P is a predicate. A proof of such a theorem is called an existence proof. There are two kinds: Constructive Existence Proof: The proof is established be giving example a such that P(a) is trueExample 16: Prove "there is a positive integer that can be written as the sum of cubes in two different ways.Proof: Consider 1729=103+93=123+13. Finding such examples may require computer assistance.Non-constructive Existence Proof: The proof is established by showing that an object a with P(a) is true must exist without explicitly demonstrating one. Proofs by contradiction are usually used in such cases.*Prepared by Dr. Zakir H. Ahmed*

    Prepared by Dr. Zakir H. Ahmed

  • Methods of proving theorems: Existence ProofsExample 17: Let x1,x2,..,xn be positive integers such that their average is m. prove that there exists xi such that xi mProof: Suppose that there is no such number, i,e.,x1 m, x2 m, , xn mBy adding these inequalities we get: x1+ x2++ xn nmDividing by n: (x1+ x2++ xn)/n mBut since the average is defined as (x1+ x2++ xn)/n Then we have m m which is a contradiction.Therefore there must be a number xi such that xi m. But we can not specify which number is that. *Prepared by Dr. Zakir H. Ahmed*

    Prepared by Dr. Zakir H. Ahmed

  • Methods of proving theorems: Uniqueness ProofsSome theorems state that there is exactly one element with a certain property. A proof of such a theorem is called a uniqueness proof. Strategy here is (1) show that an element x with the desired property exists (2) show that any other y (y != x) does not have the property, i.e., if x and y both have the property, then x must equal y.Example 18: Prove that the equation 3x+5 = 9 has a unique solution.Proof: (1) There exists a solution namely x = 4/3(2) Suppose that y and z are solution then3y+5 = 9 = 3z+5So 3y = 3zDividing by 3 we get y = zThis proves that the solution is unique *Prepared by Dr. Zakir H. Ahmed*

    Prepared by Dr. Zakir H. Ahmed

  • Methods of proving theorems:Mathematical InductionPrinciple of Mathematical Induction:Let P(n) be a statement for all the positive integers (n = 1, 2, 3, . .). If the following two properties hold:P(1) is true.P(k+1) is true if P(k) is true for each positive integer k.Then P(n) is true for all n.First part is a simple proposition we call the base stepSecond part is an inductive step. Start by assuming P(k) is true, and show that P(k+1) is also trueThe assumption that P(k) is true called the inductive hypothesisSo, we prove that (P(1) k (P(k) P(k+1))) (n P(n))*Prepared by Dr. Zakir H. Ahmed*

    Prepared by Dr. Zakir H. Ahmed

  • Methods of proving theorems:Mathematical InductionExample 19: Suppose we have an infinite ladder, and we want to know whether we can reach every step on this ladder. We know two things:We can reach the first rung of the ladderIf we can reach a particular rung of the ladder, then we can reach the next rung.How does Induction Work?Consider the above example. The rules for reaching steps can help you remember how induction worksStatements (1) and (2) are the basic step and inductive step respectively of the proof that P(n) is true for all positive integers n, where P(n) is the statement that we can reach the nth rung of the ladder.Consequently, we can invoke the mathematical induction to conclude that we can reach every rung of the ladder*Prepared by Dr. Zakir H. Ahmed*

    Prepared by Dr. Zakir H. Ahmed

  • Methods of proving theorems:Mathematical InductionExample 20 (A Summation Problem): Prove that for any integer n 1: 1 + 2 + 3 + + n = n(n+1)/2 .Proof: Let P(n) be the proposition that the sum of the first n positive integers is n(n+1)/2. Then to proof that P(n) is true for all n 1, we have to show that P(1) is true and P(k+1) is true if P(k) is true for k 1.Basic step: P(1) is true, because 1 = 1.(1+1)/2Inductive step: Let us assume that it is true for n = k, that is, 1+2+3+..+k = k(k+1)/2Now, if we can prove that it is true for n = k+1 also, then it can be said that it is true for all n.For, 1 + 2 + + k + (k + 1) = k(k + 1)/2 + (k + 1)= (k + 1)(k/2 + 1) = (k + 1)(k + 2)/2 => P(k + 1) is also true, hence P(n) is true for all integer n.*Prepared by Dr. Zakir H. Ahmed*

    Prepared by Dr. Zakir H. Ahmed

  • Methods of proving theorems:Mathematical InductionExample 21: Use induction to prove that the sum of the first n odd integers is n2.Proof: Let P(n) be the proposition that the sum of the first n odd integers is n2. Then to proof that P(n) is true for all n 1, we have to show that P(1) is true and P(k+1) is true if P(k) is true for k 1.Basic step: P(1) is true, because the sum of the first 1 odd integer is 12. Inductive step: Assume P(k): the sum of the first k odd integers is k2, that is, 1 + 3 + + (2k - 1) = k2Now, if we can prove that it is true for n = k+1 also, then it can be said that it is true for all n.For, 1 + 3 + + (2k-1) + (2k+1) = k2 + (2k + 1)= (k+1)2=> P(k + 1) is also true, hence P(n) is true for all integer n.*Prepared by Dr. Zakir H. Ahmed*

    Prepared by Dr. Zakir H. Ahmed

  • Methods of proving theorems:Mathematical InductionQ 8: Use induction to prove that 11! + 22! + + nn! = (n+1)! - 1, n

    Q 9: Use induction to prove that for all n,

    Q 10: Use induction to prove that for all n,

    *Prepared by Dr. Zakir H. Ahmed*

    Prepared by Dr. Zakir H. Ahmed

  • Methods of proving theorems:Mathematical InductionExample 22: Use induction to prove the inequality n < 2n, n > 0.Proof: Let P(n) be the proposition that n < 2nBasic step: P(1) is true, because 1 < 21 = 2.Inductive step: Assume P(k) is true, that is, k < 2KNow, if we can prove that it is true for n = k+1 also, then it can be said that it is true for all n.For, k < 2K=> k + 1 < 2K + 1 2K + 2K =2. 2K= 2K+1 => k + 1 < 2K+1 => P(k + 1) is also true, hence P(n) is true for all integer n.Q 11: Use induction to prove the inequality 2n < n! n > 3.*Prepared by Dr. Zakir H. Ahmed*

    Prepared by Dr. Zakir H. Ahmed

  • Recursion: Sometimes it is difficult to define an object explicitly. However, it may be easy to define this object in terms of itself. This process is called recursion.Recursive defined functions: We use two steps to define a function with the nonnegative integers as its domain:Basis Step: Specify the value of the function at zero.Recursive Step: Give a rule for finding its value at an integer from its values at smaller integers. Such definition is called a recursive or inductive definition.Example 25: The definition of factorial function:n! = 1 2 3 (n-1) n, n 1But equivalently, we could define it like this:*Prepared by Dr. Zakir H. Ahmed*Methods of proving theorems: Recursive Definitions

    Prepared by Dr. Zakir H. Ahmed

  • Example 26: The definition of Fibonacci Numbers:*Prepared by Dr. Zakir H. Ahmed*Methods of proving theorems: Recursive Definitions

    Prepared by Dr. Zakir H. Ahmed

  • Example 27: Suppose that f is defined byf(0) = 3,f(n+1) = 2f(n) + 3.Find f(1), f(2), f(3), and f(4).Solution: From the recursive definition, it follows thatf(1) = 2f(0) + 3 = 2.3 + 3 = 9,f(2) = 2f(1) + 3 = 2.9 + 3 = 21, f(3) = 2f(2) + 3 = 2.21 + 3 = 45,f(4) = 2f(3) + 3 = 2.45 + 3 = 93.Example 28: Give a recursive definition of Solution: The first and second part of the recursive definition are*Prepared by Dr. Zakir H. Ahmed*Methods of proving theorems: Recursive Definitions

    Prepared by Dr. Zakir H. Ahmed

  • Example 29: Give an inductive definition of S = {x: x is a multiple of 3}Solution: 3 Sx,y S x + y S x,y S x - y SNo other numbers are in S.Q 12: Find the Fibonacci numbers f(2), f(3), f(4), f(5), and f(6).Q 13: Give an inductive definition of an.*Prepared by Dr. Zakir H. Ahmed*Methods of proving theorems: Recursive Definitions

    Prepared by Dr. Zakir H. Ahmed

  • Recursively Defined Sets and Structures: Sets can be defined recursively. Recursive definition of sets have two part basis step and recursive step.Basis Step: An initial collection of elements is specified.Recursive Step: Rules for forming new elements in the set from those already known to be in the set are provided. Example 30: Consider subset S of the set of integers defined by Basis Step: 3 S.Recursive Step: If x, y S then x + y S .The new elements found to be in S are 3 by the basis step, 3+3=6 at the first application of the recursive step,3+6=6+3=9 at the second application of the recursive step,6+6=12 at the third application of the recursive step, and so on.We will show later that S is set of all positive multiples of 3.*Prepared by Dr. Zakir H. Ahmed*Methods of proving theorems: Recursive Definitions

    Prepared by Dr. Zakir H. Ahmed

  • Recursive Definitions:Recursively Defined Sets*Prepared by Dr. Zakir H. Ahmed*Example 31: The set of Natural Numbers N can be defined recursively as follows: 1N [basis step] If x N then x+1N [Recursive step]Lets try to constructs the set using this definition 1 in N [basis step]1+1=2 in N [Recursive step]2+1=3 in N [Recursive step]Etc

    Prepared by Dr. Zakir H. Ahmed

  • Algorithms*Prepared by Dr. Zakir H. Ahmed*

    Prepared by Dr. Zakir H. Ahmed

  • Algorithms:Definitions*Prepared by Dr. Zakir H. Ahmed*The word algorithm comes from the name of a Persian author, Abu Jafar Mohammad Ibn Musa Al Khowarizmi (825 AD).Definition: An algorithm is a finite set of instructions that, if followed, carries out a particular task. In addition, all algorithms must satisfy the following criteria:Input: Zero or more quantities are externally supplied.Output: At least one quantity is produced.Definiteness: Each instruction is clear and unambiguous.Finiteness: If we trace out the instructions of an algorithm, then for all cases, the algorithm terminates after a finite number of steps.Effectiveness: Every instruction must be very basic so that it can be carried out, in principle, by a person using only pen and paper. It must be feasible.

    Prepared by Dr. Zakir H. Ahmed

  • Study of Algorithms*Prepared by Dr. Zakir H. Ahmed*How to devise algorithms? Creating an algorithm is an art which may never be fully automated.How to validate algorithms?Once an algorithm is devised, it is necessary to show that it computes the correct answer for all possible legal inputs.A program can be written and then be verified.How to test a program?Debugging is the process of executing programs on sample data sets to determine whether the faulty results occur and, if so, to correct them.Profiling is the process of executing a correct program on data sets and measuring the time and space it takes to compute the results.

    Prepared by Dr. Zakir H. Ahmed

  • Pseudocode Conventions*Prepared by Dr. Zakir H. Ahmed*Comments begin with // and continue until the end of line.Blocks are indicated with matching braces: {and}.An Identifier begins with a letter: max. Assignment of values to variables is done using assignment statement : Variable:=expression. Logical operators: and, or and not are provided. Relational operators: provided. Elements of arrays are accessed using: [ and ]. While loop: while (condition) do{ statements;}

    Prepared by Dr. Zakir H. Ahmed

  • Pseudocode Conventions*Prepared by Dr. Zakir H. Ahmed*For loop: for variable:=value1 to value2 step step do{Statements;}Repeat-until loop: repeat{Statements;} until (condition)Conditional statement: if (condition) then (statement);if (condition) then (statement 1);else (statement 2);

    Prepared by Dr. Zakir H. Ahmed

  • Pseudocode Conventions*Prepared by Dr. Zakir H. Ahmed*Case statement: case{ : (condition 1): (statement 1);. : (condition n): (statement n); : else: (statement n+1);}Input and output are done using: read and write.There is only one type of procedure: Algorithm.An algorithm consists of a heading and a body.The heading of an algorithm takes the formAlgorithm Name ((parameter list))

    Prepared by Dr. Zakir H. Ahmed

  • Finding Maximum Value*Prepared by Dr. Zakir H. Ahmed*Input: A sequence of n numbers (a1, a2,, an).Output: Maximum of (a1, a2,, an).

    Algorithm Maximum(A, n) { Max:=A[1]; for i:=2 to n do { if ( Max < A[i]) then Max:=A[i]; } }

    Prepared by Dr. Zakir H. Ahmed

  • Sorting Problem*Prepared by Dr. Zakir H. Ahmed*Input: A sequence of n numbers (a1, a2,, an).Output: A permutation of n numbers (reordering) (a'1, a'2,, a'n) of the input sequence such that a'1 a'2 a'n.Bubble Sort:- It is a popular sorting algorithmIt swaps adjacent elements that are out of orderInsertion sort:-Insert an element to a sorted array such that the order of the resultant array be not changed.

    Prepared by Dr. Zakir H. Ahmed

  • Bubble Sort Algorithm*Prepared by Dr. Zakir H. Ahmed*Bubble sort: Algorithm BubbleSort (A, n) { for i:=1 to n-1 do { for j := n downto i+1 do { if ( A[j]
  • Bubble Sort Algorithm*Prepared by Dr. Zakir H. Ahmed*

    Prepared by Dr. Zakir H. Ahmed

  • Insertion Sort Algorithm*Prepared by Dr. Zakir H. Ahmed*Insertion sort: Algorithm InsertionSort (A, n){ for i:=2 to n do { key:=A[i]; // Insert A[i] into the sorted sequence A[1i-1]. j:=i-1; while ( (j>0) and (A[j]>key) ) do { A[j+1]:=A[j]; j:=j-1; } A[j+1]:=key; }}

    Prepared by Dr. Zakir H. Ahmed

  • End of Chapter III*Prepared by Dr. Zakir H. Ahmed*

    Prepared by Dr. Zakir H. Ahmed

    *Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed*Prepared by Dr. Zakir H. Ahmed