CS Study Material PDF
CS Study Material PDF
1
Example 2.1 If S ∩ T = {1, 3, 5},
24
2.1.1 Venn Diagrams notation for not is ¬. There is not much savings in
space as the definition of compliment becomes
A Venn diagram is a way of depicting the relationship
between sets. Each set is shown as a circle and circles S c = {x : ¬(x ∈ S)}
overlap if the sets intersect.
Example 2.6 Set Compliments
Example 2.5 The following are Venn diagrams for
the intersection and union of two sets. The shaded
parts of the diagrams are the intersections and unions (i) Let the universal set be the integers. Then the
respectively. compliment of the even integers is the odd inte-
gers.
(ii) Let the universal set be {1, 2, 3, 4, 5}, then the
compliment of S = {1, 2, 3} is Sc = {4, 5} while
the compliment of T = {1, 3, 5} is Tc = {2, 4}.
(iii) Let the universal set be the letters {a, e, i, o, u, y}.
Then {y}c = {a, e, i, o, u}.
The Venn diagram for Ac is
A∩B
Ac
A∪B
Notice that the rectangle containing the diagram is We now have enough set-theory operators to use them
labeled with a U representing the universal set. to define more operators quickly. We will continue to
give English and symbolic definitions.
Definition 2.9 The compliment of a set S is the
collection of objects in the universal set that are not Definition 2.10 The difference of two sets S and T
is the collection of objects in S that are not in T . The
in S. The compliment is written S c . In curly brace
notation difference is written S − T . In curly brace nota-
tion
c
S = {x : (x ∈ U ) ∧ (x ∈ / S − T = {x : x ∈ (S ∩ (Tc ))},
S)} or alternately
or more compactly as
S − T = {x : (x ∈ S) ∧ (x ∈
/ T )}
S c = {x : x ∈
/ S}
Notice how intersection and complementation can be
however it should be apparent that the compliment of used together to create the difference operation and
a set always depends on which universal set is chosen. that the definition can be rephrased to use Boolean
operations. There is a set of rules that reduces the
There is also a Boolean symbol associated with the number of parenthesis required. These are called op-
complementation operation: the not operation. The erator precedence rules.
25
(i) Other things being equal, operations are per- Another important tool for working with sets is the
formed left-to-right. ability to compare them. We have already defined
what it means for two sets to be equal, and so by
(ii) Operations between parenthesis are done first, implication what it means for them to be unequal.
starting with the innermost of nested parenthe- We now define another comparator for sets.
sis.
(iii) All complimentations are computed next. (iv) Definition 2.12 For two sets S and T we say that S
is a subset of T if each element of S is also an
All intersections are done next. element of T . In formal notation S ⊆ T if for all x
∈ S we have x ∈ T .
(v) All unions are performed next.
If S ⊆ T then we also say T contains S which
(vi) Tests of set membership and computations,
can be written T ⊇ S. If S ⊆ T and S = T then we
equality or inequality are performed last.
write S ⊂ T and we say S is a proper subset of T .
Special operations like the set difference or the
symmetric difference, defined below, are not included Example 2.9 Subsets
in the precedence rules and thus always use paren- If A = {a, b, c} then A has eight different subsets:
thesis. ∅ {a} {b} {c}
Example 2.7 Operator precedence
Since complementation is done before intersection {a, b} {a, c} {b, c} {a, b, c}
the symbolic definition of the difference of sets can be Notice that A ⊆ A and in fact each set is a subset of
rewritten: itself. The empty set ∅ is a subset of every set.
26
a subset of itself so A ⊆ A. Since A = B we may where it is false. It is thus possible for a false mathe-
substitute into this expression on the left and obtain matical statement to be “true most of the time”. In
B ⊆ A. Similarly we may substitute on the right and the next chapter we will develop the theory of prime
obtain A ⊆ B. We have thus demonstrated that if numbers. For now we will assume the reader has a
A = B then A and B are both subsets of each other, modest familiarity with the primes. The statement
giving us the first half of the iff. “Prime numbers are odd” is false once, because 2 is a
Assume now that A ⊆ B and B ⊆ A. Then prime number. All the other prime numbers are odd.
the definition of subset tells us that any element of The statement is a false one. This very strict defini-
A is an element of B. Similarly any element of B tion of what makes a statement true is a convention
is an element of A. This means that A and B have in mathematics. We call 2 a counter example. It is
the same elements which satisfies the definition of set thus necessary to find only one counter-example to
equality. We deduce A = B and we have the second demonstrate a statement is (mathematically) false.
half of the iff. ✷
A note on mathematical grammar: the symbol ✷ in- Example 2.10 Disproof by counter example
dicates the end of a proof. On a paper turned in by a Prove that the statement A ∪ B = A ∩ B is false.
student it is usually taken to mean “I think the proof
ends here”. Any proof should have a ✷ to indicate its Let A = {1, 2} and B = {3, 4}. Then A ∩ B = ∅
end. The student should also note the lack of calcu- while A ∪ B = {1, 2, 3, 4}. The sets A and B form a
lations in the above proof. If a proof cannot be read counter-example to the statement.
back in (sometimes overly formal) English then it is
probably incorrect. Mathematical symbols should be
used for the sake of brevity or clarity, not to obscure Problems
meaning.
Problem 2.1 Which of the following are sets? As-
Proposition 2.2 De Morgan’s Laws Suppose sume that a proper universal set has been chosen and
that S and T are sets. DeMorgan’s Laws state that answer by listing the names of the collections of ob-
jects that are sets. Warning: at least one of these
(i) (S ∪ T )c = S c ∩ T c , and items has an answer that, while likely, is not 100%
certain.
(ii) (S ∩ T )c = S c ∪ T c .
(i) A = {2, 3, 5, 7, 11, 13, 19}
Proof:
(ii) B = {A, E, I , O, U }
Let x ∈ (S ∪ T )c ; then x is not a member of S or √
T . Since x is not a member of S we see that x ∈ (iii) C = { x : x < 0}
S c . Similarly x ∈ T c . Since x is a member of both
these sets we see that x ∈ S c ∩ T c and we see that (iv) D = {1, 2, A, 5, B, Q, 1, V }
(S ∪ T )c ⊆ S c ∩ Tc . Let y ∈ S c ∩ T c . Then the
(v) E is the list of first names of people in the 1972
definition of intersection tells us that y ∈ S c and
c phone book in Lawrence Kansas in the order
y ∈ T . This in turn lets us deduce that y is not a
they appear in the book. There were more than
member of S ∪ T , since it is not in either set, and
c
35,000 people in Lawrence that year.
so we see that y ∈ (S ∪ T ) . This demonstrates that
S ∩ T ⊆ (S ∪ T ) . Applying Proposition 2.1 we get (vi) F is a list of the weight, to the nearest kilogram,
c c c
that (S ∪ T )c = S c ∩ T c and we have proven part (i). of all people that were in Canada at any time in
The proof of part (ii) is left as an exercise. ✷ 2007.
In order to prove a mathematical statement you must
prove it is always true. In order to disprove a mathe- (vii) G is a list of all weights, to the nearest kilogram,
matical statement you need only find a single instance that at least one person in Canada had in 2007.
27
Problem 2.2 Suppose that we have the set U = Problem 2.5 Find an example of an infinite set that
{n : 0 ≤ n < 100} of whole numbers as our has a finite complement, be sure to state the universal
universal set. Let P be the prime numbers in U , set.
let E be the even numbers in U , and let F =
{1, 2, 3, 5, 8, 13, 21, 34, 55, 89}. Describe the following Problem 2.6 Find an example of an infinite set that
sets either by listing them or with a careful English has an infinite complement, be sure to state the uni-
sentence. versal set.
(i) Write S, T , and V using symbolic notation. (iv) A∆B (v) (A∆B)c (vi) Ac ∪ B c
28
29
29
Problem 2.11 Suppose we take the universal set to 2.2 Mathematical Induction
be the set of non-negative integers. Let E be the
set of even numbers, O be the set of odd numbers Mathematical induction is a technique used in prov- ing
and F = {0, 1, 2, 3, 5, 8, 13, 21, 34, 89, 144, ...} be the mathematical assertions. The basic idea of induc- tion
set of Fibonacci numbers. The Fibonacci sequence is is that we prove that a statement is true in one case
0, 1, 1, 2, 3, 5, 8, . . . in which the next term is obtained and then also prove that if it is true in a given case it
by adding the previous two. is true in the next case. This then permits the cases for
which the statement is true to cascade from the initial
(i) Prove that the intersection of F with E and O true case. We will start with the mathe- matical
are both infinite. foundations of induction.
(ii) Make a Venn diagram for the sets E, F , and O, We assume that the reader is familiar with the sym-
and explain why this is a Mickey-Mouse problem. bols <, >, ≤ and ≥. From this point on we will
denote the set of integers by the symbol Z. The
Problem 2.12 A binary operation ⊙ is commuta- non-negative integers are called the natural numbers.
tive if x ⊙ y = y ⊙ x. An example of a commuta- The symbol for the set of natural numbers is N. Any
tive operation is multiplication. Subtraction is non- mathematical system rests on a foundation of axioms.
commutative. Determine, with proof, if union, inter- Axioms are things that we simply assume to be true.
section, set difference, and symmetric difference are We will assume the truth of the following principle,
commutative. adopting it as an axiom.
Problem 2.13 An identity for an operation ⊙ is
The well-ordering principle: Every non-empty
an object i so that, for all objects x, i ⊙ x = x ⊙ i = ¯
set of natural numbers contains a smallest element.
x. Find, with proof, identities for the operations set
union and set intersection. The well ordering principle is an axiom that
agrees with the common sense of most people famil-
Problem 2.14 Prove part (ii) of Proposition 2.2. iar with the natural numbers. An empty set does
Problem 2.15 Prove that not contain a smallest member because it contains
no members at all. As soon as we have a set of nat-
A ∪ (B ∪ C ) = (A ∪ B) ∪ C ural numbers with some members then we can order
Problem 2.16 Prove that those members in the usual fashion. Having ordered
them, one will be smallest. This intuition agreeing
A ∩ (B ∩ C ) = (A ∩ B) ∩ C with this latter claim depends strongly on the fact
the integers are “whole numbers” spaced out in in-
Problem 2.17 Prove that
crements of one. To see why this is important con-
A∆(B∆C ) = (A∆B)∆C sider the smallest positive distance. If such a distance
existed, we could cut it in half to obtain a smaller
Problem 2.18 Disprove that distance - the quantity contradicts its own existence.
A∆(B ∪ C ) = (A∆B) ∪ C The well-ordering principle can be used to prove the
correctness of induction.
Problem 2.19 Consider the set S = {1, 2, 3, 4}. For
each k = 0, 1, . . . , 4 how many k element subsets does Theorem 2.1 Mathematical Induction I Sup-
S have? pose that P (n) is a proposition that it either true or
false for any given natural numbers n. If
Problem 2.20 Suppose we have a set S with n ≥ 0
elements. Find a formula for the number of different (i) P (0) is true and,
subsets of S that have k elements.
(ii) when P (n) is true so is P (n + 1)
Problem 2.21 For finite sets S and T , prove
Then we may deduce that P (n) is true for any natural
|S ∪ T | = |S| + |T | − |S ∩ T | number.
29
30
30
Proof: induction that the proposition is true for all natural
numbers. ✷
Assume that (i) and (ii) are both true state- The set of all subsets of a given set is itself an impor-
ments. Let S be the set of all natural numbers for tant object and so has a name.
which P (n) is false. If S is empty then we are done,
so assume that S is not empty. Then, by the well Definition 2.13 The set of all subsets of a set S is
ordering principle, S has a least member m. By (i) called the powerset of S. The notation for the
above m = 0 and so m − 1 is a natural number. Since powerset of S is P (S).
m is the smallest member of S it follows that P (m−1)
is true. But this means, by (ii) above, that P (m) is This definition permits us to rephrase Proposition 2.3
true. We have a contradiction and so our assumption as follows: the power set of a set of n elements has
that S = ∅ must be wrong. We deduce S is empty size 2n .
and that as a consequence P (n) is true for all n ∈ N.
Theorem 2.1 lets us prove propositions that are true
✷
on the natural numbers, starting at zero. A small
The technique used in the above proof is called proof modification of induction can be used to prove state-
by contradiction. We start by assuming the logical ments that are true only for those n ≥ k for any
opposite of what we want to prove, in this case that integer k. All that is needed is to use induction on a
there is some m for which P (m) is false, and from proposition Q(n − k) where Q(n − k) is logically
that assumption we derive an impossibility. If an as- equivalent to P (n). If Q(n − k) is true for n − k ≥ 0
sumption can be used to demonstrate an impossibility then P (n) is true for n ≥ k and we have the modified
then it is false and its logical opposite is true. induction. The practical difference is that we start
A nice problem on which to demonstrate mathemat- with k instead of zero.
ical induction is counting how many subsets a finite
set has. Example 2.11 Prove that n2 ≥ 2n for all n ≥ 2.
30
(i) First demonstrate a base case by directly demon- up lists of numbers. If we wished to sum some for-
strating P (k), mula f (i) over a range from a to b, that is to say a
≤ i ≤ b, then we write :
(ii) Next make the induction hypothesis that P (n) is
X b
true for some n,
f (i)
i=a
(iii) Finally, starting with the assumption that P (n)
is true, demonstrate P (n + 1). On the other hand if S is a set of numbers and we
want to add up f (s) for all s ∈ S we write:
These steps permit us to deduce that P (n) is true for X
all n ≥ k. f (s)
s∈S
Example 2.12 Using induction, prove The result proved in Example 2.12 may be stated in
the following form using sigma notation.
1
1+2+··· +n = n(n + 1) X n
2 1
i= n(n + 1)
2
In this case P (n) is the statement i=1
31
DefinitionP 2.14 When we solve an expression P in- Problem 2.24 Suppose that X ⊆ Y with |Y | = n
volving to obtain a formula that does not use or and |X | = m. Compute the number of subsets of Y
”. . .” as in Example 2.12 then we say we have found a that contain X .
closed form for the P expression. Example 2.12 finds
a closed form for i=1 i.
n Problem 2.25 Compute the following sums.
P20
At this point we introduce a famous mathematical (i) i=1 i,
sequence in order to create an arena for practicing (ii) P30 i, and
i=10
proofs by induction.
P21
(iii) i=−20 i.
Definition 2.15 The Fibonacci numbers are de-
fined as follows. f1 = f2 = 1 and, for n ≥ 3, Problem 2.26 Using mathematical induction, prove
fn = fn−1 + fn−2 . the following formulas.
P n
Example 2.13 The Fibonacci numbers with four or (i) i=1 1 = n,
fewer digits are: f1 = 1, f2 = 1, f3 = 2, f4 = 3, Pn 2 n(n+1)(2n+1)
f5 = 5, f6 = 8, f7 = 13, f8 = 21, f9 = 34, f10 = 55, (ii) i=1 i = , and 6
f11 = 89, f12 = 144, f13 = 233, f14 = 377, f15 = Pn 2
3 n (n+1)
2
32
Problem 2.32 Let f (n, 3) be the number of subsets Problem 2.41 Consider the statement “All cars are
of {1, 2, . . . , n} of size 3. Using induction, prove that the same color.” and the following “proof ”.
f (n, 3) = 16 n(n − 1)(n − 2). Proof:
Problem 2.33 Suppose that we have sets
We will prove for n ≥ 1 that for any set of n
X1 , X2 , . . . , Xn and Y1 , Y2 , . . . , Yn such that Xi ⊆ Yi .
cars all the cars in the set have the same color.
Prove that the intersection of all the Xi is a subset
of the intersection of all the Yi : • Base Case: n=1 If there is only one car then
\n
\n clearly there is only one color the car can be.
Xi ⊆ Yi
i=1 i=1
• Inductive Hypothesis: Assume that for any set of
n cars there is only one color.
Problem 2.34 Suppose that S1 , S2 , . . . Sn are sets.
Prove the following generalization of DeMorgan’s • Inductive step: Look at any set of n + 1 cars.
laws: Number them: 1, 2, 3, . . . , n, n + 1. Consider the
Tn Sn sets {1, 2, 3, . . . , n} and {2, 3, 4, ..., n + 1}. Each
c
(i) ( i=1 Si ) = i=1 Sic, and is a set of only n cars, therefore for each set there
Sn c Tn is only one color. But the nth car is in both sets
(ii) ( i=1 Si ) = i=1 Sci .
so the color of the cars in the first set must be the
Problem 2.35 Prove by induction that the Fi- same as the color of the cars in the second set.
bonacci number f4n is a multiple of 3. Therefore there must be only one color among all n
+ 1 cars.
Problem 2.36 Prove that if r is a real number r = 1
• The proof follows by induction. ✷
and r = 0 then
33
The reason for defining ordered pairs at this point of ordered pairs {(r 2 , r) : r ≥ 0}. This function is
is that it permits us to make an important formal well defined because each non-negative real number is
definition that pervades the rest of mathematics. the square of some positive real number.
Definition 2.17 A function f with domain S and The ma jor contrasts between functions in calculus
range T is a set of ordered pairs (s, t) with first ele- and functions in set theory are:
ment from S and second element from T that has the
property that every element of S appears exactly once (i) The domain of functions in calculus are often
as the first element in some ordered pair. We write specified only by implication (you have to know
f : S → T for such a function. how all the functions used work) and are almost
always a subset of the real numbers. The domain
Example 2.15 Suppose that A = {a, b, c} and B =
in set theory must be explicitly specified and may
{0, 1} then
be any set at all.
f = {(a, 0), (b, 1), (c, 0)} (ii) Functions in calculus typically had graphs that
is a function from A to B. The function f : A → B you could draw and look at. Geometric intuition
can also be specified by saying f (a) = 0, f (b) = 1 and driven by the graphs plays a ma jor role in our
f (c) = 0. understanding of functions. Functions in set the-
ory are seldom graphed and often don’t have a
The set of ordered pairs {(a, 0), (b, 1)} is not a func- graph.
tion from A to B because c is not the first coordi-
nate of any ordered pair. The set of ordered pairs
A point of similarity between calculus and set the-
{(a, 0), (a, 1), (b, 0), (c, 0)} is not a function from A
ory is that the range of the function is not explicitly
to B because a appears as the first coordinate of two
specified. When we have a function f : S → T then
different ordered pairs.
the range of f is a subset of T .
In calculus you may have learned the vertical line rule Definition 2.18 If f is a function then we denote
that states that the graph of a function may not in- the domain of f by dom(f ) and the range of f by
tersect a vertical line at more than one point. This rng(f )
corresponds to requiring that each point in the do-
main of the function appear in only one ordered pair. Example 2.17 Suppose that f (n) : N → N is de-
In set theory, all functions are required to state their fined by f (n) = 2n. Then the domain and range of f
domain and range when they are defined. In calculus are the integers: dom(f ) = rng(f ) = N. If we specify
functions had a domain that was a subset of the real the ordered pairs of f we get
numbers and you were sometimes required to identify
the subset. f = {(n, 2n) : n ∈ N}
Example 2.16 This example contrasts the way There are actually two definitions of range that are
functions were treated in a typical calculus course with used in mathematics. The definition we are using, the
the way we treat them in set theory. set from which second coordinates of ordered pairs in a
Calculus: find the domain of the function function are drawn, is also the definition typically using
√ in computer science. The other definition is the set of
f (x) = x second coordinates that actually appear in or- dered
pairs. This set, which we will define formally later, is
Since we know that the square root function exists the image of the function. To make matters even worse
only for non-negative real numbers the domain is {x : the set we are calling the range of a func- tion is also
x ≥ 0}. called the co-domain. We include these confusing
√
Set theory: the function f = x from the non- terminological notes for students that may try and
negative real numbers to the real numbers is the set look up supplemental material.
34
Definition 2.19 Let X, Y, and Z be sets. The com- We use the symbol R for the real numbers. We also
position of two functions f : X → Y and g : Y → Z assume familiarity with interval notation for contigu-
is a function h : X → Z for which h(x) = g(f (x)) ous subsets of the reals. For real numbers a ≤ b
for all x ∈ X . We write g ◦ f for the composition of
g with f . (a, b) is {x : a < x < b} (a, b] is
{x : a < x ≤ b} [a, b) is {x : a ≤ x <
The definition of the composition of two functions b} [a, b] is {x : a ≤ x ≤ b}
requires a little checking to make sure it makes sense.
Since every point must appear as a first coordinate of Example 2.20 The function f : Z → Z given by
an ordered pair in a function, every result of applying f (n) = 5 − n is a surjection. If we set m = 5 − n then n
f to an element of X is an element of Y to which g can = 5 − m. This means that if we want to find some n
be applied. This means that h is a well-defined set of so that f (n) is, for example, 8, then 5 − 8 = −3 and
ordered pairs. Notice that the order of composition is we see that f (−3) = 8. This demonstrates that all m
important - if the sets X , Y , and Z are distinct there have some n so that f (n) = m, showing that all m
is only one order in which composition even makes appear as the second coordinate of an ordered pair in
sense. f.
2
The function g : R → R given by g(x) = 1+x x
2 is not a
Example 2.18 Suppose that f : N → N is given by
surjection because −1 < g(x) < 1 for all x ∈ R.
f (n) = 2n while g : N → N is given by g(n) = n + 4.
Then
Definition 2.22 A function that is both surjective
(g ◦ f )(n) = 2n + 4
and injective is said to be bijective. Bijective func-
while tions are called bijections.
(f ◦ g)(n) = 2(n + 4) = 2n + 8
Example 2.21 The function f : Z → Z given by
We now start a series of definitions that divide func- f (n) = n is a bijection. All of its ordered pairs have
tions into a number of classes. We will arrive at a the same first and second coordinate. This function is
point where we can determine if the mapping of a called the identity function.
function is reversible, if there is a function that ex- 15
actly reverses the action of a given function.
35
some numbers appear twice as second coordinates of Definition 2.24 The inverse of a function f : S →
ordered pairs in g. We can use the graph because g is T is a function g : T → S so that for all x ∈ S,
a function from the real numbers to the real numbers. g(f (x)) = x and for all y ∈ T , f (g(y)) = y.
For a function f : S → T to be a bijection every If a function f has an inverse we use the notation f
element of S appears in an ordered pair as the first −1
for that inverse. Since an exponent of −1 also
member of an ordered pair and every element of T means reciprocal in some circumstances this can be a
appears in an ordered pair as the second member of bit confusing. The notational confusion is resolved by
an ordered pair. Another way to view a bijection is as considering context. So long as we keep firmly in mind
a matching of the elements of S and T so that every that functions are sets of ordered pairs it is easy to
element of S is paired with an element of T . For prove the proposition/definition that follows after the
finite sets this is clearly only possible if the sets are next example.
the same size and, in fact, this is the formal definition
of “same size” for sets.
Example 2.23 If E is the set of even integers then
Definition 2.23 Two sets S and T are defined to be the bijection f (n) = 2n from Z to E has the inverse f
the same size or to have equal cardinality if there −1 : E → Z given by g(2n) = n. Notice that defin-
is a bijection f : S → T . ing the rule for g as depending on the argument 2n
seamlessly incorporates the fact that the domain of g is
Example 2.22 The sets A = {a, b, c} and Z = the even integers.
{1, 2, 3} are the same size. This is obvious because
they have the same number of elements, |A| = |Z | = 3
but we can construct an explicit bijection
g:Z→ E
36
technique from calculus classes. B is proven by first assuming A and deducing B and
then separately assuming B and deducing A. The
x
y = formal symbol for A iff B is A ⇔ B. Likewise we
x−1 have symbols for the ability to deduce B given A,
y(x − 1) = x
A ⇒ B and vice-versa B ⇒ A. These symbols are
xy − y = x spoken “A implies B” and “B implies A” respectively.
xy − x = y
x(y − 1) = y Proposition 2.6 Suppose that X , Y , and Z are
yx sets. If f : X → Y and g : Y → Z are bijections
= then so is g ◦ f : X → Z .
y−1
37
2.3.1 Permutations n 0 1 2 3 4 5 6 7
n! 1 1 2 6 24 120 720 5040
In this section we will look at a very useful sort of
function, bijections of finite sets. Proposition 2.7 The number of permutations of a
finite set with n elements is n!.
Definition 2.27 A permutation is a bijection of a
finite set with itself. Likewise a bijection of a finite Proof: this proof is left as an exercise.
set X with itself is called a permutation of X.
Notice that one implication of Proposition 2.6 is that
Example 2.26 Let A = {a, b, c} then the possible the composition of two permutations is a permuta-
permutations of A consist of the following six func- tion. This means that the set of permutations of a
tions: set is closed under functional composition.
abc acb bac bca cab cba (ii) A function that is a surjection but not an injec-
tion.
in one line notation. Notice the saving of space.
(iii) A function that is neither an injection nor a sur-
Definition 2.29 The factorial of a natural number jection.
n is the product
(iv) A bijection that is not the identity function.
with the convention that the factorial of 0 is 1. We (i) f (x) = x2 (ii) g(x) = x3
denote the factorial of n as n!, spoken ”n factorial”. √
x√ x≥0
(iii) h(x) =
Example 2.27 Here are the first few factorials: − −x x < 0
38
Interlude
The Collatz Conjecture
One of the most interesting features of mathematics is that it is possible to phrase problems
in a few lines that turn out to be incredibly hard. The Collatz conjecture was first posed
in 1937 by Lothar Collatz. Define the function f from the natural numbers to the natural
numbers with the rule
3n + 1 n odd
n f (n) =
2
n even
Collatz’ conjecture is that if you apply f repeatedly to a positive integer then the resulting
sequence of numbers eventually arrives at one. If we start with 17, for example, the result
of repeatedly applying f is:
f (17) = 52, f (52) = 26, f (26) = 13, f (13) = 40, f (40) = 20, f (20) = 10, f (10) = 5,
f (5) = 16, f (16) = 8, f (8) = 4, f (4) = 2, f (2) = 1
39
40 CHAPTER 2. BASIC SET THEORY
40
Problem 2.46 True or false (and explain): The Problem 2.59 Suppose that X and Y are finite sets
x−1
function f (x) = x+1 is a bijection from the real num- and that |X | = |Y | = n. Prove that there are n!
bers to the real numbers. bijections of X with Y .
Problem 2.47 Find a function that is an injection Problem 2.60 Suppose that X and Y are sets with
of the integers into the even integers that does not |X | = n, |Y | = m. Count the number of functions
appear in any of the examples in this chapter. from X to Y .
Problem 2.61 Suppose that X and Y are sets with
Problem 2.48 Suppose that B ⊂ A and that there |X | = n, |Y | = m for m > n. Count the number of
exists a bijection f : A → B. What may be reasonably injections of X into Y .
deduced about the set A?
Problem 2.62 For a finite set S with a subset T
Problem 2.49 Suppose that A and B are finite sets. prove that the permutations of S that have all mem-
Prove that |A × B| = |A| · |B|. bers of T as fixed points form a set that is closed
under functional composition.
Problem 2.50 Suppose that we define h : N → N as
Problem 2.63 Compute the number of permuta-
follows. If n is even then h(n) = n/2 but if n is odd
tions of a set S with n members that fix at least m < n
then h(n) = 3n + 1. Determine if h is a (i) surjection
points.
or (ii) injection.
Problem 2.64 Using any technique at all, estimate
Problem 2.51 Prove proposition 2.6. the fraction of permutations of an n-element set that
have no fixed points. This problem is intended as an
Problem 2.52 Prove or disprove: the composition exploration.
of injections is an injection.
Problem 2.65 Let X be a finite set with |X | = n.
Problem 2.53 Prove or disprove: the composition Let C = X × X . How many subsets of C have the
of surjections is a surjection. property that every element of X appears once as a
first coordinate of some ordered pair and once as a
Problem 2.54 Prove proposition 2.7. second coordinate of some ordered pair?
P
Problem 2.55 List all permutations of Problem Q 2.66 An alternate version of Sigma ( )
and Pi ( ) notation works by using a set as an index.
X = {1, 2, 3, 4} So if S = {1, 3, 5, 7} then
X Y
s = 16 and s = 105
using one-line notation.
s∈S s∈S
Problem 2.56 Suppose that X is a set and that f , Given all the material so far, give and defend rea-
g, and h are permutations of X . Prove that the equa- sonable values for the sum and product of an empty
tion f ◦ g = h has a solution g for any given permu- set.
tations f and h.
Problem 2.67 Suppose that fα : [0, 1] → [0, 1] for
Problem 2.57 Examine the permutation f of Q = −1 < α < ∞ is given by
{a, b, c, d, e} which is bcaed in one line notation. If (α + 1)x fα (x) = ,
we create the series f, f ◦ f, f ◦ (f ◦ f ), . . . does the αx + 1
identity function, abcde, ever appear in the series? prove that fα is a bijection.
If so, what is its first appearance? If not, why not? Problem 2.68 Find, to five decimals accuracy:
Problem 2.58 If f is a permutation of a finite set, Ln(200!)
prove that the sequence f, f ◦ f, f ◦ (f ◦ f ), . . . must
Explain how you obtained the answer.
contain repeated elements.
40
2.4. ∞ + 1 41
41
Linear Equations and Matrices
The purpose of this chapter is to learn about linear systems. We will restrict our discussion for now to
equations whose coefficients are real numbers. In order to develop the algorithmic approach to linear
systems known as Gaussian reduction, we will introduce the notion of a matrix so that we can approach any
system via its coefficient matrix. This allows us to state a set of rules called row operations to bring our
equations into a normal form called the reduced row echelon form of the system. The set of solutions may
then be expressed in terms of fundamental and particular solutions. Along the way, we will develope the
criterion for a system to have a unique solution. After we have developed some further algebraic tools, which
will come in the next chapter, we’ll be able to considerably strengthen the techniques we developed in this
chapter.
15
42
16
equations represent two surfaces in real three space R3, so we’d expect the set of common
solutions to lie on a curve. Here it’s impossible to express the solutions in a closed form, but we
can study them locally using linear methods. For example, both surfaces meet at (1, 1, 1), and
they both have a tangent plane at (1, 1, 1). The tangent line to the curve of intersection at (1,
1, 1) is the intersection of these two tangent planes. This will give us a linear approximation to
the curve near (1, 1, 1).
Nonlinear systems such as in the above example are usually difficult to solve; their theory
involves highly sophisticated mathematics. On the other
16
17
hand, it turns out that systems of linear equations are handled quite simply by elementary methods, and
modern computers make it possible to solve gigantic linear systems with fantastic speed.
A general linear system consisting of m equations in n unknowns will look like:
. (2.1)
Notice how the coefficients aij are labelled. The first index gives its row and the second index its column.
The case where all the constants bi are zero is called the homogeneous case. Otherwise, the system is said
to be nonhomogeneous
The main problem, of course, is to find a procedure or algorithm for describing the solution set of a
linear system. The principal procedure for solving a linear system is called Gaussian reduction. We will
take this up below.
17
18
2.2 Matrices
To simplify the cumbersome notation for a system used above, we will now introduce the notion
of a matrix.
Deftnition 2.2. The matrix sum (or simply the sum) A + B of two m n matrices × A
and B is defined to be the m n matrix C such
× that c ij = aij +b ij for all pairs of indices
(i, j). The scalar multiple αA of A by a real number α is the matrix obtained by
multiplying each entry of A by α.
18
19
The m × n matrix all of whose entries are zero is called the zero matrix. If O is the m n zero
matrix and A × is any m n matrix, then A + O = × A. Thus O is the additive identity for matrix
addition. Now that the additive identity for matrix addition is defined, we can observe that the
matrix −A is the additive inverse of A, in the sense that A + (−A) = (−A) + A = O.
A column matrix is usually simply called a vector. The set of all n × 1 column matrices (or vectors) is
denoted by Rn. Vectors with the same num- ber of components are combined via the component-wise addition
and scalar multiplication defined above. We will use the notation (u1, u2, . . . , un)T to express the column
matrix
in a more compact form. What the superscript T stands for will be clarified later. Vectors will usually be
written as bold faced letters. For example, x will stand for
If u1, u2, . . . um are vectors in Rn and if a1, a2, . . . , am are scalars, that is elements of R, then the
vector
a 1 u 1 + a 2 u 2 + · · · + a mu m
is called a linear combination of u1, u2, . . . um.
In the first sum, the parity of every element in the first matrix is reversed. In the second, we see every matrix
over F2 is its own additive inverse.
Example 2.3. Random Key Crypts. Suppose Rocky the flying squirrel wants to send a message to his sidekick,
Bullwinkle the moose, and he wants to make sure that the notorious villains Boris and Natasha won’t be able to
learn what it says. Here is what the ever resourceful squirrel does. First he assigns the number 1 to a, 2 to b and
so forth up to 26 to z. He also assigns 0 to the space between two words. He then computes the binary
expansion of each integer between 1 and 26. Thus 1=1, 2=10, 3=11, 4=100, . . . , 26=11010. He now converts
his message into a sequence of five digit strings. Note that 00000 represents a space. The result is his encoded
message, which is normally referred to as the plaintext. To make things more compact, he arranges the
plaintext into a matrix. For example, if there are 5 words, hence 4 spaces, he could make a 3 3 matrix of 5 digit
strings of zeros and ones.
Let’s denote the matrix containing the plaintext by P , and suppose P is m n. Now the fun starts. Rocky and
Bullwinkle have a list of m n matrices of zeros and ones that only they know. The flying squirrel selects one of
× ×
these matrices, say number 47, and tells Bullwinkle. Let E be matrix number 47. Cryptographers call E the
key. Now he sends the ciphertext encE(P ) = P +E to Bullwinkle. If only Rocky and Bullwinkle know E, then
the matrix P containing the plaintext is secure. Even if Boris
× and Natasha succeed in learning the ciphertext P
+ E, they will still have to know E to
find out what P is. The trick is that the key E has to be sufficiently random so that neither Boris nor Natasha
can guess it. For example, if E is the all ones matrix, then P isn’t very secure since Boris and Natasha will
surely try it. Notice that once Bullwinkle receives the ciphertext, all he has to do is add the key E to recover
the plaintext P since
encE(P ) + E = (P + E) + E = P + (E + E) = P + O = P.
(P + E) + (Q + E) = (P + Q) + (E + E) = P + Q + O = P + Q.
The point is that knowing P + Q may be enough for a good cryptographer to deduce both P and Q. But, as
a one time pad, the random key is quite secure (in fact, apparently secure enough for communications on the
hot line between Washington and Moscow).
Example 2.4. (Scanners) We can also interpret matrices over F2 in an- other natural way. Consider a black
and white photograph as being a rect- angular array consisting of many black and white dots. By giving the
white dots the value 0 and the black dots the value 1, the black and white photo is therefore transformed into a
matrix over F2. Now suppose we want to com- pare two black and white photographs whose matrices A
and B are both m n. It’s inefficient for a computer to scan the two matrices to see in how many
positions they agree. However, when A and BB are added, the sum A + B has a 1 in in any component
where A and B differ, and a 0 wherever they coincide. For example, the sum two identical photographs is
× 20
21
the zero matrix, and the sum of two complementary photographs is the all ones matrix. An obvious
measure of how similar the two matrices A and B are is the number of non zero entries of A + B, i.e. Σ(aij
+ bij). This easily tabulated number is known as the Hamming distance between A and B.
.
From now on, one should think of the left hand side of the linear system
Ax = b as a product.
Let us point out a basic property of multiplication.
Proposition 2.1. The matrix product Ax is distributive. That is, for any
x and y in Rn and any A ∈ Rm×n, A(x + y) = Ax + Ay.
Proof. This is obvious from the distributive property of real numbers.
21
22
Gaussian reduction is an algorithmic procedure for finding the solution set of a linear system. We
will say that two linear systems are equivalent if their solution sets are equal. The strategy in
Gaussian reduction is to replace the original system with a sequence of equivalent systems
until the final system is in reduced row echelon form. That is, its coefficient matrix is in
reduced row echelon form. The sequence of equivalent systems is produced by applying row
operations.
×
Let A be an m n matrix and consider the linear system Ax = b. The augmented coefficient
matrix of this system is (A b). The first thing is to point out the role of row operations. What
|
happens when one performs an elementary row operation on (A b)? In fact, I claim that the
new system is equivalent to the original system.
|
For example, row swaps simply interchange two equations, so they clearly leave the solution
set unchanged. Similarly, multiplying the ith equation by a non-zero constant a does likewise,
since the original system can be recaptured by multiplying the ith equation by a −1. The only
question is whether a row operation of type III changes the solutions. Suppose the ith equation
is replaced by itself plus a multiple k of the jth equation, where i = j. Then any solution of the
original system is still a solution of the new system. But any solution of the new system is also
a solution of the original system since subtracting k times the jth equation from the ith
equation of the new system gives us back the original system. Therefore the systems are
ƒ
equivalent.
To summarize this, we state
Proposition 2.3. Performing a sequence of row operations on the aug- mented
coefficient matrix of a linear system gives a new system which is equivalent to the
original system.
To reiterate, to solve a linear system by Gaussian reduction, the first step is to put the
augmented coefficient matrix in reduced row echelon form via a sequence of row operations.
The next step will be to find the solution set.
22
23
The procedure is to solve for the variables corresponding to the columns with corners, which we call the
corner variables. Since the corner variables have nonzero coefficients, they can be expressed in terms of
the remaining variables, which are called the free variables. For A, the corner columns are the second and
fourth, so x2 and x4 are the corner variables, and the variables x1, x3, x5 and x6 are the free variables.
Solving for x2 and x4 gives
x2 = −2x3 − 3x5 + x6 x4 = −2x5
In this expression, the corner variables are dependent variables which are functions of the free variables.
Now let x = (x1, x2, x3, x4, x5, x6)T denote an arbitrary vector in R6 in the solution set of the system, and
let us call x the general solution vector. Notice that we have expressed it as a column vector. Replacing
the corner variables by their expressions in terms of the free variables gives a new expression for the general
solution vector involving only the free variables. Namely
23
24
The general solution vector now depends only on the free variables, and there is a solution for
any choice of these variables.
Using a little algebra, we can compute the vector coefficients of each one of the free
variables in x. These vectors are called the fundamental solutions.
In this example, the general solution vector x has the form
x = x 1 f 1 + x 3f 2 + x 4f 3 + x 5 f 4 , (2.5)
where
The equation (2.5) tells us that every solution of Ax = 0 is a linear combi- nation of the fundamental solutions
f1, . . . , f4. −
Proof. The proof that every solution is a linear combination of the funda- mental solutions
goes exactly like the above example, so we will omit it. Equation (2.6) is an application of the
fact that every variable is either a free variable or a corner variable, but not both.
We will eventually prove several refinements of this property which will say considerably
more about the structure of the solution set.
Let us point out something a bit unusual in Example 2.6. The variable x1 never actually
appears in the system, but it does give a free variable and a corresponding fundamental
solution (1, 0, 0, 0, 0, 0)T . Suppose instead of A the coefficient matrix is
24
25
Now (1, 0, 0, 0, 0, 0)T is no longer a fundamental solution. In fact the solution set is now a subset of R5. The
corner variables are x1 and x3, and there are now only three fundamental solutions corresponding to the
free variables x2, x4, and x5.
Even though identity (2.6) is completely obvious, it gives some very useful information. Here is a
typical application.
Example 2.7. Consider a linear system with 25 variables and assume there are 10 free variables. Then there
are 15 corner variables, so the system has to have at least 15 equations. That is, there have to be at least 15
linear constraints on the 25 variables.
We can also use (2.6) to say when the homogeneous system Ax = 0 has a unique solution (that is,
exactly one solution). Note that 0 is always a solution: the trivial solution. Hence if the solution is to
be unique, then the only possibility is that (A) = 0 . But this happens exactly when there are no free
variables, since if there is a free variable there have to be non trivial solutions. Thus a homogeneous
system has a unique solution if andN only if every
{ } variable is a corner variable, which is the case exactly
when the number of corner variables is the number of columns of A. By the same reasoning, if a homogeneous
system has more variables than equations, there have to be non trivial solutions, since there has to be at least
one free variable.
Proof. The proof is quite easy. Suppose p = (p1, . . . , pn)T , and let x = (x1, . . . , xn)T be an
element of (A). By the distributivity
N of matrix mul- tiplication (Proposition 2.1),
A(p + x) = Ap + Ax = b + 0 = b.
25
26
A(p − q) = Ap − Aq = b − b = 0.
Thus, p q−is an element of (A).NTherefore q = p + x, where x = q p, −
as asserted. This completes the proof.
In the above proof, we made the statement that A(p + x) = Ap + Ax. This follows from
a general algebraic identity called the distributive law which we haven’t yet discussed.
However, our particular use of the distributive law is easy to verify from first principles.
Example 2.8. Consider the system involving the counting matrix C of Example 2.5:
where a, b and c are fixed arbitrary constants. This system has augmented coefficient matrix
26
38
reduced system turns out to be the same one we obtained by using the sequence in Example 11.2. We get
38
notation, we have
Σ
n
39
cik = aij bjk ,
j=1
so
.Σ
n
Σ
AB = aijb jk .
j=1
·
where denotes matrix multiplication.
Another way of putting the definition is to say that if the columns of A
are a1, . . . , an, then the r-th column of AB is
×
This example points out that for there exist 2 2 matrices A and B such that AB = BA,
even though both products AB and BA are defined. In general, matrix multiplication is
ƒ
not commutative. In fact, almost any pair of 2 2 matrices you choose will not commute. In
general, the multiplication of n n matrices is not commutative. The only exception is that
all 1×1 commute (why?)
3.1.1 ×
The Transpose of a Matrix ×
39
40
40
41
(2) the distributive law: Matrix addition and multiplication are dis- tributive:
. Σ . Σ
A B + C = AB + AC and A + B C = AC + BC.
(4) the commutative law for addition: Matrix addition is commu- tative: A + B
= B + A.
Verifying these properties is a routine exercise, so we will omit the details. I suggest working a
couple of examples to convince yourself, if necessary. Though the associative law for
multiplication doesn’t seem to be exciting, it often turns to be extremely useful. We will soon
see some examples of why.
Recall that the n × n identity matrix I n is the matrix having one in each diagonal entry
and zero in each entry off the diagonal. For example,
Note the interesting fact that we can also construct the identity matrix over F2. The off diagonal entries
are 0, of course, and the diagonal entries consist of the nonzero element 1, which is is the multiplicative
identity of F2.
We have
Proposition 3.3. If A is an m n matrix ×
(over R or F2), then AIn = A
and ImA = A.
Proof. This is an exercise in using the definition of multiplication.
41
Determinant of a Matrix
2
0<|x−a|<δ.
This definition is known as ε−δ− or Cauchy definition for limit.
There’s also the Heine definition of the limit of a function, which states that a
function f(x) has a limit L at x=a, if for every sequence {xn}, which has a limit
at a, the sequence f(xn) has a limit L. The Heine and Cauchy definitions of limit of a
function are equivalent.
One-Sided Limits
Let limx→a−0 denote the limit as x goes toward a by taking on values of x such
that x<a. The corresponding limit limx→a−0 f(x) is called the left-hand limit of f(x) at
the point x=a.
Similarly, let limx→a+0 denote the limit as x goes toward a by taking on values
of x such that x>a. The corresponding limit limx→a+0f(x) is called the right-hand
limit of f(x) at x=a.
Note that the 2-sided limit limx→a f(x) exists only if both one-sided limits exist and are
equal to each other, that is limx→a−0f(x) =limx→a+0f(x). In this case,
limx→a f(x)=limx→a−0 f(x)=limx→a+0f(x).
Lecture Notes on Differentiation 4
A tangent line to a function at a point is the line that best approximates the function at that
point better than any other line.
The slope of the function at a given point is the slope of the tangent line to the function at that
point.
The derivative of f that we have been talking about is called the ftrst derivative. Now, we define
the second derivative of a function to be the derivative of f J , denoted by f JJ (x)
2 . Σ
or d f 2(= d d f ) .
dx dx dx
Example 1: Given f (x) = c where c is a constant. Then f J (x) = 0 because the slope of the
function at each point is zero.
Example 2: If f (x) = 2 − 3x , then the derivative f J (x) = 2 because the slope of the
function at each point is 2.
However, f J (0) is not defined because there is no unique tangent line to f (x) at x = 0.
f (x) f J (x)
c 0
mx + c m
xa axa−1
ex ex
ln x 1
x
5
Rules of Differentiation:
1. (f ± g)J = fJ ± gJ
2. (c · f )J = cfJ 6
3. (Product Rule) (f · g) = f g + fg
J J J
. ΣJ J
f f g − fg J (where g(x) ƒ= 0)
4. (Quotient Rule) =
g g
2
5. (Chain Rule) (f ◦ g)J = (f (g(x)))J = fJ(g(x)) · gJ(x)
Deftnition
A point in the domain of a function f at which f J = 0 or f J does not exist is a critical point of
f.
Theorem
Extreme values (local or global) occur only at critical points and endpoints.
Examples:
1. Find absolute maximum and minimum values of f (x) = 4 − x2 on the interval [−3,1].
2. Find absolute maximum and minimum values of f (x) = x2/3 on the interval [−1, 8].
3. Find absolute maximum and minimum values of f (x) = x1/3 on the interval [−1, 1].
x
Example: Given f (x) = .
1 + x2
1 · (1 + x2 ) − x(2x) 1 − x2 (1 + x)(1 − x)
f J (x) = = = .
(1 + x2) 2
2
(1 + x ) 2
(1 + x2)2
We can use the Key Number Method to test the signs of f J (x).
− + −
-1 1
We know that f J (x) is positive on (−1, 1). Thus, f is increasing on (−1, 1).
Also, f J (x) < 0 on ( −∞ , −1) and (1, ∞ ). Thus, f is decreasing on ( −∞ , −1) and (1, ∞ ).
The following is the graph of f (x).
The function f (x) is concave up on the interval (a, b) if f J (x) is increasing on (a, b).
The function f (x) is concave down on the interval (a, b) if f J (x) is increasing on (a, b).
Facts:
• If f JJ (x) > 0 for all x in some interval I, then f J increases on I and thus f is concave up on I.
• If f JJ (x) < 0 for all x in some interval I, then f J decreases on this interval and thus f
is concave down on I.
The inflection point (or point of inflection) of a function f is defined to be the point at which
the concavity changes.
Below is a picture illustrating when a function is concave up or concave down. Notice the tangent
lines and their slopes. A point of inflection is also labeled on the picture.
Note: To find the inflection points, we look at the second derivative. Find all the points such
that f JJ is zero or undefined at those points. Then use the Key Number Method to test the sign
changes of f JJ at those points.
Examples:
1. f (x) = x3 − 12x − 5.
2. f (x) = x4 − 4x3 + 10.
Examples from Economics
Suppose that
r(x) = the revenue from selling x items 9
c(x) = the cost of producing the x items
p(x) = r(x) − c(x) = the profit from producing and selling x items.
The marginal revenue, marginal cost, and marginal proftt when producing and selling x
items are the derivatives
dr
= marginal revenue,
dx
dc
= marginal cost,
dx
dp
= marginal profit.
dx
Therefore,
At a production level yielding maximum profit, marginal revenue equals marginal cost.
Figure. The graph of a typical cost function starts concave down and later turns concave
up. It crosses the revenue curve at the break-even point B. To the left of B, the company
operates at a loss. To the right, the company operates at a profit, with the maximum profit
occurring where cJ (x) = rJ (x). Farther to the right, cost exceeds revenue (perhaps because of a
combination of rising labor and materials costs and market saturation) and production levels
become unprofitable again.
Integration
Mean Value Theorem Suppose f (x) is continuous on [a, b] and differentiable on (a, b).
Then there exists a point c in (a, b) at which 10
f (b) − f (a)
= f J (c). (1)
b−a
Corollary 1 If f J (x) = 0 at each point of an interval I, then f (x) = C for all x in I, where
C is a constant.
Corollary 2 If f J (x) = g J (x) at each point of an interval I, then there exists a constant C
such that f (x) = g(x) + C for all x in I.
A function, F (x), is an antiderivative of a function f (x) if F J (x) = f (x) for all x in the
domain of f .
The set of all antiderivative of f is the indefinite integral of f with respect to x, denoted
by
∫ f (x)dx
∫
The symbol is an integral sign. The function f (x) is the integrand of the integral, and
x is the variable of integration.
To verify ∫xexdx = xex − ex + C, we take the derivative of the right hand side.
d
xex − ex + C = ex + xex x x
− e = xe . Thus, the integral statement is correct.
dx
11
∫ f (x)dx = (signed or net) area between the curve and x-axis from a to b.
a
The number a is called the lower limit and the number b is called the upper limit.
Note: If the curve from a to b is below the x-axis, the definite integral of f (x) from a
to b will be negative.
12
If part of the curve from a to b is below the x-axis and part of it is above the x-axis, the definite
integral of f (x) from a to b could be zero.
Left Endpoint Rule: using rectangles with left top corner on the curve Right
Endpoint Rule: using rectangles with right top corner on the curve Midpoint Rule:
using rectangles with top midpoint on the curve
13
Example: Find the area of the region between x-axis and the graph of f (x) = x3 − x2 −
2x, −1 ≤ x ≤ 2.
If the graph of the function is not given, you may want to sketch the graph first and see what
are the regions. We also want to factor the function and find the x-intercepts. Thus, f (x) = x3 − x2
− 2x = x(x2 − x − 2) = x(x + 1)(x − 2). See the following:
Since from x = 1−to x = 0, the curve is positive and from x = 0 to x = 2, the curve is negative,
−
we can integrate the function from x = 1 to x = 0 and from x = 0 to x = 2 separately.
Note that the first integral is positive but the second is negative. Thus, the total area
Combinatorics
15
An area of mathematics primarily concerned with counting, both as a means and an end in obtaining results,
and certain properties of finite structures.
The Sum Rule: If there are n(A) ways to do A and, distinct from them, n(B) ways to do B, then the number of
ways to do A or B is n(A) + n(B). This rule generalizes: there are n(A) + n(B) + n(C) ways to do A or B or C
Example: A woman has decided to shop at one store today, either in the north part of town or the south part of town.
If she visits the north part of town, she will shop at either a mall, a furniture store, or a jewelry store (3 ways). If she
visits the south part of town then she will shop at either a clothing store or a shoe store (2 ways).
Thus there are 3+2=5 possible shops the woman could end up shopping at today.
The Product Rule: If there are n(A) ways to do A and n(B) ways to do B, then the number of ways to do A and B
is n(A) × n(B). This is true if the number of ways of doing A and B are independent; the number of choices for
doing B is the same regardless of which choice you made for A. Again, this generalizes. There are n(A)×n(B)×n(C)
ways to do A and B and C
Example: A traveling salesman wants to do a tour of all 50 state capitals. How many ways can he do this?
Answer: 50 choices for the first place to visit, 49 for the second, . . . : 50! altogether.
Permutations
A permutation of n things taken r at a time, written P(n, r), is an arrangement in a row of r things, taken from a set of
n distinct things. Order matters.
Example : How many permutations are there of 5 things taken 3 at a time?
Answer: 5 choices for the first thing, 4 for the second, 3 for the third: 5 × 4 × 3 = 60.
If the 5 things are a, b, c, d, e, some possible permutations are: abc abd abe acb acd ace adb adc ade aeb aec aed
...
In general
Combinations
A combination of n things taken r at a time, written C(n, r) or (“n choose r”) is any
16subset of r things
from n things. Order makes no difference.
Example : How many ways are there of choosing 3 things from 5?
Answer : If order mattered, then it would be 5 × 4 × 3. Since order doesn’t matter, abc, acb, bac, bca, cab, cba are all
the same.
• For way of choosing three elements, there are 3! = 6 ways of ordering them.
Therefore, the right answer is (5 × 4 × 3)/3! = 10: abc abd abe acd ace ade bcd bce bde cde
In general
Probability and Statistics
17 throwing a
Probability refers to the extent of occurrence of events. When an event occurs like
ball, picking a card from deck, etc ., then the must be some probability associated with that event.
In terms of mathematics, probability refers to the ratio of wanted outcomes to the total number of
possible outcomes. There are three approaches to the theory of probability, namely:
1. Empirical Approach
2. Classical Approach
3. Axiomatic Approach
Basic Terminologies:
Random Event :- If the repetition of an experiment occurs several times under similar
conditions, if it does not produce the same outcome everytime but the outcome in a trial is
one of the several possible outcomes, then such an experiment is called random event or a
probabilistic event.
Elementary Event – The elementary event refers to the outcome of each random event
performed. Whenever the random event is performed, each associated outcome is known as
elementary event.
Sample Space – Sample Space refers tho the set of all possible outcomes of a random
event.Example, when a coin is tossed, the possible outcomes are head and tail.
Event – An event refers to the subset of the sample space associated with a random event.
Occurrence of an Event – An event associated with a random event is said to occur if any
one of the elementary event belonging to it is an outcome.
Sure Event – An event associated with a random event is said to be sure event if it always
occurs whenever the random event is performed.
Impossible Event – An event associated with a random event is said to be impossible event
if it never occurs whenever the random event is performed.
Compound Event – An event associated with a random event is said to be compound event
if it is the disjoint union of two or more elementary events.
Mutually Exclusive Events – Two or more events associated with a random event are said
to be mutually exclusive events if any one of the event occurrs, it prevents the occurrence of
all other events.This means that no two or more events can occur simultaneously at the same
time.
Exhaustive Events – Two or more events associated with a random event are said to be
exhaustive events if their union is the sample space.
Probability of an Event – If there are total p possible outcomes associated with a random
experiment and q of them are favourable outcomes to the event A, then the probability of event A
is denoted by P(A) and is given by
P(A) = q/p
The probability of non occurrence of event A, i.e, P(A’) = 1 – P(A)
Note –
If the value of P(A) = 1, then event A is called sure event .
If the value of P(A) = 0, then event A is called impossible event.
Also, P(A) + P(A’) = 1
Theorems:
General – Let A, B, C are the events associated with a random experiment, then
1. P(A∪B) = P(A) + P(B) – P(A∩B)
2. P(A∪B) = P(A) + P(B) if A and B are mutually exclusive
3. P(A∪B∪C) = P(A) + P(B) + P(C) – P(A∩B) – P(B∩C)- P(C∩A) + P(A∩B∩C)
4. P(A∩B’) = P(A) – P(A∩B)
5. P(A’∩B) = P(B) – P(A∩B) 18
Extension of Multiplication Theorem – Let A1, A2, ….., An are n events associated with a
random experiment, then
P(A1∩A2∩A3 ….. An) = P(A1)P(A2/A1)P(A3/A2∩A1) ….. P(An/A1∩A2∩A3∩ ….. ∩An-1)
Example-1: A bag contains 10 oranges and 20 apples out of which 5 apples and 3 oranges are
defective .If a person takes out two at random, what is the probability that either both are good or
both are apples ?
Solution –
Out of 30 items, two can selected in 30C2 ways .
Thus, Total elementary events = 30C2 .
Consider the events :
A = Getting two apples
B = Getting two good items
Required Probability is :
P(A∪B) = P(A) + P(B) – P(A∩B) …(i)
There are 20 apples, out of which 2 can drawn in 20C2 ways .
P(A) = 20C2/30C2
There are 8 defective items and 22 are good, Out of 22 good items, two can be can drawn
in 22C2 ways .
P(B) = 22C2/30C2
Sice there are 15 items which are good apples, out of which 2 can be selected in 15C2 ways .
P(A∩B) = 15C2/30C2
Substituting the values of P(A), P(B) and P(A∩B) in (i)
Required probability is = (20C2/30C2) + (22C2/30C2) – (15C2/30C2)
= 316/435
Example-2: The probability that a person will get an electric contract is 2/5 and probability that
he will not get plumbing contract is 4/7 . If the probability of getting at least one contact is 2/3,
what is the probability of getting both ?
Solution:
Consider the two events:
A = Person gets electric contract
B = Person gets plumbing contract
We have,
P(A) = 2/5
P(B’) = 4/7
P(A∪B) = 2/3
Now,
P(A∩B) = P(A) + P(B) – P(A∪B)
= (2/5) + (1 – 4/7) – (2/3)
= 17/105
Total Law of Probability – Let S be the sample space associated with a random experiment and
E1, E2, …, En be n mutually exclusive and exhaustive events associated with the random
experiment . If A is any event which occurs with E1 or E2 or … or En, then
P(A) = P(E1)P(A/E1) + P(E2)P(A/E2) + ... + P(En)P(A/En)
Example-1: A bag contains 3 black balls and 4 red balls .A second bag contains 4 black balls and
2 red balls .One bag is selected at random .From the selected bag, one ball is drawn . Find the
probability that the ball drawn is red.
Solution:
A red ball can be drawn in two ways:
1. Selecting bag I and then drawing a red ball from it .
2. Selecting bag II and then drawing a red ball from it . 19
Let E1, E2 and A be the defined events as follows :
E1 = Selecting bag I
E2 = Selecting bag II
A = Drawing red ball
Since selecting one of the two bags at random .
P(E1) = 1/2
P(E2) = 1/2
Now, probability of drawing red ball when first bag has been chosen
P(A/E1) = 4/7
and, probability of drawing red ball when second bag has been chosen
P(A/E2) = 2/6
Using total law of probability, we have
P(A) = P(E1)P(A/E1) + P(E2)P(A/E2)
= (1/2)(4/7) + (1/2)(2/6)
= 19/42
Hence, the probability of drawing a red ball is 19/42
Example-2: In a bulb factory, three machines namely A, B, C produces 25%, 35% and 40% of the
total bulbs respectively . Of their output, 5, 4 and 2 percent are defective bulbs respectively . A
bulb is drawn is drawn at random from products . What is the probability that bulb drawn is
defective ?
Solution:
Let E1, E2, E3 and A be the defined events as follows :
E1 = The bulb is manufactured by machine A
E2 = The bulb is manufactured by machine B
E3 = The bulb is manufactured by machine C
A = The bulb is defective
According to given conditions ;
P(E1) = 25/100
P(E2) = 35/100
P(E3) = 40/100
Now, probability that the bulb is defective given that is produced by Machine A
P(A/E1) = 5/100
and, probability that the bulb is defective given that is produced by Machine B
P(A/E2) = 4/100
and, probability that the bulb is defective given that is produced by Machine C
P(A/E3) = 2/100
Using total law of probability, we have
P(A) = P(E1)P(A/E1) + P(E2)P(A/E2) + P(E3)P(A/E3)
= (25/100)(5/100) + (35/100)(4/100) + (40/100)(2/100)
= 0.0345
Hence, the probability that the bulb is defective is 0.0345
Mean
Mean is average of a given set of data. Let us consider below example 2, 4, 4, 4, 5, 5, 7, 9 the
mean (average) of a given set of data is 5
20
Median
Median is the middle value of a set of data. To determine the median value in a sequence of
numbers, the numbers must first be arranged in ascending order.
If there is an odd amount of numbers, the median value is the number that is in the middle, with the
same amount of numbers below and above.
If there is an even amount of numbers in the list, the median is the average of the two middle values.
Mode
Mode is the value which occurs most frequently in a set of observations. For example, {6, 3, 9, 6,
6, 5, 9, 3} the Mode is 6, as it occurs most often.
Fact about Mode :
1. Sometimes there can be more than one mode.Having two modes is called bimodal.Having more than
two modes is called multimodal.
2. There is an empirical relationship between Mean, Median, and Mode.
Mean – Mode = 3 [ Mean – Median ]
3. Mode can be useful for qualitative data.
4. Mode can be located graphically.
5. Mode can be computed in an open-end frequency table.
6. Mode is not affected by extremely large or small values.
Formula for Mode of grouped data :
22
Boolean Laws
There are six types of Boolean Laws.
Commutative law
Any binary operation which satisfies the following expression is referred to as commutative
operation.
Commutative law states that changing the sequence of the variables does not have any effect on
the output of a logic circuit.
Associative law
This law states that the order in which the logic operations are performed is irrelevant as their
effect is the same.
Distributive law
Distributive law states the following condition.
AND law
These laws use the AND operation. Therefore they are called as AND laws.
OR law
These laws use the OR operation. Therefore they are called as OR laws.
25
INVERSION law
This law uses the NOT operation. The inversion law states that double inversion of a variable
results in the original variable itself.
Logic gates
Logic gates are the basic building blocks of any digital system. It is an electronic circuit having
one or more than one input and only one output. The relationship between the input and the
output is based on a certain logic. Based on this, logic gates are named as AND gate, OR gate,
NOT gate etc.
AND Gate
A circuit which performs an AND operation is shown in figure. It has n input (n >= 2) and one
output.
Logic diagram
Truth Table
OR Gate
A circuit which performs an OR operation is shown in figure. It has n input (n >= 2) and one
output.
Logic diagram
26
Truth Table
NOT Gate
NOT gate is also known as Inverter. It has one input A and one output Y.
Logic diagram
Truth Table
NAND Gate
A NOT-AND operation is known as NAND operation. It has n input (n >= 2) and one output.
Logic diagram
Truth Table
27
NOR Gate
A NOT-OR operation is known as NOR operation. It has n input (n >= 2) and one output.
Logic diagram
Truth Table
XOR Gate
XOR or Ex-OR gate is a special type of gate. It can be used in the half adder, full adder and
subtractor. The exclusive-OR gate is abbreviated as EX-OR gate or sometime as X-OR gate. It
has n input (n >= 2) and one output.
Logic diagram
Truth Table
28
XNOR Gate
XNOR gate is a special type of gate. It can be used in the half adder, full adder and subtractor.
The exclusive-NOR gate is abbreviated as EX-NOR gate or sometime as X-NOR gate. It has n
input (n >= 2) and one output.
Logic diagram
Truth Table
Boolean Functions
A Boolean function is a special kind of mathematical function f:Xn→Xf:Xn→X of degree n,
where X={0,1}X={0,1} is a Boolean domain and n is a non-negative integer. It describes the
way how to derive Boolean output from Boolean inputs.
Example − Let, F(A,B)=A′B′F(A,B)=A′B′. This is a function of degree 2 from the set of ordered
pairs of Boolean variables to the
set {0,1}{0,1} where F(0,0)=1,F(0,1)=0,F(1,0)=0F(0,0)=1,F(0,1)=0,F(1,0)=0 and F(1,1)=0F(
1,1)=0
Boolean Expressions
A Boolean expression always produces a Boolean value. A Boolean expression is composed of a
combination of the Boolean constants (True or False), Boolean variables and logical
29
connectives.
Each Boolean expression represents a Boolean function.
Example − AB′CAB′C is a Boolean expression.
Boolean Identities
Double Complement Law
∼(∼A)=A
Complement Law
A+∼A=1 (OR Form)
A.∼A=0 (AND Form)
Idempotent Law
A+A=A (OR Form)
A.A=A (AND Form)
Identity Law
A+0=A (OR Form)
A.1=A (AND Form)
Dominance Law
A+1=1 (OR Form)
A.0=0 (AND Form)
Commutative Law
A+B=B+A (OR Form)
A.B=B.A (AND Form)
Associative Law
A+(B+C)=(A+B)+C)
A.(B.C)=(A.B).C (AND Form)
Absorption Law
A.(A+B)=A
A+(A.B)=A
Simplification Law
A.(∼A+B)=A.B
A+(∼A.B)=A+B
Distributive Law
A+(B.C)=(A+B).(A+C)
A.(B+C)=(A.B)+(A.C)
De-Morgan's Law
∼(A.B)=∼A+∼B
∼(A+B)=∼A.∼B
Canonical Forms
For a Boolean expression there are two kinds of canonical forms −
A B C Term Minterm
0 0 0 x’y’z’ m0
0 0 1 x’y’z m1
0 1 0 x’yz’ m2
0 1 1 x’yz m3
1 0 0 xy’z’ m4
1 0 1 xy’z m5
1 1 0 xyz’ m6
1 1 1 xyz m7
Example
Let, F(x,y,z)=x′y′z′+xy′z+xyz′+xyzF(x,y,z)=x′y′z′+xy′z+xyz′+xyz
Or, F(x,y,z)=m0+m5+m6+m7F(x,y,z)=m0+m5+m6+m7
Hence,
F(x,y,z)=∑(0,5,6,7)F(x,y,z)=∑(0,5,6,7)
Now we will find the complement of F(x,y,z)F(x,y,z)
F′(x,y,z)=x′yz+x′y′z+x′yz′+xy′z′F′(x,y,z)=x′yz+x′y′z+x′yz′+xy′z′
Or, F′(x,y,z)=m3+m1+m2+m4F′(x,y,z)=m3+m1+m2+m4
Hence,
F′(x,y,z)=∑(3,1,2,4)=∑(1,2,3,4)F′(x,y,z)=∑(3,1,2,4)=∑(1,2,3,4)
The Product of Max-terms (POM) or
Product of Sums (POS) form
A maxterm is addition of all variables taken either in their direct or complemented form. Any
Boolean function can be expressed as a product of its 0-maxterms and the inverse of the function
can be expressed as a product of its 1-maxterms. Hence,
F(list of variables) = ππ (list of 0-maxterm indices). 31
and
F'(list of variables) = ππ (list of 1-maxterm indices).
A B C Term Maxterm
0 0 0 x+y+z M0
0 0 1 x + y + z’ M1
0 1 0 x + y’ + z M2
0 1 1 x + y’ + z’ M3
1 0 0 x’ + y + z M4
1 0 1 x’ + y + z’ M5
1 1 0 x’ + y’ + z M6
1 1 1 x’ + y’ + z’ M7
Example
Let F(x,y,z)=(x+y+z).(x+y+z′).(x+y′+z).(x′+y+z)F(x,y,z)=(x+y+z).(x+y+z′).(x+y′+z).(x′+y+z)
Or, F(x,y,z)=M0.M1.M2.M4F(x,y,z)=M0.M1.M2.M4
Hence,
F(x,y,z)=π(0,1,2,4)F(x,y,z)=π(0,1,2,4)
F′′(x,y,z)=(x+y′+z′).(x′+y+z′).(x′+y′+z).(x′+y′+z′)F″(x,y,z)=(x+y′+z′).(x′+y+z′).(x′+y′+z).(x′+y′
+z′)
Or, F(x,y,z)=M3.M5.M6.M7F(x,y,z)=M3.M5.M6.M7
Hence,
F′(x,y,z)=π(3,5,6,7)F′(x,y,z)=π(3,5,6,7)
Logic Gates
Boolean functions are implemented by using logic gates. The following are the logic gates −
NOT Gate
A NOT gate inverts a single bit input to a single bit of output.
A ~A
0 1
1 0
AND Gate
An AND gate is a logic gate that gives a high output only if all its inputs are high, otherwise it
gives low output. A dot (.) is used to show the AND operation.
32
A B A.B
0 0 0
0 1 0
1 0 0
1 1 1
OR Gate
An OR gate is a logic gate that gives high output if at least one of the inputs is high. A plus (+) is
used to show the OR operation.
A B A+B
0 0 0
0 1 1
1 0 1
1 1 1
NAND Gate
A NAND gate is a logic gate that gives a low output only if all its inputs are high, otherwise it
gives high output.
A B ~(A.B)
0 0 1
0 1 1
1 0 1
1 1 0
NOR Gate
An NOR gate is a logic gate that gives high output if both the inputs are low, otherwise it gives
low output.
A B ~(A+B)
0 0 1
0 1 0
1 0 0
1 1 0
33
XOR (Exclusive OR) Gate
An XOR gate is a logic gate that gives high output if the inputs are different, otherwise it gives
low output.
A B A⊕B
0 0 0
0 1 1
1 0 1
1 1 0
A B A X-NOR B
0 0 1
0 1 0
1 0 0
1 1 1
Karnaugh Map
Karnaugh introduced a method for simplification of Boolean functions in an easy way. This
method is known as Karnaugh map method or K-map method. It is a graphical 34 method, which
consists of 2n cells for ‘n’ variables. The adjacent cells are differed only in single bit position.
2 Variable K-Map
The number of cells in 2 variable K-map is four, since the number of variables is two. The
following figure shows 2 variable K-Map.
3 Variable K-Map
The number of cells in 3 variable K-map is eight, since the number of variables is three. The
following figure shows 3 variable K-Map.
4 Variable K-Map
The number of cells in 4 variable K-map is sixteen, since the number of variables is four. The
following figure shows 4 variable K-Map.
35
5 Variable K-Map
The number of cells in 5 variable K-map is thirty-two, since the number of variables is 5. The
following figure shows 5 variable K-Map.
Example
Let us simplify the following Boolean function, f(W, X, Y, Z)= WX’Y’ + WY + W’YZ’ using
K-map.
The given Boolean function is in sum of products form. It is having 4 variables W, X, Y & Z. So,
we require 4 variable K-map. The 4 variable K-map with ones corresponding to the given
product terms is shown in the following figure.
Here, we got three prime implicants WX’, WY & YZ’. All these prime implicants
are essential because of following reasons.
Two ones (m8 & m9) of fourth row grouping are not covered by any other groupings. Only fourth row
grouping covers those two ones.
Single one (m15) of square shape grouping is not covered by any other groupings. Only the square shape
grouping covers that one.
Two ones (m2 & m6) of fourth column grouping are not covered by any other groupings. Only fourth
column grouping covers those two ones.
Therefore, the simplified Boolean function is
f = WX’ + WY + YZ’
Follow these rules for simplifying K-maps in order to get standard product of sums form.
Select the respective K-map based on the number of variables present in the Boolean function.
If the Boolean function is given as product of Max terms form, then place the zeroes at respective Max
term cells in the K-map. If the Boolean function is given as product of sums form, then place the
zeroes in all possible cells of K-map for which the given sum terms are valid.
Check for the possibilities of grouping maximum number of adjacent zeroes. It should be powers of
two. Start from highest power of two and upto least power of two. Highest power is equal to the
number of variables considered in K-map and least power is zero.
Each grouping will give either a literal or one sum term. It is known as prime implicant. The prime
implicant is said to be essential prime implicant, if atleast single ‘0’ is not covered with any other
groupings but only that grouping covers.
Note down all the prime implicants and essential prime implicants. The simplified Boolean function
contains all essential prime implicants and only the required prime implicants.
Note − If don’t care terms also present, then place don’t cares ‘x’ in the respective cells of K-
map. Consider only the don’t cares ‘x’ that are helpful for grouping maximum number of
adjacent zeroes. In those cases, treat the don’t care value as ‘0’.
38
Example
Let us simplify the following Boolean function, $f\left ( X,Y,Z \right )=\prod M\left ( 0,1,2,4
\right )$ using K-map.
The given Boolean function is in product of Max terms form. It is having 3 variables X, Y & Z.
So, we require 3 variable K-map. The given Max terms are M0, M1, M2 & M4. The 3 variable K-
map with zeroes corresponding to the given Max terms is shown in the following figure.
There are no possibilities of grouping either 8 adjacent zeroes or 4 adjacent zeroes. There are
three possibilities of grouping 2 adjacent zeroes. After these three groupings, there is no single
zero left as ungrouped. The 3 variable K-map with these three groupings is shown in the
following figure.
Here, we got three prime implicants X + Y, Y + Z & Z + X. All these prime implicants
are essential because one zero in each grouping is not covered by any other groupings except
with their individual groupings.
Therefore, the simplified Boolean function is
f = (X + Y).(Y + Z).(Z + X)
In this way, we can easily simplify the Boolean functions up to 5 variables using K-map method.
For more than 5 variables, it is difficult to simplify the functions using K-Maps. Because, the
number of cells in K-map gets doubled by including a new variable.
Due to this checking and grouping of adjacent ones (min terms) or adjacent zeros (Max terms)
will be complicated. We will discuss Tabular method in next chapter to overcome the
difficulties of K-map method.
Combinational Circuits
Combinational circuit is a circuit in which we combine the different gates in the circuit, for
example encoder, decoder, multiplexer and demultiplexer. Some of the 39 characteristics of
combinational circuits are following −
The output of combinational circuit at any instant of time, depends only on the levels present at input
terminals.
The combinational circuit do not use any memory. The previous state of input does not have any effect
on the present state of the circuit.
A combinational circuit can have an n number of inputs and m number of outputs.
Block diagram
Half Adder
Half adder is a combinational logic circuit with two inputs and two outputs. The half adder circuit
is designed to add two single bit binary number A and B. It is the basic building block for
addition of two single bit numbers. This circuit has two outputs carry and sum.
Block diagram
Truth Table
Circuit Diagram
40
Full Adder
Full adder is developed to overcome the drawback of Half Adder circuit. It can add two one-bit
numbers A and B, and carry c. The full adder is a three input and two output combinational
circuit.
Block diagram
Truth Table
Circuit Diagram
41
Block diagram
Block diagram
Half Subtractors
Half subtractor is a combination circuit with two inputs and two outputs (difference and borrow).
It produces the difference between the two binary bits at the input and also produces an output
(Borrow) to indicate if a 1 has been borrowed. In the subtraction (A-B), A is called as Minuend
bit and B is called as Subtrahend bit.
Truth Table
Circuit Diagram
43
Full Subtractors
The disadvantage of a half subtractor is overcome by full subtractor. The full subtractor is a
combinational circuit with three inputs A,B,C and two output D and C'. A is the 'minuend', B is
'subtrahend', C is the 'borrow' produced by the previous stage, D is the difference output and C' is
the borrow output.
Truth Table
Circuit Diagram
Multiplexers
Multiplexer is a special type of combinational circuit. There are n-data inputs, one output and m
select inputs with 2m = n. It is a digital circuit which selects one of the n data inputs and routes it
to the output. The selection of one of the n inputs is done by the selected inputs. Depending on
the digital code applied at the selected inputs, one out of n data sources 44 is selected and
transmitted to the single output Y. E is called the strobe or enable input which is useful for the
cascading. It is generally an active low terminal that means it will perform the required operation
when it is low.
Block diagram
2 : 1 multiplexer
4 : 1 multiplexer
16 : 1 multiplexer
32 : 1 multiplexer
Block Diagram
Truth Table
45
Demultiplexers
A demultiplexer performs the reverse operation of a multiplexer i.e. it receives one input and
distributes it over several outputs. It has only one input, n outputs, m select input. At a time only
one output line is selected by the select lines and the input is transmitted to the selected output
line. A de-multiplexer is equivalent to a single pole multiple way switch as shown in fig.
Demultiplexers comes in multiple variations.
1 : 2 demultiplexer
1 : 4 demultiplexer
1 : 16 demultiplexer
1 : 32 demultiplexer
Block diagram
Truth Table
Decoder
A decoder is a combinational circuit. It has n input and to a maximum m = 2n outputs. Decoder is
identical to a demultiplexer without any data input. It performs operations which are exactly
opposite to those of an encoder.
Block diagram
46
Code converters
BCD to seven segment decoders
Nixie tube decoders
Relay actuator
2 to 4 Line Decoder
The block diagram of 2 to 4 line decoder is shown in the fig. A and B are the two inputs where D
through D are the four outputs. Truth table explains the operations of a decoder. It shows that
each output is 1 for only a specific combination of inputs.
Block diagram
Truth Table
Logic Circuit
47
Encoder
Encoder is a combinational circuit which is designed to perform the inverse operation of the
decoder. An encoder has n number of input lines and m number of output lines. An encoder
produces an m bit binary code corresponding to the digital input number. The encoder accepts an
n input digital word and converts it into an m bit another digital word.
Block diagram
Priority encoders
Decimal to BCD encoder
Octal to binary encoder
Hexadecimal to binary encoder
Priority Encoder
This is a special type of encoder. Priority is given to the input lines. If two or more input line are
1 at the same time, then the input line with highest priority will be considered. There are four
input D0, D1, D2, D3 and two output Y0, Y1. Out of the four input D3 has the highest priority and
D0 has the lowest priority. That means if D3 = 1 then Y1 Y1 = 11 irrespective of the other inputs.
Similarly if D3 = 0 and D2 = 1 then Y1 Y0 = 10 irrespective of the other inputs.
Block diagram
48
Truth Table
Logic Circuit
Sequential Circuits
The combinational circuit does not use any memory. Hence the previous state of input does not
have any effect on the present state of the circuit. But sequential circuit has 49
memory so output
can vary based on input. This type of circuits uses previous input, output, clock and a memory
element.
Block diagram
Flip Flop
Flip flop is a sequential circuit which generally samples its inputs and changes its outputs only at
particular instants of time and not continuously. Flip flop is said to be edge sensitive or edge
triggered rather than being level triggered like latches.
Block Diagram
Circuit Diagram
50
Truth Table
Operation
S.N. Condition Operation
1 S = R = 0 : No change
If S = R = 0 then output of NAND gates 3 and 4 are forced to become 1.
Hence R' and S' both will be equal to 1. Since S' and R' are the input of the
basic S-R latch using NAND gates, there will be no change in the state of
outputs.
2 S = 0, R = 1, E = 1
Since S = 0, output of NAND-3 i.e. R' = 1 and E = 1 the output of NAND-
4 i.e. S' = 0.
Hence Qn+1 = 0 and Qn+1 bar = 1. This is reset condition.
3 S = 1, R = 0, E = 1
Output of NAND-3 i.e. R' = 0 and output of NAND-4 i.e. S' = 1.
Hence output of S-R NAND latch is Qn+1 = 1 and Qn+1 bar = 0. This is the
reset condition.
4 S = 1, R = 1, E = 1
As S = 1, R = 1 and E = 1, the output of NAND gates 3 and 4 both are 0
i.e. S' = R' = 0.
Hence the Race condition will occur in the basic NAND latch.
Master Slave JK Flip Flop
Master slave JK FF is a cascade of two S-R FF with feedback from the output of second to input
of first. Master is a positive level triggered. But due to the presence of the inverter
51
in the clock
line, the slave will respond to the negative level. Hence when the clock = 1 (positive level) the
master is active and the slave is inactive. Whereas when clock = 0 (low level) the slave is active
and master is inactive.
Circuit Diagram
Truth Table
Operation
S.N. Condition Operation
1 J = K = 0 (No change)
When clock = 0, the slave becomes active and master is inactive. But since
the S and R inputs have not changed, the slave outputs will also remain
unchanged. Therefore outputs will not change if J = K =0.
2 J = 0 and K = 1 (Reset)
Clock = 1 − Master active, slave inactive. Therefore outputs of the master
become Q1 = 0 and Q1 bar = 1. That means S = 0 and R =1.
Clock = 0 − Slave active, master inactive. Therefore outputs of the slave
become Q = 0 and Q bar = 1.
Again clock = 1 − Master active, slave inactive. Therefore even with the
changed outputs Q = 0 and Q bar = 1 fed back to master, its output will be
Q1 = 0 and Q1 bar = 1. That means S = 0 and R = 1.
Hence with clock = 0 and slave becoming active the outputs of slave will
remain Q = 0 and Q bar = 1. Thus we get a stable output from the Master
slave. 52
3 J = 1 and K = 0 (Set)
Clock = 1 − Master active, slave inactive. Therefore outputs of the master
become Q1 = 1 and Q1 bar = 0. That means S = 1 and R =0.
Clock = 0 − Slave active, master inactive. Therefore outputs of the slave
become Q = 1 and Q bar = 0.
Again clock = 1 − then it can be shown that the outputs of the slave are
stabilized to Q = 1 and Q bar = 0.
4 J = K = 1 (Toggle)
Clock = 1 − Master active, slave inactive. Outputs of master will toggle.
So S and R also will be inverted.
Clock = 0 − Slave active, master inactive. Outputs of slave will toggle.
These changed output are returned back to the master inputs. But since
clock = 0, the master is still inactive. So it does not respond to these
changed outputs. This avoids the multiple toggling which leads to the race
around condition. The master slave flip flop will avoid the race around
condition.
Block Diagram
Circuit Diagram
53
Truth Table
Operation
S.N. Condition Operation
1 E=0
Latch is disabled. Hence no change in output.
2 E = 1 and D = 0
If E = 1 and D = 0 then S = 0 and R = 1. Hence irrespective of the present state, the
next state is Qn+1 = 0 and Qn+1 bar = 1. This is the reset condition.
3 E = 1 and D = 1
If E = 1 and D = 1, then S = 1 and R = 0. This will set the latch and Q n+1 = 1 and
Qn+1 bar = 0 irrespective of the present state.
Symbol Diagram
Block Diagram
54
Truth Table
Operation
S.N. Condition Operation
Sequence of instructions
Computer Programming Language
To understand these terms, consider a situation when someone asks you about how to go to a
nearby KFC. What exactly do you do to tell him the way to go to KFC?
You will use Human Language to tell the way to go to KFC, something as follows −
First go straight, after half kilometer, take left from the red light and then drive around one kilometer and you
will find KFC at the right.
Here, you have used English Language to give several steps to be taken to reach KFC. If they are
followed in the following sequence, then you will reach KFC −
1. Go straight
2. Drive half kilometer
3. Take left
4. Drive around one kilometer
5. Search for KFC at your right side
Now, try to map the situation with a computer program. The above sequence of instructions is
actually a Human Program written in English Language, which instructs on how to reach KFC
from a given starting point. This same sequence could have been given in Spanish, Hindi, Arabic,
or any other human language, provided the person seeking direction knows any of these
languages.
Now, let's go back and try to understand a computer program, which is a sequence of instructions
written in a Computer Language to perform a specified task by the computer. Following is a
simple program written in Python programming Language −
print "Hello, World!"
The above computer program instructs the computer to print "Hello, World!" on the computer
screen.
A computer program is also called a computer software, which can range from two lines to millions of
lines of instructions.
Computer program instructions are also called program source code and computer programming is
also called program coding.
A computer without a computer program is just a dump box; it is programs that make computers active.
As we have developed so many languages to communicate among ourselves, computer scientists
have developed several computer-programming languages to provide instructions to the computer
(i.e., to write computer programs). We will see several computer programming languages in the
subsequent chapters. 56
Java
C
C++
Python
PHP
Perl
Ruby
Computer Programmer
Someone who can write computer programs or in other words, someone who can do computer
programming is called a Computer Programmer.
Based on computer programming language expertise, we can name a computer programmers as
follows −
C Programmer
C++ Programmer
Java Programmer
Python Programmer
PHP Programmer
Perl Programmer
Ruby Programmer
Algorithm
From programming point of view, an algorithm is a step-by-step procedure to resolve any
problem. An algorithm is an effective method expressed as a finite set of well-defined
instructions.
Thus, a computer programmer lists down all the steps required to resolve 57 a problem before
writing the actual code. Following is a simple example of an algorithm to find out the largest
number from a given list of numbers −
1. Get a list of numbers L1, L2, L3....LN
2. Assume L1 is the largest, Largest = L1
3. Take next number Li from the list and do the following
4. If Largest is less than Li
5. Largest = Li
6. If Li is last number from the list then
7. Print value stored in Largest and come out
8. Else repeat same process starting from step 3
The above algorithm has been written in a crude way to help beginners understand the concept.
You will come across more standardized ways of writing computer algorithms as you move on to
advanced levels of computer programming.
Though Environment Setup is not an element of any Programming Language, it is the first step to
be followed before setting on to write a program.
When we say Environment Setup, it simply implies a base on top of which we can do our
programming. Thus, we need to have the required software setup, i.e., installation on our PC
which will be used to write computer programs, compile, and execute them. For example, if you
need to browse Internet, then you need the following setup on your machine −
Text Editor
A text editor is a software that is used to write computer programs. Your Windows machine must
have a Notepad, which can be used to type programs. You can launch it by following these steps
−
Start Icon → All Programs → Accessories → Notepad → Mouse Click on Notepad
It will launch Notepad with the following window −
You can use this software to type your computer program and save it in a file at any location.
You can download and install other good editors like Notepad++, which is freely available.
If you are a Mac user, then you will have TextEdit or you can install some other commercial
editor like BBEdit to start with.
Compiler?
You write your computer program using your favorite programming language and save it in a
text file called the program file.
Now let us try to get a little more detail on how the computer understands a program written by
you using a programming language. Actually, the computer cannot understand 59 your program
directly given in the text format, so we need to convert this program in a binary format, which
can be understood by the computer.
The conversion from text program to binary file is done by another software called Compiler and
this process of conversion from text formatted program to binary format file is called program
compilation. Finally, you can execute binary file to perform the programmed task.
We are not going into the details of a compiler and the different phases of compilation.
The following flow diagram gives an illustration of the process −
So, if you are going to write your program in any such language, which needs compilation like C,
C++, Java and Pascal, etc., then you will need to install their compilers before you start
programming.
Interpreter
We just discussed about compilers and the compilation process. Compilers are required in case
you are going to write your program in a programming language that needs to be compiled into
binary format before its execution.
There are other programming languages such as Python, PHP, and Perl, which do not need any
compilation into binary format, rather an interpreter can be used to read such programs line by
line and execute them directly without any further conversion.
60
So, if you are going to write your programs in PHP, Python, Perl, Ruby, etc., then you will need
to install their interpreters before you start programming.
Let's discuss about a very simple but very important concept available in almost all the
programming languages which is called data types. As its name indicates, a data type represents
a type of the data which you can process using your computer program. It can be numeric,
alphanumeric, decimal, etc.
Let’s keep Computer Programming aside for a while and take an easy example of adding two
whole numbers 10 & 20, which can be done simply as follows −
10 + 20
Let's take another problem where we want to add two decimal numbers 10.50 & 20.50, which
will be written as follows −
10.50 + 20.50
The two examples are straightforward. Now let's take another example where we want to record
student information in a notebook. Here we would like to record the following information −
Name:
Class:
Section:
Age:
Sex:
Now, let's put one student record as per the given requirement −
Name: Zara Ali
61
Class: 6th
Section: J
Age: 13
Sex: F
The first example dealt with whole numbers, the second example added two decimal numbers,
whereas the third example is dealing with a mix of different data. Let's put it as follows −
Student name "Zara Ali" is a sequence of characters which is also called a string.
Student class "6th" has been represented by a mix of whole number and a string of two characters. Such
a mix is called alphanumeric.
Student section has been represented by a single character which is 'J'.
Student age has been represented by a whole number which is 13.
Student sex has been represented by a single character which is 'F'.
This way, we realized that in our day-to-day life, we deal with different types of data such as
strings, characters, whole numbers (integers), and decimal numbers (floating point numbers).
Similarly, when we write a computer program to process different types of data, we need to
specify its type clearly; otherwise the computer does not understand how different operations can
be performed on that given data. Different programming languages use different keywords to
specify different data types. For example, C and Java programming languages use int to specify
integer data, whereas char specifies a character data type.
Subsequent chapters will show you how to use different data types in different situations. For
now, let's check the important data types available in C, Java, and Python and the keywords we
will use to specify those data types.
Type Keyword Value range which can be represented by this data type
62 to build more
These data types are called primitive data types and you can use these data types
complex data types, which are called user-defined data type, for example a string will be a
sequence of characters.
Numbers
String
List
Tuple
Dictionary
Here, Number specifies all types of numbers including decimal numbers and string represents a
sequence of characters with a length of 1 or more characters. For now, let's proceed with these
two data types and skip List, Tuple, and Dictionary, which are advanced data types in Python
Variables
Variables are the names you give to computer memory locations which are used to store values in
a computer program.
For example, assume you want to store two values 10 and 20 in your program and at a later stage,
you want to use these two values
Keywords
Like int, long, and float, there are many other keywords supported by C programming language
which we will use for different purpose. Different programming languages provide different set
of reserved keywords, but there is one important & common rule in all the programming
languages that we cannot use a reserved keyword to name our variables, which means we cannot
name our variable like int or float rather these keywords can only be used to specify a variable
data type.
For example, if you will try to use any reserved keyword for the purpose of variable name, then
you will get a syntax error.
double
volatile while
assert finally or
def if return
elif in while
else is with
Arithmetic Operators
Computer programs are widely used for mathematical calculations. We can write a computer
program which can do simple calculation like adding two numbers (2 + 3) and we can also write
a program, which can solve a complex equation like P(x) = x 4 + 7x3 - 5x + 9. If you have been
even a poor student, you must be aware that in first expression 2 and 3 are operands and + is an
operator. Similar concepts exist in Computer Programming.
Take a look at the following two examples −
2+3
P(x) = x4 + 7x3 - 5x + 9.
These two statements are called arithmetic expressions in a programming language
and plus, minus used in these expressions are called arithmetic operators and the values used in
these expressions like 2, 3 and x, etc., are called operands. In their simplest form, such
expressions produce numerical results.
Similarly, a programming language provides various arithmetic operators. The following table
lists down a few of the important arithmetic operators available in C programming language.
Assume variable A holds 10 and variable B holds 20, then −
Relational Operators
Consider a situation where we create two variables and assign them some values as follows −
A = 20
B = 10
Here, it is obvious that variable A is greater than B in values. So, we need the help of some
symbols to write such expressions which are called relational expressions. If we use C
programming language, then it will be written as follows −
(A > B) 66
Here, we used a symbol > and it is called a relational operator and in their simplest form, they
produce Boolean results which means the result will be either true or false. Similarly, a
programming language provides various relational operators. The following table lists down a
few of the important relational operators available in C programming language. Assume
variable A holds 10 and variable B holds 20, then −
== Checks if the values of two operands are equal or not, if yes then (A == B) is not true.
condition becomes true.
!= Checks if the values of two operands are equal or not, if values are (A != B) is true.
not equal then condition becomes true.
> Checks if the value of left operand is greater than the value of right (A > B) is not true.
operand, if yes then condition becomes true.
< Checks if the value of left operand is less than the value of right (A < B) is true.
operand, if yes then condition becomes true.
>= Checks if the value of left operand is greater than or equal to the (A >= B) is not true.
value of right operand, if yes then condition becomes true.
<= Checks if the value of left operand is less than or equal to the value (A <= B) is true.
of right operand, if yes then condition becomes true.
Logical Operators
Logical operators are very important in any programming language and they help us take
decisions based on certain conditions. Suppose we want to combine the result of two conditions,
then logical AND and OR logical operators help us in producing the final result.
The following table shows all the logical operators supported by the C language. Assume
variable A holds 1 and variable B holds 0, then −
&& Called Logical AND operator. If both the operands are non-zero, then condition (A && B) is false.
becomes true.
|| Called Logical OR Operator. If any of the two operands is non-zero, then (A || B) is true.
condition becomes true.
67
! Called Logical NOT Operator. Use to reverses the logical state of its operand. If !(A && B) is true.
a condition is true then Logical NOT operator will make false.
Decision statements
Decision making is critical to computer programming. There will be many situations when you
will be given two or more options and you will have to select an option based on the given
conditions. For example, we want to print a remark about a student based on his secured marks.
Following is the situation −
Assume given marks are x for a student:
If given marks are less than 95 and more than 30, then
Student is average
Now, the question is how to write a programming code to handle such situations. Almost all the
programming languages provide conditional statements that work based on the following flow
diagram −
if...else statement
An if statement can be followed by an optional else statement, which executes when the Boolean
expression is false. The syntax of an if...else statement in C programming language is −
if(boolean_expression) {
68
/* Statement(s) will execute if the boolean expression is true */
} else {
An if...else statement is useful when we have to take a decision out of two options.
if...elseif...else statement
An if statement can be followed by an optional else if...else statement, which is very useful to test
various conditions.
While using if, else if, else statements, there are a few points to keep in mind −
An if can have zero or one else's and it must come after an else if.
An if can have zero to many else…if's and they must come before the else.
Once an else…if succeeds, none of the remaining else…if's or else's will be tested.
The syntax of an if...else if...else statement in C programming language is −
if(boolean_expression 1) {
default :
statement(s);
}
The expression used in a switch statement must give an integer value, which will be compared
for equality with different cases given. Wherever an expression value matches with a case value,
the body of that case will be executed and finally, the switch will be terminated using
a break statement. If no break statements are provided, then the computer continues executing
other statements available below to the matched case. If none of the cases matches, then the
default case body is executed.
The above syntax can be represented in the form of a flow diagram as shown below −
70
Looping statements
Almost all the programming languages provide a concept called loop, which helps in executing one or
more statements up to a desired number of times. All high-level programming languages provide
various forms of loops, which can be used to execute one or more statements repeatedly.
The while Loop
A while loop available in C Programming language has the following syntax −
while ( condition ) {
/*....while loop body ....*/
}
The above code can be represented in the form of a flow diagram as shown below −
71
73
Arrays
Consider a situation where we need to store five integer numbers. If we use programming's simple
variable and data type concepts, then we need five variables of int data type. It was simple, because
we had to store just five integer numbers. Now let's assume we have to store 5000 integer
numbers. Are we going to use 5000 variables?
To handle such situations, almost all the programming languages provide a concept called array.
An array is a data structure, which can store a fixed-size collection of elements of the same data
type. An array is used to store a collection of data, but it is often more useful to think of an array
as a collection of variables of the same type.
Instead of declaring individual variables, such as number1, number2, ..., number99, you just
declare one array variable number of integer type and use number1[0], number1[1], and ...,
number1[99] to represent individual variables. Here, 0, 1, 2, .....99 are index associated
with var variable and they are being used to represent individual elements available in the array.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first
element and the highest address to the last element.
Create Arrays
To create an array variable in C, a programmer specifies the type of the elements and the number
of elements to be stored in that array. Given below is a simple syntax to create an array in C
programming −
type arrayName [ arraySize ];
This is called a single-dimensional array. The arraySize must be an integer constant greater than
zero and type can be any valid C data type. For example, now to declare a 10-element array
called number of type int, use this statement − 74
int number[10];
Here, number is a variable array, which is sufficient to hold up to 10 integer numbers.
Initializing Arrays
You can initialize an array in C either one by one or using a single statement as follows −
int number[5] = {10, 20, 30, 40, 50};
The number of values between braces { } cannot be larger than the number of elements that we
declare for the array between square brackets [ ].
If you omit the size of the array, an array just big enough to hold the initialization is created.
Therefore, if you write −
int number[] = {10, 20, 30, 40, 50};
You will create exactly the same array as you did in the previous example. Following is an
example to assign a single element of the array −
number[4] = 50;
The above statement assigns element number 5th in the array with a value of 50. All arrays have
0 as the index of their first element which is also called the base index and the last index of an
array will be the total size of the array minus 1. The following image shows the pictorial
representation of the array we discussed above −
Functions
A function is a block of organized, reusable code that is used to perform a single, related action.
Functions provide better modularity for your application and a high degree of code reusing. You
have already seen various functions like printf() and main(). These are called built-in functions
provided by the language itself, but we can write our own functions as well and this tutorial will
teach you how to write and use those functions in C programming language.
Good thing about functions is that they are famous with several names. Different programming
75 procedures, etc.
languages name them differently, for example, functions, methods, sub-routines,
If you come across any such terminology, then just imagine about the same concept,
Let's start with a program where we will define two arrays of numbers and then from each array,
we will find the biggest number. Given below are the steps to find out the maximum number
from a given set of numbers −
1. Get a list of numbers L1, L2, L3....LN
2. Assume L1 is the largest, Set max = L1
3. Take next number Li from the list and do the following
4. If max is less than Li
5. Set max = Li
6. If Li is last number from the list then
7. Print value stored in max and come out
8. Else prepeat same process starting from step 3
Let's translate the above program in C programming language −
#include <stdio.h>
int main() {
int set1[5] = {10, 20, 30, 40, 50};
int set2[5] = {101, 201, 301, 401, 501};
int i, max;
Defining a Function
The general form of a function definition in C programming language is as follows −
return_type function_name( parameter list ) {
body of the function
return [expression];
}
A function definition in C programming consists of a function header and a function body. Here
are all the parts of a function −
Return Type − A function may return a value. The return_type is the data type of the value the
function returns. Some functions perform the desired operations without returning a value. In this case,
the return_type is the keyword void.
Function Name − This is the actual name of the function. The function name and the parameter list
together constitute the function signature.
Parameter List − A parameter is like a placeholder. When a function is invoked, you pass a value as a
parameter. This value is referred to as the actual parameter or argument. The parameter list refers to
the type, order, and number of the parameters of a function. Parameters are optional; that is, a function
may contain no parameters.
Function Body − The function body contains a collection of statements that defines what the function
does.
Calling a Function
While creating a C function, you give a definition of what the function has to do. To use a
function, you will have to call that function to perform a defined task.
Now, let's write the above example with the help of a function −
#include <stdio.h>
max = set[0];
i = 1;
while( i < 5 ) {
if( max < set[i] ) {
max = set[i];
}
i = i + 1; 77
}
return max;
}
main() {
int set1[5] = {10, 20, 30, 40, 50};
int set2[5] = {101, 201, 301, 401, 501};
int max;
Calling a Function
While creating a C function, you give a definition of what the function has to do. To use a
function, you will have to call that function to perform the defined task.
When a program calls a function, the program control is transferred to the called function. A
called function performs a defined task and when its return statement is executed or when its
function-ending closing brace is reached, it returns the program control back to the main
program.
To call a function, you simply need to pass the required parameters along with the function name,
and if the function returns a value, then you can store the returned value. For example −
#include <stdio.h>
/* function declaration */
int max(int num1, int num2);
int main () {
return result;
}
We have kept max() along with main() and compiled the source code. While running the final
executable, it would produce the following result −
Max value is : 200
Function Arguments
If a function is to use arguments, it must declare variables that accept the values of the arguments.
These variables are called the formal parameters of the function.
Formal parameters behave like other local variables inside the function and are created upon
entry into the function and destroyed upon exit.
While calling a function, there are two ways in which arguments can be passed to a function −
1 Call by value
This method copies the actual value of an argument into the formal parameter of the function. In this
case, changes made to the parameter inside the function have no effect on the argument.
2 Call by reference
This method copies the address of an argument into the formal parameter. Inside the function, the
address is used to access the actual argument used in the call. This means that changes made to the
parameter affect the argument.
By default, C uses call by value to pass arguments. In general, it means the code within a
function cannot alter the arguments used to call the function.
A scope in any programming is a region of the program where a defined variable can have its
existence and beyond that variable it cannot be accessed. There are three places where variables
can be declared in C programming language −
Inside a function or a block which is called local variables. 79
Local Variables
Variables that are declared inside a function or block are called local variables. They can be used
only by statements that are inside that function or block of code. Local variables are not known to
functions outside their own. The following example shows how local variables are used. Here all
the variables a, b, and c are local to main() function.
#include <stdio.h>
int main () {
/* actual initialization */
a = 10;
b = 20;
c = a + b;
return 0;
}
Global Variables
Global variables are defined outside a function, usually on top of the program. Global variables
hold their values throughout the lifetime of your program and they can be accessed inside any of
the functions defined for the program.
A global variable can be accessed by any function. That is, a global variable is available for use
throughout your entire program after its declaration. The following program show how global
variables are used in a program.
#include <stdio.h>
int main () {
return 0;
}
A program can have same name for local and global variables but the value of local variable
inside a function will take preference. Here is an example −
#include <stdio.h>
int main () {
return 0;
}
When the above code is compiled and executed, it produces the following result −
value of g = 10
Formal Parameters
Formal parameters, are treated as local variables with-in a function and they take precedence over
global variables. Following is an example −
#include <stdio.h>
int main () {
return 0;
}
return a + b;
}
When the above code is compiled and executed, it produces the following result −
value of a in main() = 10
value of a in sum() = 10
value of b in sum() = 20
value of c in main() = 30
Initializing Local and Global Variables
When a local variable is defined, it is not initialized by the system, you must initialize it yourself.
Global variables are initialized automatically by the system when you define them as follows −
int 0
char '\0'
float 0
double 0
pointer NULL
It is a good programming practice to initialize variables properly, otherwise your program may
produce unexpected results, because uninitialized variables will take some garbage value already
available at their memory location.
Terminology 82
Formal Parameter : A variable and its type as they appear in the prototype of the function or
method.
Actual Parameter : The variable or expression corresponding to a formal parameter that appears
in the function or method call in the calling environment.
Modes:
o IN: Passes info from caller to callee.
o OUT: Callee writes values in caller.
o IN/OUT: Caller tells callee value of variable, which may be updated by callee.
1. Pass By Value : This method uses in-mode semantics. Changes made to formal parameter do not
get transmitted back to the caller. Any modifications to the formal parameter variable inside the
called function or method affect only the separate storage location and will not be reflected in the
actual parameter in the calling environment. This method is also called as call by value.
// C program to illustrate
// call by value
#include <stdio.h>
// Passing parameters
func(x, y);
printf("In main, x = %d y = %d\n", x, y);
return 0;
}
83
Output:
In func, a = 12 b = 7
In main, x = 5 y = 7
Languages like C, C++, Java support this type of parameter passing. Java in fact is strictly
call by value.
Shortcomings:
// C program to illustrate
// call by reference
#include <stdio.h>
int main(void)
{
int a = 10, b = 20;
// passing parameters
swapnum(&a, &b);
84
printf("a is %d and b is %d\n", a, b);
return 0;
}
Output:
a is 20 and b is 10
C and C++ both support call by value as well as call by reference whereas Java does’nt
support call by reference.
Shortcomings:
These techniques are older and were used in earlier programming languages like Pascal, Algol and
Fortran. These techniques are not applicable in high level languages.
1. Pass by Result : This method uses out-mode semantics. Just before control is transfered back to
the caller, the value of the formal parameter is transmitted back to the actual parameter.T his
method is sometimes called call by result. In general, pass by result technique is implemented by
copy.
2. Pass by Value-Result : This method uses in/out-mode semantics. It is a combination of Pass-by-
Value and Pass-by-result. Just before the control is transferred back to the caller, the value of the
formal parameter is transmitted back to the actual parameter. This method is sometimes called as
call by value-result
3. Pass by name : This technique is used in programming language such as Algol. In this technique,
symbolic “name” of a variable is passed, which allows it both to be accessed and update.
Example:
To double the value of C[j], you can pass its name (not its value) into the following procedure.
procedure double(x);
real x;
begin
x:=x*2
end;
o The argument expression is re-evaluated each time the formal parameter is passed.
o The procedure can change the values of variables used in the argument expression and
hence change the expression’s value.
Recursion
Recursion is the process of repeating items in a self-similar way. In programming languages, if a
program allows you to call a function inside the same function, then it is called85
a recursive call of
the function.
void recursion() {
recursion(); /* function calls itself */
}
int main() {
recursion();
}
The C programming language supports recursion, i.e., a function to call itself. But while using
recursion, programmers need to be careful to define an exit condition from the function,
otherwise it will go into an infinite loop.
Recursive functions are very useful to solve many mathematical problems, such as calculating the
factorial of a number, generating Fibonacci series, etc.
Number Factorial
The following example calculates the factorial of a given number using a recursive function −
#include <stdio.h>
if(i <= 1) {
return 1;
}
return i * factorial(i - 1);
}
int main() {
int i = 12;
printf("Factorial of %d is %d\n", i, factorial(i));
return 0;
}
When the above code is compiled and executed, it produces the following result −
Factorial of 12 is 479001600
Fibonacci Series
The following example generates the Fibonacci series for a given number using a recursive
function −
#include <stdio.h>
int fibonacci(int i) {
if(i == 0) {
return 0;
}
if(i == 1) { 86
return 1;
}
return fibonacci(i-1) + fibonacci(i-2);
}
int main() {
int i;
return 0;
}
When the above code is compiled and executed, it produces the following result −
0
1
1
2
3
5
8
13
21
34
Stack
A stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is
named stack as it behaves like a real-world stack, for example – a deck of cards or a pile of plates,
etc.
A real-world stack allows operations at one end only. For example, we can place or remove a card
or plate from the top of the stack only. Likewise, Stack ADT allows all data operations at one end
only. At any given time, we can only access the top element of a stack.
This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. Here, the element
which is placed (inserted or added) last, is accessed first. In stack terminology, insertion operation
is called PUSH operation and removal operation is called POP operation.
87
Stack Representation
A stack can be implemented by means of Array, Structure, Pointer, and Linked List. Stack can
either be a fixed size one or it may have a sense of dynamic resizing. Here, we are going to
implement stack using arrays, which makes it a fixed size stack implementation.
Basic Operations
Stack operations may involve initializing the stack, using it and then de-initializing it. Apart from
these basic stuffs, a stack is used for the following two primary operations −
To use a stack efficiently, we need to check the status of stack as well. For the same purpose, the
following functionality is added to stacks −
peek() − get the top data element of the stack, without removing it.
isFull() − check if stack is full.
isEmpty() − check if stack is empty.
At all times, we maintain a pointer to the last PUSHed data on the stack. As this pointer always
represents the top of the stack, hence named top. The top pointer provides top value of the stack
without actually removing it.
88
First we should learn about procedures to support stack functions −
peek()
Algorithm of peek() function −
Example
int peek() {
return stack[top];
}
isfull()
Algorithm of isfull() function −
end procedure
Example
bool isfull() {
if(top == MAXSIZE)
return true;
else
return false;
}
isempty()
Algorithm of isempty() function −
end procedure 89
Example
bool isempty() {
if(top == -1)
return true;
else
return false;
}
Push Operation
The process of putting a new data element onto stack is known as a Push Operation. Push
operation involves a series of steps −
If the linked list is used to implement the stack, then in step 3, we need to allocate space
dynamically.
top ← top + 1 90
stack[top] ← data
end procedure
Example
Accessing the content while removing it from the stack, is known as a Pop Operation. In an array
implementation of pop() operation, the data element is not actually removed, instead top is
decremented to a lower position in the stack to point to the next value. But in linked-list
implementation, pop() actually removes data element and deallocates memory space.
if stack is empty
return null
endif 91
data ← stack[top]
top ← top - 1
return data
end procedure
Example
if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
} else {
printf("Could not retrieve data, Stack is empty.\n");
}
}
Queue
Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at
both its ends. One end is always used to insert data (enqueue) and the other is used to remove data
(dequeue). Queue follows First-In-First-Out methodology, i.e., the data item stored first will be
accessed first.
A real-world example of queue can be a single-lane one-way road, where the vehicle enters first,
exits first. More real-world examples can be seen as queues at the ticket windows and bus-stops.
Queue Representation
As we now understand that in queue, we access both ends for different reasons. The following
diagram given below tries to explain queue representation as data structure −
92
As in stacks, a queue can also be implemented using Arrays, Linked-lists, Pointers and Structures.
For the sake of simplicity, we shall implement queues using one-dimensional array.
Basic Operations
Queue operations may involve initializing or defining the queue, utilizing it, and then completely
erasing it from the memory. Here we shall try to understand the basic operations associated with
queues −
Few more functions are required to make the above-mentioned queue operation efficient. These
are −
peek() − Gets the element at the front of the queue without removing it.
isfull() − Checks if the queue is full.
isempty() − Checks if the queue is empty.
In queue, we always dequeue (or access) data, pointed by front pointer and while enqueing (or
storing) data in the queue we take help of rear pointer.
peek()
This function helps to see the data at the front of the queue. The algorithm of peek() function is as
follows −
Algorithm
Example
int peek() {
return queue[front];
}
isfull()
As we are using single dimension array to implement queue, we just check for the rear pointer to
93
reach at MAXSIZE to determine that the queue is full. In case we maintain the queue in a circular
linked-list, the algorithm will differ. Algorithm of isfull() function −
Algorithm
end procedure
Example
bool isfull() {
if(rear == MAXSIZE - 1)
return true;
else
return false;
}
isempty()
Algorithm of isempty() function −
Algorithm
end procedure
If the value of front is less than MIN or 0, it tells that the queue is not yet initialized, hence empty.
Example
bool isempty() {
if(front < 0 || front > rear)
return true;
else
return false;
}
Enqueue Operation
Queues maintain two data pointers, front and rear. Therefore, its operations are94comparatively
difficult to implement than that of stacks.
The following steps should be taken to enqueue (insert) data into a queue −
Sometimes, we also check to see if a queue is initialized or not, to handle any unforeseen
situations.
if queue is full
return overflow
endif
rear ← rear + 1
queue[rear] ← data
return true
end procedure
Example
int enqueue(int data)
if(isfull())
return 0;
rear = rear + 1; 95
queue[rear] = data;
return 1;
end procedure
Dequeue Operation
Accessing data from the queue is a process of two tasks − access the data where front is pointing
and remove the data after access. The following steps are taken to perform dequeue operation −
if queue is empty
return underflow
end if
data = queue[front]
front ← front + 1
return true
end procedure
int dequeue() {
if(isempty())
return 0; 96
return data;
}
Tree
Trees are graphs that do not contain even a single cycle. They represent hierarchical structure in a
graphical form. Trees belong to the simplest class of graphs. Despite their simplicity, they have a
rich structure.
Trees provide a range of useful applications as simple as a family tree to as complex as trees in
data structures of computer science.
Tree
A connected acyclic graph is called a tree. In other words, a connected graph with no cycles is
called a tree.
The edges of a tree are known as branches. Elements of trees are called their nodes. The nodes
without child nodes are called leaf nodes.
A tree with ‘n’ vertices has ‘n-1’ edges. If it has one more edge extra than ‘n-1’, then the extra
edge should obviously has to pair up with two vertices which leads to form a cycle. Then, it
becomes a cyclic graph which is a violation for the tree graph.
Example 1
The graph shown here is a tree because it has no cycles and it is connected. It has four vertices and
three edges, i.e., for ‘n’ vertices ‘n-1’ edges as mentioned in the definition.
Note − Every tree has at least two vertices of degree one.
Example 2
97
In the above example, the vertices ‘a’ and ‘d’ has degree one. And the other two vertices ‘b’ and
‘c’ has degree two. This is possible because for not forming a cycle, there should be at least two
single edges anywhere in the graph. It is nothing but two edges with a degree of one.
Forest
A disconnected acyclic graph is called a forest. In other words, a disjoint collection of trees is
called a forest.
Example
The following graph looks like two sub-graphs; but it is a single disconnected graph. There are no
cycles in this graph. Hence, clearly it is a forest.
Spanning Trees
H is a tree
H contains all vertices of G.
A spanning tree T of an undirected graph G is a subgraph that includes all of the vertices of G.
Example
98
Clearly, the graph H has no cycles, it is a tree with six edges which is one less than the total
number of vertices. Hence H is the Spanning tree of G.
Circuit Rank
Let ‘G’ be a connected graph with ‘n’ vertices and ‘m’ edges. A spanning tree ‘T’ of G contains
(n-1) edges.
Therefore, the number of edges you need to delete from ‘G’ in order to get a spanning tree = m-(n-
1), which is called the circuit rank of G.
This formula is true, because in a spanning tree you need to have ‘n-1’ edges. Out of ‘m’ edges,
you need to keep ‘n–1’ edges in the graph.
Hence, deleting ‘n–1’ edges from ‘m’ gives the edges to be removed from the graph in order to get
a spanning tree, which should not form a cycle.
Example
Take a look at the following graph −
For the graph given in the above example, you have m=7 edges and n=5 vertices.
Example
Let ‘G’ be a connected graph with six vertices and the degree of each vertex is three. Find the
circuit rank of ‘G’.
6 × 3 = 2|E|
|E| = 9
= 9 – (6 – 1) = 4
Kirchoff’s Theorem
Kirchoff’s theorem is useful in finding the number of spanning trees that can be formed from a
connected graph.
Example
The matrix ‘A’ be filled as, if there is an edge between two vertices, then it should be given as ‘1’,
else ‘0’.
10
0
Tree represents the nodes connected by edges. We will discuss binary tree or binary search tree
specifically.
Binary Tree is a special datastructure used for data storage purposes. A binary tree has a special
condition that each node can have a maximum of two children. A binary tree has the benefits of
both an ordered array and a linked list as search is as quick as in a sorted array and insertion or
deletion operation are as fast as in linked list.
10
1
Important Terms
Path − Path refers to the sequence of nodes along the edges of a tree.
Root − The node at the top of the tree is called root. There is only one root per tree and one
path from the root node to any node.
Parent − Any node except the root node has one edge upward to a node called parent.
Child − The node below a given node connected by its edge downward is called its child
node.
Leaf − The node which does not have any child node is called the leaf node.
Subtree − Subtree represents the descendants of a node.
Visiting − Visiting refers to checking the value of a node when control is on the node.
Traversing − Traversing means passing through nodes in a specific order.
Levels − Level of a node represents the generation of a node. If the root node is at level 0,
then its next child node is at level 1, its grandchild is at level 2, and so on.
keys − Key represents a value of a node based on which a search operation is to be carried
out for a node.
Binary Search tree exhibits a special behavior. A node's left child must have a value less than its
parent's value and the node's right child must have a value greater than its parent value.
10
2
We're going to implement tree using node object and connecting them through references.
Tree Node
The code to write a tree node would be similar to what is given below. It has a data part and
references to its left and right child nodes.
struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};
The basic operations that can be performed on a binary search tree data structure, are the following
−
We shall learn creating (inserting into) a tree structure and searching a data item in a tree in this
chapter. We shall learn about tree traversing methods in the coming chapter.
Insert Operation
The very first insertion creates the tree. Afterwards, whenever an element is to be inserted, first
locate its proper location. Start searching from the root node, then if the data is less than the key
value, search for the empty location in the left subtree and insert the data. Otherwise, search for
the empty location in the right subtree and insert the data.
Algorithm
If root is NULL
then create root node
return
endwhile
insert data
end If
Implementation
The implementation of insert function should look like this −
tempNode->data = data;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
while(1) {
parent = current;
Whenever an element is to be searched, start searching from the root node, then 10 if the data is less
4
than the key value, search for the element in the left subtree. Otherwise, search for the element in
the right subtree. Follow the same algorithm for each node.
Algorithm
If root.data is equal to search.data
return root
else
while data not found
If data found
return node
endwhile
end if
while(current->data != data) {
if(current != NULL)
printf("%d ",current->data);
//not found
if(current == NULL) {
return NULL;
}
return current;
}
}
10
5
Traversal is a process to visit all the nodes of a tree and may print their values too. Because, all
nodes are connected via edges (links) we always start from the root (head) node. That is, we
cannot randomly access a node in a tree. There are three ways which we use to traverse a tree −
In-order Traversal
Pre-order Traversal
Post-order Traversal
Generally, we traverse a tree to search or locate a given item or key in the tree or to print all the
values it contains.
In-order Traversal
In this traversal method, the left subtree is visited first, then the root and later the right sub-tree.
We should always remember that every node may represent a subtree itself.
If a binary tree is traversed in-order, the output will produce sorted key values in an ascending
order.
We start from A, and following in-order traversal, we move to its left subtree B. B is also
traversed in-order. The process goes on until all the nodes are visited. The output of inorder
traversal of this tree will be −
D→B→E→A→F→C→G
Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node. 10
Step 3 − Recursively traverse right subtree. 6
Pre-order Traversal
In this traversal method, the root node is visited first, then the left subtree and finally the right
subtree.
We start from A, and following pre-order traversal, we first visit A itself and then move to its left
subtree B. B is also traversed pre-order. The process goes on until all the nodes are visited. The
output of pre-order traversal of this tree will be −
A→B→D→E→C→F→G
Algorithm
Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.
Post-order Traversal
In this traversal method, the root node is visited last, hence the name. First we traverse the left
subtree, then the right subtree and finally the root node.
10
7
We start from A, and following Post-order traversal, we first visit the left subtree B. B is also
traversed post-order. The process goes on until all the nodes are visited. The output of post-order
traversal of this tree will be −
D→E→B→F→G→C→A
Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.
Searching Algorithms
Searching Algorithms are designed to check for an element or retrieve an element from any data
structure where it is stored. Based on the type of search operation, these algorithms are generally
classified into two categories:
1. Sequential Search: In this, the list or array is traversed sequentially and every element is checked.
For example: Linear Search.
2. Interval Search: These algorithms are specifically designed for searching in sorted data-structures.
These type of searching algorithms are much more efficient than Linear Search as they repeatedly
target the center of the search structure and divide the search space in half. For Example: Binary
Search.
Linear Search
Problem: Given an array arr[] of n elements, write a function to search a given element x in arr[].
Examples :
Input : arr[] = {10, 20, 80, 30, 60, 50,
110, 100, 130, 170}
x = 110; 10
Output : 6 8
Start from the leftmost element of arr[] and one by one compare x with each element of arr[]
If x matches with an element, return the index.
If x doesn’t match with any of elements, return -1.
Binary Search
Given a sorted array arr[] of n elements, write a function to search a given element x in arr[].
A simple approach is to do linear search.The time complexity of above algorithm is O(n).
Another approach to perform the same task is using Binary Search.
Binary Search: Search a sorted array by repeatedly dividing the search interval in half. Begin
with an interval covering the whole array. If the value of the search key is less than the item in the
middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half.
Repeatedly check until the value is found or the interval is empty.
Example :
10
9
The idea of binary search is to use the information that the array is sorted and reduce the time
complexity to O(Log n).
We basically ignore half of the elements just after one comparison.
1. Compare x with the middle element.
2. If x matches with middle element, we return the mid index.
3. Else If x is greater than the mid element, then x can only lie in right half subarray after the mid
element. So we recur for right half.
4. Else (x is smaller) recur for the left half.
Jump Search
Like Binary Search, Jump Search is a searching algorithm for sorted arrays. The basic idea is to
check fewer elements (than linear search) by jumping ahead by fixed steps or skipping some
elements in place of searching all elements.
For example, suppose we have an array arr[] of size n and block (to be jumped) size m. Then we
search at the indexes arr[0], arr[m], arr[2m]…..arr[km] and so on. Once we find the interval
(arr[km] < x < arr[(k+1)m]), we perform a linear search operation from the index km to find the
element x.
Let’s consider the following array: (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610).
Length of the array is 16. Jump search will find the value of 55 with the following steps assuming
that the block size to be jumped is 4.
STEP 1: Jump from index 0 to index 4;
STEP 2: Jump from index 4 to index 8;
STEP 3: Jump from index 8 to index 12;
STEP 4: Since the element at index 12 is greater than 55 we will jump back a step to come to
index 8.
STEP 5: Perform linear search from index 8 to get the element 55.
What is the optimal block size to be skipped?
In the worst case, we have to do n/m jumps and if the last checked value is greater
11 than the
element to be searched for, we perform m-1 comparisons more for linear search.0Therefore the
total number of comparisons in the worst case will be ((n/m) + m-1). The value of the function
((n/m) + m-1) will be minimum when m = √n. Therefore, the best step size is m = √n.
Interpolation Search
Given a sorted array of n uniformly distributed values arr[], write a function to search for a
particular element x in the array.
Linear Search finds the element in O(n) time, Jump Search takes O(√ n) time and Binary
Search take O(Log n) time.
The Interpolation Search is an improvement over Binary Search for instances, where the values in
a sorted array are uniformly distributed. Binary Search always goes to the middle element to
check. On the other hand, interpolation search may go to different locations according to the value
of the key being searched. For example, if the value of the key is closer to the last element,
interpolation search is likely to start search toward the end side.
To find the position to be searched, it uses following formula.
Fibonacci Search
Given a sorted array arr[] of size n and an element x to be searched in it. Return index of x if it is
present in array else return -1.
Examples:
Input: arr[] = {2, 3, 4, 10, 40}, x = 10
Output: 3
Element x is present at index 3.
Basics of Sorting
Sorting refers to arranging data in a particular format. Sorting algorithm specifies the way to
arrange data in a particular order. Most common orders are in numerical or lexicographical order.
The importance of sorting lies in the fact that data searching can be optimized to a very high
level, if data is stored in a sorted manner. Sorting is also used to represent data in more readable
formats. Following are some of the examples of sorting in real-life scenarios −
Telephone Directory − The telephone directory stores the telephone numbers of people sorted by their
names, so that the names can be searched easily.
Dictionary − The dictionary stores words in an alphabetical order so that searching of any word
becomes easy. 11
3
In-place Sorting and Not-in-place Sorting
Sorting algorithms may require some extra space for comparison and temporary storage of few
data elements. These algorithms do not require any extra space and sorting is said to happen in-
place, or for example, within the array itself. This is called in-place sorting. Bubble sort is an
example of in-place sorting.
However, in some sorting algorithms, the program requires space which is more than or equal to
the elements being sorted. Sorting which uses equal or more space is called not-in-place sorting.
Merge-sort is an example of not-in-place sorting.
If a sorting algorithm, after sorting the contents, changes the sequence of similar content in which
they appear, it is called unstable sorting.
Stability of an algorithm matters when we wish to maintain the sequence of original elements,
like in a tuple for example.
Important Terms
Some terms are generally coined while discussing sorting techniques, here is a brief introduction
to them −
Increasing Order
A sequence of values is said to be in increasing order, if the successive element is greater than
the previous one. For example, 1, 3, 4, 6, 8, 9 are in increasing order, as every next element is
greater than the previous element.
Decreasing Order
A sequence of values is said to be in decreasing order, if the successive element is less than the
current one. For example, 9, 8, 6, 4, 3, 1 are in decreasing order, as every next element is less
than the previous element.
Non-Increasing Order
A sequence of values is said to be in non-increasing order, if the successive element is less than
or equal to its previous element in the sequence. This order occurs when the sequence contains
duplicate values. For example, 9, 8, 6, 3, 3, 1 are in non-increasing order, as every next element is
less than or equal to (in case of 3) but not greater than any previous element.
Non-Decreasing Order
A sequence of values is said to be in non-decreasing order, if the successive element is greater
than or equal to its previous element in the sequence. This order occurs when the sequence
contains duplicate values. For example, 1, 3, 3, 6, 8, 9 are in non-decreasing order, as every next
element is greater than or equal to (in case of 3) but not less than the previous one.
11
5
In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we compare 33
with 27.
We find that 27 is smaller than 33 and these two values must be swapped.
Next we compare 33 and 35. We find that both are in already sorted positions.
We know then that 10 is smaller 35. Hence they are not sorted.
We swap these values. We find that we have reached the end of the array. After one iteration, the
array should look like this −
To be precise, we are now showing how an array should look like after each iteration. After the
second iteration, it should look like this −
Notice that after each iteration, at least one value moves at the end.
11 sorted.
And when there's no swap required, bubble sorts learns that an array is completely
6
Algorithm
We assume list is an array of n elements. We further assume that swap function swaps the values
of the given array elements.
begin BubbleSort(list)
return list
end BubbleSort
Insertion sort
This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained which is
always sorted. For example, the lower part of an array is maintained to be sorted. An element
which is to be 'insert'ed in this sorted sub-list, has to find its appropriate place and then it has to
be inserted there. Hence the name, insertion sort.
The array is searched sequentially and unsorted items are moved and inserted into the sorted sub-
list (in the same array). This algorithm is not suitable for large data sets as its average and worst
case complexity are of Ο(n2), where n is the number of items.
It finds that both 14 and 33 are already in ascending order. For now, 14 is in sorted sub-list.
Insertion sort moves ahead and compares 33 with 27. 11
7
It swaps 33 with 27. It also checks with all the elements of sorted sub-list. Here we see that the
sorted sub-list has only one element 14, and 27 is greater than 14. Hence, the sorted sub-list
remains sorted after swapping.
By now we have 14 and 27 in the sorted sub-list. Next, it compares 33 with 10.
So we swap them.
11
8
This process goes on until all the unsorted values are covered in a sorted sub-list. Now we shall
see some programming aspects of insertion sort.
Algorithm
Now we have a bigger picture of how this sorting technique works, so we can derive simple steps
by which we can achieve insertion sort.
Step 1 − If it is the first element, it is already sorted. return 1;
Step 2 − Pick next element
Step 3 − Compare with all elements in the sorted sub-list
Step 4 − Shift all the elements in the sorted sub-list that is greater than the
value to be sorted
Step 5 − Insert the value
Step 6 − Repeat until list is sorted
Selection sort
Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-
based algorithm in which the list is divided into two parts, the sorted part at the left end and the
unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire
list.
The smallest element is selected from the unsorted array and swapped with the leftmost element,
and that element becomes a part of the sorted array. This process continues moving unsorted
array boundary by one element to the right.
This algorithm is not suitable for large data sets as its average and worst case complexities are of
Ο(n2), where n is the number of items.
For the first position in the sorted list, the whole list is scanned sequentially. The first position
where 14 is stored presently, we search the whole list and find that 10 is the lowest value.
11
So we replace 14 with 10. After one iteration 10, which happens to be the minimum value in the
list, appears in the first position of the sorted list. 9
For the second position, where 33 is residing, we start scanning the rest of the list in a linear
manner.
We find that 14 is the second lowest value in the list and it should appear at the second place. We
swap these values.
After two iterations, two least values are positioned at the beginning in a sorted manner.
The same process is applied to the rest of the items in the array.
Following is a pictorial depiction of the entire sorting process −
12
0
Algorithm
Step 1 − Set MIN to location 0
Step 2 − Search the minimum element in the list
Step 3 − Swap with value at location MIN
Step 4 − Increment MIN to point to next element
Step 5 − Repeat until list is sorted
Merge sort
Merge sort is a sorting technique based on divide and conquer technique. With worst-case time
complexity being Ο(n log n), it is one of the most respected algorithms.
Merge sort first divides the array into equal halves and then combines them in a sorted manner.
12
How Merge Sort Works? 1
We know that merge sort first divides the whole array iteratively into equal halves unless the
atomic values are achieved. We see here that an array of 8 items is divided into two arrays of size
4.
This does not change the sequence of appearance of items in the original. Now we divide these
two arrays into halves.
We further divide these arrays and we achieve atomic value which can no more be divided.
Now, we combine them in exactly the same manner as they were broken down. Please note the
color codes given to these lists.
We first compare the element for each list and then combine them into another list in a sorted
manner. We see that 14 and 33 are in sorted positions. We compare 27 and 10 and in the target
list of 2 values we put 10 first, followed by 27. We change the order of 19 and 35 whereas 42 and
44 are placed sequentially.
In the next iteration of the combining phase, we compare lists of two data values, and merge them
into a list of found data values placing all in a sorted order.
After the final merging, the list should look like this −
Now we should learn some programming aspects of merge sorting.
Algorithm
Merge sort keeps on dividing the list into equal halves until it can no more 12 be divided. By
2
definition, if it is only one element in the list, it is sorted. Then, merge sort combines the smaller
sorted lists keeping the new list sorted too.
Step 1 − if it is only one element in the list it is already sorted, return.
Step 2 − divide the list recursively into two halves until it can no more be divided.
Step 3 − merge the smaller lists into new list in sorted order.
Quick sort
Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of data into
smaller arrays. A large array is partitioned into two arrays one of which holds values smaller than
the specified value, say pivot, based on which the partition is made and another array holds
values greater than the pivot value.
Quick sort partitions an array and then calls itself recursively twice to sort the two resulting
subarrays. This algorithm is quite efficient for large-sized data sets as its average and worst case
complexity are of Ο(n2), where n is the number of items.
The pivot value divides the list into two parts. And recursively, we find the pivot for each sub-
lists until all lists contains only one element.
12
3
Graph and its representation
A graph is a pictorial representation of a set of objects where some pairs of objects are connected
by links. The interconnected objects are represented by points termed as vertices, and the links
that connect the vertices are called edges.
Formally, a graph is a pair of sets (V, E), where V is the set of vertices and E is the set of edges,
connecting the pairs of vertices. Take a look at the following graph −
Basic Operations
Following are basic primary operations of a Graph −
Add Vertex − Adds a vertex to the graph.
Add Edge − Adds an edge between the two vertices of the graph.
Display Vertex − Displays a vertex of the graph.
12
We check the stack top for 7return to the previous
node and check if it has any unvisited nodes. Here,
we find D to be on the top of the stack.
As C does not have any unvisited adjacent node so we keep popping the stack until we find a
node that has an unvisited adjacent node. In this case, there's none and we keep popping until the
stack is empty.
As in the example given above, BFS algorithm traverses from A to B to E to F first then to C and
G lastly to D. It employs the following rules.
Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert it in a queue.
Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue.
Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.
12
9
We start from visiting S (starting node), and mark it
as visited.
13
0
Now, S is left with no unvisited adjacent nodes. So,
we dequeue and find A.
At this stage, we are left with no unmarked (unvisited) nodes. But as per the algorithm we keep
on dequeuing in order to get all unvisited nodes. When the queue gets emptied, the program is
over.