0% found this document useful (0 votes)
100 views22 pages

BITS Pilani BITS Pilani: CS/IS F214 Logic in Computer Science

This document discusses the concept of structural induction and provides examples of its application. It begins by defining structural induction and noting that it can be used to prove properties about structures that are built recursively from basic cases. It then provides two examples: 1) Proving that for any binary tree T, the number of nodes N(T) equals the number of links L(T) plus one. It identifies the base case and two inductive cases to prove the property by structural induction. 2) Proving that strings generated by a grammar having production rules S → e, S → (S), and S → SS will have matching parentheses. It presents the grammar and proof, identifying the base case and two inductive

Uploaded by

SHEFALI TRIPATHI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views22 pages

BITS Pilani BITS Pilani: CS/IS F214 Logic in Computer Science

This document discusses the concept of structural induction and provides examples of its application. It begins by defining structural induction and noting that it can be used to prove properties about structures that are built recursively from basic cases. It then provides two examples: 1) Proving that for any binary tree T, the number of nodes N(T) equals the number of links L(T) plus one. It identifies the base case and two inductive cases to prove the property by structural induction. 2) Proving that strings generated by a grammar having production rules S → e, S → (S), and S → SS will have matching parentheses. It presents the grammar and proof, identifying the base case and two inductive

Uploaded by

SHEFALI TRIPATHI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

CS/IS F214

Logic in Computer Science


BITS Pilani
Pilani Campus

MODULE: PROPOSITIONAL LOGIC


Proofs by Induction: Structural Induction:
•Motivating Example
•Proof Principle
•Relation to Mathematical Induction
•Example and Exercises
09-09-2017 Sundar B. CS&IS, BITS Pilani 0
09-09-2017

Structural Induction – Motivating Example


• Definition:
• A binary tree is a tree in which every node has at most
two children.
• Exercise:
• Using induction, prove that:
• For any binary tree T, N(T) = L(T) + 1
• where N(T) is the number of nodes in T and
• L(T) is the number of links / edges in T.
• Note that:
• A binary tree is structured as
• a root node and
• one or two sub-trees
Sundar B. CS&IS, BITS Pilani 1
09-09-2017

Structural Induction – Example Problem 1: Proof


• Induction Basis:
• Consider a binary tree T with only the root.
• N(T) = 1 and L(T) = 0
• Inductive Cases: There are two cases:
1. a binary tree T with a root, and one sub-tree T1 or
2. a binary tree T with a root, and two sub-trees T1 and T2

Sundar B. CS&IS, BITS Pilani 2


09-09-2017

Structural Induction – Example Problem 1: Proof [2]

• Inductive Case 1:
Consider a binary tree T with a root, and one sub-tree T1

• Induction Hypothesis:
• Assume that the property is true for (any given) sub-tree T1
• Induction Step:
• N(T) = N(T1) + 1 (+1 is for root node)
= L(T1) + 2 (by hypothesis)
• L(T) = L(T1) + 1
i.e.
• N(T) = L(T) + 1

Sundar B. CS&IS, BITS Pilani 3


09-09-2017

Structural Induction – Example Problem 1: Proof [3]

• Inductive Case 2:
Consider a binary tree T with a root, and two sub-trees T1 and T2

• Induction Hypothesis:
• Assume that the property is true for (any two given) sub-trees T1
and T2

• Induction Step:
• N(T) = N(T1) + N(T2) + 1 = L(T1) + L(T2) + 3 (by hypotheses)
• L(T) = L(T1) + L(T2) + 2
• i.e.
• N(T) = L(T) + 1

Sundar B. CS&IS, BITS Pilani 4


09-09-2017

Structural Induction – Proof Principle


• Proof Principle:
• Let P be a property applicable on a structure S that is constructed
out of:
• base (atomic) cases B0, B1, ... Bk
• inductive cases:
• R1(S1,0, S1,1, ... S1,r1)
• R2(S2,0, S2,1, ... S2,r2)
• ...
• Rm(Sm,0, Sm,1, ... Sm,rm)
• If
• P is true for each of the base cases B0, B1, ... Bk
• for each inductive case Rj :
P is true for Sj,0, Sj,1, ... Sj,rj implies P is true for Rj(Sj,0,Sj,1,... Sj,rj)
• then
• P is true for all S

