0% found this document useful (0 votes)
49 views145 pages

L1-L21 Merged DSA

The document outlines the course CS F211: Data Structures & Algorithms, taught by Dr. Chittaranjan Hota at BITS-Pilani Hyderabad Campus, covering topics such as C++, data structures, algorithms, and problem-solving techniques. It includes course administration details, evaluation components, and examples of C++ programming concepts like object-oriented design, inheritance, and dynamic arrays. The document also discusses the importance of studying the course and provides insights into practical applications of data structures and algorithms.

Uploaded by

f20220937
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views145 pages

L1-L21 Merged DSA

The document outlines the course CS F211: Data Structures & Algorithms, taught by Dr. Chittaranjan Hota at BITS-Pilani Hyderabad Campus, covering topics such as C++, data structures, algorithms, and problem-solving techniques. It includes course administration details, evaluation components, and examples of C++ programming concepts like object-oriented design, inheritance, and dynamic arrays. The document also discusses the importance of studying the course and provides insights into practical applications of data structures and algorithms.

Uploaded by

f20220937
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 145

CS F211: DATA STRUCTURES & ALGORITHMS Chittaranjan Hota, PhD

(2 ND SEMESTER 2024-25) Sr. Professor of Computer Sc.


BITS-Pilani Hyderabad Campus
INTRODUCTION hota[AT]hyderabad.bits-pilani.ac.in
WHY SHOULD YOU STUDY THE COURSE?

Image source: https://www.quora.com/


FIBONACCI: RECURSIVE OR ITERATIVE?
WHAT KIND OF PROBLEMS CAN YOU SOLVE?
THE PROBLEM SOLVING PROCESS
Mathematical Abstract Data
Data Structures
Model Types

Pseudo-language
Informal Algorithm C++ Program
Program If I had 1 hour to
save the world, …
•finding currents in electrical circuits, predicting spread of covid-19
•Language translator
interface
•An abstract data type (ADT), is a logical description of how we view the
data and the operations that are allowed without knowing how they will be impleme
implemented. ntation
operations
•Data structure (physical description) is the implementation of ADTs.
AN EXAMPLE: A TRAFFIC LIGHTING SYSTEM
1. Select an uncolored vertex
and color it with a new color.
2. Scan the list of uncolored
vertices. For each uncolored
vertex, determine whether it
has an edge to any vertex
already colored with the
new color. If there is no such
edge, color the present
vertex with the new color.
(Problem of road intersection) (Graph with incompatible turns) (Greedy coloring algorithm)
• The approach is called "greedy" because it colors a vertex whenever it can, without
considering the potential drawbacks inherent in making such a move.
AB,AC,AD,BA,DC,ED; BC,BD,EA; DA,DB; EB,EC.
CONTINUED…
GRAPH ADT: G
SET greedy_graph_coloring (Input:G:GRAPH, 1. graphNew(): creating a graph
Output: Newclr: SET) { 2. addVertex (v)
Newclr  Ø; 3. addEdge (v1, v2)
for (each uncolored vertex ‘v’ G) 4. getVertex (unclored)
5. markVertex (colored)
{
6. …
if ‘v’ is not adjacent to any vertex in Newclr
{
v  colored;
Newclr  Newclr ‘v’
}
}
}

(Pseudo Code) (Adjacency matrix to implement the graph)


COURSE CONTENT AND ADMINISTRATION
Text and Refs:
Course Content:
Intro to C++, Elementary
data structures, Algorithm
analysis techniques, More
common data structures,
Advanced data structures,
Understanding algorithmic
techniques.

Course notices and material: google class page Chamber consultation hour: Every Monday (5 to 6 pm)
EVALUATIONS
Component Duration Weightage(%) Date & Time Nature of the
component
Mid sem Test 1.5 hrs. 25% 03/03/2025 (2:00- Closed Book
3:30pm)
Lab Test (One) 1 hr. 10% 4th week of March, Open Book
2025 (in lab hours) (Labsheets)
Lab quizzes (in every lab) 10 mins 10% - Open Book
(best 10) (Labsheets)
Class quizzes (in theory class, 10 mins 15% - Open Book (Class
weekly once) (best 10) notes)
Tutorial quizzes (in every tutorial 10 mins 10% - Open Book (Tutorial
class) (best 10) notes)
Comprehensive examination 3 hrs. 30% 02/05/2025 (FN) Part Open(20% CB
+10%OB)
THANK YOU!
Next Class: Introduction to C++
CS F211: DATA STRUCTURES & ALGORITHMS Chittaranjan Hota, PhD
(2 ND SEMESTER 2024-25) Sr. Professor of Computer Sc.
BITS-Pilani Hyderabad Campus
INTRODUCTION TO C++ hota[AT]hyderabad.bits-pilani.ac.in
WHY C++ FOR CS F211? Bjarne Stroustrup

Adobe, Microsoft, Apple, MySQL, Mozilla, Amazon AWS SDK, Meta, IBM, Cap
Gemini etc.
C++ EXAMPLES Functions and Arrays
#include <iostream>
using namespace std;
int main() {
double num1, num2;
cout << "Enter the first number: ";
cin >> num1;
cout << "Enter the second number: ";
cin >> num2;
cout << "Sum: " << ??? << endl;
cout << "Difference: " << ??? << endl;
cout << "Product: " << ??? << endl;
if (num2 != 0) {
cout << "Quotient: " << ??? << endl;
}
else {
cout << "Division by zero is not allowed." << endl; }
return 0;
}
OBJECT-ORIENTED DESIGN: GOALS AND PRINCIPLES
(Design Principles)
What is Object-Oriented Design?
• Style of writing computer programs using
objects, and their interactions. (Minor Abstraction (ADTs are realized by
degree admissions at BITS, Hyderabad: classes in C++)
How many objects and what are their
interactions)

