5. COUNTING ELEMENTS OF 1-DIMENSIONAL ARRAY
Many computer algorithms requires skill at counting the elements of
one-dimensional array.
Let A[1],A[2],A[3],…,A[n] be a one-dimensional array , where n is a
positive integer.
Suppose....The array is cut at the middle value A[m] so the two sub
arrays are formed:
A[1],A[2],….,A[m]--------(1)
A[1+m],A[2+m],….,A[n]-------(2)
6. CONTD.
☻How many elements does each sub-array have?????
From eq. (1)
Array has the same number of elements as the list of integers from ‘1’
through ‘m’. So by Theorem 1.1,
(The number of elements in list can be calculated if m and n are integers
and m ≤ n, then there are n – m +1 integers from m to n inclusive.)
It has m, or m-1+1, elements.
7. CONTD.
From eq. (2)
Array has the same number of elements as the list of integers from m+1
through n. So by Theorem 1.1
It has n - m, or n – (m+1) +1, elements.
☻What is probability that a random chosen element of the array has an
element subscript.
(1)If n is even? (2)If n is odd?
8. CONTD.
If n is even, each even subscript starting with 2 and ending with n can be
matched up with an integer from 1 to n/2.
1 2 3 4 5 6 7 8 9 10 ….. n
2.1 2.2 2.3 2.4 2.5 2. n/2
So there are n/2 array elements with even subscripts. Since the entire array has
n elements ,the probability that a random chosen element has an even
subscript is
𝑛/2
𝑛
=
1
2
.
9. CONTD.
If n is odd, then the greatest even subscript of the array is n-1 .So there as many
even subscript between 1 and n as there are from 2 through n-1. Then the reasoning
of (i) can be used to conclude that there are (n−1)/2 array elements with even
subscripts.
1 2 3 4 5 6 7 8 9 10 ….. n-1 n
2.1 2.2 2.3 2.4 2.5 ….. 2. (n-1)/2
Since, the entire array has n elements, the probability that a randomly chosen
element has an even subscript is
𝑛−1/2
𝑛
=
𝑛−1
2𝑛
Observe that as n gets larger and
larger ,this probability gets closer and closer to 1/2.
10. RECURSIVELY DEFINED SEQUENCES
A sequence can be defined in a variety of different ways. One informal
way is to write the first few terms with the expectation that the general
pattern will be obvious. We might say, for instance, “consider the
sequence 3, 5, 7,....” Unfortunately, misunderstandings can occur when
this approach is used. The next term of the sequence could be 9 if we
mean a sequence of odd integers, or it could be 11 if we mean the
sequence of odd prime numbers.
11. SECOND WAY
The second way to define a sequence is to give an explicit formula for
its nth term. For example, a sequence a0, a1, a2 . . . can be specified by
writing
an=
−1 𝑛
𝑛+1
for all integers n ≥ 0
Advantages:
The advantage of defining a sequence by such an explicit formula is that
each term of the sequence is uniquely determined and can be computed
in a fixed, finite number of steps by substituting values.
12. THIRD WAY
This requires giving both an equation, called a recurrence relation, that
relates later term in the sequence by reference to earlier terms and also
one or more initial values for the sequence.
Defination:
A recurrence relation for a sequence a0, a1, a2, . . . is a formula that
relates each term, ak to certain of its predecessors ak−1, ak−2, . . . , ak−i ,
where i is an integer with k ≥ i. The initial conditions for such a
recurrence relation specify the values of a0, a1, a2, . . . , ai−1.
13. COMPUTING TERMS OF RECURSIVELY DEFINED
SEQUENCE:-
Define a sequence c0,c1,c2,….. Recursively as follows;
For all integer k ≥ 2
1)ck=ck-1+k*ck-2+1 recursion relation
2)c0=1 and c1=2 initial conditions
Find c2,c3 and c4……
14. CONTD.
c2=c1+2*c0+1 by substituting k=2 into (1)
= 2+(2*1)+1 since c1 = 2 and c0=1 by (2)
=2+2+1
c2 =5 --------(3)
c3=c2+3*c1+1 by substituting k=3 into (1)
=5+(3*2)+1 since c2=5 by (3) and c1=2 by (2)
=5+6+1
c3= 12----------(4)
15. CONTD.
c4= c3+4*c2+1 by substituting k=4 into (1)
=12+(4*5)+1 since c3=12 by(4) and c2=5 by (1)
=12+20+1
=32+1
c4=33
Hence, c2=5 ,c3=12 and c4=33
16. SEQUENCES THAT SATISFY THE SAME RECURRENCE
RELATION
Let a1,a2,a3,... And b1,b2,b3,... Satisfy there recurrence relation that the kth term
equals 3 times the (k−1)st term for all integers k ≥ 2 :
(1) ak=3ak−1 and bk=3bk−1.
But suppose that the initial conditions for the sequences are different:
(2) a1=2 and b1=1.
Find (a) a2,a3,a4 and (b) b2,b3,b4.
19. Let A = {2, 3, 4, 6, 7, 9} and define a relation R on A as follows: For all x, y ∈ A,
x R y ⇔ 3 | (x − y).
Then 2 R 2 because 2 − 2 = 0, and 3 | 0. Similarly, 3 R 3, 4 R 4, 6 R 6, 7 R 7, and 9
R 9. Also 6 R 3 because 6 − 3 = 3, and 3 | 3. And 3 R 6 because 3 − 6 = −(6
− 3) = −3, and 3 | (−3). Similarly, 3 R 9, 9 R 3, 6 R 9, 9 R 6, 4 R 7, and 7 R 4.
Note For reference:
x R y ⇔ 3 | (x − y).
21. CONTD.
This graph has three important properties:
Each point of the graph has an arrow looping around from it back to itself.
In each case where there is an arrow going from one point to a second, there is
an arrow going from the second point back to the first.
In each case where there is an arrow going from one point to a second and from
the second point to a third, there is an arrow going from the first point to the third.
That is there are no “incomplete directed triangles” in the graph.
22. CONTD.
Properties (1), (2), and (3) correspond to properties of general relations
called reflexivity , symmetry, and transitivity.
Defination:
Let R be a relation on a set A.
Reflexive:
R is reflexive if, and only if, for all x ∈ A,x R x.
23. CONTD.
Symmetric:
R is symmetric if, and only if, for all x, y ∈ A, if x R y then y R x.
Transitive:
R is transitive if, and only if, for all x, y, z ∈ A, if x R y and y R z then x R
z.
Because of the equivalence of the expressions x R y and (x, y) ∈ R for
all x and y in A, the reflexive, symmetric, and transitive properties can
also be written as follows:
24. CONTD.
R is reflexive ⇔ for all x in A, (x, x) ∈ R.
R is symmetric ⇔ for all x and y in A, if (x, y) ∈ R then (y, x) ∈ R.
R is transitive ⇔ for all x, y and z in A, if (x, y) ∈ R and (y, z) ∈ R then (x, z) ∈ R.
In informal terms, properties (1)–(3) say the following:
1. Reflexive: Each element is related to itself.
2. Symmetric: If any one element is related to any other element, then the second
element is related to the first.
3. Transitive: If any one element is related to a second and that second element is
related to a third, then the first element is related to the third.
25. THANK YOU FOR LISTENING US.
YOUR’S QUESTIONS,SUGGESTIONS AND CRITICISM ARE WELCOMED!!!!!!!!!!!!!!!