You are on page 1of 30

AV-223

INTRODUCTION TO
ALGORITHMS AND DATA
STRUCTURES
Lecture No 01
Programming: A general Overview
Section 1.2 : Mathematical Review
Section 1.3 : Introduction to Recursion
16 April 2020- Academic Week No 01/18
Number of Slides: 30

1
DESCRIPTION
• Course Title : Introduction to Algorithms and Data Structures

• Credit Hours : 1-1


• Classes : 16
• Labs : 16
• Labs will include a final project submission at the end of the course

• Total Chapter : 8
• One Hour Tests : 2
• Quizzes : 3 or more
• Assignments : 2

2
ASSESSMENT SYSTEM
Theoretical/Instruction 50%
Assignments 5-10%
Quizzes 10-15%
OHT Exams 30-40%
End Semester Exam 35-45%
Total 100%
Practical/Instruction 50%
Labs 70-80%
Viva 20-30%
Total 100%

3
TEXT AND MATERIAL
• Textbook (s)
• Data Structures and Algorithm Analysis in C++ by Mark Allen Weiss,
4th Edition, Pearson Education Limited, 2014.
 
• References Material:
• Introduction to Algorithms by Charles E. Leiserson, Clifford Stein, Ronald
Rivest, and Thomas H. Cormen, 3rd Edition, MIT Press, 2009.
• Data Structures and Algorithms in C++ by Adam Drozdek 4th edition,
Cengage Learning, 2012.
• Algorithms by Robert Sedgewick and Kevin Wayne, 4th Edition, Addison-
Wesley Professional, 2011.

4
LEARNING OBJECTIVES
• Brief Definition
• Data Structures
• Algorithms

• Mathematics Review
• Logarithms
• Arithmetic series
• Proof by induction
• Weighted averages
• Combination
• Introduction to Recursion

5
DATA STRUCTURES
• A data structure is a data organization, management, and storage format
that enables efficient access and modification
Or
• A data structure is a collection of data values, the relationships among
them, and the functions or operations that can be applied to the data

6
ALGORITHMS
• A process or set of rules to be followed in calculations or other problem-
solving operations, especially by a computer

Note:
Different structuring will lead to
different algorithm
For a good algorithm, the data
structure has to be good

1. Find places you can go from home


2. From those, find all paths
3. Keep track of the distance
4. Repeat the process until you reach school
5. Compare the distances you travelled
6. Find the shortest path

7
Mathematics review

8
LOGARITHMS
• We will begin with a review of logarithms:

If n = em, we define
m = ln( n )

• It is always true that eln(n) = n; however, ln(en) = n requires that n is real

9
LOGARITHMS
• Similarly 1.2.2 specifies

• Proof

10
ARITHMETIC SERIES
• Now wee will look various series

• Each term in an arithmetic series is increased by a constant value (usually


1)
n
n  n  1
0 1 2  3   n   k 
k 0 2

11
Induction
• Proof by Induction

• A proof by induction has two standard parts

• The first step is proving a base case


• Next, an inductive hypothesis is assumed
• Using this assumption, the theorem is then shown to be true for the next
value
• This proves the theorem as long as values are finite

12
Induction
• Suppose we have a formula F(n) which we wish to show is true for all
values n ≥ n0
• Usually n0 = 0 or n0 = 1

• For example, we may wish to show that

n n  n  1
F  n   k 
k 0 2
• for all n ≥ 0

13
Induction
• We then proceed by:

• Demonstrating that F(n0) is true

• Assuming that the formula F(n) is true for an arbitrary n

• If we are able to demonstrate that this assumption allows us to also show that the
formula is true for F(n + 1), the inductive principle allows us to conclude that the
formula is true for all n ≥ n0

14
ARITHMETIC SERIES
• Proof (by induction):
The statement is true for n = 0:
0
0 1 0  0  1

i 0
k 0
2

2
• Assume that the statement is true for an arbitrary n:

n
n  n  1
k 
k 0 2

15
ARITHMETIC SERIES
• Using the assumption that

n
n  n  1
i 
i 0 2

• for n, we must show that :

n 1
 n  1  n  2 

k 0
k
2

16
ARITHMETIC SERIES
• Then, for n + 1, we have
n 1 n

 k   n  1   k
k 0 i 0

• By assumption, the second sum is known :


n  n  1
  n  1 
2


 n  1 2   n  1 n
2


 n  1  n  2 
2
17
ARITHMETIC SERIES
• The statement is true for n = 0 and
• The truth of the statement for n implies
• The truth of the statement for n + 1.