Encapsulation (Access to data is


What are the Design Goals?
provided through member functions)
• Robustness

• Adaptability
Modularity (different components):
supported through hierarchy.
• Reusability
CLASSES IN C++
class Passerger {
private: Passenger pass;
string name; if (!pass.isFrequentFlyer()) {
MealType mealPref; pass.makeFrequentFlyer (“12345”);
bool isFreqflyer; }
string freqFlyerNo;
public:
Passenger( );
bool isFrequentFlyer( ) const { return isFreqFlyer; }
void makeFrequentFlyer(const string& newFreqFlyerNo) {
isFreqFlyer = true;
freqFlyerNo = newFreqFlyerNo; ILLEGAL: pass.name = “Amit”;
}
};
ACCESS MODIFIERS: PUBLIC, PRIVATE
CS F211: DATA STRUCTURES & ALGORITHMS Chittaranjan Hota, PhD
(2 ND SEMESTER 2024-25) Sr. Professor of Computer Sc.
BITS-Pilani Hyderabad Campus
C++ CONTINUED… hota[AT]hyderabad.bits-pilani.ac.in
PROTECTED ACCESS MODIFIER
class Student{ class Result : public Student {
protected: private:
string name; float marks;
int rollNumber;
public: public:
Student(string n, int roll) { Result(string n, int roll, float m):
name = n; Student(n, roll) {
rollNumber = roll; marks = m;
} }
void displayBasicDetails() { void displayCompleteDetails() {
cout << "Name: " << name << endl; cout << "Name: " << name << endl;
cout << "Roll Number: " << cout << "Roll Number: " <<
rollNumber << endl; rollNumber << endl;
} cout << "Marks: " << marks << endl;
}; }
};
What type of Constructor is used here? Are there any return types for constructors?
CONTINUED…

Constructor types?
CLASS INHERITANCE IN C++
Why is inheritance used in C++? class Student : public Person {
class Person { private:
private: string branch;
string name; int gradYear;
int Aadhaar; double cgpa;
public: string idNo;
void print(); public:
string getName(); void print();
}; };
How will you draw the class inheritance diagram?
CONTINUED…
POLYMORPHISM IN C++
Polymorphism

Compile Time Run Time

Function Operator
Virtual Functions
Overloading Overloading
• A C++ virtual function is a member function in the
Operator Overloading base class that you redefine in a derived class.

Runtime Polymorphism

Function Overriding
CS F211: DATA STRUCTURES & ALGORITHMS Chittaranjan Hota, PhD
(2 ND SEMESTER 2024-25) Sr. Professor of Computer Sc.
BITS-Pilani Hyderabad Campus
C++ CONTINUED… hota[AT]hyderabad.bits-pilani.ac.in
FRIEND CLASS IN C++
 A friend class is a class whose members have
access to the private members of another
class.

 Rectangle is a friend of Square allowing


Rectangle’s member functions to access what
members of Square?

 Is friendship transitive?

 Can a friend not access protected members?


FRIEND FUNCTION IN C++
What
all
accesses
min()
has
from
A
and
B
?
ABSTRACT CLASSES IN C++
•What is the need?
• If a class inheriting an abstract class does not provide a definition for the pure virtual
function, then what will happen?
DESIGN PATTERNS: TEMPLATES IN C++
template <typename T>
T myMax(T x, T y)
{ Compiler will internally generate
return (x > y)? x: y; what code?
}
int main()
{
cout << myMax<int>(4, 8) << endl;
cout << myMax<char>(‘b’, ‘m’) << endl;
cout << myMax<double>(7.2, 5.0) << endl;
return 0;
}
STANDARD TEMPLATE LIBRARY (STL) IN C++
• A library of container classes, algorithms,
and iterators.
Can you name some?
n-1
InitialValue+∑ a[i]
i=0
How to do this using STLs?

Can you name some of the STL functions in


this code?
STL on strings:
insert, append, swap, size, resize, reverse etc.
CS F211: DATA STRUCTURES & ALGORITHMS Chittaranjan Hota, PhD
(2 ND SEMESTER 2024-25) Sr. Professor of Computer Sc.
BITS-Pilani Hyderabad Campus
DYNAMIC ARRAYS hota[AT]hyderabad.bits-pilani.ac.in
ARRAYS: WHY?
Let us assume that we have to read, process and print 10 numbers (integers) and keep
those in the memory throughout the execution.

score0 Read score0


Print score0
Read score1 Definitely, not a
score1
.. good idea for
Print score1
. 10000 numbers.
score2 ..
. .
Read score10
. Print score10
. Process
score9
CONTINUED… An array of scores: Only one variable is enough.

score0 score[0]

score1 score[1]
… …
score9 score[9]

Subscripting Indexing

start 1 2

i=0 i=0
i++ i++
i < 10 Process i < 10
read score[i] write score[i]
1 2 stop
DYNAMIC ARRAYS
• What are they?

Applications of arrays: Maths (vectors, matrices, polynomials,…), databases,


compilers (control flow), dynamic memory allocations etc.
void Dynamic1DArray :: shrink() {

capacity >>= 1;

DYNAMIC ARRAYS EXAMPLE: LAB3 int *newArr = new int[capacity];

for (int i = 0; i < size; i++)


newArr[i] = arr[i];

• Let us understand the operations needed to // update the global array pointer
implement a dynamic array: insert, remove etc. }
arr = newArr;
(shrink)

(DynamicArray.cpp given in the next week’s lab sheet)


(Output)
USING ARRAYS: AN EXAMPLE

{An entries array of length 10 with 6 {Copying the new entry into the position.
GameEntry objects (maxExntries: 10, Scenario after addition}
numEntries: 6)}

