Sei sulla pagina 1di 61

Theory of Computation(contd)

Last Minute Notes Theory of Computation


Computer Organization and Architecture(contd)
Compiler Design (contd)
Digital Electronics(Contd) Last Minute Preparation
Discrete Mathematics(Contd)
Computer Network
Last minute notes C/C++
LMNs- Algorithm
LMN-DATA STRUCTURES

Theory of Computation(contd)
Theory of Computation | Pumping Lemma
There are two Pumping Lemmas, which are defined for
1. Regular Languages, and
2. Context Free Languages
Pumping Lemma for Regular Languages
For any regular language L, there exists an integer n, such that for all x L with |x| n, there
exists u, v, w , such that x = uvw, and
(1) |uv| n
(2) |v| 1
(3) for all i 0: uviw L
In simple terms, this means that if a string v is pumped, i.e., if v is inserted any number of
times, the resultant string still remains in L.
Pumping Lemma is used as a proof for irregularity of a language. Thus, if a language is regular,
it always satisfies pumping lemma. If there exists at least one string made from pumping which
is not in L, then L is surely not regular.
The opposite of this may not always be true. That is, if Pumping Lemma holds, it does not mean
that the language is regular.

For example, let us prove L01 = {0n1n | n 0} is irregular.


Let us assume that L is regular, then by Pumping Lemma the above given rules follow.
Now, let x L and |x| n. So, by Pumping Lemma, there exists u, v, w such that (1) (3) hold.
We show that for all u, v, w, (1) (3) does not hold.
If (1) and (2) hold then x = 0n1n = uvw with |uv| n and |v| 1.
So, u = 0a, v = 0b, w = 0c1n where : a + b n, b 1, c 0, a + b + c = n
But, then (3) fails for i = 0
uv0w = uw = 0a0c1n = 0a + c1n L, since a + c n.

Pumping Lemma for Context-free Languages (CFL)


Pumping Lemma for CFL states that for any Context Free Language L, it is possible to find two
substrings that can be pumped any number of times and still be in the same language. For any
language L, we break its strings into five parts and pump second and fourth substring.
Pumping Lemma, here also, is used as a tool to prove that a language is not CFL. Because, if any
one string does not satisfy its conditions, then the language is not CFL.
Thus, if L is a CFL, there exists an integer n, such that for all x L with |x| n, there exists u, v,
w, x, y , such that x = uvwxy, and
(1) |vwx| n
(2) |vx| 1
(3) for all i 0: uviwxiy L

For above example, 0n1n is CFL, as any string can be the result of pumping at two places, one for
0 and other for 1.
Let us prove, L012 = {0n1n2n | n 0} is not Context-free.
Let us assume that L is Context-free, then by Pumping Lemma, the above given rules follow.
Now, let x L and |x| n. So, by Pumping Lemma, there exists u, v, w, x, y such that (1) (3)
hold.
We show that for all u, v, w, x, y (1) (3) do not hold.
If (1) and (2) hold then x = 0n1n2n = uvwxy with |vwx| n and |vx| 1.
(1) tells us that vwx does not contain both 0 and 2. Thus, either vwx has no 0s, or vwx has no
2s. Thus, we have two cases to consider.
Suppose vwx has no 0s. By (2), vx contains a 1 or a 2. Thus uwy has n 0s and uwy either has
less than n 1s or has less than n 2s.
But (3) tells us that uwy = uv0wx0y L.
So, uwy has an equal number of 0s, 1s and 2s gives us a contradiction. The case where vwx
has no 2s is similar and also gives us a contradiction. Thus L is not context-free.

Source : John E. Hopcroft, Rajeev Motwani, Jeffrey D. Ullman (2003). Introduction to Automata
Theory, Languages, and Computation.

This article has been contributed by Nirupam Singh.


Please write comments if you find anything incorrect, or you want to share more information
about the topic discussed above
See GATE Corner for all information about GATE CS and Quiz Corner for all Quizzes on
GeeksQuiz.

Designing Finite Automata from Regular Expression


In this article, we will see some popular regular expressions and how we can convert them to
finite automata.

Even number of as : The regular expression for even number of as is (b|ab*ab*)*. We can
construct a finite automata as shown in Figure 1.

The above automata will accept all strings which have even number of as. For zero as, it
will be in q0 which is final state. For one a, it will go from q0 to q1 and the string will
not be accepted. For two as at any positions, it will go from q0 to q1 for 1st a and q1 to
q0 for second a. So, it will accept all strings with even number of as.

String with ab as substring : The regular expression for strings with ab as substring is
(a|b)*ab(a|b)*. We can construct finite automata as shown in Figure 2.

The above automata will accept all string which have ab as substring. The automata will
remain in initial state q0 for bs. It will move to q1 after reading a and remain in same
state for all a afterwards. Then it will move to q2 if b is read. That means, the string
has read ab as substring if it reaches q2.

String with count of a divisible by 3 : The regular expression for strings with count of a divisible
by 3 is {a3n | n >= 0}. We can construct automata as shown in Figure 3.

The above automata will accept all string of form a3n. The automata will remain in initial
state q0 for and it will be accepted. For string aaa, it will move from q0 to q1 then q1
to q2 and then q2 to q0. For every set of three as, it will come to q0, hence accepted.
Otherwise, it will be in q1 or q2, hence rejected.
Note : If we want to design a finite automata with number of as as 3n+1, same automata
can be used with final state as q1 instead of q0.
If we want to design a finite automata with language {akn | n >= 0}, k states are required.
We have used k = 3 in our example.

Binary numbers divisible by 3 : The regular expression for binary numbers which are divisible by
three is (0|1(01*0)*1)*. The examples of binary number divisible by 3 are 0, 011, 110, 1001,
1100, 1111, 10010 etc. The DFA corresponding to binary number divisible by 3 can be shown in
Figure 4.

The above automata will accept all binary numbers divisible by 3. For 1001, the automata
will go from q0 to q1, then q1 to q2, then q2 to q1 and finally q2 to q0, hence accepted.
For 0111, the automata will go from q0 to q0, then q0 to q1, then q1 to q0 and finally q0
to q1, hence rejected.

String with regular expression (111 + 11111)* : The string accepted using this regular expression
will have 3, 5, 6(111 twice), 8 (11111 once and 111 once), 9 (111 thrice), 10 (11111 twice) and all
other counts of 1 afterwards. The DFA corresponding to given regular expression is given in
Figure 5.

Question : What will be the minimum number of states for strings with odd number of as?
Solution : The regular expression for odd number of a is b*ab*(ab*ab*)* and corresponding
automata is given in Figure 6 and minimum number of states are 2.

This article has been contributed by Sonal Tuteja.


Please write comments if you find anything incorrect, or you want to share more information
about the topic discussed above
See GATE Corner for all information about GATE CS and Quiz Corner for all Quizzes on GeeksQuiz.

Regular Expressions, Regular Grammar and Regular


Languages
As discussed in Chomsky Hierarchy, Regular Languages are the most restricted types of
languages and are accepted by finite automata.
Regular Expressions
Regular Expressions are used to denote regular languages. An expression is regular if:

is a regular expression for regular language .


is a regular expression for regular language {}.
If a ( represents the input alphabet), a is regular expression with language {a}.
If a and b are regular expression, a + b is also a regular expression with language {a,b}.
If a and b are regular expression, ab (concatenation of a and b) is also regular.
If a is regular expression, a* (0 or more times a) is also regular.

Regular Grammar : A grammar is regular if it has rules of form A -> a or A -> aB or A


-> where is a special symbol called NULL.
Regular Languages : A language is regular if it can be expressed in terms of regular
expression.
Closure Properties of Regular Languages
Union : If L1 and If L2 are two regular languages, their union L1 L2 will also be
regular. For example, L1 = {an | n 0} and L2 = {bn | n 0}
L3 = L1 L2 = {an bn | n 0} is also regular.
Intersection : If L1 and If L2 are two regular languages, their intersection L1 L2 will
also be regular. For example,
L1= {am bn | n 0 and m 0} and L2= {am bn bn am | n 0 and m 0}
L3 = L1 L2 = {am bn | n 0 and m 0} is also regular.
Concatenation : If L1 and If L2 are two regular languages, their concatenation L1.L2
will also be regular. For example,
L1 = {an | n 0} and L2 = {bn | n 0}
L3 = L1.L2 = {am . bn | m 0 and n 0} is also regular.
Kleene Closure : If L1 is a regular language, its Kleene closure L1* will also be regular.
For example,

L1 = (a b)
L1* = (a b)*
Complement : If L(G) is regular language, its complement L(G) will also be regular.
Complement of a language can be found by subtracting strings which are in L(G) from all
possible strings. For example,
L(G) = {an | n > 3}
L(G) = {an | n <= 3}
Note : Two regular expressions are equivalent if languages generated by them are same.
For example, (a+b*)* and (a+b)* generate same language. Every string which is
generated by (a+b*)* is also generated by (a+b)* and vice versa.
How to solve problems on regular expression and regular languages?

Question 1 : Which one of the following languages over the alphabet {0,1} is described
by the regular expression?
(0+1)*0(0+1)*0(0+1)*
(A) The set of all strings containing the substring 00.
(B) The set of all strings containing at most two 0s.
(C) The set of all strings containing at least two 0s.
(D) The set of all strings that begin and end with either 0 or 1.
Solution : Option A says that it must have substring 00. But 10101 is also a part of
language but it does not contain 00 as substring. So it is not correct option.
Option B says that it can have maximum two 0s but 00000 is also a part of language. So
it is not correct option.
Option C says that it must contain atleast two 0. In regular expression, two 0 are present.
So this is correct option.
Option D says that it contains all strings that begin and end with either 0 or 1. But it can
generate strings which start with 0 and end with 1 or vice versa as well. So it is not
correct.
Question 2 : Which of the following languages is generated by given grammar?
S -> aS | bS |
(A) {an bm | n,m 0}
(B) {w {a,b}* | w has equal number of as and bs}
(C) {an | n 0} {bn | n 0} {an bn | n 0}
(D) {a,b}*
Solution : Option (A) says that it will have 0 or more a followed by 0 or more b. But S ->
bS => baS => ba is also a part of language. So (A) is not correct.
Option (B) says that it will have equal no. of as and bs. But But S -> bS => b is also a
part of language. So (B) is not correct.
Option (C) says either it will have 0 or more as or 0 or more bs or as followed by bs.
But as shown in option (A), ba is also part of language. So (C) is not correct.
Option (D) says it can have any number of as and any numbers of bs in any order. So
(D) is correct.

