Proof Technique

advertisement
CS104 : Discrete Structures
Chapter III
Proof Techniques
4/9/2015
Prepared by Dr. Zakir H. Ahmed
1
Rules of Inference – Valid Arguments in
Propositional Logic
 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.”
4/9/2015
Prepared by Dr. Zakir H. Ahmed
2
Rules of Inference – Valid Arguments in
Propositional Logic
 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
p→q
p
q
where  is the symbol that denotes “therefore.”
 The statement ((p → q) ⌃p) → q is a tautology.
4/9/2015
Prepared by Dr. Zakir H. Ahmed
3
Rules of Inference – Rule for
Propositional Logic
 The argument form with premises p1, p2, …., pn and
conclusion q is valid, when (p1⌃p2⌃….⌃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.
4/9/2015
Prepared by Dr. Zakir H. Ahmed
4
Rules of Inference – Rules for
Propositional Logic
 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.
3
3
3
" If 2  , then ( 2 )    . We knowthat 2  .
 Q1: Determine whether
argument
given here is
2
2
2
the
9
3
valid and determine
Consequently, ( whether
2 )  2     ."its conclusion must be
4
2
true because of the validity of the argument.
2
2
2
2
4/9/2015
Prepared by Dr. Zakir H. Ahmed
5
Rules of Inference – Rules for
Propositional Logic
4/9/2015
Prepared by Dr. Zakir H. Ahmed
6
Rules of Inference – Rule for
Propositional Logic
 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 q
This 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”.
4/9/2015
Prepared by Dr. Zakir H. Ahmed
7
Introduction to Proofs
 A theorem is a statement that can be shown to be
true (usually important statement)
 Less important theorem sometimes are called
propositions
 A proof is a sequence of statements (valid argument)
to show that a theorem is true
 The 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 theorem
 Previously proven theorems
 Rules of inference used to draw conclusions and
to move from one step to another
4/9/2015
Prepared by Dr. Zakir H. Ahmed
8
Introduction to Proofs
Axioms
Hypothesis
proven theorems
Rules of
inference
New theorem
 A less important theorem that is helpful in the proof
of other results is called a lemma
 A corollary is a theorem that can be established
directly from a theorem that has been proved
 A conjuncture is a statement that is being proposed
to be a true statement, usually on the basis of some
partial evidence
 When a proof of a conjuncture is found, the
conjuncture becomes a theorem
4/9/2015
Prepared by Dr. Zakir H. Ahmed
9
Introduction to Proofs
 Example 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).
1.
2.
3.
4.
5.
6.
7.
8.
9.
4/9/2015
GF
F
G
GH
H C
G C
C
C M
M
Hypothesis
Hypothesis
Disjunctive syllogism rule using 1 and 2
Axiom 
Hypothesis
Hypo. syllogism using 4,5
Modus ponens using 3 and 6
Hypothesis
Modus ponens using 7 and 8
Prepared by Dr. Zakir H. Ahmed
10
Methods of proving theorems:
Direct proofs
 A direct proof of a conditional statement pq 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 true
 In 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 true
 Def: 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.
4/9/2015
Prepared by Dr. Zakir H. Ahmed
11
Methods of proving theorems:
Direct proofs
 Example 2: Use a direct proof to show that “if n is
even then n2 is even”
 Proof: 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 even
 Q 3: Use a direct proof to show that “if n is odd
then n2 is odd”
4/9/2015
Prepared by Dr. Zakir H. Ahmed
12
Methods of proving theorems:
Proof by contraposition
 An 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.
4/9/2015
Prepared by Dr. Zakir H. Ahmed
13
Methods of proving theorems:
Proof by contraposition
 Example 4: Use an indirect proof to show that “if
n2 is even then n is even”
 Proof: The contraposition is “if n is not even then
n2 is not even”
Assume 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 + 2k
Since r is integer (Axiom) => n2 is not even
 Q 4: Use an indirect proof to show that “if n is odd
then n2 is odd”
4/9/2015
Prepared by Dr. Zakir H. Ahmed
14
Methods of proving theorems:
Proof by contraposition
 Example 5: Prove that if n = ab then a  n or b  n
where a and b are positive integers
 Proof: Let p=“an”, q=“bn” and r=“n=ab”
We want to prove that r pq
=> The contraposition is (pq )   r (By
definition)
=> p  q   r (De Morgan’s law)
Now, assume that an and bn (p  q)
=> a.b  n.n = n (by multiplying above twos)
=> ab  n
=>  r
4/9/2015
Prepared by Dr. Zakir H. Ahmed
15
Methods of proving theorems:
Vacuous Proofs
 Vacuous Proofs: A conditional
p
q
pq
statement p  q is TRUE
F
F
T
if p is FALSE. If we can show
F
T
T
that p is False, then we have
T
F
F
a proof, called vacuous proof,
T
T
T
of the conditional statement p  q
 Example 6: Prove that if x2  0 then 1=2 where x is
a real number
 Proof: Since x2  0 for every real number then the