{Preparing to add a new GameEntry {Removing an element at index i requires


object by shifting all the erntries with moving all the entries at indices higher than i
smaller scores to the right by one one position to the left}
position}
IMPLEMENTATION: STORING GAME ENTRIES
class GameEntry { class Scores {
public: public:
GameEntry ( const string &n = "", int s = 0); Scores(int maxEnt = 10);
string getName() const; ~Scores();
int getScore() const; void add(const GameEntry &e);
private: GameEntry remove(int i) ;
string name; void printAllScores();
int score; private:
}; int maxEntries; //maximum number of entries
int numEntries; //actual number of entries
( A Class representing a Game entry)
GameEntry *entries;
}; ( A Class for storing Game scores)
GameEntry::GameEntry(const string &n, int s) : name(n),
score(s) { } Scores::Scores(int maxEnt) {
string GameEntry::getName() const { return name; } maxEntries = maxEnt; // save the max size
int GameEntry::getScore() const { return score; } entries = new GameEntry[maxEntries];
numEntries = 0;
( Constructor and member functions) } Scores::~Scores() { delete[ ] entries; }
INSERTING INTO AND DELETING FROM ARRAY
void Scores::add(const GameEntry &e) {
int newScore = e.getScore(); // score to add
if (numEntries == maxEntries) { // the array is full
if (newScore <= entries[maxEntries - 1].getScore())
return; // not high enough - ignore GameEntry Scores::remove(int i)
} {
else numEntries++; // if not full, one more entry if ((i < 0) || (i >= numEntries)) // invalid index
throw("IndexOutOfBounds - Invalid index");
int i = numEntries - 2; // start with the next to last
while (i >= 0 && newScore > entries[i].getScore() ) { GameEntry e = entries[i]; // save the removed object
entries[i + 1] = entries[i]; // shift right if smaller for (int j = i + 1; j < numEntries; j++)
i--; entries[j - 1] = entries[j]; // shift entries left
} numEntries--; // one fewer entry
entries[i + 1] = e; // put e in the empty spot return e; // return the removed object
} }

(Inserting a Game entry object) (Removing a Game entry object)


DRIVER AND OTHER CLASSES FOR GAME ENTRY EX.

(Lab3: GameEntry.cpp)
LAB3 TASKS: GAME ENTRY

(How many number of entries are there for each player? Option 4)

(display unique entries for each player?)


(Display players in a score range: Option 6) (GameEntry_Unique.cpp)
SORTING & SEARCHING IN AN ARRAY
void Dynamic1DArray ::sort() int Dynamic1DArray
{ ::binarySearch(const int item)
for (int j = 1; j < size; j++) {
{ int low = 0, high = size - 1;
while (low <= high){
int key = arr[j]; int mid = low + ((high –
int i = j - 1; low) >> 1);
while (i > -1 && arr[i]>key) if (item == arr[mid])
{ return mid;
arr[i + 1] = arr[i]; if (item < arr[mid])
i = i - 1; high = mid - 1;
} else
arr[i + 1] = key; low = mid + 1;
} }
return -1; }
}
(Insertion Sort) More sorting & searching algos later… (Binary Search)
MULTI-DIMENSIONAL ARRAYS
Month
0 1 2 3 4 5 6 7 8 9 10 11 Arrays in C++ are one-dimensional.
0 30 40 75 95 130 220 210 185 135 80 40 45 However, we can define a 2D array
1 25 25 80 75 115 270 200 165 85 5 10 16
as “an array of arrays”.
Year

2 35 45 90 80 100 205 135 140 170 75 60 95


3 30 40 70 70 90 180 180 210 145 35 85 80
4 30 35 40 90 150 230 305 295 60 95 80 30
3-dimensional
Average Yearly Rainfall (in mm of Hyd)
0 1 2 3 4 5 6 7 8 9 10 11
0 20 60 75 95 130 220 210 185 135 80 40 45
Hyd
1 29 25 80 75 115 270 200 165 85 5 10 16
0 1 2 3 4 5 6 7 8 9 10 11
2 35 45 90 80 100 205 135 140 170 75 60 95
0 10
3 30 40 70
20 35 95 130 220 210 185 135 80 40
70 90 180 180 210 145 35 85 80
45 Delhi
1 5 17 9 8 115 270 200 165 85 5 10 16
4 30 35 40 90 0 1501 230
2 305
3 4295 5 60 695 80
7 30 8 9 10 11
2 35 45 90 80 100 205 135 140 170 75 60 95
0 10 20 35 95 130 220 210 185 135 80 40 45
3 30 40 70 70 90 180 180 210 145 35 85 80
1 5 17 9 8 115 270 200 165 85 5 10 16
4 30 35 40 90 150 230 305 295 60 95 80
2 35 45 90 80 100 205 135 140 170
30
75 60 95 Goa
3 30 40 70 70 90 180 180 210 145 35 85 80
4 30 35 40 90 150 230 305 295 60 95 80 30
CS F211: DATA STRUCTURES & ALGORITHMS Chittaranjan Hota, PhD
(2 ND SEMESTER 2024-25) Sr. Professor of Computer Sc.
BITS-Pilani Hyderabad Campus
LINKED LISTS hota[AT]hyderabad.bits-pilani.ac.in
LINKED LISTS
• What are these?

• Arrays Vs Linked lists

• What are some of the applications