Question 3 : The regular expression 0*(10*)* denotes the same set as


(A) (1*0)*1*
(B) 0 + (0 + 10)*
(C) (0 + 1)* 10(0 + 1)*
(D) none of these
Solution : Two regular expressions are equivalent if languages generated by them are
same.
Option (A) can generate 101 but 0*(10*)* cannot. So they are not equivalent.
Option (B) can generate 0100 but 0*(10*)* cannot. So they are not equivalent.
Option (C) will have 10 as substring but 0*(10*)* may or may not. So they are not
equivalent.
This article has been contributed by Sonal Tuteja.
Please write comments if you find anything incorrect, or you want to share more
information about the topic discussed above
See GATE Corner for all information about GATE CS and Quiz Corner for all Quizzes on
GeeksQuiz.

Theory of Computation | Pushdown Automata


We have already discussed finite automata. But finite automata can be used to accept only
regular languages.
Pushdown Automata is a finite automata with extra memory called stack which helps Pushdown
automata to recognize Context Free Languages.
A Pushdown Automata (PDA) < Q, , , , q0, Z, F > can be defined as :

Q is the set of states


is the set of input symbols
is the set of pushdown symbols (which can be pushed and popped from stack)
q0 is the initial state
Z is the initial pushdown symbol (which is initially present in stack)
F is the set of final states
is a transition function which maps Q x { } x into Q x *. In a given state,
PDA will read input symbol and stack symbol (top of the stack) and move to a new state
and change the symbol of stack.

Example : Define the pushdown automata for language {anbn | n > 0}


Solution : M = < Q, , , , q0, Z, F > where Q = { q0, q1 } and = { a, b } and = { A, Z }
and is given by :
( q0, a, Z ) = { ( q0, AZ ) }
( q0, a, A) = { ( q0, AA ) }
( q0, b, A) = { ( q1, ) }
( q1, b, A) = { ( q1, ) }
( q1, , Z) = { ( q1, ) }
Let us see how this automata works for aaabbb.

Explanation : Initially, the state of automata is q0 and symbol on stack is Z and the input is
aaabbb as shown in row 1. On reading a (shown in bold in row 2), the state will remain q0 and
it will push symbol A on stack. On next a (shown in row 3), it will push another symbol A on
stack. After reading 3 as, the stack will be AAAZ with A on the top. After reading b (as shown
in row 5), it will pop A and move to state q1 and stack will be AAZ. When all bs are read, the
state will be q1 and stack will be Z. In row 8, on input symbol and Z on stack, it will pop Z
and stack will be empty. This type of acceptance is known as acceptance by empty stack.
Note :

The above pushdown automaton is deterministic in nature because there is only one move
from a state on an input symbol and stack symbol.

The non-deterministic pushdown automata can have more than one move from a state on
an input symbol and stack symbol.
It is not always possible to convert non-deterministic pushdown automata to deterministic
pushdown automata.
Power of non-deterministic PDA is more as compared to deterministic PDA as some
languages which are accepted by NPDA but not by deterministic PDA which will be
discussed in next article.
The push down automata can either be implemented using empty stack or by final state
and one can be converted to another.

Question : Which of the following pairs have DIFFERENT expressive power?


A. Deterministic finite automata(DFA) and Non-deterministic finite automata(NFA)
B. Deterministic push down automata(DPDA)and Non-deterministic push down
automata(NPDA)
C. Deterministic single-tape Turing machine and Non-deterministic single-tape Turing machine
D. Single-tape Turing machine and multi-tape Turing machine
Solution : Every NFA can be converted into DFA. So, there expressive power is same. As
discussed above, every NPDA cant be converted to DPDA. So, the power of NPDA and DPDA
is not same. Hence option (B) is correct.
This article has been contributed by Sonal Tuteja.
Please write comments if you find anything incorrect, or you want to share more information
about the topic discussed above
See GATE Corner for all information about GATE CS and Quiz Corner for all Quizzes on
GeeksQuiz.

Theory of Computation | Closure Properties of Context Free


Languages
Context Free languages are accepted by pushdown automata but not by finite automata. Context
free languages can be generated by context free grammar which has the form :
A -> (where A N and (T N)* and N is a non-terminal and T is a terminal)
Properties of Context Free Languages
Union : If L1 and If L2 are two context free languages, their union L1 L2 will also be context

free. For example,


L1 = { anbncm | m >= 0 and n >= 0 } and L2 = { anbmcm | n >= 0 and m >= 0 }
L3 = L1 L2 = { anbncm anbmcm | n >= 0, m >= 0 } is also context free.
L1 says number of as should be equal to number of bs and L2 says number of bs should be
equal to number of cs. Their union says either of two conditions to be true. So it is also context
free language.
Concatenation : If L1 and If L2 are two context free languages, their concatenation L1.L2 will
also be context free. For example,
L1 = { anbn | n >= 0 } and L2 = { cmdm | m >= 0 }
L3 = L1.L2 = { anbncmdm | m >= 0 and n >= 0} is also context free.
L1 says number of as should be equal to number of bs and L2 says number of cs should be
equal to number of ds. Their concatenation says first number of as should be equal to number
of bs, then number of cs should be equal to number of ds. So, we can create a PDA which will
first push for as, pop for bs, push for cs then pop for ds. So it can be accepted by pushdown
automata, hence context free.
Kleene Closure : If L1 is context free, its Kleene closure L1* will also be context free. For
example,
L1 = { anbn | n >= 0 }
L1* = { anbn | n >= 0 }* is also context free.
Intersection and complementation : If L1 and If L2 are two context free languages, their
intersection L1 L2 need not be context free. For example,
L1 = { anbncm | n >= 0 and m >= 0 } and L2 = (ambncn | n >= 0 and m >= 0 }
L3 = L1 L2 = { anbncn | n >= 0 } need not be context free.
L1 says number of as should be equal to number of bs and L2 says number of bs should be
equal to number of cs. Their intersection says both conditions need to be true, but push down
automata can compare only two. So it cannot be accepted by pushdown automata, hence not
context free.
Similarly, complementation of context free language L1 which is * L1, need not be context
free.
Deterministic Context-free Languages
Deterministic CFL are subset of CFL which can be recognized by Deterministic PDA.
Deterministic PDA has only one move from a given state and input symbol. For example, L1= {
anbncm | m >= 0 and n >= 0} is a DCFL because for as, we can push on stack and for bs we can
pop. It can be recognized by Deterministic PDA. On the other hand, L3 = { anbncm anbmcm | n
>= 0, m >= 0 } cannot be recognized by DPDA because either number of as and bs can be
equal or either number of bs and cs can be equal. So, it can only be implemented by NPDA.
Thus, it is CFL but not DCFL.
Note : Out of union, concatenation, complementation, intersection and kleene-closure, DCFL are
closed only under complementation.
Question : Consider the language L1,L2,L3 as given below.
L1 = { ambn | m, n >= 0 }

L2 = { anbn | n >= 0 }
L3 = { anbncn | n >= 0 }
Which of the following statements is NOT TRUE?
A. Push Down Automata (PDA) can be used to recognize L1 and L2
B. L1 is a regular language
C. All the three languages are context free
D. Turing machine can be used to recognize all the three languages
Solution : Option (A) says PDA can be used to recognize L1 and L2. L1 contains all strings with
any no. of a followed by any no. of b. So, it can be accepted by PDA. L2 contains strings with n
no. of as followed by n no. of bs. It can also be accepted by PDA. So, option (A) is correct.
Option (B) says that L1 is regular. It is true as regular expression for L1 is a*b*.
Option (C) says L1, L2 and L3 are context free. L3 languages contains all strings with n no. of
as followed by n no. of bs followed by n no. of cs. But it cant be accepted by PDA. So option
( C) is not correct.
Question : The language L = { 0i12i | i 0 } over the alphabet {0, 1, 2} is :
A. Not recursive
B. Is recursive and deterministic CFL
C. Is regular
D. Is CFL bot not deterministic CFL.
Solution : The above language is deterministic CFL as for 0s, we can push 0 on stack and for
2s we can pop corresponding 0s. As there is no ambiguity which moves to take, it is
deterministic. So, correct option is (B). As CFL is subset of recursive, it is recursive as well.
Question : Consider the following languages:
L1 = { 0n1n| n0 }
L2 = { wcwr | w {a,b}* }
L3 = { wwr | w {a,b}* }
Which of these languages are deterministic context-free languages?
A. None of the languages
B. Only L1
C. Only L1 and L2
D. All three languages
Solution : Languages L1 contains all strings in which n 0s are followed by n 1s. Deterministic
PDA can be constructed to accept L1. For 0s we can push it on stack and for 1s, we can pop
from stack. Hence, it is DCFL.
L2 contains all strings of form wcwr where w is a string of as and bs and wr is reverse of w.
For example, aabbcbbaa. To accept this language, we can construct PDA which will push all
symbols on stack before c. After c, if symbol on input string matches with symbol on stack, it is
popped. So, L2 can also be accepted with deterministic PDA, hence it is also DCFL.
L3 contains all strings of form wwr where w is a string of as and bs and wr is reverse of w. But
we dont know where w ends and wr starts. e.g.; aabbaa is a string corresponding to L3. For first
a, we will push it on stack. Next a can be either part of w or wr where w=a. So, there can be