implication is vacuously true
 Example 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.
4/9/2015
Prepared by Dr. Zakir H. Ahmed
16
Methods of proving theorems:
Trivial Proofs
 Trivial Proofs: A conditional
p
q
pq
statement p  q is TRUE if q
F
F
T
is TRUE. If we can show that
F
T
T
T
F
F
q is TRUE, then we have a
T
T
T
proof, called trivial proof, of
the conditional statement p  q
 Example 8: Prove that if x=2 then x2  0 for all real
numbers
 Proof: Since x2  0 is true then the implication is
trivially true. (we didn’t use the fact x=2)
 Q 5: Use a trivial proof to show that if n > 1 then n2
≥ n for all integers
4/9/2015
Prepared by Dr. Zakir H. Ahmed
17
Methods of proving theorems:
Proofs by Contradiction



Proof 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
irrational
Proof: Let 2 is rational
=> 2 = a/b for some integers a and b (b0) (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
4/9/2015
Prepared by Dr. Zakir H. Ahmed
18
Methods of proving theorems:
Proofs by Contradiction






Why 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=2
Proof: Suppose that 21 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=b
Hence it follows that 1=2
Can you find the error 
4/9/2015
Prepared by Dr. Zakir H. Ahmed
19
Methods of proving theorems:
Proofs by Contradiction



To 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 odd
Proof: 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.
4/9/2015
Prepared by Dr. Zakir H. Ahmed
20
Methods of proving theorems:
Proofs of Equivalence
 Proofs of Equivalence: To prove p q we have to prove p  q
and q  p
 Example 12: prove “n is even if and only if n2 is even”
 “if n is even then n2 is even” proved in example 2
 “n2 is even then n is even” proved in example 4
 Therefore, n is even if and only if n2 is even
 Proving equivalence of several propositions: If we want to prove
that p1 p2  p3 …  pn
 Then it is sufficient to prove p1  p2,, p2  p3 … pn  p1
 Disproof by Counterexample
 Example 13: Prove that “For all real numbers x2 > x” is false
 Proof: X=0.5 is a counterexample since 0.52 > 0.5 is not true
 Q 6: Prove that “If n is not positive, then n2 is not positive” is
false
4/9/2015
Prepared by Dr. Zakir H. Ahmed
21
Methods of proving theorems:
Proof by Cases
 Proof by Cases: A proof by cases must cover all possible cases
that arise in a theorem. Each case may cover an infinite number
of instances
 Example 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 negative
 Common errors with exhaustive proof and proof by cases:
 Draw conclusion from non-exhaustive examples
 Not covering all possible cases
4/9/2015
Prepared by Dr. Zakir H. Ahmed
22
Methods of proving theorems:
Existence Proofs
 Many 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 true
 Example 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.
4/9/2015
Prepared by Dr. Zakir H. Ahmed
23
Methods of proving theorems:
Existence Proofs
 Example 17: Let x1,x2,..,xn be positive integers such that their
average is m. prove that there exists xi such that xi ≥ m
 Proof: Suppose that there is no such number, i,e.,
x1  m, x2  m, … , xn  m
By adding these inequalities we get: x1+ x2+…+ xn  nm
Dividing by n: (x1+ x2+…+ xn)/n  m
But 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.
4/9/2015
Prepared by Dr. Zakir H. Ahmed
24
Methods of proving theorems:
Uniqueness Proofs
 Some 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 then
3y+5 = 9 = 3z+5
So 3y = 3z
Dividing by 3 we get y = z
This proves that the solution is unique
4/9/2015
Prepared by Dr. Zakir H. Ahmed
25
Methods of proving theorems:
Mathematical Induction
 Principle 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 step
 Second part is an inductive step. Start by assuming P(k) is
true, and show that P(k+1) is also true
 The assumption that P(k) is true called the inductive
hypothesis
 So, we prove that
(P(1)  k (P(k)  P(k+1)))  (n P(n))
4/9/2015
Prepared by Dr. Zakir H. Ahmed
26
Methods of proving theorems:
Mathematical Induction
 Example 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:
1. We can reach the first rung of the ladder
2. If 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 works
 Statements (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
4/9/2015
Prepared by Dr. Zakir H. Ahmed
27
Methods of proving theorems:
Mathematical Induction
 Example 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)/2
Inductive step: Let us assume that it is true for n = k, that is,
1+2+3+…..+k = k(k+1)/2
Now, 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.
4/9/2015
Prepared by Dr. Zakir H. Ahmed
28
Methods of proving theorems:
Mathematical Induction
 Example 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) = k2
Now, 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.
4/9/2015
Prepared by Dr. Zakir H. Ahmed
29
Methods of proving theorems:
Mathematical Induction
 Q 8: Use induction to prove that
11! + 22! + … + nn! = (n+1)! - 1, n
 Q 9: Use induction to prove that for all n,
n(n  1)(2n  1)
k 
6
k 1
n
2
 Q 10: Use induction to prove that for all n,
n 1
a
(
r
 1)