of Linked lists?
IMPLEMENTING A SINGLY LINKED LIST
Step 1: Define a class for the Node Step 3: Define a set of member functions for the
class StringNode { Linked list class defined in Step 2
private: string elem;
StringNode* next;
StringLinkedList::StringLinkedList() : head(???){ }
friend class StringLinkedList;
StringLinkedList::~StringLinkedList() {
};
Step 2: Define a class for the Linked list while(!empty())
class StringLinkedList { ???;
public: StringLinkedList(); }
~StringLinkedList(); bool StringLinkedList::empty() const { //Is list empty?
bool empty() const; return head == NULL;
const string& front() const; }
void addFront(const string& e);
void removeFront(); const string& StringLinkedList::front() const {
private: StringNode* head; return ???;
}; }
INSERTING & REMOVING AT THE HEAD OF LINKED LIST
1. Create a new node
2. Store data into this node
3. Have new node point to old head X Goa
Dubai
4. Update head to point to new node
void StringLinkedList::addFront(const string& e)
{ Pilani
StringNode* v = new StringNode; Inserting at the head
v->elem = e;
v->next = head; void StringLinkedList::removeFront()
head = v; {
} StringNode* old = head;
head = old->next;
1. Save old head
delete old; 2. Advance head to the next
} node
Deleting at the head
3. Delete the old head node
INSERTING AT THE TAIL & INSIDE A LINKED LIST
1. Allocate a new node
2. Insert new element (Hyd)
3. Have new node point to null (v->next Pilani Dubai Goa 
= NULL)
4. Have old last node point to new node 
(last_node -> next = v)

Head Hyd 
Pilani Dubai Hyd 

void insertAfter(Node* prev_node, int new_data)


Node* new_node = new Node();
new_node->data = new_data;
Head Goa new_node->next = prev_node->next;
 prev_node->next = new_node;
DELETING THE LAST NODE
Algorithm:

1. If (headNode == null) //how many nodes in list?


then what should you do?
2. If (headNode.next == null) //how many nodes in list?
then what should you do?
3. While secondLast.next.next != null //traverse till secondLast
secondLast = secondLast.nextNode
4. Delete last node and set the pointer of secondLast to null.
STACK & QUEUE AS SINGLY LINKED LISTS
rear
nodes nodes
? front
 
elements elements

Stack: We can implement stack as a Queue: We can implement a queue as a linked


linked list. How will you implement? list. Front element is stored as first element of the
linked list, and rear element is stored as the last
Implementation in later chapters… element.
SWAPPING TWO NODES IN A LINKED LIST

prev1 prev2
X ?

X X

Lab 4 next week (week no:4)


CS F211: DATA STRUCTURES & ALGORITHMS Chittaranjan Hota, PhD
(2 ND SEMESTER 2024-25) Sr. Professor of Computer Sc.
BITS-Pilani Hyderabad Campus
LINKED LISTS CONTINUED… hota[AT]hyderabad.bits-pilani.ac.in
RECAP
GENERIC SINGLY LINKED LISTS: USING TEMPLATES