multiple moves from a state on an input symbol. So, only non-deterministic PDA can be used to
accept this type of language. Hence, it is NCFL not DCFL.
So, correct option is (C). Only, L1 and L2 are DCFL.
Question : Which one of the following grammars generate the language L = { aibj | i j }
S -> AC | CB, C -> aCb | a | b, A -> aA | , B -> Bb |
S -> aS | Sb | a | b
S -> AC | CB, C -> aCb | , A -> aA | , B -> Bb |
S -> AC | CB, C -> aCb | , A -> aA | a, B -> Bb | b
Solution : The best way to solve these type of questions is to eliminate options which do not
satisfy conditions. The conditions for language L is no. of as and no. of bs should be unequal.
In option (B), S => aS => ab. It can generate strings with equal as and bs. So, this option is
incorrect.
In option (C), S => AC => C => . In , as and bs are equal (0), so it is not correct option.
In option (A), S will be replaced by either AC or CB. C will either generate no. of as more than
no. of bs by 1 or no. of bs more than no. of as by 1. But one more a or one more b can be
compensated by B -> bB | or A -> aA | respectively. So it may give strings with equal no. of
as and bs. So, it is not a correct option.
In option (D), S will be replaced by either AC or CB. C will always generate equal no. of as and
bs. If we replace S by AC, A with add atleast one extra a. and if we replace S by CB, B will add
atleast one extra b. So this grammar will never generate equal no. of as and bs. So, option (D)
is correct.
This article has been contributed by Sonal Tuteja.
Please write comments if you find anything incorrect, or you want to share more information
about the topic discussed above
See GATE Corner for all information about GATE CS and Quiz Corner for all Quizzes on GeeksQuiz

Theory of Computation | Conversion from NFA to DFA


An NFA can have zero, one or more than one move from a given state on a given input symbol.
An NFA can also have NULL moves (moves without input symbol). On the other hand, DFA has
one and only one move from a given state on a given input symbol.
Conversion from NFA to DFA
Suppose there is an NFA N < Q, , q0, , F > which recognizes a language L. Then the DFA D
< Q, , q0, , F > can be constructed for language L as:
Step 1: Initially Q = .
Step 2: Add q0 to Q.
Step 3: For each state in Q, find the possible set of states for each input symbol using transition

function of NFA. If this set of states is not in Q, add it to Q.


Step 4: Final state of DFA will be all states with contain F (final states of NFA)
Example
Consider the following NFA shown in Figure 1.

Following are the various parameters for NFA.


Q = { q0, q1, q2 }
= ( a, b )
F = { q2 }
(Transition Function of NFA)

Step 1: Q =
Step 2: Q = {q0}
Step 3: For each state in Q, find the states for each input symbol.
Currently, state in Q is q0, find moves from q0 on input symbol a and b using transition function
of NFA and update the transition table of DFA.
(Transition Function of DFA)

Now { q0, q1 } will be considered as a single state. As its entry is not in Q, add it to Q.
So Q = { q0, { q0, q1 } }
Now, moves from state { q0, q1 } on different input symbols are not present in transition table of
DFA, we will calculate it like:
( { q0, q1 }, a ) = ( q0, a ) ( q1, a ) = { q0, q1 }
( { q0, q1 }, b ) = ( q0, b ) ( q1, b ) = { q0, q2 }
Now we will update the transition table of DFA.

(Transition Function of DFA)

Now { q0, q2 } will be considered as a single state. As its entry is not in Q, add it to Q.
So Q = { q0, { q0, q1 }, { q0, q2 } }
Now, moves from state {q0, q2} on different input symbols are not present in transition table of
DFA, we will calculate it like:
( { q0, q2 }, a ) = ( q0, a ) ( q2, a ) = { q0, q1 }
( { q0, q2 }, b ) = ( q0, b ) ( q2, b ) = { q0 }
Now we will update the transition table of DFA.
(Transition Function of DFA)

As there is no new state generated, we are done with the conversion. Final state of DFA will be
state which has q2 as its component i.e., { q0, q2 }
Following are the various parameters for DFA.
Q = { q0, { q0, q1 }, { q0, q2 } }
= ( a, b )
F = { { q0, q2 } } and transition function as shown above. The final DFA for above NFA has
been shown in Figure 2.

Note : Sometimes, it is not easy to convert regular expression to DFA. First you can convert
regular expression to NFA and then NFA to DFA.

Question : The number of states in the minimal deterministic finite automaton corresponding to
the regular expression (0 + 1)* (10) is ____________.
Solution : First, we will make an NFA for the above expression. To make an NFA for (0 + 1)*,
NFA will be in same state q0 on input symbol 0 or 1. Then for concatenation, we will add two
moves (q0 to q1 for 1 and q1 to q2 for 0) as shown in Figure 3.

This article has been contributed by Sonal Tuteja.


Please write comments if you find anything incorrect, or you want to share more information
about the topic discussed above
See GATE Corner for all information about GATE CS and Quiz Corner for all Quizzes on GeeksQuiz.

Theory of Computation | Minimization of DFA


DFA minimization stands for converting a given DFA to its equivalent DFA with minimum
number of states.
Minimization of DFA
Suppose there is a DFA D < Q, , q0, , F > which recognizes a language L. Then the minimized
DFA D < Q, , q0, , F > can be constructed for language L as:
Step 1: We will divide Q (set of states) into two sets. One set will contain all final states and
other set will contain non-final states. This partition is called P0.
Step 2: Initialize k = 1
Step 3: Find Pk by partitioning the different sets of Pk-1. In each set of Pk-1, we will take all
possible pair of states. If two states of a set are distinguishable, we will split the sets into
different sets in Pk.
Step 4: Stop when Pk = Pk-1 (No change in partition)
Step 5: All states of one set are merged into one. No. of states in minimized DFA will be equal to
no. of sets in Pk.
How to find whether two states in partition Pk are distinguishable ?
Two states ( qi, qj ) are distinguishable in partition Pk if for any input symbol a, ( qi, a ) and (
qj, a ) are in different sets in partition Pk-1.
Example
Consider the following DFA shown in figure.

Step 1. P0 will have two sets of states. One set will contain q1, q2, q4 which are final states of
DFA and another set will contain remaining states. So P0 = { { q1, q2, q4 }, { q0, q3, q5 } }.
Step 2. To calculate P1, we will check whether sets of partition P0 can be partitioned or not:
For set { q1, q2, q4 } :
( q1, 0 ) = ( q2, 0 ) = q2 and ( q1, 1 ) = ( q2, 1 ) = q5, So q1 and q2 are not

distinguishable.
Similarly, ( q1, 0 ) = ( q4, 0 ) = q2 and ( q1, 1 ) = ( q4, 1 ) = q5, So q1 and q4 are not
distinguishable.
Since, q1 and q2 are not distinguishable and q1 and q4 are also not distinguishable, So q2 and q4
are not distinguishable. So, { q1, q2, q4 } set will not be partitioned in P1.
For set { q0, q3, q5 } :
( q0, 0 ) = q3 and ( q3, 0 ) = q0
( q0, 1) = q1 and ( q3, 1 ) = q4
Moves of q0 and q3 on input symbol 0 are q3 and q0 respectively which are in same set in
partition P0. Similarly, Moves of q0 and q3 on input symbol 1 are q3 and q0 which are in same
set in partition P0. So, q0 and q3 are not distinguishable.
( q0, 0 ) = q3 and ( q5, 0 ) = q5 and ( q0, 1 ) = q1 and ( q5, 1 ) = q5
Moves of q0 and q5 on input symbol 0 are q3 and q5 respectively which are in different set in
partition P0. So, q0 and q5 are distinguishable. So, set { q0, q3, q5 } will be partitioned into { q0,
q3 } and { q5 }. So,
P1 = { { q1, q2, q4 }, { q0, q3}, { q5 } }
To calculate P2, we will check whether sets of partition P1 can be partitioned or not:
For set { q1, q2, q4 } :
( q1, 0 ) = ( q2, 0 ) = q2 and ( q1, 1 ) = ( q2, 1 ) = q5, So q1 and q2 are not
distinguishable.
Similarly, ( q1, 0 ) = ( q4, 0 ) = q2 and ( q1, 1 ) = ( q4, 1 ) = q5, So q1 and q4 are not
distinguishable.
Since, q1 and q2 are not distinguishable and q1 and q4 are also not distinguishable, So q2 and q4
are not distinguishable. So, { q1, q2, q4 } set will not be partitioned in P2.
For set { q0, q3 } :
( q0, 0 ) = q3 and ( q3, 0 ) = q0
( q0, 1 ) = q1 and ( q3, 1 ) = q4
Moves of q0 and q3 on input symbol 0 are q3 and q0 respectively which are in same set in
partition P1. Similarly, Moves of q0 and q3 on input symbol 1 are q3 and q0 which are in same
set in partition P1. So, q0 and q3 are not distinguishable.
For set { q5 }:
Since we have only one state in this set, it cant be further partitioned. So,
P2 = { { q1, q2, q4 }, { q0, q3 }, { q5 } }
Since, P1=P2. So, this is the final partition. Partition P2 means that q1, q2 and q4 states are
merged into one. Similarly, q0 and q3 are merged into one. Minimized DFA corresponding to

DFA of Figure 1 is shown in Figure 2 as:

Question : Consider the given DFA. Which of the following is false?


1. Complement of L(A) is context-free.
2. L(A) = L ( ( 11 * 0 + 0 ) ( 0 + 1 )* 0* 1* )
3. For the language accepted by A, A is the minimal DFA.
4. A accepts all strings over { 0, 1 } of length atleast two.

