Data Structures and Algorithms
by Sarfraz Raza
https://sites.google.com/site/sarfrazraza
TAs: Maryam, Usman, Nadeem, Hamza
by Sarfraz Raza
https://sites.google.com/site/sarfrazraza
TAs: Maryam, Usman, Nadeem, Hamza
Other related course:
DSA 2021: https://sites.google.com/view/dsa-itu-2021
DM 2017: https://sites.google.com/view/ds-ucp-2017/home
DSA 2017: https://sites.google.com/view/dsa-ucp2017
Algo 2020: https://sites.google.com/view/algo-ucp-2020/home
Algo 2021: https://sites.google.com/view/algo-itu-2021/home
The Central Question of Data structures
We started with variable data structures
Their pros
Their limitation
We moved to static arrays
Their applications
Sorting
Searching
Their issues
Dynamic Arrays (templates implementation)
Growable Array with fixed addition
Its push_back function time complexity analysis
Vector Implementation
Its push_back function time complexity analysis
Arraylist implementation
Its push_back function time complexity analysis (exercise)
(Unfortunately we lost this lecture's recording, you may see the previous year recording)
Why iterators?
Forward Iterators
Using Iterators to:
Insert
Erase
Erase at an index
Erase the range
Details of the operators
begin() and end() functions
Discussion on reverseIterator
We covered:
Stacks
Fixed Size Stack
Dynamic Size Stack
Queue
Why shifting will not work? Time Complexity Violation.
Why only two pointers approach will not work? Space Waste Issue.
Why Circular Array like structure will work?
Quiz
Question 1:
How to implement Dynamic Queue using vector in the background? Implement them?
Question 2:
How to implement Dynamic Queue using Two Dynamic Stacks?
Stack
Integer to any base convertor + Complexity Analysis
Queue
Dynamic Queue using Heap Array and Vector
Shifting and two pointer approach - O(N^2)
Efficient two pointer approach (in-place reversal) only when resizing - O(N)
Queue using 2 dynamic stacks
Stueue data structure
Implemented using 3 stacks
Works both as a stack and a queue
Deque (double-ended queue)
Next Lab
Question 1: Implementing Dynamic Queue using vector
Question 2: Implementing Dynamic Queue using vector (reversal strategy)
Question 2: double-ended queue
Next Class
Expression Evaluator
MinStack
Trapping Rain Water Problem
Sliding Window Problem
Expression Evaluator
Solitaire Project Design
Big O
Little o
Big Omega
Little omega
Big Theta
Recursion Review
Use Of Base Case And Recursive Calls
Denary To Binary Conversion Using Recursion
Step By Step Analysis Of Function Call Stack During Recursion
(Unfortunately we lost this lecture recording, you may see the previous year's recording)
Problems In Using Vector Data Structure Involving Insertion/Deletion At iTh Index While Maintaining Sorted Order
Forward List(Train) Implementation Using A Dummy Engine Node
InsertAtFront(), DeleteAtFront(), And InsertAtTail() Functions, Doing Insertions And Deletion In O(1)
Why DeleteAtTail() Is Not Possible In Forward List, Hence Requiring The Need For Doubly Linked List
Printing Forward List Using Loop
Discussion On Implementing Stack And Queue Data Structures Using Forward List
A recap on variables, arrays ,Dynamic Arrays ,Growable array, vector ,stack , queue,(Min/Max stack),Dequeue , strew,Forward List.
Forward List without a Dummy Engine Node.
insertFront() ,insertAtBack() functions and iterators .begin(), .end() ,postincrement, preincrement,== operator and Dereference operator(*) were implemented.
Doubly ended list by maintaining a previous node pointer (pre), updating all functions accordingly.
Discussion on reverse iterators of Doubly ended list.
A recap on variables, arrays ,Dynamic Arrays ,Growable array, vector ,stack , queue,(Min/Max stack),Dequeue , strew,Forward List.
Talk about graphs, adjacency matrix
Idea on how to store user's friends and posts etc
need fast access to such information with billions of users
advantages & disadvantages of vectors, lists etc for such data.
Road intersection on map can be thought of as a vertex of directed graph, and each edge is the possible road we can take from there.
Problems In Using Linked List Data Structure, Searching Being An O(N) Operation
Recursive Definition Of A Binary Tree: Being Either Empty Or Having Some Data And A Sub Tree To Its Left Or To Its Right
Constructing Binary Tree Using A Node Having Data, Left Node Pointer, and Right Node Pointer
Insertion Function, Setting A Criteria For Insertion Converts A Binary Tree To A Binary Search Tree
Minimum Value Is On The Extreme Left Of A BST While Maximum Value Stored Is On The Extreme Right
IN-Order Traversal LNR ,RNL Using Recursion To Sort The Binary Tree In Ascending And Descending
Pre-Order, Post-Order Traversal Discussion
Post-Order LRN, RLN, Solves Most Problems Of BST.
Finding Size, Counting Leaves Of BST, Counting Primes, Finding Height All Using Post-Order Traversal
Printing Nodes Kth Distance Away Also Using Post-Order Traversal
Pre-Order NLR,NRL Saves The Shape Of A Tree
Level-Order Traversal Using Queue
Is Equal Tree And Is Mirror Tree Done Using Pre-Order Traversal
Special Print For Binary Search Tree
Is Balanced Tree Problem Discussion, O(N) Solution By Going Down To Up Instead Of Up To Down
Implementing BST Using Parent Pointer
Deletion In BST Using Parent Pointer, 3 Special Cases: 1-Leaf, 2- One Node, 3-Two Node
Successor And Predecessor Discussion
Implementation Of Successor In BST
In-Order LNR Iterator Class Implementation Using Successor And Predecessor Functions
Creating MinNode And MaxNode Helper Functions For In-Order Iterator
LNRBegin() And LNREnd() Functions For BST
FindNode Implementation, O(Depth Of The Tree)
Maintaining Synthesize Attribute In Binary Tree
IsBalancedTree() Implementation
IsBST() Problem Discussion
Finding Common Ancestor Of Two Nodes
Path From Root To Leaf For Target Sum Problem
Symetric tree problem (leetcode 101)
Construct BST from inorder and postorder traversals (leetcode 106)
Largest value in tree row
Level order traversal II (leetcode 107)
Validate BST (leetcode 98)
Same tree (leetcode 100)
Problem with BST is it can get skewed if data inserted in in ascending order.
Scapegoat and AVL are both self balancing trees.
Scapegoat saves a size variable in every node, where size is the size of tree below the node.
at insertion, the tree is imbalanced, (difference in size of both subtrees is more than 1) then the subtree is reconstructed.
AVL saves height at every node
If imbalance happens, it applies zig or zag movement of subtree to balance it.
Problems discussed in Lecture:
Longest uploaded prefix (Leet code problem no. 2424)
Merge similar Items (Leet code 2363)
Design a number container system (Leet code 234)
Stock Price Fluctuation (Leet code 2034)
The number of Smallest Unoccupied (Leet code 1942)
Abstract Data type of Heap: Insert, Remove Max, Build Heap, Heapify (Up and Down)
Definition of Complete Tree and its Advantages Over BST
Recursive Definition Of Min Heap and Max Heap
Height Of Complete Tree is Log(N)
How To Know If Tree is Complete And If Is Heap
Insertion Operation, Heapifying Up To Maintain Heap Property
Extraction Operation, Heapifying Down To Maintain Heap Property
Build Heap and Its Time Complexity Analysis
Heap Sort, with Discussion On How To Do Heap Sort Using Emplace
Implementation Of Disk Sort Using Priority Queue (Introduction to I/O Models)
Introduction To Randomized Data Structures
Treaps Data Structure (Tree +Heap)
Insertion In Treap Based On BST Property, Order is Maintained According to Heap Property
Splay Trees (Last Searched Or Inserted Elements are Kept At Or Near The Top)
Visualization And Discussion On Implementation Of Skiplist
Proof that Height Of SkipList is O(Log N)
dos shell/cmd intro
basic dos commands
directory structure explained.
root directory having sub directories or files, which is true recursively downwards.
can have multiple root directories (C: D:).
or Linux like implementation where only one root directory (/)
some macro commands.
Ctrl+C to terminate commands.
Vim intro
basic vim functionality
command mode and insert mode.
Expected to use std::list
Why B Tree are Important?
Split in B Tree.
Insert in B Tree using Preemptive split.
Further Discussion:
B Trees to store large amount of data (on hard disk)
How many disk reads are required to find data in 1GB and 1TB of data?
Deletion of node in B Tree
Cases:
If k key of a node x and the x is a leaf.
If the key k is in node x and x is an internal node.
If the key k is not present in internal node x.
We need a DSA which:
store 2d-points / nd-points
find/locate if point exists.
find nearest neighbour.
KD-Tree
points on a plane
subdivide the plane such that no more than one point lies in a single partition.
divide in x-axis in middle
divide in y-axis in middle.
and so, on
basically, partitioning in half space on each point
Merge Sort
split the array and sort in chunks.
and merge back in sorted fashion
cost: O(2N*LogN)
Heap Sort
build Heap (Lecture 19 for more info)
and read it back.
implemented with std::priority_queue
Quick Sort
pick random pivot point.
sort around that pivot recursively
partition and call quick sort on both sections.
cost: O(N^2)
Count Sort
find frequency or count of each element
then rebuild the original collection based on the count of each element in sorted fashion
Bucket Sort
each value divided into "buckets"
sort them
K-way merge them (see lecture 20)
Radix Sort
sorts 2d matrix
start from last column, sort it and move to previous column
sort until first column
matrix sorted such that each row is a number and is sorted as a whole.
Disjoint Union Set Implementation
Difference Between log(N) and log*(N)
Union Operation Based on Ranking Of Elements
Find Operation, Basic and Then Enhanced To Compress Path
Kruskal Algorithm, Application of Disjoint Union Sets
Problems discussed in the Lecture:
Re-visited Data Structures covered and discussed their downside:
Fixed Size Arrays, Dynamic Arrays, Vectors
Stack, Queue, Stueue, Deque, MinMaxStack
Singly Linked List, Doubly Linked List
Binary Tree, Balance Tree, Scapegoat tree, AVL Trees, Graphs
Heap/Priority Queue, Treap, splay
BTrees, KD Trees
Hashing
key, value pair concept
searching through key
Direct Addressing: Search, Insert, Delete
Hash Tables and Hash Functions
Collisions because of keys
Solution for Collisions: Chaining, Linear Probing, Quadratic Probing, Double Hashing
Review Of The Concept Of Hashing
Importance of Load Factor In Hash Table
Case Of Deletion If Collisions Occurred During Insertion
Analysis of different methods
For Chaining: usually cost O(α), where α is N/m
No more than m^2 probes in open addressing
Double hashing
rerun on collission
Universal Hashing
chance of collision is 1/m
Aim: Perform Queries Such as Word Search, Sentence Search From Over Large Sum Of Data
Tries Implementation
Hashing Implementation
Saving Indexes Of Each Word Of Every File
Discussion On How To Find Sentence Occurrences
Setting Priorities In Query Results
Comparison Between The Two Implementations