SNode<E>
*SLinkedList<E>::search(const E &e){
//complete code here
} (Next week Lab 4)
REVERSING A LINKED LIST
void listReverse(LinkedList& L) {
LinkedList T;
How will you do it using two pointers
while (!L.empty()) {
string s = L.front();
(in-place)?
L.removeFront();
T.addFront(s); Let us see…
}
while (!T.empty()) {
string s = T.front();
T.removeFront();
L.addBack(s);
}
} Is it In-place reversal?
DOUBLY LINKED LIST
Applications:
•Deleting the last node in a singly linked list is • Used by browsers for what functionality?
not efficient. Why? (rather any node other • Used to implement MRU, and LRU caches?
than first one or two) • Undo/ Redo functionality in Word.
• Used to implement hash tables, stacks, binary
•What is a doubly linked list?
tree etc.
•Insertions and deletions are more efficient.
nodes trailer
typedef string Elem;
class DNode {
private: Elem elem; header
DNode* prev;

elements
DNode* next;
friend class DLinkedList;
};
(Implementation of DLL Node)
(A doubly linked list with two special nodes (sentinels)
INSERTING INTO DOUBLY-LINKED LIST
header u p trailer
X
X

Algorithm insert(p, e): //insert e


before p
Let us write the pseudo code in
v
parallel…
REMOVING A NODE IN DOUBLY-LINKED LIST

Algorithm remove (p: position ) {


header p trailer
if (p->previous != nil) // not first X
X
p->previous->next = ???;
if (p->next != nil) //not the last
p->next->previous = ???;
}
CS F211: DATA STRUCTURES & ALGORITHMS Chittaranjan Hota, PhD
(2 ND SEMESTER 2024-25) Sr. Professor of Computer Sc.
BITS-Pilani Hyderabad Campus
LINKED LISTS CONTINUED… hota[AT]hyderabad.bits-pilani.ac.in
RECAP
FINDING MIDDLE NODE AND LOOP IN A LINKED LIST
Node* findMiddle(Node* head) { How about detecting a closed loop using this
if (head == null) { algo?
return ???; if (slow == fast) {
} return true;
Node* slow = head; }
Node* fast = head; How about removing a closed loop using this
while (fast != null && fast->next != null) { algo? // node before loop start
//what will you do here? // start node of loop Node* prev = slow;
} slow = head; while (prev->next != fast) {
while (slow != fast) { prev = prev->next;
return slow; slow = slow->next; }
} fast = fast->next; // Break the loop
Floyd's Tortoise and Hare algorithm (Lab 4) } prev->next = nullptr;
the cursor
// remove the node after
void CircleList::remove() {
CNode* old = cursor->next;
if (old == cursor)

CIRCULAR LINKED LISTS


cursor = NULL;
else
cursor->next = old->next;
delete old;
}
•A circular linked list is a singly-linked list except for the last element
of the list pointing to the first. Without starting over we can go back
to the first.
•What is the need of cursor node?
class CircleList; CircleList::CircleList() : cursor(NULL) { }
typedef string Elem; CircleList::~CircleList() { while (!empty()) remove(); }
class CNode { bool CircleList::empty() const {return cursor== NULL; }
private: Elem elem; const Elem& CircleList::back() const {
CNode* next; return cursor->elem; }
friend class CircleList; const Elem& CircleList::front() const {
}; return cursor->next->elem; }
class CircleList { void CircleList::advance() { cursor = cursor->next; }
public: CircleList(); void CircleList::add(const Elem& e) { //add after the cursor
~CircleList(); CNode* v = new CNode;
bool empty() const; v->elem = e;
const Elem& front() const; if (cursor == NULL) {
const Elem& back() const; v->next = v; cursor = v; }
void advance(); else {
void add(const Elem& e); v->next = cursor->next; cursor->next = v;
void remove(); }
private: CNode* cursor;
}; }
THANK YOU!

Next class: Recursion and Algo Complexity…


CS F211: DATA STRUCTURES & ALGORITHMS Chittaranjan Hota, PhD
(2 ND SEMESTER 2024-25) Sr. Professor of Computer Sc.
BITS-Pilani Hyderabad Campus
RECURSION & ALGORITHM COMPLEXITY hota[AT]hyderabad.bits-pilani.ac.in
WHAT IS RECURSION?
(How many people work under Amit?)

Amit

Raj Deb

?
Ragini Rohit Sachin

https://abetterscientist.wordpress.com/
(Business organization chart)
Many more: Factorial, Fibonacci seq., Towers of Hanoi, Merge sort, Quick sort, Binary search …
LINEAR RECURSION
•A linear recursive function is a function int sumArrayRecursive(int arr[], int n) {
that makes at most one recursive call // What is the base case?
each time it is invoked (as opposed to
one that would call itself multiple times //Recursive step:
during its execution).
return arr[0] + sumArrayRecursive(???, ???);
}
int gcd(int a, int b) {
}
if (b == 0) {
int main() {
return a;
int arr[] = {1, 2, 3, 4, 5};
} else {
int n = sizeof(arr) / sizeof(arr[0]);
return gcd(b, a % b);
int sum = sumArrayRecursive(arr, n);
}
cout << "Sum of array elements: " << sum << endl;
}
return 0;
Euclidean Algorithm (Recursive) }
TAIL RECURSION: REVERSING AN ARRAY
void reverseArray(int arr[], int start, int end)void reverseArray(int arr[], int size) {
{ int start = 0;
if (start >= end) { //reached ??? int end = size - 1;
return; while (start < end) {
} swap(arr[start], arr[end]);
swap(arr[start], arr[end]); start++;
reverseArray(arr, start + 1, end - 1); end--;
} }
}
• Tail recursion occurs when a linearly recursive method makes its recursive call as its
last step.
• Such methods can be easily converted to non-recursive methods (which saves on some
resources).
WHAT ABOUT FACTORIAL?
int factorial(int n) { int tail_factorial(int n, int acc) {
if (n == 0) { if (n == 0) {
return acc;
return 1; } else {
} else { return tail_factorial(n - 1, n * acc);
return ; }
}
}
int factorial(int n) {
} return tail_factorial(n, 1);
Is it tail recursive? } What about this?

int factorial_iterative(int n) {int prod = 1; for (int i = 1; i <= n; ++i) { prod *= i;} return prod;}
BINARY RECURSION
• What is binary recursion? void towerOfHanoi(int n, char source, char
dest, char aux) {
if (n == 1) {
Algorithm BinarySum(A, i, n): cout << "Move disk 1 from " <<
Input: An array A and integers i and n source << " to " << dest << endl;
Output: The sum of n integers in A starting at index i return;
if n == 1 then }
return A[i ]; towerOfHanoi(n - 1, …, …, …);
return cout << "Move disk " << n << " from "
BinarySum(A, i, n/ 2) + BinarySum(A, i + n/ 2, n/ 2) << source << " to " << dest << endl;
towerOfHanoi(n - 1, …, …, …);
}
Let us see the recursion trace… Used heavily in merging and tree traversals…
CS F211: DATA STRUCTURES & ALGORITHMS Chittaranjan Hota, PhD
(2 ND SEMESTER 2024-25) Sr. Professor of Computer Sc.
BITS-Pilani Hyderabad Campus
ALGORITHM COMPLEXITY hota[AT]hyderabad.bits-pilani.ac.in
RECAP

(Fibonacci using Binary Recursion and using Tail Recursion… Which one is less complex?
Let us see here…)
WHAT IS ALGORITHM COMPLEXITY?
A A
L L
G G
O O
R
I
✓ R
I
T T
H H
M M
1 2
(A metaphor: searching car keys in your home)
FEW MORE COMPLEXITY EXAMPLES…
Mr X?

TSP

Exponential: 2n Quadratic (greedy heuristics): n2 Linear: n

Constant: 1 Logarithmic: log(n) Log-linear: n.log(n)


TASK FOR YOU…

You want to look for a word in a dictionary that has every word
sorted alphabetically. How many algorithms are there and which one
would you prefer?
CS F211: DATA STRUCTURES & ALGORITHMS Chittaranjan Hota, PhD
(2 ND SEMESTER 2024-25) Sr. Professor of Computer Sc.
BITS-Pilani Hyderabad Campus
ALGORITHM COMPLEXITY CONTINUED… hota[AT]hyderabad.bits-pilani.ac.in
WHY IS IT SO IMPORTANT? Metaphor: Daily Budget and Spending

Problem Statement: Input :arr[]= {100, 200, 300, 400}, k = 2 Output : ???
Problem Name: "Maximum Subarray for (int i = 0; i < n; i++) { for(int i = 0; i < n; i++) {
Sum" for (int j = i; j < n; j++) { int currentSum = 0;
int currentSum = 0;

Hypothetical Example
Input: An array of integers. for(int j = i; j < n; j++) {
for (int k = i; k <= j; k++) { currentSum += arr[j];
Output: maximum sum. currentSum += arr[k]; if (currentSum>maxSum){
} maxSum = currentSum;
Constraints: if (currentSum > maxSum) { }
• Time Limit: 1 second maxSum = currentSum; } Complexity?
} }
• Memory Limit: 256 MB } for (int i = 1; i < n; i++) {
• 1 <= Array Size <= 10^6 } currentSum=max(arr[i], currentSum + arr[i]);
maxSum = max(maxSum, currentSum);
• -10^9 <= Array Element <= 10^9 Complexity? }
Efficient solution: Kadane's Algorithm using DP (Ignore –ve subarray sum): Complexity is ???
FUNCTIONS FOR ALGORITHM ANALYSIS
(Seating in the class everyday) (Reading chapters of book) (Climbing a ladder quickly) (Population growth)