A. 1 and 3 only
B. 2 and 4 only
C. 2 and 3 only
D. 3 and 4 only
Solution : Statement 4 says, it will accept all strings of length atleast 2. But it accepts 0 which is
of length 1. So, 4 is false.
Statement 3 says that the DFA is minimal. We will check using the algorithm discussed above.
P0 = { { q2 }, { q0, q1 } }
P1 = { q2 }, { q0, q1 } }. Since, P0 = P1, P1 is the final DFA. q0 and q1 can be merged. So
minimal DFA will have two states. Therefore, statement 3 is also false.
So correct option is (D).
This article has been contributed by Sonal Tuteja.
Please write comments if you find anything incorrect, or you want to share more information
about the topic discussed above.

See GATE Corner for all information about GATE CS and Quiz Corner for all Quizzes on GeeksQuiz.

Last Minute Notes Theory of Computation


We will discuss the important key points useful for GATE exams in summarized form. For details
you may refer this.

Finite Automata: It is used to recognize patterns of specific type input. It is the most restricted type
of automata which can accept only regular languages (languages which can be expressed by regular
expression using OR (+), Concatenation (.), Kleene Closure(*) like a*b*, (a+b) etc.)

Deterministic FA and Non-Deterministic FA: In deterministic FA, there is only one move from
every state on every input symbol but in Non-Deterministic FA, there can be zero or more than one
move from one state for an input symbol.
Note:

Language accepted by NDFA and DFA are same.


Power of NDFA and DFA is same.
No. of states in NDFA is less than or equal to no. of states in equivalent DFA.
Every NFA can be converted to corresponding DFA.

Push Down Automata: Pushdown Automata has extra memory called stack which gives more
power than Finite automata. It is used to recognize context free languages.

Deterministic and Non-Deterministic PDA: In deterministic PDA, there is only one move from
every state on every input symbol but in Non-Deterministic PDA, there can be more than one move
from one state for an input symbol.

Note:

Power of NPDA is more than DPDA.


It is not possible to convert every NPDA to corresponding DPDA.
Language accepted by DPDA is subset of language accepted by NPDA.
The languages accepted by DPDA are called DCFL (Deterministic Context Free Languages)
which are subset of NCFL (Non Deterministic CFL) accepted by NPDA.

Linear Bound Automata: Linear Bound Automata has finite amount of memory called tape which
can be used to recognize Context Sensitive Languages.

Turing Machine: Turing machine has infinite size tape and it is used to accept Recursive
Enumerable Languages.

Deterministic and Non-Deterministic Turing Machines: In deterministic turing machine, there is


only one move from every state on every input symbol but in Non-Deterministic turing machine,
there can be more than one move from one state for an input symbol.
Note:

Language accepted by NTM, multi-tape TM and DTM are same.


Power of NTM, Multi-Tape TM and DTM is same.
Every NTM can be converted to corresponding DTM.

Chomsky Classification of Languages:


Grammar
Type

Production Rules

Language
Accepted

Automata

Closed Under

Type-3
(Regular
Gramar)

Aa or AaB
where A,B N(non
terminal) and
aT(Terminal)

Regular

Finite Automata

Union, Intersection,
Complementation,
Concatenation,
Kleene Closure

Type-2
(Context Free
Grammar)

A-> where A N

Context Free

Push Down
Automata

Union,
Concatenation,
Kleene Closure

and (TN)*

Type-1
(Context
Sensitive
Grammar)

where ,
(TN)* and len()
<= len() and
should contain
atleast 1 non
terminal.

Context
Sensitive

Linear Bound
Automata

Union, Intersection,
Complementation,
Concatenation,
Kleene Closure

Type-0
(Recursive
Enumerable)

where ,
(TN)* and
contains atleast 1
non-terminal

Recursive
Enumerable

Turing Machine

Union, Intersection,
Concatenation,
Kleene Closure

Relationship between these can be represented as:

Decidable and Undecidable Problems:


A language is Decidable or Recursive if a Turing machine can be constructed which accepts the
strings which are part of language and rejects others. e.g.; A number is prime or not is a decidable
problem.
A language is SemiDecidable or Recursive Enumerable if a turing machine can be constructed
which accepts the strings which are part of language and it may loop forever for strings which are not
part of language.
A problem is undecidable if we cant construct an algorithms and Turing machine which can give
yes or no answer. e.g.; Whether a CFG is ambiguous or not is undecidable.

This article has been contributed by Sonal Tuteja.


Please write comments if you find anything incorrect, or you want to share more information about
the topic discussed above
See GATE Corner for all information about GATE CS and Quiz Corner for all Quizzes on
GeeksQuiz.

Computer Organization and Architecture(contd)


Pipelining | Set 1 (Execution, Stages and Throughput)
Please see Set 2 for Dependencies and Data Hazard and Set 3 for Types of pipeline and Stalling.
To improve the performance of a CPU we have two options:
1) Improve the hardware by introducing faster circuits.
2) Arrange the hardware such that more than one operation can be performed at the same time.
Since, there is a limit on the speed of hardware and the cost of faster circuits is quite high, we
have to adopt the 2nd option.
Pipelining : Pipelining is a process of arrangement of hardware elements of the CPU such that
its overall performance is increased. Simultaneous execution of more than one instruction takes
place in a pipelined processor.
Let us see a real life example that works on the concept of pipelined operation. Consider a water
bottle packaging plant. Let there be 3 stages that a bottle should pass through, Inserting the
bottle, Filling water in the bottle, and Sealing the bottle. Let us consider these stages as stage 1,
stage 2 and stage 3 respectively. Let each stage take 1 minute to complete its operation.
Now, in a non pipelined operation, a bottle is first inserted in the plant, after 1 minute it is moved
to stage 2 where water is filled. Now, in stage 1 nothing is happening. Similarly, when the bottle
moves to stage 3, both stage 1 and stage 2 are idle. But in pipelined operation, when the bottle is
in stage 2, another bottle can be loaded at stage 1. Similarly, when the bottle is in stage 3, there
can be one bottle each in stage 1 and stage 2. So, after each minute, we get a new bottle at the
end of stage 3. Hence, the average time taken to manufacture 1 bottle is :
Without pipelining = 1 min + 1 min + 1 min = 3 minutes
With pipelining = 1 minute

Thus, pipelined operation increases the efficiency of a system.


Design of a basic pipeline

In a pipelined processor, a pipeline has two ends, the input end and the output end. Between
these ends, there are multiple stages/segments such that output of one stage is connected to
input of next stage and each stage performs a specific operation.
Interface registers are used to hold the intermediate output between two stages. These
interface registers are also called latch or buffer.
All the stages in the pipeline along with the interface registers are controlled by a common
clock.

Execution in a pipelined processor


Execution sequence of instructions in a pipelined processor can be visualized using a space-time
diagram. For example, consider a processor having 4 stages and let there be 2 instructions to be
executed. We can visualize the execution sequence through the following space-time diagrams:

Pipeline Stages

RISC processor has 5 stage instruction pipeline to execute all the instructions in the RISC
instruction set. Following are the 5 stages of RISC pipeline with their respective operations:

Stage 1 (Instruction Fetch)


In this stage the CPU reads instructions from the address in the memory whose value is present
in the program counter.
Stage 2 (Instruction Decode)
In this stage, instruction is decoded and the register file is accessed to get the values from the
registers used in the instruction.
Stage 3 (Instruction Execute)
In this stage, ALU operations are performed.
Stage 4 (Memory Access)
In this stage, memory operands are read and written from/to the memory that is present in the
instruction.
Stage 5 (Write Back)
In this stage, computed/fetched value is written back to the register present in the instruction.

Performance of a pipelined processor


Consider a k segment pipeline with clock cycle time as Tp. Let there be n tasks to be
completed in the pipelined processor. Now, the first instruction is going to take k cycles to
come out of the pipeline but the other n 1 instructions will take only 1 cycle each, i.e, a total
of n 1 cycles. So, time taken to execute n instructions in a pipelined processor:
ETpipeline = k + n 1 cycles
= (k + n 1) Tp

In the same case, for a non-pipelined processor, execution time of n instructions will be:
ETnon-pipeline = n * k * Tp

So, speedup (S) of the pipelined processor over non-pipelined processor, when n tasks are
executed on the same processor is:
S = Performance of pipelined processor / Performance of Non-pipelined
processor

As the performance of a processor is inversely proportional to the execution time, we have,


S = ETnon-pipeline / ETpipeline
=> S = [n * k * Tp] / [(k + n 1) * Tp]
S = [n * k] / [k + n 1]

When the number of tasks n are significantly larger than k, that is, n >> k
S n * k / n
S k

where k are the number of stages in the pipeline.


Also, Efficiency = Given speed up / Max speed up = S / Smax
We know that, Smax = k
So, Efficiency = S / k
Throughput = Number of instructions / Total time to complete the instructions
So, Throughput = n / (k + n 1) * Tp
Note: The cycles per instruction (CPI) value of an ideal pipelined processor is 1

Sources : goo.gl/J9KVNt
https://en.wikipedia.org/wiki/Hazard_(computer_architecture)
https://en.wikipedia.org/wiki/Data_dependency
This article has been contributed by Saurabh Sharma.

Please write comments if you find anything incorrect, or you want to share more information
about the topic discussed above
See GATE Corner for all information about GATE CS and Quiz Corner for all Quizzes on GeeksQuiz.

Computer Organization and Architecture | Pipelining | Set 2


(Dependencies and Data Hazard)
Please see Set 1 for Execution, Stages and Performance (Throughput) and Set 3 for Types of
Pipeline and Stalling.
Dependencies in a pipelined processor
There are mainly three types of dependencies possible in a pipelined processor. These are :
1) Structural Dependency
2) Control Dependency
3) Data Dependency
These dependencies may introduce stalls in the pipeline.
Stall : A stall is a cycle in the pipeline without new input.
Structural dependency

This dependency arises due to the resource conflict in the pipeline. A resource conflict is a
situation when more than one instruction tries to access the same resource in the same cycle. A
resource can be a register, memory, or ALU.
Example:

In the above scenario, in cycle 4, instructions I1 and I4 are trying to access same resource
(Memory) which introduces a resource conflict.
To avoid this problem, we have to keep the instruction on wait until the required resource
(memory in our case) becomes available. This wait will introduce stalls in the pipeline as shown
below:

