Lecture 1
Introduction & Recap
Class Information
2
Introduction
• Course materials and updates will
be posted in Google Classroom
➢Theory: axfdwfw
➢Lab: 4jtui4k
• If you have any course related
query, you can talk to me in the
office or email me at:
iftekharul.islam@bu.edu.bd
Course Title: Algorithm
Course Code: CSE-2201
Credit: 3.00
Contact
Hours:
3 Hrs./Week
Total Marks: 100
Course Information
3
Introduction
Prerequisites
4
Introduction
Programming
(CSE 1201)
Data Structure
(CSE 2103)
Discrete Math
(CSE 2105)
Strong conceptual clarity
will be assumed
Recursion will come into
play in various chapters
Resources
5
Textbooks:
✓ ‘Introduction to Algorithms’ (3rd Edition)
by Cormen, Leiserson, Rivest and Stein
✓ ‘Fundamentals of Computer
Algorithms’ by Horowitz, Sahni,
Rajasekaran
Recommended Online Reads:
✓ https://www.tutorialspoint.com/design_an
d_analysis_of_algorithms/index.htm
Introduction
Note: Materials from other resources may also be used
in the course for better conceptual clarity.
Evaluation
6
Introduction
Sl
No.
Description
Marks
distribution
1 Mid-term examination 30
2 Assignments/Viva 10
3 Attendance 10
4 Final term examination 50
Total 100
Evaluation (Lab)
7
Introduction
Sl
No.
Description
Marks
distribution
1 Assignments (3-4) 40-80%
2 Viva/Quiz 10%
3 Attendance 10%
4 Final Evaluation 0-40%
Total 100
Tips for Doing Well in this Course
8
Introduction
Review the lecture
topics at home
(using your notes, books
and online resources)
Attend all classes &
be attentive
Take good notes
(during and after class)
Complete given
readings and
assignments
A lot of topics to cover! 😄
(Click here to have a glance)
Data Structure
• The logical or mathematical model of a particular
organization of data is called a data structure.
Data Structures Recap 9
• Linear DS
• the elements are stored in a
linear or sequential order
(logically or conceptually)
• Examples:
• Array
• Linked list
• Stack
• Queue
• Non-linear DS
• the elements are not stored
in a linear or sequential order
(logically or conceptually)
• Examples:
• Tree
• Graph
Classification of Data Structure
Array
• An array is a collection of similar data elements
• stored in consecutive memory locations
• referenced by an index (subscript)
✓ Subscript Notation:
• A1, A2, A3, . . . , An
✓ Parenthesis Notation
• A(1), A(2), A(3),. . . , A(n)
✓ Bracket Notation
• A[1], A[2], A[3], . . . , A[n]
10
An array named A with 8 elements (integers).
Data Structures Recap
Classification of Data Structure
Linked List
• A linked list or one way list is a linear collection of data elements,
called nodes, where the linear order is given by means of pointers.
• Each node is divided into two parts:
– The first part - the data of the element/node
– The second part- the address of the next node
• special pointer: Start (or Head)
11
Data Structures Recap
Classification of Data Structure
Stack
• Last-in-First-out (LIFO)
• A linear list in which
insertion and deletions can
take place only at one end,
called the top.
12
A stack of dishes
https://en.wikipedia.org/wiki/Stack_(abstract_data_type)
Data Structures Recap
TOP
Classification of Data Structure
Queue
• First-in-First-out (FIFO)
• A linear list in which
– Deletions (or dequeue) can take place only at one end of the list (The
front of the list)
– Insertion (or enqueue) can take place only at the other end of the list
(The rear of the list)
13
Data Structures Recap
REAR FRONT
Classification of Data Structure
Tree
• A tree is a non-linear data
structure which consists of a
collection of nodes arranged in
a hierarchical order.
• One of the nodes is designated
as the root node, and the
remaining nodes can be
partitioned into disjoint sets
such that each set is a sub-tree
of the root.
14
Directory Structure in a computer
Data Structures Recap
https://comp.chem.nottingham.ac.uk
Classification of Data Structure
Graphs
• A graph is a non-linear data structure which is a collection of vertices
(also called nodes) and edges that connect these vertices.
• A graph is often viewed as a generalization of the tree structure,
where instead of a purely parent-to-child relationship between tree
nodes, any kind of complex relationships between the nodes can exist.
15
A Graph
Data Structures Recap
Summary
of
Time
Complexities
for
various
DS
(worst
case)
Data structure Access Search Insertion Deletion
Array O(1) O(N)
at the
beginning
in the
middle
at the end
from the
beginning
from the
middle
from the
end
O(N) O(N) O(1) O(N) O(N) O(1)
Singly Linked list O(N) O(N) O(1) O(N)
O(N) or
O(1)
O(1) O(N) O(N)
Doubly Linked List O(N) O(N) O(1) O(N) O(1) O(1) O(N) O(1)
Stack O(N) O(N) O(1) O(1)
Queue O(N) O(N) O(1) O(1)
Binary Search Tree O(N) O(N) O(N) O(N)
AVL Tree O(log N) O(log N) O(log N) O(log N)
Data Structures Recap 16
Summary
of
Time
Complexities
for
various
DS
(worst
case)
Data Structures Recap 17
Graph Storage Add Vertex Add Edge
Remove
Vertex
Remove Edge
Adjacency Matrix O( 𝑉 2) O( 𝑉 2) O(1) O( 𝑉 2) O(1)
Adjacency List O(|V| + |E|) O(1) O(1) O(|V| + |E|) O(|E|)
Recursion
Recursion: What and Why
• Recursion: A programming technique where a function calls itself
continuously until a termination condition is met
• Algorithmically, it is a way of designing solutions to problems by divide
and conquer or decrease and conquer.
• reduce a problem to simpler versions of the same problem
• Many problems can be elegantly specified or solved in a recursive
manner.
Data Structures Recap 19
Example
• Line in ticket counter
Data Structures Recap 20
1
2
3
4
5
6
7
8
9
10
Recursive calls Base case
General Structure of a Recursive Function
• Two major cases in a recursive function:
➢Base case: no further calls to the same function is made.
➢Recursive case: the function calls itself again (recursive call). The
recursive calls should progress in such a way that every time a
recursive call is made it comes closer to the base criteria.
Data Structures Recap 21
General Structure of a Recursive Function
Data Structures Recap 22
rec( input )
{
if ( condition ) {
// base case
.
.
.
}
else {
// recursive case
.
.
.
rec( smaller input );
.
.
.
}
}
[ Note: There can be
multiple base and
recursive cases. ]
Recursive
call
The Working of Function Call
General Memory Layout of a Program:
• When we run a program, it takes up space in
the memory.
• Each running program has its own memory
layout, separated from other programs. The
layout consists of different segments,
including:
1. stack: stores local variables
2. heap: dynamic memory for programmer to
allocate
3. data: stores global variables, separated into
initialized and uninitialized
4. text: stores the code being executed
Data Structures Recap 23
stack
heap
uninitialized data
data
initialized data
text
The Working of Function Call
Use of Stack in Function Call
Whenever a function is called a new activation frame is created with all the
function’s data and this activation frame is pushed in the program stack,
and the stack pointer that always points the top of the program stack
points the activation frame pushed as it is on the top of the program stack.
• Program Stack: the stack which holds all the function calls, with bottom
elements as the main function
• Activation Frame: a buffer memory that is an element of program stack
and has data of the called function i.e., Return Address, Input Parameters,
Local Variables etc.
• Stack Pointer: the pointer that points to the top of program stack i.e., the
most recent function called.
Data Structures Recap 24
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 25
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 26
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 27
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 28
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 29
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
3
x
.
.
.
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 30
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
3
x
.
.
.
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 31
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
3
x
.
.
.
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 32
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
3
x
.
.
.
printHi
3
n
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 33
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
3
x
.
.
.
printHi
3
n
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 34
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
3
x
.
.
.
printHi
3
n
.
.
.
i
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 35
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
3
x
.
.
.
printHi
3
n
.
.
.
i
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 36
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
3
x
.
.
.
printHi
3
n
.
.
.
1
i
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 37
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
3
x
.
.
.
printHi
3
n
.
.
.
1
i
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 38
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
3
x
.
.
.
printHi
3
n
.
.
.
1
i
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 39
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
3
x
.
.
.
printHi
3
n
.
.
.
1
i
Program stack
Output:
Hi!
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 40
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
3
x
.
.
.
printHi
3
n
.
.
.
1
i
Program stack
Output:
Hi!
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 41
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
3
x
.
.
.
printHi
3
n
.
.
.
2
i
Output:
Hi!
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 42
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
3
x
.
.
.
printHi
3
n
.
.
.
2
i
Output:
Hi!
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 43
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
3
x
.
.
.
printHi
3
n
.
.
.
2
i
Output:
Hi!
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 44
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
3
x
.
.
.
printHi
3
n
.
.
.
2
i
Output:
Hi!
Hi!
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 45
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
3
x
.
.
.
printHi
3
n
.
.
.
2
i
Output:
Hi!
Hi!
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 46
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
3
x
.
.
.
printHi
3
n
.
.
.
3
i
Output:
Hi!
Hi!
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 47
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
3
x
.
.
.
printHi
3
n
.
.
.
3
i
Output:
Hi!
Hi!
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 48
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
3
x
.
.
.
printHi
3
n
.
.
.
3
i
Output:
Hi!
Hi!
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 49
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
3
x
.
.
.
printHi
3
n
.
.
.
3
i
Output:
Hi!
Hi!
Hi!
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 50
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
3
x
.
.
.
printHi
3
n
.
.
.
3
i
Output:
Hi!
Hi!
Hi!
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 51
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
3
x
.
.
.
printHi
3
n
.
.
.
4
i
Output:
Hi!
Hi!
Hi!
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 52
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
3
x
.
.
.
printHi
3
n
.
.
.
4
i
Output:
Hi!
Hi!
Hi!
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 53
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
3
x
.
.
.
Output:
Hi!
Hi!
Hi!
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 54
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
main
3
x
.
.
.
Output:
Hi!
Hi!
Hi!
Program stack
The Working of Function Call
Use of Stack in Function Call
Data Structures Recap 55
void printHi(int n) {
int i;
for(i=1; i<=n; i++)
printf(“Hi! n”);
}
int main() {
int x = 3;
printHi(x);
return 0;
}
Output:
Hi!
Hi!
Hi!
Program stack
The Working of Function Call
Use of Stack in Function Call
• Since a recursive function repeatedly calls itself, it makes use of the
program stack to temporarily store the return address, local
variables and other information of the calling function.
• Consequently, recursive algorithms require more memory and
computation compared with iterative algorithms, but they are
simpler and for many cases a natural way of thinking about the
problem.
Data Structures Recap 57
Tracing Recursion
Ex.1: Determine output of the following function for test(3).
Data Structures Recap 58
void test(int n) {
if (n > 0)
{
printf(“%d”, n);
test(n-1);
}
} Check lecture notes
Tracing Recursion
Ex.2: Determine output of the following function for test(3).
Data Structures Recap 59
void test(int n) {
if (n > 0)
{
test(n-1);
printf(“%d”, n);
}
} Check lecture notes
Tracing Recursion
Ex.3: Determine output of the following function for test(3).
Data Structures Recap 60
void test(int n) {
if (n > 0)
{
test(n-1);
printf(“%d”, n);
test(n-1);
}
}
Check lecture notes
Try Yourself!
Ex.4: Determine output of the
following functions for test(3).
Data Structures Recap 61
void test(int n) {
if (n > 0)
{
printf(“%d”, n);
test(n-1);
test(n-1);
}
}
Ex.5: Determine output of the
following functions for test(3).
void test(int n) {
if (n > 0)
{
test(n-1);
test(n-1);
printf(“%d”, n);
}
}
References & Other Resources
➢ Data Structures (Seymour Lischutz)
• Chapter 6
➢ https://www.enjoyalgorithms.com/blog/recursion-explained-how-
recursion-works-in-programming
Data Structures Recap 62