• Therefore, by the process of mathematical induction, the statement is true


for all values of n ≥ 0.

18
WEIGHTED AVERAGES
• Given n objects x1, x2, x3, ..., xn, the average is
x1  x2  x3    xn
n
• Given a sequence of coefficients c1 , c2 , c3 , … , cn where

c1  c2  c3    cn  1

• Then we refer to c1 x1  c2 x2  c3 x3    cn xn

as a weighted average
1
c1  c2  c3    cn 
For an average, n

19
COMBINATIONS
• Given n distinct items, in how many ways can you choose k of these?
i.e., “In how many ways can you combine k items from n?”
• For example, given the set {1, 2, 3, 4, 5}, I can choose three of these
in any of the following ways:

{1, 2, 3}, {1, 2, 4}, {1, 2, 5}, {1, 3, 4}, {1, 3, 5},
{1, 4, 5}, {2, 3, 4}, {2, 3, 5}, {2, 4, 5}, {3, 4, 5},

The number of ways such items can be chosen is written


n n!
 
 k  k ! n  k  !
n
where  is
 read as “n choose k”s
k
 
20
COMBINATIONS
• The most common question we will ask in this vein:
• Given n items, in how many ways can we choose two of them?
• In this case, the formula simplifies to:

n n! n  n  1
  
 2  2! n  2  ! 2

• For example, given {0, 1, 2, 3, 4, 5, 6}, we have the following 21 pairs:

{0, 1}, {0, 2}, {0, 3}, {0, 4}, {0, 5}, {0, 6},
{1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6},
{2, 3}, {2, 4}, {2, 5}, {2, 6},
{3, 4}, {3, 5}, {3, 6},
{4, 5}, {4, 6},
{5, 6}
21
INTRODUCTION TO RECURSION
• Recursion is a way of solving problem by having a function call itself.

• Mathematical functions are sometimes defined in a less standard form

• As an example, we can define a function f , valid on nonnegative integers

• That satisfies f (0)=0 and f (x) = 2f (x-1)+x^2

• From this definition we see that f (1)=1, f (2)=6, f(3)= 21 and so on.

• This a function defined in terms of itself is called RECURSIVE

22
RECURSION
• C++ Implementation

• Line 3 and 4 handles what is called the BASE CASE

• Base Case: The value for function is directly known without resorting to
recursion

23
EXAMPLE FOR BETTER UNDERSTANDING

24
EXAMPLE FOR BETTER UNDERSTANDING
• Comprehension

n!=n.(n-1).(n-2)…….3.2.1
n!=n.(n-1)!
(n-1)!=(n-1).(n-2)…3.2.1

2!=2.1!=2 Redefining the Function


1!=1.0!=1 n!={n.(n-1)! If n≥1
0!=0.(-1)! 1 otherwise (if n=0)
But -1! Is not defined

25
EXAMPLE FOR BETTER UNDERSTANDING
• C++ program
int factorial(int n){
if (n>=1) Base Case f(0)=1 (Rule 1)
return (n*factorial (n-1));
else {
return 1;
}
f(1)=1.f(0)
f(2)=2.f(1)
f(3)=3.f(2)
Rule (2) f(4)=4.f(3)
26
RULES OF RECURSION
• When writing recursive routines, it is crucial to keep in mind the four
basic rules of recursion

• Base cases. You must always have some base cases, which can be solved
without recursion.

• Making progress. For the cases that are to be solved recursively, the
recursive call must always be to a case that makes progress toward a base
case.

• Design rule. Assume that all the recursive calls work.

• Compound interest rule. Never duplicate work by solving the same instance
of a problem in separate recursive calls. (Will be elaborated in Chap 3)
27
ASSIGNMENT
• As in slide 25, the factorial function was redefined so that it could be
written in recursion form. The same exercise is to be performed by all
class individual on a sequence called FIBONACCI SEQUENCE.
Submit your assignment on the email address given on first slide with
in one week.

Fibonacci Sequence is that the no coming next is the sum of the two
previous numbers. That is

1,1,2,3,5,8,13 and so on

28
SUMMARY
• Basic definitions of
• Data Structure
• Algorithms

• Mathematics Review

• Brief Description of Recursion

• Example and redefining function for recursion

• Basic rules of recursion

29
REVISION
• Go through section 1.2 and 1.3 in detail

• Question may be posted on the email ID

• Practice C++ basic programming in your own time

30

You might also like