Execution time
Execution time

Execution time
Execution time
O(1) O(n)
2n
log2(n)

Input size(n) Input size(n) Input size(n) Input size(n)


(Constant Function) (Linear Function) (Logarithmic Function) (Exponential Function)

Ex.s: Array indexing, Vari Ex.s: Searching in unsorted Ex.s: Finding a word in a d Ex.s: Fibonacci sequence, T
able assignment, Basic arit array, Printing all element ictionary, Treasure hunt, et owers of Hanoi, Generatin
hmetic operations, … s of a list, … c… g all subsets of set, etc…
COMPLEXITY EXAMPLES FROM REAL LIFE
1km 1km 1km • Is it Linear?
… (Ex.1)
• Total distance over time?
Mon Tues Wed
• Per day effort?

1km 2kms 3kms


… (Ex. 2)
Mon Tues Wed

Que: 1 + 4 + 9 + 16 + 25 +…n cubic {[n(n+1)(2n+1)]/6}


TASKS FOR YOU…COMPLEXITY?
function isEvenOrOdd(n) { list<int> numbers {1, 2, 3, 4}; int binarySearch(int array[], int x,
for(int number : numbers) int low, int high)
if (n%2 == 0)
{ {
return even; cout << number <<", "; while (low <= high)
else } {
int mid = low + (high - low) / 2;
return odd;
if (array[mid] == x) return mid;
} (printing out all the elements)
if (array[mid] < x)
int partition(int arr[], int low, int high) { int pivot=arr[high]; low = mid +1;
int i = (low - 1); else
for (int j = low; j <= high - 1; j++) { high = mid - 1;
if (arr[j] < pivot) { i++; swap(&arr[i], &arr[j]); } } }
swap(&arr[i + 1], &arr[high]); return (i + 1); return -1;
} }
CUBIC EXAMPLE AND POLYNOMIAL FUNCTIONS
1E+29
1E+27 Cubic
1E+25
Quadratic
No of multiplications: 1E+23
1E+21 Linear
1E+19
n n n n n 1E+17
∑∑∑1 ∑∑n

T(n)
1E+15
i=1 j=1 k=1 i=1 j=1 1E+13
1E+11
n 1E+9
∑ n2 n3 1E+7
1E+5
i=1
1E+3
1E+1
1E-1
1E-1 1E+2 1E+5 1E+8
Interestingly, all the functions that we have listed are n
part of large class of functions called, polynomials: (In this log-log graph, the slope of the line
g(n) = a0 + a1n + a2n2 + a3n3 + ...+ adnd What is d? corresponds to the growth rate)
CS F211: DATA STRUCTURES & ALGORITHMS Chittaranjan Hota, PhD
(2 ND SEMESTER 2024-25) Sr. Professor of Computer Sc.
BITS-Pilani Hyderabad Campus
ALGO. COMPLEXITY CONTINUED… hota[AT]hyderabad.bits-pilani.ac.in
GROWTH RATE OF AN ALGORITHM
BEST CASE, WORST CASE, AVERAGE CASE
Natural measure of “goodness”

• Why worst case is important and Average case is most difficult to compute?
(This week’s Lab)

EMPIRICAL ANALYSIS: COMPLEMENT TO BIG-O


void bubbleSort(int arr[], int n) { Flat profile:
for (int i = 0; i < n-1; i++) { % cumulative self self total
for (int j = 0; j < n-i-1; j++) { time seconds seconds calls ms ms name
if (arr[j] > arr[j+1]) { 95.00 1.90 1.90 1 1900 1900 bubbleSort
5.00 2.00 0.10 main
int temp = arr[j];
arr[j] = arr[j+1]; gprof Call graph:
arr[j+1] = temp;
}
caller
}
}
} callee
… (with a main function calling it)
Question: How do you now verify the quadratic complexity of this algorithm? What are the challenges?
USING A TIMER FROM CHRONO This week’s Lab

recursive

Inside main()
CONTINUED…

Lab no:5
CS F211: DATA STRUCTURES & ALGORITHMS Chittaranjan Hota, PhD
(2 ND SEMESTER 2024-25) Sr. Professor of Computer Sc.
BITS-Pilani Hyderabad Campus
ASYMPTOTIC ANALYSIS OF ALGORITHMS hota[AT]hyderabad.bits-pilani.ac.in
CS F211: DATA STRUCTURES & ALGORITHMS Chittaranjan Hota, PhD
(2 ND SEMESTER 2024-25) Senior Professor, Computer Sc.
BITS-Pilani Hyderabad Campus
ASYMPTOTIC ANALYSIS hota[AT]hyderabad.bits-pilani.ac.in
CS F211: DATA STRUCTURES & ALGORITHMS Chittaranjan Hota, PhD
(2 ND SEMESTER 2024-25) Senior Professor, Computer Sc.
BITS-Pilani Hyderabad Campus
ASYMPTOTIC ANALYSIS CONTINUED… hota[AT]hyderabad.bits-pilani.ac.in
RECAP: BIG-O
• Why is it called tight upper bound?
• What is the relation between Big-O and growth
T rate?
I • How to choose an appropriate combination of c
M and n0 out of many possible ones?
E

There exists c > 0 and n0 such


that f(n) ≤ cg(n) whenever n ≥ n0.