Solution for structural dependency


To minimize structural dependency stalls in the pipeline, we use a hardware mechanism called
Renaming.
Renaming : According to renaming, we divide the memory into two independent modules used
to store the instruction and data separately called Code memory(CM) and Data memory(DM)
respectively. CM will contain all the instructions and DM will contain all the operands that are

required for the instructions.

Control Dependency (Branch Hazards)


This type of dependency occurs during the transfer of control instructions such as BRANCH,
CALL, JMP, etc. On many instruction architectures, the processor will not know the target
address of these instructions when it needs to insert the new instruction into the pipeline. Due to
this, unwanted instructions are fed to the pipeline.
Consider the following sequence of instructions in the program:
100: I1
101: I2 (JMP 250)
102: I3
.
.
250: BI1
Expected output: I1 -> I2 -> BI1
NOTE: Generally, the target address of the JMP instruction is known after ID stage only.

Output Sequence: I1 -> I2 -> I3 -> BI1


So, the output sequence is not equal to the expected output, that means the pipeline is not
implemented correctly.
To correct the above problem we need to stop the Instruction fetch until we get target address of
branch instruction. This can be implemented by introducing delay slot until we get the target
address.

Output Sequence: I1 -> I2 -> Delay (Stall) -> BI1


As the delay slot performs no operation, this output sequence is equal to the expected output
sequence. But this slot introduces stall in the pipeline.
Branch penalty : The number of stalls introduced during the branch operations in the pipelined
processor is known as branch penalty.
NOTE : As we see that the target address is available after the ID stage, so the number of stalls
introduced in the pipeline is 1. Suppose, the branch target address would have been present after
the ALU stage, there would have been 2 stalls. Generally, if the target address is present after the
kth stage, then there will be (k 1) stalls in the pipeline.
Total number of stalls introduced in the pipeline due to branch instructions = Branch frequency
* Branch Penalty

Data Dependency (Data Hazard)


Let us consider an ADD instruction S, such that
S : ADD R1, R2, R3
Addresses read by S = I(S) = {R2, R3}
Addresses written by S = O(S) = {R1}
Now, we say that instruction S2 depends in instruction S1, when

This condition is called Bernstein condition.


Three cases exist:

Flow (data) dependence: O(S1) I (S2), S1 S2 and S1 writes after something read by S2
Anti-dependence: I(S1) O(S2), S1 S2 and S1 reads something before S2 overwrites it
Output dependence: O(S1) O(S2), S1 S2 and both write the same memory location.

Example: Let there be two instructions I1 and I2 such that:


I1 : ADD R1, R2, R3
I2 : SUB R4, R1, R2
When the above instructions are executed in a pipelined processor, then data dependency
condition will occur, which means that I2 tries to read the data before I1 writes it, therefore, I2
incorrectly gets the old value from I1.

To minimize data dependency stalls in the pipeline, operand forwarding is used.


Operand Forwarding : In operand forwarding, we use the interface registers present between
the stages to hold intermediate output so that dependent instruction can access new value from
the interface register directly.
Considering the same example:
I1 : ADD R1, R2, R3
I2 : SUB R4, R1, R2

Data Hazards

Data hazards occur when instructions that exhibit data dependence, modify data in different
stages of a pipeline. Hazard cause delays in the pipeline. There are mainly three types of data
hazards:
1) RAW (Read after Write) [Flow dependency]
2) WAR (Write after Read) [Anti-Data dependency]
3) WAW (Write after Write) [Output dependency]
Let there be two instructions I and J, such that J follow I. Then,

RAW hazard occurs when instruction J tries to read data before instruction I writes it.
Eg:
I: R2 <- R1 + R3 J: R4 <- R2 + R3
WAR hazard occurs when instruction J tries to write data before instruction I reads it.
Eg:
I: R2 <- R1 + R3 J: R3 <- R4 + R5
WAW hazard occurs when instruction J tries to write output before instruction I writes it.
Eg:
I: R2 <- R1 + R3 J: R2 <- R4 + R5

WAR and WAW hazards occur during the out-of-order execution of the instructions.

Sources : goo.gl/J9KVNt
https://en.wikipedia.org/wiki/Hazard_(computer_architecture)
https://en.wikipedia.org/wiki/Data_dependency
This article has been contributed by Saurabh Sharma.

Please write comments if you find anything incorrect, or you want to share more information
about the topic discussed above
See GATE Corner for all information about GATE CS and Quiz Corner for all Quizzes on GeeksQuiz.

Computer Organization and Architecture | Pipelining | Set 3


(Types and Stalling)
Please see Set 1 for Execution, Stages and Performance (Throughput) and Set 2 for
Dependencies and Data Hazard.
Types of pipeline

Uniform delay pipeline


In this type of pipeline, all the stages will take same time to complete an operation.
In uniform delay pipeline, Cycle Time (Tp) = Stage Delay

If buffers are included between the stages then, Cycle Time (Tp) = Stage Delay +
Buffer Delay

Non-Uniform delay pipeline


In this type of pipeline, different stages take different time to complete an operation.
In this type of pipeline, Cycle Time (Tp) = Maximum(Stage Delay)

For example, if there are 4 stages with delays, 1 ns, 2 ns, 3 ns, and 4 ns, then
Tp = Maximum(1 ns, 2 ns, 3 ns, 4 ns) = 4 ns
If buffers are included between the stages,
Tp = Maximum(Stage delay + Buffer delay)
Example : Consider a 4 segment pipeline with stage delays (2 ns, 8 ns, 3 ns, 10 ns). Find
the time taken to execute 100 tasks in the above pipeline.
Solution : As the above pipeline is a non-linear pipeline,
Tp = max(2, 8, 3, 10) = 10 ns
We know that ETpipeline = (k + n 1) Tp = (4 + 100 1) 10 ns = 1030 ns
NOTE: MIPS = Million instructions per second

Performance of pipeline with stalls


Speed Up (S) = Performancepipeline / Performancenon-pipeline
=> S = Average Execution Timenon-pipeline / Average Execution Timepipeline
=> S = CPInon-pipeline * Cycle Timenon-pipeline / CPIpipeline * Cycle Timepipeline

Ideal CPI of the pipelined processor is 1. But due to stalls, it becomes greater than 1.
=> S = CPInon-pipeline * Cycle Timenon-pipeline / (1 + Number of stalls per Instruction) * Cycle
Timepipeline
As Cycle Timenon-pipeline = Cycle Timepipeline,
Speed Up (S) = CPInon-pipeline / (1 + Number of stalls per instruction)
Sources : goo.gl/J9KVNt
https://en.wikipedia.org/wiki/Hazard_(computer_architecture)
https://en.wikipedia.org/wiki/Data_dependency
This article has been contributed by Saurabh Sharma.
Please write comments if you find anything incorrect, or you want to share more information
about the topic discussed above
See GATE Corner for all information about GATE CS and Quiz Corner for all Quizzes on GeeksQuiz.

Compiler Design (contd)


Classification of Context Free Grammars
Context Free Grammars (CFG) can be classified on the basis of following two properties:
1) Based on number of strings it generates.

If CFG is generating finite number of strings, then CFG is Non-Recursive (or the grammar is said
to be recursive grammar)
If CFG can generate infinite number of strings then the grammar is said to be Recursive
grammar

During Compilation, the parser uses the grammar of the language to make a parse tree(or
derivation tree) out of the source code. The grammar used must be unambiguous. An ambiguous
grammar must not be used for parsing.
2) Based on number of derivation trees.

If there is only 1 derivation tree then the CFG is unambiguous.


If there are more than 1 derivation tree, then the CFG is ambiguous.

Examples of Recursive and Non-Recursive Grammars


Recursive Grammars
1) S->SaS
S->b

The language(set of strings) generated by the above grammar is :{b, bab, babab,}, which is
infinite.
2) S-> Aa
A->Ab|c

The language generated by the above grammar is :{ca, cba, cbba }, which is infinite.
Non-Recursive Grammars
S->Aa
A->b|c

The language generated by the above grammar is :{ba, ca}, which is finite.

Types of Recursive Grammars


Based on the nature of the recursion in a recursive grammar, a recursive CFG can be again
divided into the following:

Left Recursive Grammar (having left Recursion)


Right Recursive Grammar (having right Recursion)
General Recursive Grammar(having general Recursion)

Please write comments if you find anything incorrect, or you want to share more information
about the topic discussed above
See GATE Corner for all information about GATE CS and Quiz Corner for all Quizzes on GeeksQuiz.

Compiler Design | Ambiguous Grammar


You can also read our previously discussed article on Classification of Context Free Grammars.

Context Free Grammars(CFGs) are classified based on:

Number of Derivation trees


Number of strings

Depending on Number of Derivation trees, CFGs are sub-divided into 2 types:

Ambiguous grammars
Unambiguous grammars

Ambiguous grammar:
A CFG is said to ambiguous if there exists more than one derivation tree for the given input
string i.e., more than one LeftMost Derivation Tree (LMDT) or RightMost Derivation Tree
(RMDT).
Definition: G = (V,T,P,S) is a CFG is said to be ambiguous if and only if there exist a string in
T* that has more than on parse tree.
where V is a finite set of variables.
T is a finite set of terminals.
P is a finite set of productions of the form, A -> , where A is a variable and (V T)* S is a
designated variable called the start symbol.

For Example:
1. Let us consider this grammar : E -> E+E|id
We can create 2 parse tree from this grammar to obtain a string id+id+id :
The following are the 2 parse trees generated by left most derivation:

Both the above parse trees are derived from same grammar rules but both parse trees are
different. Hence the grammar is ambiguous.
2. Let us now consider the following grammar:
Set of alphabets = {0,,9, +, *, (, )}
E
E
E
E
I

->
->
->
->
->

I
E + E
E * E
(E)
| 0 | 1 | | 9

