SlideShare a Scribd company logo
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

More Related Content

PDF
Data Structure Algorithm - Recursion Book Slides
PPTX
Presentation on Elementary data structures
PPTX
ELEMENTARY DATASTRUCTURES
PPTX
data structure notes for engineering DSA3.pptx
PPTX
Data structures and algorithms
PPT
PPTX
DSA_Ques ewoifhjerofhefhehfreofheek.pptx
PPTX
My lecture stack_queue_operation
Data Structure Algorithm - Recursion Book Slides
Presentation on Elementary data structures
ELEMENTARY DATASTRUCTURES
data structure notes for engineering DSA3.pptx
Data structures and algorithms
DSA_Ques ewoifhjerofhefhehfreofheek.pptx
My lecture stack_queue_operation

Similar to L1 - Recap.pdf (20)

PPTX
linkedlist.pptx
PPT
The life of a query (oracle edition)
PPTX
data structures with algorithms vtu 2023 notes.pptx
PDF
9 python data structure-2
PDF
Data structure
PDF
14_linked list_updated-updated-updated(1).pdf
PPTX
python-numwpyandpandas-170922144956.pptx
PPTX
Python - Numpy/Pandas/Matplot Machine Learning Libraries
PPTX
R1-Intro (2udsjhfkjdshfkjsdkfhsdkfsfsffs
PDF
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
PPTX
19. Java data structures algorithms and complexity
PPT
Funddamentals of data structures
PPTX
python-numpyandpandas-170922144956 (1).pptx
PPTX
Data structure , stack , queue
PPT
Fundamentals of data structures
PPTX
Data structure
PPTX
The presention is about the queue data structure
PPTX
DA_02_algorithms.pptx
PPTX
Fundamental Data Structures and Algorithms.pptx
linkedlist.pptx
The life of a query (oracle edition)
data structures with algorithms vtu 2023 notes.pptx
9 python data structure-2
Data structure
14_linked list_updated-updated-updated(1).pdf
python-numwpyandpandas-170922144956.pptx
Python - Numpy/Pandas/Matplot Machine Learning Libraries
R1-Intro (2udsjhfkjdshfkjsdkfhsdkfsfsffs
Matplotlib adalah pustaka plotting 2D Python yang menghasilkan gambar berkual...
19. Java data structures algorithms and complexity
Funddamentals of data structures
python-numpyandpandas-170922144956 (1).pptx
Data structure , stack , queue
Fundamentals of data structures
Data structure
The presention is about the queue data structure
DA_02_algorithms.pptx
Fundamental Data Structures and Algorithms.pptx

Recently uploaded (20)

PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
PDF
STL Containers in C++ : Sequence Container : Vector
PPTX
GSA Content Generator Crack (2025 Latest)
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
DOCX
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
PPTX
assetexplorer- product-overview - presentation
PPTX
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
PPTX
Patient Appointment Booking in Odoo with online payment
PDF
EaseUS PDF Editor Pro 6.2.0.2 Crack with License Key 2025
PDF
Wondershare Recoverit Full Crack New Version (Latest 2025)
PDF
Cost to Outsource Software Development in 2025
PDF
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
PPTX
Cybersecurity: Protecting the Digital World
PDF
iTop VPN Crack Latest Version Full Key 2025
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PDF
How Tridens DevSecOps Ensures Compliance, Security, and Agility
PPTX
Trending Python Topics for Data Visualization in 2025
Digital Systems & Binary Numbers (comprehensive )
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
Computer Software and OS of computer science of grade 11.pptx
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
STL Containers in C++ : Sequence Container : Vector
GSA Content Generator Crack (2025 Latest)
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
assetexplorer- product-overview - presentation
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
Patient Appointment Booking in Odoo with online payment
EaseUS PDF Editor Pro 6.2.0.2 Crack with License Key 2025
Wondershare Recoverit Full Crack New Version (Latest 2025)
Cost to Outsource Software Development in 2025
Top 10 Software Development Trends to Watch in 2025 🚀.pdf
Cybersecurity: Protecting the Digital World
iTop VPN Crack Latest Version Full Key 2025
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
How Tridens DevSecOps Ensures Compliance, Security, and Agility
Trending Python Topics for Data Visualization in 2025

L1 - Recap.pdf

  • 2. 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
  • 3. Course Title: Algorithm Course Code: 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 (CSE 2103) Discrete Math (CSE 2105) Strong conceptual clarity will be assumed Recursion will come into play in various chapters
  • 5. 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.
  • 6. 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
  • 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 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)
  • 9. 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
  • 10. 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
  • 11. 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
  • 12. 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
  • 13. 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
  • 14. 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
  • 15. 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
  • 16. 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
  • 17. 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|)
  • 19. 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
  • 20. Example • Line in ticket counter Data Structures Recap 20 1 2 3 4 5 6 7 8 9 10 Recursive calls Base case
  • 21. 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
  • 22. 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
  • 23. 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
  • 24. 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
  • 25. 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
  • 26. 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
  • 27. 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
  • 28. 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
  • 29. 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
  • 30. 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
  • 31. 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
  • 32. 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
  • 33. 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
  • 34. 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
  • 35. 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
  • 36. 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
  • 37. 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
  • 38. 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
  • 39. 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!
  • 40. 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!
  • 41. 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
  • 42. 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
  • 43. 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
  • 44. 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
  • 45. 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
  • 46. 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
  • 47. 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
  • 48. 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
  • 49. 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
  • 50. 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
  • 51. 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
  • 52. 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
  • 53. 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
  • 54. 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
  • 55. 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
  • 56. 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
  • 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
  • 58. 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
  • 59. 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
  • 60. 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); } }
  • 61. 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