AV-223 Programming Overview (Lecture 1)
AV-223 Programming Overview (Lecture 1)
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
• 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
7
Mathematics review
8
LOGARITHMS
• We will begin with a review of logarithms:
If n = em, we define
m = ln( n )
9
LOGARITHMS
• Similarly 1.2.2 specifies
• Proof
10
ARITHMETIC SERIES
• Now wee will look various series
11
Induction
• Proof by Induction
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
n n n 1
F n k
k 0 2
• for all n ≥ 0
13
Induction
• We then proceed by:
• 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
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
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.
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},
n n! n n 1
2 2! n 2 ! 2
{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.
• From this definition we see that f (1)=1, f (2)=6, f(3)= 21 and so on.
22
RECURSION
• C++ Implementation
• 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
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.
• 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
29
REVISION
• Go through section 1.2 and 1.3 in detail
30