From the above grammar String 3*2+5 can be derived in 2 ways:


I) First leftmost derivation
E=>E*E
=>I*E
=>3*E+E
=>3*I+E
=>3*2+E
=>3*2+I
=>3*2+5

II) Second leftmost derivation


E=>E+E
=>E*E+E
=>I*E+E
=>3*E+E
=>3*I+E
=>3*2+I
=>3*2+5

Following are some examples of ambiguous grammars:

S-> aS |Sa|
E-> E +E | E*E| id
A -> AA | (A) | a
S -> SS|AB , A -> Aa|a , B -> Bb|b

Whereas following grammars are unambiguous:

S -> (L) | a, L -> LS | S


S -> AA , A -> aA , A -> b

Inherently ambiguous Language:


Let L be a Context Free Language (CFL). If every Context Free Grammar G with Language L =
L(G) is ambiguous, then L is said to be inherently ambiguous Language. Ambiguity is a property
of grammar not languages. Ambiguous grammar is unlikely to be useful for a programming
language, because two parse trees structures(or more) for the same string(program) implies two
different meanings (executable programs) for the program.
Note : Ambiguity of a grammar is undecidable, i.e. there is no particular algorithm for removing
the ambiguity of a grammar, but we can remove ambiguity by:
Disambiguate the grammar i.e., rewriting the grammar such that there is only one derivation or
parse tree possible for a string of the language which the grammar represents.

This article is compiled by Saikiran Goud Burra.


Please write comments if you find anything incorrect, or you want to share more information
about the topic discussed above
See GATE Corner for all information about GATE CS and Quiz Corner for all Quizzes on GeeksQuiz.

Digital Electronics(Contd) Last Minute Preparation


LMN Digital Electronics
We will discuss the important key points useful for GATE exams in summarized form. For
details you may refer this.

This article has been contributed by Sonal Tuteja.


Please write comments if you find anything incorrect, or you want to share more information
about the topic discussed above
See GATE Corner for all information about GATE CS and Quiz Corner for all Quizzes on GeeksQuiz.

Discrete Mathematics(Contd)
Discrete Mathematics | The Pigeonhole Principle
Theorem:
I) If A is the average number of pigeons per hole, where A is not an integer then

At least one pigeon hole contains seal[A] (smallest integer greater than or equal to A) pigeons
Remaining pigeon holes contains at most floor[A] (largest integer less than or equal to A)
pigeons

Or
II) We can say as, if n + 1 objects are put into n boxes, then at least one box contains two or
more objects.
The abstract formulation of the principle: Let X and Y be finite sets and let f: X > Y be a
function.

If X has more elements than Y, then f is not one-to-one.


If X and Y have the same number of elements and f is onto, then f is one-to-one.
If X and Y have the same number of elements and f is one-to-one, then f is onto.

Pigeonhole principle is one of the simplest but most useful ideas in mathematics. We will see
more applications that proof of this theorem.
Example 1: If (Kn+1) pigeons are kept in n pigeon holes where K is a positive integer, what
is the average no. of pigeons per pigeon hole?
Solution: average number of pigeons per hole = (Kn+1)/n
= K + 1/n
Therefore at least a pigeonholes contains (K+1) pigeons i.e., seal[K +1/n] and remaining contain
at most K i.e., floor[k+1/n] pigeons.
i.e., the minimum number of pigeons required to ensure that at least one pigeon hole contains
(K+1) pigeons is (Kn+1).
Example 2: A bag contains 10 red marbles, 10 white marbles, and 10 blue marbles. What is
the minimum no. of marbles you have to choose randomly from the bag to ensure that we
get 4 marbles of same color?
Solution: Apply pigeonhole principle.
No. of colors (pigeonholes) n = 3
No. of marbles (pigeons) K+1 = 4
Therefore the minimum no. of marbles required = Kn+1
By simplifying we get Kn+1 = 10.
Verification: seal[Average] is [Kn+1/n] = 4

[Kn+1/3] = 4
Kn+1 = 10
i.e., 3 red + 3 white + 3 blue + 1(red or white or blue) = 10
Pigeonhole principle strong form.
Theorem: Let q1, q2, . . . , qn be positive integers.
If q1+ q2+ . . . + qn n + 1 objects are put into n boxes, then either the 1st box contains at least q1
objects, or the 2nd box contains at least q2 objects, . . ., the nth box contains at least qn objects.
Application of this theorem is more important, so let us see how we apply this theorem in
problem solving.
Example 1: In a computer science department, a student club can be formed with either 10
members from first year or 8 members from second year or 6 from third year or 4 from
final year. What is the minimum no. of students we have to choose randomly from
department to ensure that a student club is formed?
Solution: we can directly apply from the above formula where,
q1 =10, q2 =8, q3 =6, q4 =4 and n=4
Therefore the minimum number of students required to ensure department club to be formed is
10 + 8 + 6 + 4 4 + 1 = 25
Example 2: A box contains 6 red, 8 green, 10 blue, 12 yellow and 15 white balls. What is the
minimum no. of balls we have to choose randomly from the box to ensure that we get 9
balls of same color?
Solution: Here in this we cannot blindly apply pigeon principle. First we will see what happens
if we apply above formula directly.
From the above formula we have get answer 47 because 6 + 8 + 10 + 12 + 15- 5 + 1 = 47
But it is not correct. In order to get the correct answer we need to include only blue, yellow and
white balls because red and green balls are less than 9. But we are picking randomly so we
include after we apply pigeon principle.
i.e., 9 blue + 9 yellow + 9 white 3 + 1 = 25
Since we are picking randomly so we can get all the red and green balls before the above 25
balls. Therefore we add 6 red + 8 green + 25 = 39
We can conclude that in order to pick 9 balls of same color randomly, one has to pick 39 balls
from a box.
This article is contributed by Saikiran Goud Burra. Please write comments if you find anything
incorrect, or you want to share more information about the topic discussed above.
See GATE Corner for all information about GATE CS and Quiz Corner for all Quizzes on GeeksQuiz.

Whats difference between http:// and https://


?
In address bar of a browser, have you noticed either http:// or https:// at the time of browsing a
website? If neither of these are present then most likely, its http:// Lets find out the
difference
In short, both of these are protocols using which the information of a particular website is
exchanged between Web Server and Web Browser. But whats difference between these two?
Well, extra s is present in https and that makes it secure! What a difference
A very short and
concise difference between http and https is that https is much more secure compared to http.
Let us dig a little more. HyperText Transfer Protocol (HTTP) is a protocol using which
hypertext is transferred over the Web. Due to its simplicity, http has been the most widely used
protocol for data transfer over the Web but the data (i.e. hypertext) exchanged using http isnt as
secure as we would like it to be. In fact, hyper-text exchanged using http goes as plain text i.e.
anyone between the browser and server can read it relatively easy if one intercepts this exchange
of data. But why do we need this security over the Web. Think of Online shopping at Amazon
or Flipkart. You might have noticed that as soon as we click on the Check-out on these online
shopping portals, the address bar gets changed to use https. This is done so that the subsequent
data transfer (i.e. financial transaction etc.) is made secure. And thats why https was introduced
so that a secure session is setup first between Server and Browser. In fact, cryptographic
protocols such as SSL and/or TLS turn http into https i.e. https = http + cryptographic
protocols. Also, to achieve this security in https, Public Key Infrastructure (PKI) is used because
public keys can be used by several Web Browsers while private key can be used by the Web
Server of that particular website. The distribution of these public keys is done via Certificates
which are maintained by the Browser. You can check these certificates in your Browser settings.
Well detail out this setting up secure session procedure in another post.
Also, another syntactic difference between http and htpps is that http uses default port 80 while
https uses default port 443. But it should be noted that this security in https is achieved at the cost
of processing time because Web Server and Web Browser needs to exchange encryption keys
using Certificates before actual data can be transferred. Basically, setting up of a secure session
is done before the actual hypertext exchange between server and browser.
Please do Like/Tweet/G+1 if you find the above useful. Also, please do leave us comment for
further clarification or info. We would love to help and learn
See GATE Corner for all information about GATE CS and Quiz Corner for all Quizzes on
GeeksQuiz.

Computer Network | Circuit Switching VS Packet Switching


Circuit Switching
In circuit switching there are 3
phases
i) Connection Establishment.
ii) Data Transfer.
iii) Connection Released.

Packet Switching

In Packet switching directly data


transfer takes place .

In Packet switching, each data unit just


In circuit switching, each data unit
know the final destination address
know the entire path address which
intermediate path is decided by the
is provided by the source
routers.
In Circuit switching, data is
processed at source system only

In Packet switching, data is processed


at all intermediate node including
source system.

Delay between data units in circuit Delay between data units in packet
switching is uniform.
switching is not uniform.
Resource reservation is the feature There is no resource reservation
of circuit switching because path is because bandwidth is shared among
fixed for data transmission.
users.
Circuit switching is more reliable.

Packet switching is less reliable.

Wastage of resources are more in


Circuit Switching

Less wastage of resources as compared


to Circuit Switching

Please write comments if you find anything incorrect, or you want to share more information
about the topic discussed above

See GATE Corner for all information about GATE CS and Quiz Corner for all Quizzes on GeeksQuiz.

Commonly asked Computer Networks Interview Questions |


Set 1
What are Unicasting, Anycasting, Multiccasting and Broadcasting?
If the message is sent from a source to a single destination node, it is called Unicasting. This is
typically done in networks.
If the message is sent from a source to a any of the given destination nodes. This is used a lot in
Content delivery Systems where we want to get content from any server.
If the message is sent to some subset of other nodes, it is called Multicasting. Used in situation
when there are multiple receivers of same data. Like video conferencing, updating something on
CDN servers which have replica of same data.
If the message is sent to all the nodes in a network it is called Broadcasting. This is typically
used in Local networks, for examples DHCP and ARP use broadcasting.
What are layers in OSI model?
There are total 7 layers
1. Physical Layer
2. Data Link Layer
3. Network Layer
4. Transport Layer
5. Session Layer
6. Presentation Layer
7. Application Layer
What is Stop-and-Wait Protocol?
In Stop and wait protocol, a sender after sending a frame waits for acknowledgement of the
frame and sends the next frame only when acknowledgement of the frame has received.
What is Piggybacking?
Piggybacking is used in bi-directional data transmission in the network layer (OSI model). The
idea is to improve efficiency piggy back acknowledgement (of the received data) on the data
frame (to be sent) instead of sending a separate frame.
Differences between Hub, Switch and Router?
Hub