f (n) is O(g(n))
RECURSIVE FUNCTIONS: RECURRENCE RELATION (EX1)
void fun(int n) { • A recurrence relation is a way to define a function or
if (n > 0) sequence in terms of itself.
{
printf(“%d”, n); • Let us solve it?
fun(n-1);
𝑐, 𝑛 ≤ 0 O(n)
} 𝑇 𝑛 =ቊ
𝑇 𝑛−1 +𝑘 ,n >0
}
Int main() { T(n-1) = T(n-1-1) + k
int x = 4;
fun(x);  T(n) = T(n-2) + k + k = T(n-2) + 2k
return 0; …
}  T(n) = T(n-i) + i.k  n-i = 0  T(0) + n.k  c + nk
EXAMPLE 2
T(n) = 2 T(n/2) + n Where, T(1) = 1 Assuming n/2i = 1

 n = 2i
T(n/2) = 2 T(n/4) + (n/2)
T(n) = n.T(1) + log2n.n
T(n) = 2 {2.T(n/4) + (n/2)} + n = 4 T(n/4) + 2.n
T(n) = n + nlog2n
T(n/4) = 2.T(n/8) + (n/4)
O(nlog2n)
 T(n) = 4. {2.T(n/8) + (n/4)} + 2.n = 8.T(n/8) + 3.n
Log-linear
T(n) = 2i.T(n/2i) + i.n
BIG-O RULES int findBiggestNumber(int arr[], int size) {
int biggest = arr[0];
for (int i = 1; i < size; i++) {
1. If an algorithm performs a certain sequence of if (arr[i] > biggest) {
steps f(N)times for a function f, it takes O(f(N)) biggest = arr[i];
steps. }
This algorithm examines each of the N items once, }
so it’s performance ???. return biggest;
}
2. If an algorithm performs an operation that takes
int findBiggestNumber(int arr[], int size) {
f(N) steps and then performs another operation
int biggest = arr[0]; // ?
that takes g(N) steps for function f and g, the
for (int i = 1; i < size; i++) { //?
algorithm’s total performance is ???.
if (arr[i] > biggest) {
biggest = arr[i];
The total runtime of the algorithm is ???. }
}
return biggest; //?
}
CONTINUED…
int findBiggestNumber(int arr[], int size) {
int biggest = arr[0]; // O(1)
3. If an algorithm takes O (f(N) + g(N)) steps and for (int i = 1; i < size; i++) { //O(n)
the function f(N) is bigger than g(N), if (arr[i] > biggest) {
algorithm’s performance can be simplified to biggest = arr[i];
O (f(N)). }
}
findBiggestNumber algorithm has O(N+ return biggest; //O(1)
2) runtime. When N grows very large, the }
function N is larger than our constant value 2,
so algorithm’s runtime can be simplified to ???. bool containsDuplicates(int arr[], int n) {
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
4. If an algorithm performs an operation that
if (arr[i] == arr[j]) {
takes f(N) steps, and every step performs return true; // duplicate found
another operation that takes g(N) steps, } } }
O(n 2)
algorithm’s total performance is ???. return false; }
STRICT UPPER BOUND: SMALL-O
• What about small o ? (Upper bound that is not tight) Prove that: n = o(n2)
• Used in Asymptotic proofs. 𝑛 1
• If f(n) = o(g(n)), it means f(n) grows strictly slower lim 2 lim =0
𝑛→∞ 𝑛 𝑛→∞ n
than g(n).
not a tight bound but an overestimate Hence, n = o(n2)
𝑓 𝑛
0≤𝑓 𝑛 <𝑐⋅g 𝑛 Or, lim =0
𝑛→∞ 𝑔 𝑛 Prove that: n2 ≠ o(n2)
Let f(n) = nlogn, and g(n) = n2 Prove that f(n) is o(g(n))
𝑛2
lim =1 ≠ 0
𝑓 𝑛 nlogn logn 𝑛→∞ 𝑛2
lim lim lim
𝑛→∞ 𝑔 𝑛 𝑛→∞ n2 𝑛→∞ n
Hence, n2 ≠ o(n2) Rather, n2 = O(n2)
d(logn)/dn 1/n
L-Hospital Rule lim lim =0
𝑛→∞ d(n)/dn 𝑛→∞ 1 Big-O allows equality, but small-o requires
Hence, nlogn = o(n2) strict growth separation.
For You: Linear search, Binary search? O(n), (1), (n/2) O(logn), (1), (logn)

BEST CASE BIG  AND AVERAGE CASE


BIG :
T
I
M
Big-Omega Notation () E
•Just like Bio-O provides asymptotic upper-bound, Big-
provides asymptotic lower-bound on the running time.
•f(n) is (g(n)) if there exists a constant c > 0 and an
integer constant n0  1 such that f(n)  c.g(n) for all n  n0 Ω(nlog n) f(n) = Ω(g(n))

Let, f(n) = 3n.logn + 2n Justification: 3n.logn+2n ≥ 3n.logn, for n ≥ 2


T
Big-Theta Notation () I
M
f(n) is Θ(g(n)) , if: f(n) is both O(g(n)) and Ω(g(n)) E
f(n) is (g(n)) if there are constants c1 > 0 and c2 > 0 and an
integer constant n0  1 such that c1.g(n)  f(n)  c2.g(n) for n  n0
f(n) = Θ(g(n))
3nlogn+4n+5logn is Θ(nlog n) 3nlogn  3nlogn+4n+5logn  (3+4+5) nlogn for n  2
THANK YOU!

Next Class: Common Data structures (Stacks, Queues, Deques etc.)


CS F211: DATA STRUCTURES & ALGORITHMS Chittaranjan Hota, PhD
(2 ND SEMESTER 2024-25) Senior Professor, Computer Sc.
BITS-Pilani Hyderabad Campus
STACK ADT hota[AT]hyderabad.bits-pilani.ac.in
STACK ABSTRACT DATA TYPE (STACK ADT)
- What is a Stack?
- What type of policy does a stack implement?

