DSA Cheat Sheet
DSA Cheat Sheet
DSA Cheat Sheet
Cheat Sheet
Table of Contents
C++ Data Structures and Algorithms Cheat Sheet
Table of Contents
1.0 Data Structures
1.1 Overview
1.2 Vector std::vector
1.3 Deque std::deque
1.4 List std::list and std::forward_list
1.5 Map std::map and std::unordered_map
1.6 Set std::set
1.7 Stack std::stack
1.8 Queue std::queue
1.9 Priority Queue std::priority_queue
1.10 Heap std::priority_queue
2.0 Trees
2.1 Binary Tree
2.2 Balanced Trees
2.3 Binary Search
2.4 Depth-First Search
2.5 Breadth-First Search
3.0 NP Complete Problems
3.1 NP Complete
3.2 Traveling Salesman Problem
3.3 Knapsack Problem
4.0 Algorithms
4.1 Insertion Sort
4.2 Selection Sort
4.3 Bubble Sort
4.4 Merge Sort
4.5 Quicksort
1.1 Overview
1.2 Vector std::vector
Use for
Simple storage
Adding but not deleting
Serialization
Quick lookups by index
Easy conversion to C-style arrays
Efficient traversal (contiguous CPU caching)
Time Complexity
Example Code
std::vector<int> v;
//---------------------------------
// General Operations
//---------------------------------
// Size
unsigned int size = v.size();
// Iterate
for(std::vector<int>::iterator it = v.begin(); it != v.end(); it++) {
std::cout << *it << std::endl;
}
Use for
Notes
Pronounced 'deck'
Stands for Double Ended Queue
Example Code
std::deque<int> d;
//---------------------------------
// General Operations
//---------------------------------
// Size
unsigned int size = d.size();
// Iterate
for(std::deque<int>::iterator it = d.begin(); it != d.end(); it++) {
std::cout << *it << std::endl;
}
Direct access
Time Complexity
Example Code
std::list<int> l;
//---------------------------------
// General Operations
//---------------------------------
// Size
unsigned int size = l.size();
// Iterate
for(std::list<int>::iterator it = l.begin(); it != l.end(); it++) {
std::cout << *it << std::endl;
}
// Clear
l.clear();
//---------------------------------
// Container-Specific Operations
//---------------------------------
Use for
Key-value pairs
Constant lookups by key
Searching if key/value exists
Removing duplicates
std::map
Ordered map
std::unordered_map
Hash table
Sorting
Notes
Time Complexity
std::map
Insert O(log(n))
std::unordered_map
Insert O(1)
Find/Remove Value --
Example Code
std::map<std::string, std::string> m;
//---------------------------------
// General Operations
//---------------------------------
// Insert
m.insert(std::pair<std::string, std::string>("key", "value"));
// Access by key
std::string value = m.at("key");
// Size
unsigned int size = m.size();
// Iterate
for(std::map<std::string, std::string>::iterator it = m.begin(); it != m.end(); i
std::cout << *it << std::endl;
}
// Remove by key
m.erase("key");
// Clear
m.clear();
//---------------------------------
// Container-Specific Operations
//---------------------------------
Use for
Removing duplicates
Ordered dynamic storage
Simple storage
Direct access by index
Notes
Time Complexity
Operation Time Complexity
Insert O(log(n))
Remove O(log(n))
Find O(log(n))
Example Code
std::set<int> s;
//---------------------------------
// General Operations
//---------------------------------
// Insert
s.insert(20);
// Size
unsigned int size = s.size();
// Iterate
for(std::set<int>::iterator it = s.begin(); it != s.end(); it++) {
std::cout << *it << std::endl;
}
// Remove
s.erase(20);
// Clear
s.clear();
//---------------------------------
// Container-Specific Operations
//---------------------------------
Use for
Push O(1)
Pop O(1)
Top O(1)
Example Code
std::stack<int> s;
//---------------------------------
// Container-Specific Operations
//---------------------------------
// Push
s.push(20);
// Size
unsigned int size = s.size();
// Pop
s.pop();
// Top
int top = s.top();
Use for
Notes
Example Code
std::queue<int> q;
//---------------------------------
// General Operations
//---------------------------------
// Insert
q.push(value);
// Size
unsigned int size = q.size();
// Remove
q.pop();
Use for
Notes
Example Code
std::priority_queue<int> p;
//---------------------------------
// General Operations
//---------------------------------
// Insert
p.push(value);
// Access
int top = p.top(); // 'Top' element
// Size
unsigned int size = p.size();
// Remove
p.pop();
1.10 Heap std::priority_queue
Notes
2.0 Trees
When trees are not balanced the benefit of log(n) operations is lost due to the
highly vertical structure
Examples of balanced trees:
AVL Trees
Red-Black Trees
Data Structures:
Tree
Sorted array
Space:
O(1)
Best Case:
O(1)
Worst Case:
O(log n)
Average:
O(log n)
Visualization:
Data Structures:
Tree
Graph
Space:
Performance:
Visualization:
2.5 Breadth-First Search
Idea:
Data Structures:
Tree
Graph
Space:
Performance:
Visualization:
3.0 NP Complete Problems
3.1 NP Complete
NP Complete means that a problem is unable to be solved in polynomial time
NP Complete problems can be verified in polynomial time, but not solved
4.0 Algorithms
Details
Advantages
Easy to code
Intuitive
Better than selection sort and bubble sort for small data sets
Can sort in-place
Disadvantages
Visualization
Idea
Details
Advantages
Simple
Can sort in-place
Low memory usage for small datasets
Disadvantages
Visualization
4.3 Bubble Sort
Idea
Details
Advantages
Disadvantages
Visualization
4.4 Merge Sort
Idea
Details
Advantages
Disadvantages
Visualization
4.5 Quicksort
Idea
Details
Advantages
Optimizations
Choice of pivot:
Choose median of the first, middle, and last elements as pivot
Counters worst-case complexity for already-sorted and reverse-sorted
Visualization