Switch

Router

Physical Layer Device

Data Link Layer Device

Network Layer Device

Simply repeats signal to all ports

Doesnt simply repeat, but filters


content by MAC or LAN address

Routes data based on IP


address

Connects devices within a single LAN

Can connect multiple sub-LANs

Connect multiple LANS

Collision domain of all hosts connected


through Hub remains one. i.e., if signal
sent by any two devices can collide.

within a single LAN

and WANS together.

Switch divides collision domain,


but broadcast domain of
connected devices remains same.

It divides both collision


and broadcast domains,

See network devices for more details.


What happens when you type a URL in web browser?
A URL may contain request to HTML, image file or any other type.
1. If content of the typed URL is in cache and fresh, then display the content.
2. Else find IP address for the domain so that a TCP connection can be setup. Browser does a DNS
lookup.
3. Browser needs to know IP address for a url, so that it can setup a TCP connection. This is why
browser needs DNS service. Browser first looks for URL-IP mapping browser cache, then in OS
cache. If all caches are empty, then it makes a recursive query to the local DNS server. The local
DNS server provides the IP address.
4. Browser sets up a TCP connection using three way handshake.
5. Browser sends a HTTP request.
6. Server has a web server like Apache, IIS running that handles incoming HTTP request and sends
a HTTP response.
7. Browser receives the HTTP response and renders the content.

What is DHCP, how does it work?


1. The idea of DHCP (Dynamic Host Configuration Protocol) is to enable devices to get IP
address without any manual configuration.
2. The device sends a broadcast message saying I am new here
3. The DHCP server sees the message and responds back to the device and typically
allocates an IP address. All other devices on network ignore the message of new device
as they are not DHCP server.

In Wi Fi networks, Access Points generally work as a DHCP server.


What is ARP, how does it work?
ARP stands for Address Resolution Protocol. ARP is used to find LAN address from Network
address. A node typically has destination IP to send a packet, the nodes needs link layer address
to send a frame over local link. The ARP protocol helps here.
1. The node sends a broadcast message to all nodes saying what is the MAC
address of this IP address.
2. Node with the provided IP address replies with the MAC address.

Like DHCP, ARP is a discovery protocol, but unlike DHCP there is not server here.

See this video for detailed explanation.


We will soon be adding more questions.
You may like to see below also
Please write comments if you find anything incorrect, or you want to share more information
about the topic discussed above
See GATE Corner for all information about GATE CS and Quiz Corner for all Quizzes on GeeksQuiz.

LAST MINUTE NOTES


LMNs-C/C++
In modern systems, size of all pointer types on a given platform is same i.e. whether it is
pointer to char or pointer to int or pointer to pointer to int etc., the size of all these
pointers is same.
In C, an identifier (e.g. variable) can consists of letters, digits and underscores only. But it
cant start with a digit e.g. 9var is invalid.
Unlike long long int, theres nothing like long long double in C. Besides, real data
types (i.e. float, double, long double) cant be unassigned.
In C, printf(%d,090); is invalid because an octal constant consists of the prefix 0
optionally followed by a sequence of the digits 0 through 7 only.
Real constants (e.g. 1.414) has data type as double. To convert them to float, we need to
suffix them with f or F (e.g. 1.414f or 1.414F).

Modulus operator (%d) isnt defined for real types however library functions fmod(),
fmodf(), fmodl() from math.h can be used for double, float and long double respectively.
In C, any of the 3 expressions of for loop i.e. for(exp1 ; exp2 ; exp3) can be empty. Even
all of the three can be empty.
The controlling expression of a switch statement i.e. switch(exp) shall have integer type
(i.e. int, char etc.) only.
When continue statement is hit in while or do-while loops, the next executed statement is
controlling expression of while or do-while loops. But when continue statement is hit in
for loop, the next executed statement is expression3 which is called increment expression
as well.
As per C standard, continue can be used in loop body only. And break can be used in
loop body and switch body only.

In C, goto statement can be used inside functions only and its label can point to anywhere
in the same function only.
In switch body, two case cant result in same value though having only one case or only
default is okay. In fact, switch body can be empty also.
As per C standard, a jump statement causes an unconditional jump to another place and
all goto, continue, break, return are jump statements.
In C, typedef is used to create alias of any other type. It can be used to create alias for
array and function pointer as well.
Multiple aliases of different types can be created using one typedef only. For example,
typedef int INT, *INTPTR, ONEDARR[10]; is completely valid.

The only storage-class specifier that shall occur in a parameter declaration is register.
Thats why even fun(auto int arg) is incorrect.
In C, signed, unsigned, short and long are Type specifiers and when they are used, int is
implicitly assumed in all of these. So signed i; unsigned j; short k; long l; is valid.

Though const and volatile look opposite to each other but a variable can be both const
and volatile.
const is a Type qualifier and a variable qualified with const means that the value of
variable isnt modifiable by the program.
volatile is a Type qualifier and a variable qualified with volatile means that value of
variable is subject to sudden change (possibly from outside the program)

A function cant have an explicit array as return type i.e. int [5] func(int arg1) is invalid.
However, indirect methods can be used if array like info needs to be output from a
function (e.g. using pointers).
Though sizeof() looks like a function, its actually an operator in C. Also, sizeof() is a
compile time operator. Thats why the output of printf(%d,sizeof(printf(GQ)));
would be same as printf(%d,sizeof(int));. Basically, operand of sizeof() operator isnt
evaluated at run time. Variable length array (introduced in C99) is exception for this.
While assigning any function to a function pointer, & (address of) is optional. Same way,
while calling a function via function pointer, * (value at address) is optional.
In C, for macros with arguments, there cant be any space between macro name and open
parenthesis.
C language doesnt provide any true support for 2D array or multidimensional arrays. For
example, a 2D array is simulated via 1D array of arrays.

Important point is that array size can be derived from its initialization but thats
applicable for first dimension only. For example, int arr[][2] = {1,2,3,4} is valid but int
arr[2][] = {1,2,3,4} is not valid.
Dereferencing of void pointer isnt allowed because void is an incomplete data type.
In C, initialization of array can be done for selected elements as well. Specific elements
in array can be initialized using []. For example, int arr[10] = {100, [5]=100,[9]=100};
is legal in C. This initializes arr[0], arr[5] and arr[9] to 100. All the remaining elements
would be 0.
As per C standard, if array size is defined using variable, the array cant be initialized at
definition itself. For example, int size = 2, arr[size]; is valid but int size = 2, arr[size] =
{1,2}; is invalid. Also, an array whose size is specified using variable cant be defined
out any function i.e. this array cant be global.
In C, struct members can be initialized even out of order using field name and using dot
operator. For example, struct {int i; char c;} myVar = {.c =A,.i = 100}; is valid.

LMNs- Algorithms

Analyze an algorithm
1) Worst Case Analysis (Usually Done):In the worst case analysis, we calculate upper bound
on running time of an algorithm by considering worst case (a situation where algorithm takes
maximum time) 2) Average Case Analysis (Sometimes done) :In average case analysis, we
take all possible inputs and calculate computing time for all of the inputs. 3) Best Case Analysis
(Bogus) :In the best case analysis, we calculate lower bound on running time of an algorithm.
Asymptotic Notations

Notation:The theta notation bounds a functions from above and below, so it defines exact
asymptotic behavior.