Sundar B. CS&IS, BITS Pilani 5


09-09-2017

Structural Induction - Exercises

• Exercise:
1. Look back at the proof on binary trees:
• identify the property P
• identify the structure S with its bases cases and
recursive cases.
2. Argue that Mathematical Induction is a special case of
Structural Induction:
• express natural numbers as a structure

Sundar B. CS&IS, BITS Pilani 6


09-09-2017

Structural Induction – Proof Principle


• Proof Principle:
• Let P be a property applicable on a structure S that is constructed out
of:
• base (atomic) cases B0, B1, ... Bk
• inductive cases:
• R1(S1,0, S1,1, ... S1,r1)
• R2(S2,0, S2,1, ... S2,r2)
• ...
• Rm(Sm,0, Sm,1, ... Sm,rm)
• If
• P is true for each of the base cases B0, B1, ... Bk
• for each inductive case Rj :
P being true for Sj,0, Sj,1, ... Sj,rj implies P is true for Rj(Sj,0,Sj,1,... Sj,rj)
• then
• P is true for all S

Sundar B. CS&IS, BITS Pilani 7


09-09-2017

Structural Induction – Example 2 : Matching Parentheses

• A string is said to have matching parentheses if


• every left parenthesis is matched by a corresponding right
parenthesis occurring after it (somewhere to the right)
and
• every right parenthesis is matched by a corresponding left
parenthesis occurring before it (somewhere to the left)

• e.g.
• (((a) (b))) is a string with matching parentheses
• (a))( is not a string with matching parentheses

Sundar B. CS&IS, BITS Pilani 8


09-09-2017

Matching Parenthesis: Context

• Verifying whether a string has matching parentheses is often a


requirement
• e.g. when a program is compiled :
• a compiler verifies whether the program is
syntactically well-formed before it is translated

• For the sake of simplifying the discussion


• we will ignore all other characters – other than the left and
the right parentheses – and refer to strings of matching
parentheses

Sundar B. CS&IS, BITS Pilani 9


09-09-2017

Matching Parentheses - Grammar

• Given the following grammar (Gr-MP)


1. S ---> e
2. S ---> (S)
3. S ---> SS
prove that all strings generated by Gr-MP have matching
parentheses:
• Note: e denotes the empty string i.e. string of length 0.
• Exercise:
• Note that the definition is inductive:
• identify the base case and the inductive cases .
Sundar B. CS&IS, BITS Pilani 10
09-09-2017

Matching Parentheses: Proof by Structural Induction


Grammar (Gr-MP):
1. S ---> e
2. S ---> (S)
3. S ---> SS

Prove that all strings


generated by Gr-MP
have matching
parentheses.

Sundar B. CS&IS, BITS Pilani 11


09-09-2017

Matching Parentheses: Proof by Structural Induction


Grammar (Gr-MP): Proof:
1. S ---> e • Induction Basis:
2. S ---> (S) • S ---> e
3. S ---> SS • The empty string has
matching parentheses
Prove that all strings (trivially)
generated by Gr-MP • Induction Step:
have matching • Case 2: S ---> (S)
parentheses.
• Case 3: S ---> SS

Sundar B. CS&IS, BITS Pilani 12


09-09-2017

Matching Parentheses: Proof by Structural Induction (contd.)


Grammar (Gr-MP): Case 2: S1 ---> (S2)
1. S ---> e •Induction Hypothesis:
2. S ---> (S) • Assume S2 has matching parentheses.
3. S ---> SS •Induction Step:
• Then S1 generates ( S2 ) where
Prove that all strings • the left-most left-parenthesis
generated by Gr-MP matches with the right-most right-
have matching parenthesis and
parentheses.
• in the rest of the string i.e. in S2
• all parentheses have already
been matched (by hypothesis).

Sundar B. CS&IS, BITS Pilani 13


09-09-2017

Matching Parentheses: Proof by Structural Induction (contd.)


Grammar (Gr-MP): Case 3: S1 ---> S2 S3
1. S ---> e • Induction Hypothesis:
2. S ---> (S) • Assume S2 and S3 have matching
3. S ---> SS parentheses.
•Induction Step:
Prove that all strings • Then S1 generates S2 S3 where
generated by Gr-MP • all parentheses in substring S2
have matching have already been matched (by
parentheses. assumption) and
• all parentheses in substring S3
have already been matched (by
assumption)

Sundar B. CS&IS, BITS Pilani 14


09-09-2017

Matching Parentheses – Proof of correctness


• Exercise:
Prove that any string of matching parentheses can be
generated by the grammar Gr-MP.
[Hint: Prove by induction on the length of a string. End of
Hint.]

Sundar B. CS&IS, BITS Pilani 15


09-09-2017

Matching Parentheses – Generalization


Consider a generalized version of strings of matching parentheses:
where not just parentheses,
i. but braces (i.e. ‘{‘, and ‘}’), and brackets (i.e. ‘[‘, and ‘]’)
as well must be matched and
ii. they may be nested.
e.g.
• ((([]))){()[]} is well-formed but
• {(}) is not well-formed

Sundar B. CS&IS, BITS Pilani 16


09-09-2017

Matching Parentheses – Generalization


Exercise:
1. Define a grammar (say Gr-MPg) to generate/validate
strings of matching parentheses (according to the
generalized version in the previous slide).
2. Repeat the proofs of correctness i.e. argue that
i. any string generated by Gr-MPg has matching
parentheses and
ii. Gr-MPg generates all such strings

Sundar B. CS&IS, BITS Pilani 17


09-09-2017

Matching Parentheses – Generalization and Text


• Consider the addition of other characters (alphabetic,
numeric, and other punctuation / operator symbol) to strings
of matching parentheses (generalized to include braces and
brackets):
• i.e. (((a[i]+b)*c)/d){((*f)())[100]);} is a well-formed string
of matching parentheses.
• Exercises:
• Modify grammar Gr-MPg to grammar Gr-MPgt
• that generates (or recognizes) all well-formed strings
of matching parentheses (generalized) with other
characters allowed.
• Argue that the proofs on Gr-MPg (see Ex. 2(i) and 2(ii) in
the previous slide) are applicable for Gr-MPgt.