L1 - Recap.pdf

  • 1.
  • 2.
    Class Information 2 Introduction • Coursematerials and updates will be posted in Google Classroom ➢Theory: axfdwfw ➢Lab: 4jtui4k • If you have any course related query, you can talk to me in the office or email me at: iftekharul.islam@bu.edu.bd
  • 3.
    Course Title: Algorithm CourseCode: CSE-2201 Credit: 3.00 Contact Hours: 3 Hrs./Week Total Marks: 100 Course Information 3 Introduction
  • 4.
    Prerequisites 4 Introduction Programming (CSE 1201) Data Structure (CSE2103) Discrete Math (CSE 2105) Strong conceptual clarity will be assumed Recursion will come into play in various chapters
  • 5.
    Resources 5 Textbooks: ✓ ‘Introduction toAlgorithms’ (3rd Edition) by Cormen, Leiserson, Rivest and Stein ✓ ‘Fundamentals of Computer Algorithms’ by Horowitz, Sahni, Rajasekaran Recommended Online Reads: ✓ https://www.tutorialspoint.com/design_an d_analysis_of_algorithms/index.htm Introduction Note: Materials from other resources may also be used in the course for better conceptual clarity.
  • 6.
    Evaluation 6 Introduction Sl No. Description Marks distribution 1 Mid-term examination30 2 Assignments/Viva 10 3 Attendance 10 4 Final term examination 50 Total 100
  • 7.
    Evaluation (Lab) 7 Introduction Sl No. Description Marks distribution 1 Assignments(3-4) 40-80% 2 Viva/Quiz 10% 3 Attendance 10% 4 Final Evaluation 0-40% Total 100
  • 8.
    Tips for DoingWell in this Course 8 Introduction Review the lecture topics at home (using your notes, books and online resources) Attend all classes & be attentive Take good notes (during and after class) Complete given readings and assignments A lot of topics to cover! 😄 (Click here to have a glance)
  • 9.
    Data Structure • Thelogical or mathematical model of a particular organization of data is called a data structure. Data Structures Recap 9 • Linear DS • the elements are stored in a linear or sequential order (logically or conceptually) • Examples: • Array • Linked list • Stack • Queue • Non-linear DS • the elements are not stored in a linear or sequential order (logically or conceptually) • Examples: • Tree • Graph
  • 10.
    Classification of DataStructure Array • An array is a collection of similar data elements • stored in consecutive memory locations • referenced by an index (subscript) ✓ Subscript Notation: • A1, A2, A3, . . . , An ✓ Parenthesis Notation • A(1), A(2), A(3),. . . , A(n) ✓ Bracket Notation • A[1], A[2], A[3], . . . , A[n] 10 An array named A with 8 elements (integers). Data Structures Recap
  • 11.
    Classification of DataStructure Linked List • A linked list or one way list is a linear collection of data elements, called nodes, where the linear order is given by means of pointers. • Each node is divided into two parts: – The first part - the data of the element/node – The second part- the address of the next node • special pointer: Start (or Head) 11 Data Structures Recap
  • 12.
    Classification of DataStructure Stack • Last-in-First-out (LIFO) • A linear list in which insertion and deletions can take place only at one end, called the top. 12 A stack of dishes https://en.wikipedia.org/wiki/Stack_(abstract_data_type) Data Structures Recap TOP
  • 13.
    Classification of DataStructure Queue • First-in-First-out (FIFO) • A linear list in which – Deletions (or dequeue) can take place only at one end of the list (The front of the list) – Insertion (or enqueue) can take place only at the other end of the list (The rear of the list) 13 Data Structures Recap REAR FRONT
  • 14.
    Classification of DataStructure Tree • A tree is a non-linear data structure which consists of a collection of nodes arranged in a hierarchical order. • One of the nodes is designated as the root node, and the remaining nodes can be partitioned into disjoint sets such that each set is a sub-tree of the root. 14 Directory Structure in a computer Data Structures Recap https://comp.chem.nottingham.ac.uk
  • 15.
    Classification of DataStructure Graphs • A graph is a non-linear data structure which is a collection of vertices (also called nodes) and edges that connect these vertices. • A graph is often viewed as a generalization of the tree structure, where instead of a purely parent-to-child relationship between tree nodes, any kind of complex relationships between the nodes can exist. 15 A Graph Data Structures Recap
  • 16.
    Summary of Time Complexities for various DS (worst case) Data structure AccessSearch Insertion Deletion Array O(1) O(N) at the beginning in the middle at the end from the beginning from the middle from the end O(N) O(N) O(1) O(N) O(N) O(1) Singly Linked list O(N) O(N) O(1) O(N) O(N) or O(1) O(1) O(N) O(N) Doubly Linked List O(N) O(N) O(1) O(N) O(1) O(1) O(N) O(1) Stack O(N) O(N) O(1) O(1) Queue O(N) O(N) O(1) O(1) Binary Search Tree O(N) O(N) O(N) O(N) AVL Tree O(log N) O(log N) O(log N) O(log N) Data Structures Recap 16
  • 17.
    Summary of Time Complexities for various DS (worst case) Data Structures Recap17 Graph Storage Add Vertex Add Edge Remove Vertex Remove Edge Adjacency Matrix O( 𝑉 2) O( 𝑉 2) O(1) O( 𝑉 2) O(1) Adjacency List O(|V| + |E|) O(1) O(1) O(|V| + |E|) O(|E|)
  • 18.
  • 19.
    Recursion: What andWhy • Recursion: A programming technique where a function calls itself continuously until a termination condition is met • Algorithmically, it is a way of designing solutions to problems by divide and conquer or decrease and conquer. • reduce a problem to simpler versions of the same problem • Many problems can be elegantly specified or solved in a recursive manner. Data Structures Recap 19
  • 20.
    Example • Line inticket counter Data Structures Recap 20 1 2 3 4 5 6 7 8 9 10 Recursive calls Base case
  • 21.
    General Structure ofa Recursive Function • Two major cases in a recursive function: ➢Base case: no further calls to the same function is made. ➢Recursive case: the function calls itself again (recursive call). The recursive calls should progress in such a way that every time a recursive call is made it comes closer to the base criteria. Data Structures Recap 21
  • 22.
    General Structure ofa Recursive Function Data Structures Recap 22 rec( input ) { if ( condition ) { // base case . . . } else { // recursive case . . . rec( smaller input ); . . . } } [ Note: There can be multiple base and recursive cases. ] Recursive call
  • 23.
    The Working ofFunction Call General Memory Layout of a Program: • When we run a program, it takes up space in the memory. • Each running program has its own memory layout, separated from other programs. The layout consists of different segments, including: 1. stack: stores local variables 2. heap: dynamic memory for programmer to allocate 3. data: stores global variables, separated into initialized and uninitialized 4. text: stores the code being executed Data Structures Recap 23 stack heap uninitialized data data initialized data text
  • 24.
    The Working ofFunction Call Use of Stack in Function Call Whenever a function is called a new activation frame is created with all the function’s data and this activation frame is pushed in the program stack, and the stack pointer that always points the top of the program stack points the activation frame pushed as it is on the top of the program stack. • Program Stack: the stack which holds all the function calls, with bottom elements as the main function • Activation Frame: a buffer memory that is an element of program stack and has data of the called function i.e., Return Address, Input Parameters, Local Variables etc. • Stack Pointer: the pointer that points to the top of program stack i.e., the most recent function called. Data Structures Recap 24
  • 25.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 25 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } Program stack
  • 26.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 26 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } Program stack
  • 27.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 27 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main Program stack
  • 28.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 28 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main Program stack
  • 29.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 29 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main 3 x . . . Program stack
  • 30.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 30 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main 3 x . . . Program stack
  • 31.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 31 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main 3 x . . . Program stack
  • 32.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 32 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main 3 x . . . printHi 3 n Program stack
  • 33.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 33 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main 3 x . . . printHi 3 n Program stack
  • 34.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 34 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main 3 x . . . printHi 3 n . . . i Program stack
  • 35.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 35 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main 3 x . . . printHi 3 n . . . i Program stack
  • 36.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 36 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main 3 x . . . printHi 3 n . . . 1 i Program stack
  • 37.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 37 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main 3 x . . . printHi 3 n . . . 1 i Program stack
  • 38.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 38 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main 3 x . . . printHi 3 n . . . 1 i Program stack
  • 39.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 39 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main 3 x . . . printHi 3 n . . . 1 i Program stack Output: Hi!
  • 40.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 40 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main 3 x . . . printHi 3 n . . . 1 i Program stack Output: Hi!
  • 41.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 41 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main 3 x . . . printHi 3 n . . . 2 i Output: Hi! Program stack
  • 42.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 42 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main 3 x . . . printHi 3 n . . . 2 i Output: Hi! Program stack
  • 43.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 43 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main 3 x . . . printHi 3 n . . . 2 i Output: Hi! Program stack
  • 44.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 44 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main 3 x . . . printHi 3 n . . . 2 i Output: Hi! Hi! Program stack
  • 45.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 45 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main 3 x . . . printHi 3 n . . . 2 i Output: Hi! Hi! Program stack
  • 46.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 46 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main 3 x . . . printHi 3 n . . . 3 i Output: Hi! Hi! Program stack
  • 47.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 47 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main 3 x . . . printHi 3 n . . . 3 i Output: Hi! Hi! Program stack
  • 48.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 48 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main 3 x . . . printHi 3 n . . . 3 i Output: Hi! Hi! Program stack
  • 49.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 49 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main 3 x . . . printHi 3 n . . . 3 i Output: Hi! Hi! Hi! Program stack
  • 50.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 50 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main 3 x . . . printHi 3 n . . . 3 i Output: Hi! Hi! Hi! Program stack
  • 51.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 51 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main 3 x . . . printHi 3 n . . . 4 i Output: Hi! Hi! Hi! Program stack
  • 52.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 52 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main 3 x . . . printHi 3 n . . . 4 i Output: Hi! Hi! Hi! Program stack
  • 53.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 53 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main 3 x . . . Output: Hi! Hi! Hi! Program stack
  • 54.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 54 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } main 3 x . . . Output: Hi! Hi! Hi! Program stack
  • 55.
    The Working ofFunction Call Use of Stack in Function Call Data Structures Recap 55 void printHi(int n) { int i; for(i=1; i<=n; i++) printf(“Hi! n”); } int main() { int x = 3; printHi(x); return 0; } Output: Hi! Hi! Hi! Program stack
  • 56.
    The Working ofFunction Call Use of Stack in Function Call • Since a recursive function repeatedly calls itself, it makes use of the program stack to temporarily store the return address, local variables and other information of the calling function. • Consequently, recursive algorithms require more memory and computation compared with iterative algorithms, but they are simpler and for many cases a natural way of thinking about the problem. Data Structures Recap 57
  • 57.
    Tracing Recursion Ex.1: Determineoutput of the following function for test(3). Data Structures Recap 58 void test(int n) { if (n > 0) { printf(“%d”, n); test(n-1); } } Check lecture notes
  • 58.
    Tracing Recursion Ex.2: Determineoutput of the following function for test(3). Data Structures Recap 59 void test(int n) { if (n > 0) { test(n-1); printf(“%d”, n); } } Check lecture notes
  • 59.
    Tracing Recursion Ex.3: Determineoutput of the following function for test(3). Data Structures Recap 60 void test(int n) { if (n > 0) { test(n-1); printf(“%d”, n); test(n-1); } } Check lecture notes
  • 60.
    Try Yourself! Ex.4: Determineoutput of the following functions for test(3). Data Structures Recap 61 void test(int n) { if (n > 0) { printf(“%d”, n); test(n-1); test(n-1); } } Ex.5: Determine output of the following functions for test(3). void test(int n) { if (n > 0) { test(n-1); test(n-1); printf(“%d”, n); } }
  • 61.
    References & OtherResources ➢ Data Structures (Seymour Lischutz) • Chapter 6 ➢ https://www.enjoyalgorithms.com/blog/recursion-explained-how- recursion-works-in-programming Data Structures Recap 62