Example usage: Matching parenthesis, Expression evaluation, Function call stack,


Stock span, Backtracking, etc.
ANOTHER EX. USAGE: RAT IN A MAZE (BACKTRACKING)
STACK USAGES CONTINUED…
#include <iostream>
using namespace std;
void functionB() {
cout << "Inside Function B" << endl;
return;
}
void functionA() {
cout << "Inside Function A" << endl;
functionB();
cout << "Inside Function A" << endl;
}
int main() {
cout << "Inside Main function" << endl;
functionA();
cout << "Main fun finished" << endl;
return 0;
} (Stock span: Nvidia)
STACK
INTERFACE
•What is an ADT?
•Exs: Graph ADT (introductory
classes), Stock trading (BST,
Heaps, Hash maps, etc.)
•STACK ADT:
•Data?
•Operations?
•Auxiliary operations?
ARRAY-BASED STACK IMPLEMENTATION
CS F211: DATA STRUCTURES & ALGORITHMS Chittaranjan Hota, PhD
(2 ND SEMESTER 2024-25) Senior Professor, Computer Sc.
BITS-Pilani Hyderabad Campus
STACK ADT CONTINUED… hota[AT]hyderabad.bits-pilani.ac.in
IMPLEMENTING A STACK WITH A GENERIC LINKED LIST
EX-2: MATCHING TAGS

Lab6: Next week


STACK USAGE EXAMPLE-3: STOCK SPAN
• Stock span can be defined as the number of consecutive days before the current
day where the price of the stock was equal to or less than the current price.

Lab 6: Next week’s lab


(Stock span: Nvidia)
COMPUTING STOCK SPAN CONTINUED…
X STOCK_SPAN(prices):
n = length(prices)
span = array of size n // To store the stock spans
stack = empty stack // Stack to store indices

for i from 0 to n-1:


while stack is not empty AND prices[stack.top()] <= prices[i]:
stack.pop()

if stack is empty:
span[i] = i + 1 // Span is from the beginning
else:
span[i] = i - stack.top() // Span is difference of indices

stack.push(i) // Push the current index


A
S return span
STACK USAGE EX-4: BACKTRACKING 11111
11001
11111
11001
10001 10101
m01e0 m01e0

Chakravyuha from Mahabharata Lab 6: Next week’s Lab


CS F211: DATA STRUCTURES & ALGORITHMS Chittaranjan Hota, PhD
(2 ND SEMESTER 2024-25) Senior Professor, Computer Sc.
BITS-Pilani Hyderabad Campus
QUEUE ADT hota[AT]hyderabad.bits-pilani.ac.in
CS F211: DATA STRUCTURES & ALGORITHMS Chittaranjan Hota, PhD
(2 ND SEMESTER 2024-25) Senior Professor, Computer Sc.
BITS-Pilani Hyderabad Campus
QUEUE & DEQUE ADT hota[AT]hyderabad.bits-pilani.ac.in
A QUEUE USING TWO STACKS
5 Enqueue: push the elements into
2
the Stack 1.
3 2 5 Dequeue: push all the elements
3 Pop Out from Stack 1 into Stack 2, and
3 5
then pop from Stack 2.
2 2
5 3
Which operation is costly here?
Stack 1 Stack 2
WHILE current_time < simulation_time: // Step 2a: Check for new customer arrival IF random() < arrival_rate: new_customer = {arrival_time: current_time, service_time: random(1, 5)} shortest_lane = lane with the shortest queue lanes[shortest
#include <queue> using std::queue; queue<int>myQueue; push(e); pop(); front(); back(); size(); empty;

C++ STL QUEUE (SUPERMARKET CHECKOUT)


Output:

Lab 7: next week


Lab 7: next week
CS F211: DATA STRUCTURES & ALGORITHMS Chittaranjan Hota, PhD
(2 ND SEMESTER 2024-25) Senior Professor, Computer Sc.
BITS-Pilani Hyderabad Campus
VECTORS, AND SEQUENCES hota[AT]hyderabad.bits-pilani.ac.in
ADAPTER DESIGN PATTERN
Deque Stack Queue typedef string Elem; // A queue using a deque:
DequeStack {// stack as deq template<typename E>
insertFront() - - public: void Queue<E>::enqueue(E elem)
insertBack() Push() Enqueue() DequeStack(); {
int size() const; dq.insertBack(elem);
removeFront() - Dequeue() bool empty() const; }
const Elem& top(); template<typename E>
removeBack() Pop() - void push(const Elem& e); void Queue<E>::dequeue() {
void pop(); if(dq.empty())
private: throw “Queue Underflows!”;
Size() Size() Size() LinkedDeque D; dq.removeFront();
Empty() Empty() Empty() }; }
STL VECTORS WITH
ALGORITHMS
POSITION ADT & ITERATORS
•The Position ADT models the notion of place within a
data structure where a single object is stored.
•It gives a unified view of diverse ways of storing
data, such as A List container: Index of an element may
change but not the position/ memory
• a cell of an array location.
• a node of a linked list
•Just one method:
• object p.element(): returns the element at position
• In C++ it is convenient to implement this as *p
Although a position is a useful object, it would be more useful still to be able to navigate through the
container, for example, by advancing to the next position in the container. Such an object is called an
iterator.
CS F211: DATA STRUCTURES & ALGORITHMS Chittaranjan Hota, PhD
(2 ND SEMESTER 2024-25) Senior Professor, Computer Sc.
BITS-Pilani Hyderabad Campus
SEQUENCES AND ITERATORS hota[AT]hyderabad.bits-pilani.ac.in
STL LISTS IN C++
#include <list>
using std::list;
list<int>myList;

program source: https://www.geeksforgeeks.org/


SEQUENCE ADT
•The Sequence ADT generalizes the
Vector and List ADTs.
•Elements are accessed by:
- Index, or Position

You might also like