i
ar 
, when r  1.

r 1
i 0
n
4/9/2015
Prepared by Dr. Zakir H. Ahmed
30
Methods of proving theorems:
Mathematical Induction
 Example 22: Use induction to prove the inequality n < 2n, n > 0.
 Proof: Let P(n) be the proposition that n < 2n
Basic step: P(1) is true, because 1 < 21 = 2.
Inductive step: Assume P(k) is true, that is, k < 2K
Now, 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.
4/9/2015
Prepared by Dr. Zakir H. Ahmed
31
Methods of proving theorems:
Recursive Definitions
 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  1
But equivalently, we could define it like this:
n  (n  1)!, if n  1
n! 
ifn  1
1,
4/9/2015
Prepared by Dr. Zakir H. Ahmed
Recursive Step
Base Step
32
Methods of proving theorems:
Recursive Definitions
 Example 26: The definition of Fibonacci Numbers:
0
nif  0

f (n)  1
nif 1
f (n  1)  f (n  2) if n  1

Base Step
Recursive Step
Is there a non-recursive definition for
the Fibonacci Numbers?

n
n 




1  1  5
1  5 
f (n) 

  

5 
 2   2  

4/9/2015

Prepared by Dr. Zakir H. Ahmed
33
Methods of proving theorems:
Recursive Definitions
 Example 27: Suppose that f is defined by
f(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 that
f(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.
n
ak

 Example 28: Give a recursive definition of
k 0
 Solution: The first and second part of the recursive definition are
0
a
k 0
k
Base Step
 a0
 n 1

a

a



k
k   an
k 0
 k 0

n
4/9/2015
Recursive Step
Prepared by Dr. Zakir H. Ahmed
34
Methods of proving theorems:
Recursive Definitions
 Example 29: Give an inductive definition of S = {x: x is a
multiple of 3}
 Solution:
Base Step
1. 3  S
2. x,y  S  x + y  S
Recursive Step
3. x,y  S  x - y  S
4. No 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.
4/9/2015
Prepared by Dr. Zakir H. Ahmed
35
Methods of proving theorems:
Recursive Definitions
 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.
4/9/2015
Prepared by Dr. Zakir H. Ahmed
36
Recursive Definitions:
Recursively Defined Sets
Example 31: The set of Natural Numbers N can be defined
recursively as follows:
1N
[basis step]
If x N then x+1N
[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…

4/9/2015
Prepared by Dr. Zakir H. Ahmed
37
Algorithms
4/9/2015
Prepared by Dr. Zakir H. Ahmed
38
Algorithms:
Definitions


The word ‘algorithm’ comes from the name of a Persian author,
Abu Ja’far 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.
4/9/2015
Prepared by Dr. Zakir H. Ahmed
39
Study of Algorithms
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.
4/9/2015
Prepared by Dr. Zakir H. Ahmed
40

Pseudocode Conventions








4/9/2015
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: <, ≤, =, ≠, ≥ and > provided.
Elements of arrays are accessed using: [ and ].
While loop:
while (condition) do
{
statements;
}
Prepared by Dr. Zakir H. Ahmed
41
Pseudocode Conventions

For loop:
for variable:=value1 to value2 step step do
{
Statements;
}

Repeat-until loop:
repeat
{
Statements;
} until (condition)

Conditional statement:


4/9/2015
if (condition) then (statement);
if (condition) then (statement 1);
else (statement 2);
Prepared by Dr. Zakir H. Ahmed
42
Pseudocode Conventions





4/9/2015
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 form
Algorithm Name ((parameter list))
Prepared by Dr. Zakir H. Ahmed
43
Finding Maximum Value
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];
}
}
4/9/2015
Prepared by Dr. Zakir H. Ahmed
44
Sorting Problem




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 algorithm
 It swaps adjacent elements that are out of order
Insertion sort: Insert an element to a sorted array such that the order of
the resultant array be not changed.
4/9/2015
Prepared by Dr. Zakir H. Ahmed
45
Bubble Sort Algorithm

Bubble sort:
Algorithm BubbleSort (A, n)
{
for i:=1 to n-1 do
{
for j := n downto i+1 do
{
if ( A[j]<A[j-1]) then
exchange A[j] ↔ A[j-1];
}
}
}
4/9/2015
Prepared by Dr. Zakir H. Ahmed
46
Bubble Sort Algorithm
4/9/2015
Prepared by Dr. Zakir H. Ahmed
47
Insertion Sort Algorithm

Insertion sort:
Algorithm InsertionSort (A, n)
{ for i:=2 to n do
{ key:=A[i];
// Insert A[i] into the sorted sequence A[1…i-1].
j:=i-1;
while ( (j>0) and (A[j]>key) ) do
{ A[j+1]:=A[j];
j:=j-1;
}
A[j+1]:=key;
}
}
4/9/2015
Prepared by Dr. Zakir H. Ahmed
48
End of Chapter III
4/9/2015
Prepared by Dr. Zakir H. Ahmed
49
Download