((g(n)) = {f(n): there exist positive constants c1, c2 and n0 such that
0 <= c1*g(n) <= f(n) <= c2*g(n) for all n >= n0}

Big O Notation: The Big O notation defines an upper bound of an algorithm, it bounds a function
only from above.
O(g(n)) = { f(n): there exist positive constants c and n0 such that
0 <= f(n) <= cg(n) for all n >= n0}

Notation: Just as Big O notation provides an asymptotic upper bound on a function,


notation provides an asymptotic lower bound.
(g(n)) = {f(n): there exist positive constants c and n0 such that
0 <= cg(n) <= f(n) for all n >= n0}.

Solving recurrences

Substitution Method: We make a guess for the solution and then we use mathematical
induction to prove the guess is correct or incorrect.
Recurrence Tree Method: We draw a recurrence tree and calculate the time taken by every
level of tree. Finally, we sum the work done at all levels.
Master Method: Only for following type of recurrences or for recurrences that can be
transformed to following type.
T(n) = aT(n/b) + f(n) where a >= 1 and b > 1

Sorting
Algorithm

Worst
Case

Average
Case

Best
Case

Min. no. of swaps

Max. no. of swaps

Bubble

(n2)

(n2)

(n2)

(n2)

Selection (n2)

(n2)

(n2)

(n)

Insertion (n2)

(n2)

(n)

(n2)

Quick

(n2)

(nlgn)

(nlgn)

(n2)

Merge

(nlgn)

(nlgn)

(nlgn)

Is not in-place
sorting

Is not in-place
sorting

Heap

(nlgn)

(nlgn)

(nlgn)

O(nlgn)

(nlgn)

Searching
Algorithm

Worst Case

Average Case

Best Case

Linear Search

(n)

(n)

(1)

Binary Search

O (logn)

O (logn)

O (1)

Trees
Trees: Unlike Arrays, Linked Lists, Stack and queues, which are linear data structures, trees are
hierarchical data structures. Depth First Traversals: (a) Inorder (b) Preorder (c) Postorder
Important Tree Properties and Formulas
Binary Search Tree
Binary Search Tree, is a node-based binary tree data structure which has the following
properties:

The left subtree of a node contains only nodes with keys less than the nodes key.
The right subtree of a node contains only nodes with keys greater than the nodes key.
The left and right subtree each must also be a binary search tree. There must be no duplicate
nodes.

1. Insertion
2. Deletion

AVL Tree
AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of
left and right subtrees cannot be more than one for all nodes.
1. Insertion
2. Deletion

B-Tree
B-Tree is a self-balancing search tree. In most of the other self-balancing search trees
(like AVL and Red Black Trees), it is assumed that everything is in main memory. To
understand use of B-Trees, we must think of huge amount of data that cannot fit in main
memory. When the number of keys is high, the data is read from disk in the form of blocks. Disk
access time is very high compared to main memory access time. The main idea of using B-Trees
is to reduce the number of disk accesses. Properties of B-Tree
1. B-Tree Insertion
2. B-Tree Deletion

Graph
Graph is a data structure that consists of following two components: 1. A finite set of vertices
also called as nodes. 2. A finite set of ordered pair of the form (u, v) called as edge. The pair is
ordered because (u, v) is not same as (v, u) in case of directed graph(di-graph). The pair of form
(u, v) indicates that there is an edge from vertex u to vertex v. The edges may contain
weight/value/cost. Following two are the most commonly used representations of graph.
1. Adjacency Matrix: Adjacency Matrix is a 2D array of size V x V where V is the number of
vertices in a graph.
2. Adjacency List : An array of linked lists is used. Size of the array is equal to number of vertices.

Graph Algorithms
Algorithm

Time Complexity

Breadth First Traversal for a Graph

O(V+E) for adjacency list representation and O(V


* V) for adjacency matrix representation.

Depth First Traversal for a Graph

O(V+E) for adjacency list representation and O(V


* V) for adjacency matrix representation.

Dijkstras shortest path algorithm

Adjacency matrix- O(V^2). Adjacency list- O(E log


V)

Topological Sorting: Shortest Path in Directed


Acyclic Graph

O(V+E)

Some Interesting Graph Questions


Minimum Spanning Tree
Minimum Spanning Tree (MST) problem: Given connected graph G with positive edge weights,
find a min weight set of edges that connects all of the vertices. MST is fundamental problem
with diverse applications.
1. Network design telephone, electrical, hydraulic, TV cable, computer, road
2. Approximation algorithms for NP-hard problems traveling salesperson problem, Steiner tree
3. Cluster analysis- k clustering problem can be viewed as finding an MST and deleting the k-1
most expensive edges.

Example: Prims Minimum Spanning Tree Algorithm, Kruskals Minimum Spanning Tree
Algorithm
Divide and Conquer
1. Divide:Break the given problem into subproblems of same type.
2. Conquer: Recursively solve these subproblems
3. Combine: Appropriately combine the answers

Following are some standard algorithms that are Divide and Conquer algorithms. 1) Binary
Search is a searching algorithm. In each step, the algorithm compares the input element x with

the value of the middle element in array. If the values match, return the index of middle.
Otherwise, if x is less than the middle element, then the algorithm recurs for left side of middle
element, else recurs for right side of middle element. 2) Quicksort is a sorting algorithm. The
algorithm picks a pivot element, rearranges the array elements in such a way that all elements
smaller than the picked pivot element move to left side of pivot, and all greater elements move to
right side. Finally, the algorithm recursively sorts the subarrays on left and right of pivot
element. 3) Merge Sort is also a sorting algorithm. The algorithm divides the array in two
halves, recursively sorts them and finally merges the two sorted halves. 4) Closest Pair of
Points The problem is to find the closest pair of points in a set of points in x-y plane. The
problem can be solved in O(n^2) time by calculating distances of every pair of points and
comparing the distances to find the minimum. The Divide and Conquer algorithm solves the
problem in O(nLogn) time.
Greedy Approach
Greedy is an algorithmic paradigm that builds up a solution piece by piece, always choosing the
next piece that offers the most obvious and immediate benefit. Greedy algorithms are used for
optimization problems. An optimization problem can be solved using Greedy if the problem has
the following property: At every step, we can make a choice that looks best at the moment, and
we get the optimal solution of the complete problem. Following are some standard algorithms
that are Greedy algorithms. 1) Kruskals Minimum Spanning Tree (MST): In Kruskals
algorithm, we create a MST by picking edges one by one. The Greedy Choice is to pick the
smallest weight edge that doesnt cause a cycle in the MST constructed so far. 2) Prims
Minimum Spanning Tree: In Prims algorithm also, we create a MST by picking edges one by
one. We maintain two sets: set of the vertices already included in MST and the set of the vertices
not yet included. The Greedy Choice is to pick the smallest weight edge that connects the two
sets. 3) Dijkstras Shortest Path: The Dijkstras algorithm is very similar to Prims algorithm.
The shortest path tree is built up, edge by edge. We maintain two sets: set of the vertices already
included in the tree and the set of the vertices not yet included. The Greedy Choice is to pick the
edge that connects the two sets and is on the smallest weight path from source to the set that
contains not yet included vertices. 4) Huffman Coding: Huffman Coding is a loss-less
compression technique. It assigns variable length bit codes to different characters. The Greedy
Choice is to assign least bit length code to the most frequent character.
Dynamic Programming
Dynamic Programming is an algorithmic paradigm that solves a given complex problem by
breaking it into subproblems and stores the results of subproblems to avoid computing the same
results again. Properties:
1. Overlapping Subproblems: Dynamic Programming is mainly used when solutions of same
subproblems are needed again and again. In dynamic programming, computed solutions to
subproblems are stored in a table so that these dont have to be recomputed.

Uses: Fibonacci Numbers

2. Optimal Substructure: A given problems has Optimal Substructure Property if optimal solution
of the given problem can be obtained by using optimal solutions of its subproblems.

Uses: Longest Increasing Subsequence, Shortest Path Two Approaches:


1. Memoization (Top Down)
2. Tabulation (Bottom Up)

Examples: Floyd Warshall Algorithm, BellmanFord Algorithm for Shortest Paths


BackTracking
Backtracking is an algorithmic paradigm that tries different solutions until finds a solution that
works. Backtracking works in an incremental way to attack problems. Typically, we start from
an empty solution vector and one by one add items .Meaning of item varies from problem to
problem. Example: Hamiltonian Cycle
See GATE Corner for all information about GATE CS and Quiz Corner for all Quizzes on GeeksQuiz.

LMN-DATA STRUCTURES
Arrays
An array is collection of items stored at continuous memory locations. The idea is to declare
multiple items of same type together. Array declaration: In C, we can declare an array by
specifying its and size or by initializing it or by both.
// Array declaration by specifying size
int arr[10];
// Array declaration by initializing elements
int arr[] = {10, 20, 30, 40};
// Array declaration by specifying size and
// initializing elements
int arr[6] = {10, 20, 30, 40}

Formulas:
Length of Array = UB - LB + 1

Given the address of first element, address of any other element is calculated using the formula:Loc (arr [k]) = base (arr) + w * k
w = number of bytes per storage location
of for one element
k = index of array whose address we want
to calculate

Elements of two-dimensional arrays (mXn) are stored in two ways:

Column major order: Elements are stored column by column, i.e. all elements of first column are
stored, and then all elements of second column stored and so on.

Loc(arr[i][j]) = base(arr) + w (m *j + i)

Row major order: Elements are stored row by row, i.e. all elements of first row are stored, and
then all elements of second row stored and so on.

Loc(arr[i][j]) = base(arr) + w (n*i + j)

Stacks
Stack is a linear data structure which follows a particular order in which the operations are
performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).
Basic operations :

Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition.
(Top=Top+1) Pop: Removes an item from the stack. The items are popped in the reversed order
in which they are pushed. If the stack is empty, then it is said to be an Underflow
condition.(Top=Top-1) Peek: Get the topmost item.
Infix, prefix, Postfix notations
Infix notation: X + Y Operators are written in-between their operands. This is the usual way
we write expressions. An expression such as
A * ( B + C ) / D

Postfix notation (also known as Reverse Polish notation): X Y + Operators are written after
their operands. The infix expression given above is equivalent to
A B C + * D/

Prefix notation (also known as Polish notation): + X Y Operators are written before their
operands. The expressions given above are equivalent to
/ * A + B C D

Converting between these notations: Click here


Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective
of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
1) Only one disk can be moved at a time.
2) Each move consists of taking the upper disk from one of the stacks and placing it on top of
another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
3) No disk may be placed on top of a smaller disk.
For n disks, total 2n 1 moves are required
Time complexity : O(2n) [exponential time]

Queues
Queue is a linear structure which follows a particular order in which the operations are
performed. The order is First In First Out (FIFO). A good example of queue is any queue of
consumers for a resource where the consumer that came first is served first. Stack : Remove the
item the most recently added Queue: Remove the item the least recently added Operations on
Queue:

Enqueue: Adds an item to the queue. If the queue is full, then it is said to be an Overflow
condition.
Dequeue: Removes an item from the queue. The items are popped in the same order in which
they are pushed. If the queue is empty, then it is said to be an Underflow condition.
Front: Get the front item from queue.
Rear: Get the last item from queue.
Linked Lists
Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at
contiguous location; the elements are linked using pointers.
Advantages over arrays 1) Dynamic size 2) Ease of insertion/deletion Drawbacks: 1) Random
access is not allowed. We have to access elements sequentially starting from the first node. So
we cannot do binary search with linked lists. 2) Extra memory space for a pointer is required
with each element of the list. Representation in C: A linked list is represented by a pointer to the
first node of the linked list. The first node is called head. If the linked list is empty, then value of
head is NULL. Each node in a list consists of at least two parts: 1) data 2) pointer to the next
node In C, we can represent a node using structures. Below is an example of a linked list node
with an integer data.
// A linked list node
struct node
{
int data;
struct node *next;
};

Potrebbero piacerti anche