Sundar B. CS&IS, BITS Pilani 18


09-09-2017

Matching Parentheses – Program Syntax

• Provide valid examples of nesting of parentheses, braces,


and brackets inside each other in a C program:
• nesting of () inside {} and nesting of [] inside {}
• nesting of [] inside () and nesting of () inside []
• nesting of () inside () and nesting of [] inside []
• nesting of {} inside {}
• Question:
• Can {} ever be nested inside () or inside [] ?

Sundar B. CS&IS, BITS Pilani 19


09-09-2017

Matching Parentheses – Program Syntax - Exercises

• Exercise:
• Modify grammar Gr-MPgt to incorporate permissible nesting
in a program in C (or in your favorite language).

• Study a grammar specifying the syntax of a program in C (or in


your favorite language):
• In what ways positioning of text with respect to (generalized)
parentheses is restricted?
• For instance, is a sequence of the form ()[] possible
in a C program?
• , , , and  are strings not containing a left/right
parenthesis, a left/right bracket, or a left/right brace.
• What about a sequence of the form (){} ?
Sundar B. CS&IS, BITS Pilani 20
09-09-2017

Exercise – Grammar for DNF

• Prove that
• any formula generated by the grammar Gr-PropL-OE-3
(see Lecture 12) can be
• rewritten into an equivalent formula in canonical
form (say DNF)
• that can be generated by the grammar Gr-PropL-DNF
(see Lecture 13b).
• [Hint: Use structural induction on Gr-PropL-OE-3:
• Prove that --> can be eliminated
• Prove that the increasing order of precedence (, , and
) can be maintained i.e. parentheses are not needed.
End of Hint.]

Sundar B. CS&IS, BITS Pilani 21

You might also like