Learn algorithms
Subject: Data Models and Algorithms
50 chapters
1. Quicksort
[Verse 1]
Got an array that's all mixed up, elements scattered everywhere
Time to sort it quick and clean, with an algorithm that's fair
Pick a pivot from the bunch, could be first or middle ground
Partition left and right sides, watch the magic come around
All the smaller values go left, bigger ones move to the right
Pivot finds its final spot, everything falls into sight
Recursive calls on both sides, divide and conquer is the way
Quicksort breaks it down so smooth, efficiency saves the day
[Chorus]
Pick pivot partition recurse, that's the quicksort way
Pick pivot partition recurse, sorting every day
Divide and conquer left and right, average case n log n time
Pick pivot partition recurse, quicksort's paradigm
[Verse 2]
Choose your pivot strategy wise, random keeps performance tight
Median of three works well, avoids the worst case blight
Lomuto scheme keeps it simple, two pointers track the dance
Hoare partition runs faster, given the proper chance
Base case hits when size is small, single elements are done
Merge the sorted pieces back, the algorithm's won
In place sorting saves the space, no extra arrays to make
Just swap the elements around, for memory's sake
[Chorus]
Pick pivot partition recurse, that's the quicksort way
Pick pivot partition recurse, sorting every day
Divide and conquer left and right, average case n log n time
Pick pivot partition recurse, quicksort's paradigm
[Bridge]
When the pivot's always worst, n squared time will make you cry
But randomization helps, keeps performance riding high
Stack overflow can bite you, when recursion goes too deep
Iterative solutions, make the call stack promises keep
[Verse 3]
Unstable sort by nature, equal elements may flip
But speed makes up for order loss, when performance is your grip
Cache friendly memory access, locality principle strong
Sequential reads and writes, keep the pipeline moving along
Tail call optimization, can help with memory cost
But iterative conversion, means recursion limit's lost
Industry standard algorithm, libraries use it wide
From C plus plus to Python, quicksort's the sorting guide
[Chorus]
Pick pivot partition recurse, that's the quicksort way
Pick pivot partition recurse, sorting every day
Divide and conquer left and right, average case n log n time
Pick pivot partition recurse, quicksort's paradigm
[Outro]
When you need to sort it fast, and memory's running thin
Quicksort's got your back covered, let the sorting begin
Pick pivot partition recurse, remember this refrain
Quicksort mastery in your hands, algorithm's domain
2. Mergesort
[Verse 1]
Got an unsorted array, chaos in the mix
Elements scattered like they're playing dirty tricks
But I got a strategy, divide and conquer clean
Split it down the middle, most efficient you've seen
Take the left half, take the right half too
Recursively break them down, that's what we do
Keep on splitting till you get to single nodes
Then we merge them back up, following the codes
[Chorus]
Divide and merge, divide and merge
Breaking down arrays then making them converge
O of n log n, that's the time we earn
Stable sorting guaranteed, watch the patterns turn
Divide and merge, divide and merge
Split it in the middle, let the order emerge
[Verse 2]
When you're merging back together, here's the master plan
Two sorted halves combine with a helping hand
Compare the first elements, pick the smaller one
Place it in result array, but we're not done
Move the pointer forward where the winner came
Keep comparing elements, playing merge game
Till one half is empty, then append the rest
Mergesort delivers results that are the best
[Chorus]
Divide and merge, divide and merge
Breaking down arrays then making them converge
O of n log n, that's the time we earn
Stable sorting guaranteed, watch the patterns turn
Divide and merge, divide and merge
Split it in the middle, let the order emerge
[Bridge]
Base case is single element, already sorted clean
Recursive calls build up the sorting machine
Extra space required, that's the trade we make
O of n memory for performance sake
Worst case, best case, average stays the same
Logarithmic levels in this sorting game
[Verse 3]
From the bottom up we build our sorted runs
Merging pairs of singles till the job is done
Then merge pairs of pairs, doubling every round
Most reliable sort that can ever be found
When stability matters and you need it fast
Mergesort's the algorithm that's built to last
Predictable performance, no quadratic fears
The divide and conquer champion for all these years
[Chorus]
Divide and merge, divide and merge
Breaking down arrays then making them converge
O of n log n, that's the time we earn
Stable sorting guaranteed, watch the patterns turn
Divide and merge, divide and merge
Split it in the middle, let the order emerge
[Outro]
Split it down the middle
Merge it back together
Mergesort forever
Divide and merge the answer
3. Heapsort
[Verse 1]
Start with an array, unsorted and wild
Heapsort gonna tame it, structured and styled
First we build a heap, max property strong
Parent beats the children, nothing can go wrong
Bottom up we go, heapify the base
Bubble up the largest, put them in their place
Array transformation, heap property true
Left child at two i, right at two i plus two
[Chorus]
Build it up, tear it down, that's the heapsort way
Max heap to the top, sorted array
Extract the root, put it at the end
Heapify again, let the process blend
Build it up, tear it down, order from the mess
Log n operations, efficiency blessed
[Verse 2]
Phase one is construction, make that heap complete
Every parent stronger than children they meet
Start from last parent, work your way up high
Sift down the violators, let the big ones fly
Index parent minus one divided by two
Mathematics guides us in what we gotta do
Now we got a max heap, largest at the top
Time for phase two, the extraction won't stop
[Chorus]
Build it up, tear it down, that's the heapsort way
Max heap to the top, sorted array
Extract the root, put it at the end
Heapify again, let the process blend
Build it up, tear it down, order from the mess
Log n operations, efficiency blessed
[Bridge]
Swap the root with last, shrink the heap size down
Restore the heap property, largest wears the crown
Repeat until we're done, one by one they fall
Sorted in ascending, we conquered them all
In place algorithm, memory stays tight
N log n complexity, performance feels right
[Verse 3]
Heapify function, the heart of our game
Compare with children, largest takes the name
If parent is smaller, then we gotta swap
Recursively downward until violations stop
Complete binary tree, packed in array form
No pointers needed, efficiency transform
Unstable sorting, equal elements shift
But guaranteed performance is heapsort's gift
[Outro]
From chaos to order, heap shows the way
Build it up, tear it down, sorted array
Remember the process, build then extract
Heapsort delivers, that's algorithm fact
4. Insertion sort
[Verse 1]
Start with the second element, that's the way we begin
Compare it to the left side, see where it fits in
If it's smaller than the neighbor, time to make a swap
Shift the bigger one over, let the smaller one drop
Building sorted portions from the left side growing strong
Each insertion finds its place where it belongs
[Chorus]
Insert and compare, shift if you dare
Building order one by one with care
Left side sorted, right side wild
Insertion sort keeps it compiled
Insert and compare, shift if you dare
Linear search through sorted pairs
[Verse 2]
Take the current element, hold it in your hand
Search the sorted portion to understand
Where this new piece belongs in the grand plan
Shift the greater elements, make room if you can
Starting from position one, not zero we begin
'Cause single element's already sorted within
[Chorus]
Insert and compare, shift if you dare
Building order one by one with care
Left side sorted, right side wild
Insertion sort keeps it compiled
Insert and compare, shift if you dare
Linear search through sorted pairs
[Bridge]
Best case scenario when it's already neat
Big O of n makes it complete
But worst case reversed takes n squared time
Still stable and in-place, working fine
Small datasets love this algorithm's flow
Adaptive nature helps performance grow
[Verse 3]
Outer loop controls which element we select
Inner loop finds where it should connect
Backwards through the sorted section we probe
Moving larger values, making space to load
When we find the spot or hit the start
That's where our current element gets its part
[Chorus]
Insert and compare, shift if you dare
Building order one by one with care
Left side sorted, right side wild
Insertion sort keeps it compiled
Insert and compare, shift if you dare
Linear search through sorted pairs
[Outro]
From chaos comes order, one step at a time
Insertion sort's rhythm, steady and prime
Remember the pattern: select, compare, shift
Simple but powerful, that's insertion's gift
5. Bubble sort
[Verse 1]
Let me tell you bout the simplest sort around
Bubble sort's the name, watch the data get down
Start from the beginning, check each pair in line
If the left is bigger, then it's switching time
Move through the array, left to right we go
Heavy elements sink down, light ones rise up slow
Like bubbles in champagne, floating to the top
That's why we call it bubble, watch the small ones pop
[Chorus]
Bubble up, bubble down, compare and swap around
Big O of n squared, when efficiency's not found
But it's stable and in-place, adaptive to the core
Bubble sort's the first one that we learn to explore
Bubble up, bubble down, adjacent pairs we check
Simple nested loops, keep the algorithm in spec
[Verse 2]
Outer loop controls how many passes that we make
Inner loop compares each pair, decisions it will take
If array of n minus one is greater than the next
Swap those two positions, keep the sorting context
Each pass guarantees the largest finds its home
At the end of unsorted section, no more need to roam
Optimization trick: if no swaps in a pass
Array is sorted early, we can finish fast
[Chorus]
Bubble up, bubble down, compare and swap around
Big O of n squared, when efficiency's not found
But it's stable and in-place, adaptive to the core
Bubble sort's the first one that we learn to explore
Bubble up, bubble down, adjacent pairs we check
Simple nested loops, keep the algorithm in spec
[Bridge]
Best case linear time when the data's already neat
Worst case quadratic when reverse order we meet
Equal elements maintain their relative position
Stable sorting property, that's the definition
No extra memory needed, sorts right where it sits
In-place algorithm, that's one of its hits
[Verse 3]
Though it's not efficient for large datasets today
Educational value in a pedagogical way
Teaches basic concepts of comparison-based sorts
Shows how swapping works and algorithm reports
Simple to implement, easy to debug and trace
Watch each element slowly find its rightful place
From chaos comes order with each bubble rise
Fundamental sorting wisdom before your eyes
[Chorus]
Bubble up, bubble down, compare and swap around
Big O of n squared, when efficiency's not found
But it's stable and in-place, adaptive to the core
Bubble sort's the first one that we learn to explore
Bubble up, bubble down, adjacent pairs we check
Simple nested loops, keep the algorithm in spec
[Outro]
Remember bubble sort, though simple it may seem
Foundation for the complex sorting algorithms' dream
Bubble up to the top, let the knowledge flow
First step in the journey of the sorts you need to know
6. Radix sort
[Verse 1]
Listen up, we got a sorting algorithm that's clean
No comparisons needed, it's the fastest you've seen
Takes your integers, breaks them down digit by digit
Stable sorting power, yeah you know we're gonna get it
Start from the least significant, that's the rightful place
Counting sort on each position, keeping perfect pace
Linear time complexity when the range is right
Radix sort's the champion when your data's finite
[Chorus]
Digit by digit, we're sorting tonight
LSD to MSD, keeping order tight
Counting sort buckets, redistribute the load
Radix sort's the method, it's the optimal code
Least significant first, then we work our way up
O of n times k, fill that sorting cup
[Verse 2]
Create your buckets, zero through nine in a row
Count the frequencies, let the histogram grow
Stable sorting matters, keep the relative stance
Same key elements maintain their original dance
Decimal base ten, but binary works too
Hexadecimal sorting, radix handles it through
When your keys are integers and the range is bound
Radix sort's efficiency is the best around
[Chorus]
Digit by digit, we're sorting tonight
LSD to MSD, keeping order tight
Counting sort buckets, redistribute the load
Radix sort's the method, it's the optimal code
Least significant first, then we work our way up
O of n times k, fill that sorting cup
[Bridge]
Most significant digit when you need early stopping
Lexicographic order, keep those strings from dropping
Space complexity's linear, memory we consume
But the speed that we deliver makes our algorithms bloom
No divide and conquer, no recursive calls
Just systematic bucketing through numerical halls
[Verse 3]
Pass through every digit position, left to right we go
Counting sort as subroutine, maintaining stable flow
When k is small and n is large, we dominate the game
Linear sorting power, that's our radix claim to fame
Fixed-width integers, that's our sweet spot zone
Character strings and records, radix stands alone
[Chorus]
Digit by digit, we're sorting tonight
LSD to MSD, keeping order tight
Counting sort buckets, redistribute the load
Radix sort's the method, it's the optimal code
Least significant first, then we work our way up
O of n times k, fill that sorting cup
[Outro]
When comparison sorts hit n log n ceiling
Radix breaks the barrier with that linear feeling
Digit by digit, that's the radix way
Stable sorting champion, every single day
7. Counting sort
[Verse 1]
Got an array of numbers, all within a bound
From zero to some max value, that's where power's found
First we build a counting array, size of range plus one
Initialize with zeros, that's how we begun
Walk through every element in our input set
Count frequency of each value, don't forget to get
Every single occurrence stored in proper place
Counting array holds the key to sorting with such grace
[Chorus]
Count the frequencies, that's step number one
Build that counting array until the count is done
Cumulative sum it up, positions now we know
Place them in their final spots, watch the sorted flow
Count, accumulate, and place
Linear time, we win the race
Stable sort when done with care
Counting sort beyond compare
[Verse 2]
Now we modify our counts to cumulative form
Each position tells us where each element belongs
Starting from the second slot, add the one before
Building up the prefix sums, that's the counting core
Walk backwards through input, maintaining stable nature
Place each element where counts array shows its future
Decrement the count each time, making room for more
Same values stay in order, that's what stable's for
[Chorus]
Count the frequencies, that's step number one
Build that counting array until the count is done
Cumulative sum it up, positions now we know
Place them in their final spots, watch the sorted flow
Count, accumulate, and place
Linear time, we win the race
Stable sort when done with care
Counting sort beyond compare
[Bridge]
Space complexity trade-off, memory for the speed
Range size matters most, that's what you need to heed
When the range is reasonable, counting sort's your friend
Linear performance guaranteed from start to end
Not comparison based, we count instead of compare
O of n plus k runtime, efficiency we share
[Verse 3]
Perfect for those situations when you know the bounds
Grades from zero to hundred, where efficiency's found
Character frequencies, histogram creation
Radix sort foundation, digit separation
But beware the memory usage when the range grows wide
Sparse data wastes space, choose your algorithm guide
Integers are required, floats need not apply
Counting sort's domain is clear, now you know just why
[Chorus]
Count the frequencies, that's step number one
Build that counting array until the count is done
Cumulative sum it up, positions now we know
Place them in their final spots, watch the sorted flow
Count, accumulate, and place
Linear time, we win the race
Stable sort when done with care
Counting sort beyond compare
[Outro]
Three simple steps and you're done
Count, accumulate, place each one
When the range is small and tight
Counting sort will treat you right
Linear time complexity
That's the counting guarantee
8. Timsort
[Verse 1]
Started back in Python with a hybrid mind
Merge sort meets insertion, perfectly designed
Tim Peters saw the patterns in the real world data
Sorted runs and galloping, made algorithms better
Binary insertion when the size is small
Merge the longer sequences, efficiency for all
Adaptive to the input, stable as can be
Natural runs ascending, that's the Timsort key
[Chorus]
Tim-sort, Tim-sort, hybrid is the way
Merge and insert together, stable sorting stays
Gallop mode when one run wins the race
Min-merge keeps the stack in perfect place
Tim-sort, Tim-sort, runs are what we find
Adaptive algorithm for the data that's designed
[Verse 2]
Scan the array forward, find the natural runs
Strictly descending flipped, ascending ones
Minimum run length calculated by the size
Extend with binary insertion, that's the compromise
Stack maintains the runs until it's time to merge
Invariants keep balance as the process does converge
When one run keeps winning, galloping takes control
Double up the jump size, that's the searching goal
[Chorus]
Tim-sort, Tim-sort, hybrid is the way
Merge and insert together, stable sorting stays
Gallop mode when one run wins the race
Min-merge keeps the stack in perfect place
Tim-sort, Tim-sort, runs are what we find
Adaptive algorithm for the data that's designed
[Bridge]
Seven element threshold for insertion sort to shine
Merge sort handles bigger, crossing over the line
Stable means equal elements keep their original place
Linear time best case when data shows its grace
Worst case n log n, average case the same
But real world performance puts Timsort in the fame
[Verse 3]
Two finger merge technique with temporary space
Copy smaller run first, then merge into place
Gallop threshold seven, then it drops to zero
Exit when the wins fall below the counting hero
Merge collapse maintains three rules on the stack
Balance keeps efficiency, performance on track
From Python's standard library to Java's arrays
Timsort rules the sorting in these modern days
[Chorus]
Tim-sort, Tim-sort, hybrid is the way
Merge and insert together, stable sorting stays
Gallop mode when one run wins the race
Min-merge keeps the stack in perfect place
Tim-sort, Tim-sort, runs are what we find
Adaptive algorithm for the data that's designed
[Outro]
When your data's partially sorted, Timsort's your friend
Hybrid adaptation means efficiency to the end
Remember Tim Peters and his sorting innovation
Stable, fast, and smart for every application
9. Binary search
[Verse 1]
Got a sorted list, a million items long
Need to find one element, can't take too long
Linear search would check them one by one
But binary's the method when you want it done
Start in the middle, that's your first guess
Compare your target, is it more or less
If it's too high, cut the right side out
If it's too low, left side's what it's about
[Chorus]
Cut in half, cut in half, that's the binary way
Log of n, log of n, keeps the time delay
Sorted data, start middle, compare and decide
Eliminate half, then repeat the ride
Cut in half, cut in half, divide and conquer style
O log n complexity makes it all worthwhile
[Verse 2]
Low index zero, high index at end
Middle equals low plus high divided friend
If middle value matches what you seek
Return that index, mission complete unique
But if the target's greater than mid-point
Move low to middle plus one, that's the joint
If target's smaller, high becomes mid minus
Keep the search space tight, that's how we find it
[Chorus]
Cut in half, cut in half, that's the binary way
Log of n, log of n, keeps the time delay
Sorted data, start middle, compare and decide
Eliminate half, then repeat the ride
Cut in half, cut in half, divide and conquer style
O log n complexity makes it all worthwhile
[Bridge]
While low is less than or equal high
Keep searching till you reach the sky
Base case hit when bounds have crossed
Return negative one, element's lost
From million items down to one
In twenty steps your search is done
That's the power of the binary search
Logarithmic time, put efficiency first
[Verse 3]
Recursive version calls itself with bounds
Iterative loops until the answer's found
Both approaches give the same result
Just different styles, neither one's at fault
Remember prerequisite, data must be sorted
Random order makes the algorithm thwarted
Binary search trees extend this concept wide
Self-balancing structures keep efficiency as guide
[Chorus]
Cut in half, cut in half, that's the binary way
Log of n, log of n, keeps the time delay
Sorted data, start middle, compare and decide
Eliminate half, then repeat the ride
Cut in half, cut in half, divide and conquer style
O log n complexity makes it all worthwhile
[Outro]
When you need to find that needle in the stack
Binary search will get you on the right track
Divide the problem, conquer with precision
Logarithmic time, that's the optimal decision
10. Linear search
[Verse 1]
Starting from the top with my array in hand
Got a target value that I need to understand
Check position zero is it what I seek
If it matches then my search is complete
But if not then I move to the next spot
One by one till I find what I got
Sequential scanning through each memory slot
Linear search baby this is how we rock
[Chorus]
One by one check every single one
Start to finish till the job is done
O of n that's the time we need
Linear search is guaranteed
First to last we never skip ahead
Check each element till we find what we said
One by one check every single one
Linear search until the job is done
[Verse 2]
Worst case scenario target's at the end
Or maybe not there gotta check again
Best case lucky it's the very first
Average case halfway through the list we traverse
No assumptions bout the data structure
Unsorted arrays this is what we nurture
Simple algorithm easy to code
Step by step down the linear road
[Chorus]
One by one check every single one
Start to finish till the job is done
O of n that's the time we need
Linear search is guaranteed
First to last we never skip ahead
Check each element till we find what we said
One by one check every single one
Linear search until the job is done
[Bridge]
When the data's all scrambled no order in sight
Linear search gonna treat you right
Binary search needs that sorted flow
But linear works wherever you go
Initialize index start at zero
Loop through positions be the hero
Compare and contrast till you find your match
Linear search got your back
[Verse 3]
For loop running while the index is small
Check if current equals target call
If condition true return the spot
If not increment give it another shot
When you reach the end and nothing's found
Return negative one or false profound
Simple logic but it gets things done
Linear search for everyone
[Chorus]
One by one check every single one
Start to finish till the job is done
O of n that's the time we need
Linear search is guaranteed
First to last we never skip ahead
Check each element till we find what we said
One by one check every single one
Linear search until the job is done
[Outro]
From arrays to lists it never fails
Sequential search always prevails
Linear time complexity
That's the algorithmic key
One by one we check them all
Linear search standing tall
11. Interpolation search
[Verse 1]
Got a sorted array and I need to find
A value hidden somewhere inside
Linear search is slow, binary's better
But interpolation makes me a go-getter
Calculate the position, don't just divide by two
Use the value's proportion to guide me through
High minus low times target minus low
Divided by high value minus low value, let's go
[Chorus]
Interpolate, don't just estimate
Jump closer to the target, calculate the rate
Probe position equals low plus the fraction
Better than binary when data's in action
Interpolate, don't just estimate
Uniform distribution, that's the key trait
O of log log n when the spacing is clean
Most efficient search that you've ever seen
[Verse 2]
Start with low index at zero position
High index pointing at the last transmission
Check if target's between the boundary values
If it's outside the range, then the search through
Calculate probe position with the magic formula
Low plus the ratio, that's the criteria
If probe equals target, then we found our prize
If target's smaller, search the left side
[Chorus]
Interpolate, don't just estimate
Jump closer to the target, calculate the rate
Probe position equals low plus the fraction
Better than binary when data's in action
Interpolate, don't just estimate
Uniform distribution, that's the key trait
O of log log n when the spacing is clean
Most efficient search that you've ever seen
[Bridge]
When data's uniform, we shine the brightest
Random distribution makes the worst case tightest
Falls back to linear when the spread is crazy
But with even spacing, performance ain't lazy
Phone book pages, salary ranges
Sequential data where the pattern never changes
[Verse 3]
Update the boundaries after every probe
If target's greater, move low to the node
If target's lesser, move high to the left
Keep interpolating till there's nothing left
Worst case linear, best case logarithmic
Average case performance is quite algorithmic
Remember the assumption that makes this work fine
Data distribution must be in a line
[Chorus]
Interpolate, don't just estimate
Jump closer to the target, calculate the rate
Probe position equals low plus the fraction
Better than binary when data's in action
Interpolate, don't just estimate
Uniform distribution, that's the key trait
O of log log n when the spacing is clean
Most efficient search that you've ever seen
[Outro]
Interpolation search, the smart way to find
When your data's uniform and well-defined
Jump with precision, not just hope and prayer
Mathematical magic gets you right there
12. Exponential search
[Verse 1]
Start with one then double the bound
When your target ain't easily found
Linear search is way too slow
Exponential's the way to go
Jump by powers of two each time
Until you pass your target line
One two four eight sixteen rise
Growing bounds before your eyes
[Chorus]
Double jump until you overshoot
Then binary search to find the root
Exponential then divide and conquer
Time complexity makes you stronger
Oh en log en that's the key
Unbounded arrays set you free
Double jump until you overshoot
Then binary search to find the root
[Verse 2]
When the size is undefined
And the data's not confined
Start at index number one
Keep on doubling til you're done
Found a value that's too high
Now you know your upper sky
Lower bound is half of that
Binary search where it's at
[Chorus]
Double jump until you overshoot
Then binary search to find the root
Exponential then divide and conquer
Time complexity makes you stronger
Oh en log en that's the key
Unbounded arrays set you free
Double jump until you overshoot
Then binary search to find the root
[Bridge]
Two phases make it work so clean
First phase finds the range between
Second phase cuts down the space
Binary search picks up the pace
Sorted data is required
Infinite streams get you fired
Up to search beyond the known
Exponential claims the throne
[Verse 3]
Implementation's crystal clear
Set your low and high frontier
While the high index is less
Than your target under test
Double high and set low too
Previous high's the clue for you
When you overshoot the mark
Binary lights up the dark
[Chorus]
Double jump until you overshoot
Then binary search to find the root
Exponential then divide and conquer
Time complexity makes you stronger
Oh en log en that's the key
Unbounded arrays set you free
Double jump until you overshoot
Then binary search to find the root
[Outro]
From the known into unknown
Exponential search has grown
Doubling bounds then cut in half
Efficient search is quite a craft
13. Breadth-first search (BFS)
[Verse 1]
Starting at the root, we begin our quest
Queue it up first, that's how we progress
Level by level, spreading out wide
Before going deep, we explore each side
Mark it as visited, don't come back twice
FIFO ordering keeps our search precise
Neighbors get added to the waiting line
Breadth before depth, that's the grand design
[Chorus]
BFS, queue it up, level by level we go
First in first out, that's the flow we know
Shortest path finder, layer by layer we grow
BFS, guarantee, minimum hops in a row
Queue it up, spread it out, that's how the search will show
Level by level, breadth first, here we go
[Verse 2]
Graph or tree structure, doesn't matter which
Adjacent nodes waiting, ready to switch
Enqueue the children, dequeue the parent
Systematic searching, path lengths apparent
No recursion needed, just iteration clean
Queue holds our frontier, visited nodes are seen
When target is found, we know for sure
The path that we took is optimal and pure
[Chorus]
BFS, queue it up, level by level we go
First in first out, that's the flow we know
Shortest path finder, layer by layer we grow
BFS, guarantee, minimum hops in a row
Queue it up, spread it out, that's how the search will show
Level by level, breadth first, here we go
[Bridge]
While the queue ain't empty, keep the process alive
Pop from the front, let the algorithm thrive
Check if it's the goal, if not add more nodes
Unweighted graphs, BFS always knows
The shortest distance, minimum edge count
Layer exploration, that's the amount
[Verse 3]
Time complexity running at V plus E
Space complexity queue size, worst case V
Complete and optimal when weights are the same
Breadth first searching earned its fame
From web crawlers to social networks wide
Six degrees separation, BFS as guide
Remember the pattern, queue-based exploration
Level order traversal across the nation
[Chorus]
BFS, queue it up, level by level we go
First in first out, that's the flow we know
Shortest path finder, layer by layer we grow
BFS, guarantee, minimum hops in a row
Queue it up, spread it out, that's how the search will show
Level by level, breadth first, here we go
[Outro]
Queue it up, spread it wide
Breadth first search, your pathfinding guide
Level by level, that's the BFS way
Shortest paths found, every single day
14. Depth-first search (DFS)
[Verse 1]
Starting at the root, we pick a path and dive
Going deep before we spread, that's how DFS stays alive
Mark the node as visited, push it on the stack
Explore each neighbor fully before we double back
Recursive calls or iterative, both ways get it done
Visit children first completely, one by one by one
[Chorus]
Go Deep, Don't Spread - that's the DFS way
Stack it up, mark it down, visit all the way
Go Deep, Don't Spread - through the tree we roam
Backtrack when you hit the end, then find another home
DFS, DFS, diving to the core
Stack or recursion, either way explore
[Verse 2]
Three main applications keep this algorithm hot
Topological sorting when dependencies we've got
Cycle detection in a graph, DFS will find the loop
Connected components grouping, putting nodes in their troop
Time complexity linear, vertices plus edges count
Space complexity depends on how deep your tree can mount
[Chorus]
Go Deep, Don't Spread - that's the DFS way
Stack it up, mark it down, visit all the way
Go Deep, Don't Spread - through the tree we roam
Backtrack when you hit the end, then find another home
DFS, DFS, diving to the core
Stack or recursion, either way explore
[Bridge]
Pre-order, in-order, post-order traversal
DFS gives you options, take your pick for every trial
Discovery time and finish time, timestamps as you go
Parenthesis theorem shows the structure that you know
From maze solving to web crawling, DFS runs the show
[Verse 3]
Implementation choices, let me break it down for you
Recursive feels natural but the stack might overflow too
Iterative with explicit stack gives you more control
Mark visited in a set or boolean array to reach your goal
Colors work for tracking: white, gray, black progression
Forward, back, and cross edges tell the graph's confession
[Chorus]
Go Deep, Don't Spread - that's the DFS way
Stack it up, mark it down, visit all the way
Go Deep, Don't Spread - through the tree we roam
Backtrack when you hit the end, then find another home
DFS, DFS, diving to the core
Stack or recursion, either way explore
[Outro]
When breadth-first goes wide, DFS goes deep
Memory efficient path, promises to keep
From root to leaf completely, then backtrack and repeat
DFS mastery, now your toolkit's complete
15. Dijkstra's algorithm
[Verse 1]
Graph with weighted edges, need the shortest path to find
Dijkstra had the vision, brilliant algorithmic mind
Start with source vertex, mark distance as zero clean
All other nodes infinity, the largest you've ever seen
Priority queue ready, min-heap keeps us organized
Extract the smallest distance, that's how we optimize
[Chorus]
Select, relax, repeat - that's the Dijkstra beat
Never visit twice, greedy choice so neat
Distance gets smaller, neighbors get updated
Shortest path revealed when algorithm's completed
Select, relax, repeat - Dijkstra's guarantee
No negative weights allowed, positive paths only
[Verse 2]
Pull the minimum from queue, mark that vertex as done
Check each neighbor node, see if distance can be won
Current distance plus edge weight, compare it to what's stored
If it's smaller update parent, new best path explored
Push updated to the queue, let priority decide
Relaxation is the key, distances subside
[Chorus]
Select, relax, repeat - that's the Dijkstra beat
Never visit twice, greedy choice so neat
Distance gets smaller, neighbors get updated
Shortest path revealed when algorithm's completed
Select, relax, repeat - Dijkstra's guarantee
No negative weights allowed, positive paths only
[Bridge]
Time complexity big O, V squared with simple array
V log V plus E log V when binary heap's in play
Fibonacci heap improves it, V log V plus E straight
But implementation matters for the runtime fate
[Verse 3]
Visited set grows larger, unvisited shrinks down
When destination's processed, shortest path is found
Backtrack through the parents, reconstruct the route
From source to every vertex, optimal path pursuit
GPS navigation systems, network routing protocols
Dijkstra's algorithm working, solving real world goals
[Chorus]
Select, relax, repeat - that's the Dijkstra beat
Never visit twice, greedy choice so neat
Distance gets smaller, neighbors get updated
Shortest path revealed when algorithm's completed
Select, relax, repeat - Dijkstra's guarantee
No negative weights allowed, positive paths only
[Outro]
Greedy algorithm power, locally optimal choice
Leads to global solution, give Dijkstra your voice
Single source shortest path, the master of his trade
Graph traversal genius, foundation he has made
16. Bellman-Ford algorithm
[Verse 1]
Graph got edges with some weights that might be negative
Shortest path finder when Dijkstra can't be definitive
Bellman-Ford steps up when cycles bring the drama
Detects the negative loops like algorithmic karma
Start with source vertex, distance set to zero
Every other node infinity, that's how we play hero
Relax the edges, that's the key to our success
If distance plus weight is less, update and progress
[Chorus]
Relax relax relax, V minus one times through
Check every single edge, that's what we gotta do
If we can still improve after all those rounds are done
Negative cycle found, the algorithm's won
Bellman-Ford don't quit when weights go below zero
Finding shortest paths like a computational hero
[Verse 2]
V minus one iterations, that's the magic number
Any path that's optimal can't have more to lumber
Each round we're guaranteeing one more edge precision
Building up the answer with mathematical vision
Take an edge from U to V, check the relaxation
If U distance plus weight beats V's calculation
Update V's distance, keep the parent pointer too
Trace back the shortest path when the work is through
[Chorus]
Relax relax relax, V minus one times through
Check every single edge, that's what we gotta do
If we can still improve after all those rounds are done
Negative cycle found, the algorithm's won
Bellman-Ford don't quit when weights go below zero
Finding shortest paths like a computational hero
[Bridge]
One more pass to catch the lies
If distances still dropping that's our warning sign
Negative cycle means no shortest path exists
Infinite improvement, something's been missed
Time complexity O of V times E
Space complexity O of V, that's the key
[Verse 3]
Unlike Dijkstra's greedy approach with priority queue
Bellman-Ford examines every edge, sees the whole view
Works with negative weights but not negative cycles
Dynamic programming vibes, breaking down the riddles
From source to every vertex, find the minimal cost
Even when the edge weights make other algorithms lost
Johnson's algorithm uses us as preprocessing stage
Bellman-Ford's the foundation, turn another page
[Chorus]
Relax relax relax, V minus one times through
Check every single edge, that's what we gotta do
If we can still improve after all those rounds are done
Negative cycle found, the algorithm's won
Bellman-Ford don't quit when weights go below zero
Finding shortest paths like a computational hero
[Outro]
When the graph gets complicated and the weights turn mean
Bellman-Ford's the algorithm keeping pathways clean
Relax those edges, check for cycles, find your way
Shortest path solution at the end of the day
17. Floyd-Warshall algorithm
[Verse 1]
Started with a graph, weighted and directed
Need the shortest paths, all pairs connected
Matrix D-I-J holds our distances tight
Initialize with edges, infinite for no sight
Direct connections get their weight assigned
All other pairs start undefined
This is the foundation, build it strong
Floyd-Warshall journey, come along
[Chorus]
K-I-J, that's the order we go
Through every vertex, let the magic flow
If D-I-K plus D-K-J is less than D-I-J
Update the distance, that's the Floyd way
All pairs shortest, no stone unturned
Three loops nested, knowledge earned
[Verse 2]
Outer loop K, that's our intermediate
Through every vertex, we mediate
Can we go from I to J through K instead
Check the sum, update what's in our head
If the detour's shorter than direct route
Replace the value, that's absolute
Relaxation step, optimization game
Floyd and Warshall, remember the name
[Chorus]
K-I-J, that's the order we go
Through every vertex, let the magic flow
If D-I-K plus D-K-J is less than D-I-J
Update the distance, that's the Floyd way
All pairs shortest, no stone unturned
Three loops nested, knowledge earned
[Bridge]
Time complexity, O of N cubed
Space complexity, N squared, that's the mood
Works with negative weights, but no negative cycles
Dynamic programming, breaking big problems to little
Bottom up approach, building solutions
Matrix transformation, evolution
[Verse 3]
After K iterations, we got the truth
All pairs shortest paths, that's the proof
From any vertex to any other node
Optimal distance, we cracked the code
Transitive closure, just change the rule
OR operation, Boolean tool
Floyd-Warshall flexes, adapts to need
Algorithmic power, guaranteed
[Chorus]
K-I-J, that's the order we go
Through every vertex, let the magic flow
If D-I-K plus D-K-J is less than D-I-J
Update the distance, that's the Floyd way
All pairs shortest, no stone unturned
Three loops nested, knowledge earned
[Outro]
N cubed time but the result's complete
All pairs shortest, can't be beat
Floyd-Warshall algorithm, now you know
Dynamic programming, watch it grow
18. A* search
[Verse 1]
Started with a problem, need to find the way
From the starting point to goal, what's the price to pay
Dijkstra's got us covered but he's moving slow
Every single neighbor gets explored, you know
But what if we could guide him with a crystal ball
Heuristic function showing us the optimal call
Manhattan distance, Euclidean too
Admissible estimates that always stay true
[Chorus]
A-star, A-star, f equals g plus h
Best first search with guidance, never second guess
G cost from the start, H cost to the goal
F score guides the way, that's how we take control
Open list, closed list, pick the smallest f
A-star, A-star, better than the rest
[Verse 2]
Initialize the open set with starting node
G cost zero, H from heuristic code
While the open set ain't empty, we continue on
Pick the node with smallest f, then it's gone
Move it to the closed set, mark it as explored
Check each neighbor carefully, see what's in store
If it's in the closed set, skip it, move along
If the path is shorter, update, make it strong
[Chorus]
A-star, A-star, f equals g plus h
Best first search with guidance, never second guess
G cost from the start, H cost to the goal
F score guides the way, that's how we take control
Open list, closed list, pick the smallest f
A-star, A-star, better than the rest
[Bridge]
Admissible heuristic, never overestimate
Consistent is better, monotonic all the way
Parent pointers tracking, reconstruct the path
When you reach the target, do the optimal math
[Verse 3]
Time complexity depends upon the heuristic choice
Perfect information gives algorithms a voice
Space complexity grows with the frontier size
But the paths we discover are the optimal prize
Greedy best first searches fast but might be wrong
Dijkstra finds optimal but takes way too long
A-star combines the best of both these worlds
Optimal and efficient, watch the magic unfurl
[Chorus]
A-star, A-star, f equals g plus h
Best first search with guidance, never second guess
G cost from the start, H cost to the goal
F score guides the way, that's how we take control
Open list, closed list, pick the smallest f
A-star, A-star, better than the rest
[Outro]
From pathfinding games to GPS navigation
A-star algorithm serves every application
Remember the formula, keep the heuristic tight
A-star finds the way through the algorithmic night
19. Topological sort
[Verse 1]
Got a graph with arrows pointing every way
Dependencies tangled like a web today
Need an order that respects the flow
Start with nodes that have no arrows coming home
Kahn's algorithm is the way to go
Queue up vertices with indegree zero
Remove them one by one and update the count
Linear ordering is what it's all about
[Chorus]
Topological sort, gotta respect the order
No cycles allowed, that's the golden border
Directed acyclic graph is what we need
Follow dependencies, let the sorting lead
In-degree zero, that's where we start
Remove and repeat, it's algorithmic art
[Verse 2]
Depth first search gives another route
Visit nodes deep, mark them on the way out
Finish time tells us the reverse rank
Stack them up as each traversal ends blank
If you hit a back edge, cycle's found
Topological order can't be crowned
Prerequisites must come before their class
Like compiling code, respect what has to pass
[Chorus]
Topological sort, gotta respect the order
No cycles allowed, that's the golden border
Directed acyclic graph is what we need
Follow dependencies, let the sorting lead
In-degree zero, that's where we start
Remove and repeat, it's algorithmic art
[Bridge]
Course scheduling, task management flow
Build systems need to know which way to go
Deadlock detection, dependency chains
Linear time complexity, algorithmic gains
Two methods same result, choose your style
Kahn or DFS, both worthwhile
[Verse 3]
Implementation time, let's break it down
Adjacency list keeps the data sound
Count incoming edges for each node
Queue or stack depending on your code
Time complexity is big O of V plus E
Vertices and edges, that's the key
Space is linear, efficient and clean
Best sorting algorithm you've ever seen
[Outro]
When dependencies rule your data game
Topological sort will stake its claim
No cycles, just order, respect the flow
That's how the algorithm's supposed to go
20. Tarjan's algorithm (strongly connected components)
[Verse 1]
Walking through the graph with purpose and a plan
Every node gets numbered by my steady hand
Discovery time first, then low-link comes to play
Stack is growing tall as I traverse this way
DFS is running deep into the core
Marking every vertex that I haven't seen before
Building up a forest from the roots I find
Tarjan's got the method to reveal what's intertwined
[Chorus]
Stack it up, number down, low-link all around
Strongly connected pieces waiting to be found
When discovery equals low-link at the top
Pop until you're back and let the cycle stop
Tarjan's algorithm, one pass is all you need
Linear time complexity, maximum speed
Components in the graph, circles in the flow
Stack and recurse, that's how the strong ones show
[Verse 2]
Low-link holds the minimum that I can reach
From this vertex going down, that's what I teach
If there's a back edge to a node that's on the stack
Update low-link value, keep the connection track
But if it's a cross edge to a finished part
Don't update the low-link, keep them apart
The magic happens when we're backing out
Discovery equals low-link, time to shout
[Chorus]
Stack it up, number down, low-link all around
Strongly connected pieces waiting to be found
When discovery equals low-link at the top
Pop until you're back and let the cycle stop
Tarjan's algorithm, one pass is all you need
Linear time complexity, maximum speed
Components in the graph, circles in the flow
Stack and recurse, that's how the strong ones show
[Bridge]
Root of a component has the special sign
Discovery time and low-link align
Pop the stack until you reach that root
Every node between them forms the group
Mutual reachability is the key
If you can get there, you can get back free
That's the definition of the strongest bond
Tarjan found the way to look beyond
[Verse 3]
Kosaraju needs two passes, we just need one
DFS with bookkeeping until we're done
Applications everywhere from web page rank
To finding deadlocks where the processes sank
Social network clusters, circuit analysis too
Strongly connected components guide us through
Time complexity linear, space is just the same
Tarjan wrote his name in algorithmic fame
[Chorus]
Stack it up, number down, low-link all around
Strongly connected pieces waiting to be found
When discovery equals low-link at the top
Pop until you're back and let the cycle stop
Tarjan's algorithm, one pass is all you need
Linear time complexity, maximum speed
Components in the graph, circles in the flow
Stack and recurse, that's how the strong ones show
[Outro]
When the graph is calling and you need to know
Which nodes stick together in the data flow
Remember Tarjan's wisdom, let the stack grow high
One DFS traversal and the truth won't lie
21. Kosaraju's algorithm
[Verse 1]
Graph connections running deep and wide
Strongly connected components we must find
Kosaraju knew the secret to the game
Two DFS passes bring components to their name
Start with any vertex take your pick
Depth first search until the stack gets thick
Fill it up with finish times in order
Then transpose the graph and cross the border
[Chorus]
First pass forward stack them high
Second pass backward components fly
Transpose the edges flip the flow
Kosaraju's method this is how we go
DFS twice and you will see
Connected groups in harmony
Stack and flip the magic trick
Kosaraju's algorithm does it quick
[Verse 2]
Forward pass explores each unvisited node
Push to stack when backtracking down the road
Finishing times determine the sequence
Later finishers get stack precedence
Now we flip every single edge around
Transpose graph with connections turned around
What pointed left now points to right
Setting up for our second flight
[Chorus]
First pass forward stack them high
Second pass backward components fly
Transpose the edges flip the flow
Kosaraju's method this is how we go
DFS twice and you will see
Connected groups in harmony
Stack and flip the magic trick
Kosaraju's algorithm does it quick
[Verse 3]
Pop the stack in reverse finish order
Start DFS from each node like a reporter
In transposed graph each tree we find
Represents one component combined
All vertices reached in single traversal
Form a group that's universal
Mutually reachable every way
That's strongly connected we can say
[Bridge]
Linear time complexity we achieve
Two passes through is all we need
O of V plus E running clean
Most efficient algorithm you've seen
When cycles exist in directed graphs
Kosaraju finds the connected paths
[Chorus]
First pass forward stack them high
Second pass backward components fly
Transpose the edges flip the flow
Kosaraju's method this is how we go
DFS twice and you will see
Connected groups in harmony
Stack and flip the magic trick
Kosaraju's algorithm does it quick
[Outro]
Two DFS one transpose stack
Strongly connected that's a fact
Kosaraju showed us the way
Components found in linear day
22. Prim's algorithm
[Verse 1]
Got a graph with vertices scattered around
Need to connect them with minimum cost found
Prim's algorithm gonna show us the way
Start with one vertex, let's begin today
Keep a priority queue of edges so neat
Pick the smallest weight, make connections complete
Growing our tree one edge at a time
Finding that spanning tree, rhythm and rhyme
[Chorus]
Start small, grow tall, minimum spanning tree
Pick the lightest edge that connects you and me
Cut property guarantees we're on the right track
Prim's algorithm, never looking back
Greedy choice, optimal voice, MST is the key
O of V squared with arrays, or V log V with heap, you see
[Verse 2]
Initialize with arbitrary vertex as root
Mark it visited, that's our starting suit
For every neighbor, add edge to the queue
Weighted by distance, keeping costs true
Extract minimum from priority storage
Cross the cut boundary, that's our voyage
Add new vertex to our growing set
Another edge chosen, minimum debt
[Chorus]
Start small, grow tall, minimum spanning tree
Pick the lightest edge that connects you and me
Cut property guarantees we're on the right track
Prim's algorithm, never looking back
Greedy choice, optimal voice, MST is the key
O of V squared with arrays, or V log V with heap, you see
[Bridge]
Cut respect means we're crossing the divide
Between visited and unvisited side
Light edge theorem proves our greedy way
Safe choice every step, never led astray
Update distances as we explore
Each vertex connected, can't ask for more
[Verse 3]
Dense graphs benefit from the matrix approach
Sparse graphs prefer heaps, that's no reproach
Fibonacci heaps can decrease the key
Making updates fast as fast can be
When all vertices join our spanning tree
Total weight minimized, algorithm free
Connected components now unified
Prim's guarantee, mathematically verified
[Outro]
From one to all, we built it right
Minimum spanning tree shining bright
Prim's algorithm, the optimal way
Connecting graphs efficiently every day
23. Kruskal's algorithm
[Verse 1]
Start with a graph, edges all scattered around
Weighted connections, some heavy, some light to be found
Kruskal's the name of the game we're about to play
Find the minimum spanning tree, that's the only way
Sort all the edges from smallest to largest weight
Union-Find structure keeps track of our connected state
Check every edge, make sure no cycles form
Connect the components, that's the algorithm's norm
[Chorus]
Sort the edges, check for cycles, union if it's clear
Minimum spanning tree, Kruskal makes it appear
Disjoint sets and weighted paths, greedy choice each time
Connect them all with minimum cost, algorithm so fine
Sort, check, union, repeat until the tree's complete
Kruskal's algorithm, makes the solution sweet
[Verse 2]
Initialize each vertex as its own separate set
Union-Find will tell us if components have met
Take the lightest edge that hasn't been processed yet
If endpoints are in different sets, then it's a safe bet
Add it to our spanning tree, union those two sets
Keep the forest growing, but no cycles we'll beget
Edges equal to vertices minus one we need
Kruskal's greedy strategy will surely succeed
[Chorus]
Sort the edges, check for cycles, union if it's clear
Minimum spanning tree, Kruskal makes it appear
Disjoint sets and weighted paths, greedy choice each time
Connect them all with minimum cost, algorithm so fine
Sort, check, union, repeat until the tree's complete
Kruskal's algorithm, makes the solution sweet
[Bridge]
Time complexity O of E log E for the sort
Union-Find operations keep the runtime short
Path compression and union by rank optimize
Find and union operations in nearly constant time
Greedy algorithm that always makes the right choice
Minimum spanning tree gives networks a strong voice
[Verse 3]
Applications everywhere from networks to design
Connecting cities with cables, keeping costs in line
Circuit boards and water pipes, roads between the towns
Kruskal finds the cheapest way to link without breaking down
Start with sorted edges, use Union-Find to track
Which components are connected, there's no looking back
When all vertices connected in one spanning tree
Kruskal's work is finished, minimum cost guarantee
[Chorus]
Sort the edges, check for cycles, union if it's clear
Minimum spanning tree, Kruskal makes it appear
Disjoint sets and weighted paths, greedy choice each time
Connect them all with minimum cost, algorithm so fine
Sort, check, union, repeat until the tree's complete
Kruskal's algorithm, makes the solution sweet
[Outro]
Remember Kruskal's method when you need to span
Sort then union-find, that's the master plan
Minimum cost connections, no cycles in sight
Spanning tree algorithm, get it right every time
24. Knuth-Morris-Pratt (KMP)
[Verse 1]
When you're searching through a string for a pattern that you seek
Brute force checking every spot makes your algorithm weak
Moving one by one is slow when mismatches appear
But there's a smarter way to go, let me make it crystal clear
[Chorus]
KMP, don't restart from scratch
Build a table, find the match
Longest prefix suffix same
That's the KMP game
Skip ahead when patterns fail
Let the preprocessing tell the tale
Linear time is what we gain
KMP breaks the searching chain
[Verse 2]
First we build the failure function, scan the pattern left to right
When characters repeat themselves, we store that insight
If we match then hit a wall, don't go back to square one
Use the table, make the call, jump ahead until we're done
[Chorus]
KMP, don't restart from scratch
Build a table, find the match
Longest prefix suffix same
That's the KMP game
Skip ahead when patterns fail
Let the preprocessing tell the tale
Linear time is what we gain
KMP breaks the searching chain
[Verse 3]
Pattern A-B-A-B gives us values zero one zero three
Each position tells us how far back in history
When we fail at position four, we don't start from the beginning
Jump to where the pattern's core shows us we're still winning
[Bridge]
Preprocessing takes some time but searching flies so fast
O of n plus m combined, efficiency that lasts
While naive search quadratic might leave your program hanging
KMP stays logarithmic, keeps your algorithm banging
[Chorus]
KMP, don't restart from scratch
Build a table, find the match
Longest prefix suffix same
That's the KMP game
Skip ahead when patterns fail
Let the preprocessing tell the tale
Linear time is what we gain
KMP breaks the searching chain
[Outro]
From Knuth and Morris-Pratt, this wisdom they bestowed
Smart string matching is the path, now you know the code
When patterns hide in lengthy text, don't search like a fool
KMP algorithm's next level searching tool
25. Rabin-Karp
[Verse 1]
String matching problem got you feeling stuck
Brute force searching gonna test your luck
But there's a better way to find your text
Rolling hash technique keeps algorithms flexed
Rabin and Karp they showed us how to roll
Polynomial hashing is the key to our goal
Take each character multiply by base
Modulo keeps the numbers in their place
[Chorus]
Roll the hash roll it right
Sliding window through the night
When the hashes match we found
Something special in the ground
But don't forget the collision test
Compare the strings to be your best
Rolling hash saves us time
Rabin-Karp the paradigm
[Verse 2]
Start with pattern calculate its hash
Then slide through text like lightning flash
Remove the leftmost add the right
Rolling calculation burning bright
Base to the power of pattern length
That's the factor for your hash strength
Subtract old character times the power
Add new character to the tower
[Chorus]
Roll the hash roll it right
Sliding window through the night
When the hashes match we found
Something special in the ground
But don't forget the collision test
Compare the strings to be your best
Rolling hash saves us time
Rabin-Karp the paradigm
[Bridge]
Worst case still takes quadratic time
When every hash collision aligns
But average case is linear flow
That's the beauty that we need to know
Choose your base and modulus wise
Prime numbers help minimize
The chances that false matches appear
Keep your algorithm running clear
[Verse 3]
Multiple patterns no problem here
Rabin-Karp can handle them with cheer
Calculate each pattern hash up front
Then search for all in single hunt
Text preprocessing takes just one pass
Rolling through with polynomial class
Linear expected time complexity
That's string matching efficiency
[Chorus]
Roll the hash roll it right
Sliding window through the night
When the hashes match we found
Something special in the ground
But don't forget the collision test
Compare the strings to be your best
Rolling hash saves us time
Rabin-Karp the paradigm
[Outro]
From text editors to DNA strands
Rabin-Karp algorithm commands
Rolling hash through infinite space
Finding patterns in every place
When you need to search and find
Keep this algorithm in mind
Roll the hash and test with care
Rabin-Karp will get you there
26. Boyer-Moore
[Verse 1]
When you're searching through a string for a pattern that you need
Linear scanning takes forever, that's a slow and costly deed
But Boyer-Moore came along with a smarter way to play
Skip ahead when mismatches happen, don't check every single day
Two tables guide the movement, bad character and good suffix
When the pattern doesn't match up, these rules help you get your fix
[Chorus]
Boyer-Moore, skip ahead, don't look back
Bad character, good suffix, stay on track
Right to left we check the pattern
But left to right we slide and happen
Boyer-Moore, Boyer-Moore, skip the noise
Efficient searching is our choice
[Verse 2]
Bad character table tells us how far we can jump ahead
When we hit a mismatch letter, use the table like we said
If the bad char's in our pattern, align it with the text
If it's not there skip the whole thing, move the pattern to what's next
Preprocessing is essential, build the tables before you start
This investment pays dividends when you're searching through the part
[Chorus]
Boyer-Moore, skip ahead, don't look back
Bad character, good suffix, stay on track
Right to left we check the pattern
But left to right we slide and happen
Boyer-Moore, Boyer-Moore, skip the noise
Efficient searching is our choice
[Bridge]
Good suffix rule is trickier but powerful when applied
When a suffix matches perfectly but prefix is denied
Find where that suffix appears again within the pattern's frame
Or find the longest border that can keep us in the game
Maximum of both rules determines how far we can slide
Boyer-Moore beats brute force when we let these tables guide
[Verse 3]
Best case gives us linear time divided by pattern length
When every character mismatches, that's where we show our strength
Worst case still hits quadratic but that's rare in real world use
Preprocessing takes some time but the payoff's no excuse
For text editors and databases, this algorithm's the king
When you need to find patterns fast, Boyer-Moore's the thing
[Chorus]
Boyer-Moore, skip ahead, don't look back
Bad character, good suffix, stay on track
Right to left we check the pattern
But left to right we slide and happen
Boyer-Moore, Boyer-Moore, skip the noise
Efficient searching is our choice
[Outro]
Two tables, right to left check, skip ahead when things go wrong
Boyer-Moore algorithm makes your pattern searching strong
27. Aho-Corasick
[Verse 1]
Multiple patterns need to find their place
In a single text we're searching through the space
Naive approach would scan from left to right
But that's inefficient, not so bright
Aho-Corasick comes to save the day
Linear time complexity, that's the way
Build a trie structure, add the patterns in
Then construct failure links where we begin
[Chorus]
Trie plus failure links, that's the key
A-C algorithm, search efficiently
When mismatch happens, don't restart from scratch
Follow failure pointers, make the perfect match
Linear time scanning, all patterns at once
Aho-Corasick never lets you down
[Verse 2]
Start with building trie from all your strings
Each character becomes a node with rings
Root connects to first chars of each word
Shared prefixes merged, that's what occurred
Then we calculate the failure function right
Longest proper suffix matching prefix tight
BFS traversal sets each failure link
To the deepest node where patterns sync
[Chorus]
Trie plus failure links, that's the key
A-C algorithm, search efficiently
When mismatch happens, don't restart from scratch
Follow failure pointers, make the perfect match
Linear time scanning, all patterns at once
Aho-Corasick never lets you down
[Bridge]
Output links point to pattern ends we've seen
Dictionary matching keeps the search clean
From any state we know exactly where
All matching patterns hiding everywhere
Preprocessing time is sum of pattern lengths
Searching time is just the text's defense
[Verse 3]
Now we scan the text from left to right
Current state guides us through the night
Character matches, move along the trie
No match found, failure link's our guide
When we reach a final state node
Pattern found, add it to our code
Output links reveal more patterns too
All occurrences come into view
[Chorus]
Trie plus failure links, that's the key
A-C algorithm, search efficiently
When mismatch happens, don't restart from scratch
Follow failure pointers, make the perfect match
Linear time scanning, all patterns at once
Aho-Corasick never lets you down
[Outro]
Finite automaton with failure transitions
Simultaneous search, no repetitions
From virus scanning to text processing tools
Aho-Corasick follows all the rules
Linear complexity, optimal and clean
Best multi-pattern search you've ever seen
28. Levenshtein distance (edit distance)
[Verse 1]
Two strings sitting side by side, need to know the difference
Count the moves to make them match, that's our main reference
Insert a letter, delete one, or substitute in place
Three operations rule the game in this algorithmic space
Start with empty, build a table, dynamic programming flow
Bottom up approach we take, watch the numbers grow
[Chorus]
Edit distance, count the changes
Three moves only, rearranges
Insert, delete, substitute
Minimum path, that's the route
Levenshtein will show the way
Count the edits, save the day
[Verse 2]
Matrix building, row by row, initialize with care
First row counting, first column too, baseline values there
If characters match exactly, take diagonal for free
Otherwise add one to the min of three choices that we see
Left cell plus one for insertion, top cell for deletion
Diagonal plus one substitution, pick the best solution
[Chorus]
Edit distance, count the changes
Three moves only, rearranges
Insert, delete, substitute
Minimum path, that's the route
Levenshtein will show the way
Count the edits, save the day
[Bridge]
Applications everywhere, spell check and DNA
Fuzzy matching, auto-correct, helping every day
Version control and plagiarism, text comparison too
Natural language processing, machine translation crew
[Verse 3]
Time complexity quadratic, space we can optimize
Keep just two rows if you want, memory to minimize
Traceback shows the actual path, not just final score
Reconstruct the edit sequence, see what changes were
From kitten turning into sitting, three edits is the cost
Substitute k with s, insert g, no efficiency is lost
[Chorus]
Edit distance, count the changes
Three moves only, rearranges
Insert, delete, substitute
Minimum path, that's the route
Levenshtein will show the way
Count the edits, save the day
[Outro]
When strings need transformation
Use this calculation
Levenshtein distance
Measures the resistance
29. Fibonacci (memoized)
[Verse 1]
Started with recursion but the call stack's getting deep
Same calculations running while my program's losing sleep
Zero gives me zero, one returns just one
But higher numbers spiral till my memory is done
Fibonacci's beauty turned into a curse
Exponential time complexity just making things worse
Tree of recursive calls spreading way too wide
Need a better strategy, gotta optimize my ride
[Chorus]
Memoize, memorize, save what you compute
Store the past results so you don't have to recompute
Cache it, stash it, in a table keep it clean
Linear time complexity, the fastest you've ever seen
Memo-ize, memo-ize, remember what you've done
Bottom up or top down, either way you've won
[Verse 2]
Dictionary in my hand, mapping keys to values tight
N maps to fibonacci N, stored throughout the night
Check the cache before you calculate, that's the golden rule
If it's there just return it, like a computational tool
Base cases still important, zero one we handle first
Then for every other number, quench that recursive thirst
But this time when we call ourselves, we're building up our store
Each result gets cached away, so we don't compute no more
[Chorus]
Memoize, memorize, save what you compute
Store the past results so you don't have to recompute
Cache it, stash it, in a table keep it clean
Linear time complexity, the fastest you've ever seen
Memo-ize, memo-ize, remember what you've done
Bottom up or top down, either way you've won
[Bridge]
Two to the power N, that's where we used to be
Now it's just O of N, running efficiently
Dynamic programming in its purest form
Taking exponential chaos, making performance warm
Space and time we're trading, memory for speed
Overlapping subproblems, memoization's what we need
[Verse 3]
Top down with recursion plus the memo cache we made
Or bottom up iteration, either way the game is played
From the smallest problems building up to what we want
No repeated calculations, efficiency we flaunt
Forty-five fibonacci used to take forever long
Now it's instant with our cache, singing optimization's song
[Chorus]
Memoize, memorize, save what you compute
Store the past results so you don't have to recompute
Cache it, stash it, in a table keep it clean
Linear time complexity, the fastest you've ever seen
Memo-ize, memo-ize, remember what you've done
Bottom up or top down, either way you've won
[Outro]
Remember your computations
Avoid those duplications
Memoized fibonacci
Algorithm's lullaby
30. Longest common subsequence
[Verse 1]
Two strings sitting side by side
Need to find what they both provide
Not contiguous, that's the key
Subsequence means we can skip freely
Dynamic programming's our way
Build a table, step by step each day
Row by row and column clean
Finding patterns in between
[Chorus]
LCS, longest common subsequence
Break it down with table reference
Bottom up or top down flow
Match or skip, that's how we go
LCS, dynamic solution
Character by character resolution
When they match we add one more
When they don't we take the score
[Verse 2]
Start with empty string base case
Zero length in bottom space
If the characters are the same
Diagonal plus one's the game
If they differ, here's the rule
Take the maximum, that's our tool
Left or up, whichever's high
Copy that value, don't be shy
[Chorus]
LCS, longest common subsequence
Break it down with table reference
Bottom up or top down flow
Match or skip, that's how we go
LCS, dynamic solution
Character by character resolution
When they match we add one more
When they don't we take the score
[Bridge]
Traceback time to build the string
Follow arrows, that's the thing
Diagonal means we found a match
Left or up, no character catch
Applications everywhere
Edit distance, diff compare
Bioinformatics DNA
Version control, merge array
[Verse 3]
Time complexity quadratic
Space the same, but that's not tragic
Optimization possible though
Keep just two rows, let memory go
Memoization top down style
Recursion with a storage file
Both approaches get us there
Subsequences we can compare
[Chorus]
LCS, longest common subsequence
Break it down with table reference
Bottom up or top down flow
Match or skip, that's how we go
LCS, dynamic solution
Character by character resolution
When they match we add one more
When they don't we take the score
[Outro]
From empty strings to full compare
LCS is everywhere
Dynamic programming at its best
Longest common subsequence test
31. Longest increasing subsequence
[Verse 1]
Got an array and I'm looking for the pattern
Numbers climbing up, that's what really matters
Not consecutive, just strictly increasing
Find the longest chain, that's what I'm seeking
Dynamic programming gonna solve this right
Building up solutions from left to right
Each position holds the best we've seen
Longest sequence ending at that scene
[Chorus]
LIS, LIS, longest increasing subsequence
Dynamic table, optimal reference
Bottom up, we build the solution clean
N squared time, but the logic's pristine
LIS, LIS, find the maximum length
Patience sorting gives us extra strength
Remember the path, trace it back with care
Longest climb is waiting for us there
[Verse 2]
Start with base case, every single stands alone
Length of one at every position shown
For each element, scan what came before
If it's smaller, we can add one more
Take the maximum from all valid picks
That's the recurrence relation that clicks
Fill the table left to right with grace
Each cell holds the best case for that place
[Chorus]
LIS, LIS, longest increasing subsequence
Dynamic table, optimal reference
Bottom up, we build the solution clean
N squared time, but the logic's pristine
LIS, LIS, find the maximum length
Patience sorting gives us extra strength
Remember the path, trace it back with care
Longest climb is waiting for us there
[Bridge]
Binary search can make it faster still
N log N time if you've got the skill
Active list of tails, replace with care
Patience game strategy, winners everywhere
But sometimes you need the actual sequence
Not just length, but full coherence
Parent pointers help you reconstruct
The path that made your algorithm struck
[Verse 3]
Applications everywhere you look around
Scheduling tasks where time constraints abound
Version control and file comparisons too
DNA sequencing, bioinformatics crew
Box stacking problems, nested rectangles
Investment planning, profit trajectles
Any time you need the optimal chain
LIS algorithm breaks the coding strain
[Chorus]
LIS, LIS, longest increasing subsequence
Dynamic table, optimal reference
Bottom up, we build the solution clean
N squared time, but the logic's pristine
LIS, LIS, find the maximum length
Patience sorting gives us extra strength
Remember the path, trace it back with care
Longest climb is waiting for us there
[Outro]
From the simple to the optimized
Every subsequence gets analyzed
Build your table, trace your path
LIS mastery, that's the aftermath
32. Knapsack (0/1 and unbounded)
[Verse 1]
Got a knapsack problem on my mind today
Items with their weights and values in the way
Zero-one means each item just once or never
Dynamic programming makes the solver clever
Build a table row by row, decision by decision
Include or exclude with mathematical precision
Weight capacity sets the boundary line
Maximum value is what we're trying to find
[Chorus]
Knapsack zero-one, take it or leave it
Dynamic table helps you to achieve it
Row by row, column by column we go
Max of include or exclude, let the values flow
Unbounded means unlimited supply
Take as many as the weight allows you to try
Same item over and over again
Till the knapsack reaches capacity's end
[Verse 2]
Zero-one knapsack, each item's unique
Previous row tells you what results to seek
If weight's too heavy, take the value above
Otherwise compare, choose the one you love
Take the item plus remaining space value
Or skip the item, previous row's rescue
Whichever gives you more, that's your choice
Let the maximum value be your guide's voice
[Chorus]
Knapsack zero-one, take it or leave it
Dynamic table helps you to achieve it
Row by row, column by column we go
Max of include or exclude, let the values flow
Unbounded means unlimited supply
Take as many as the weight allows you to try
Same item over and over again
Till the knapsack reaches capacity's end
[Bridge]
Time complexity's order n times w
Space can be optimized to just one-d view
Unbounded's simpler, just one dimension needed
Current row references, efficiency succeeded
Coin change problems use this pattern too
Unlimited denominations working through
[Verse 3]
Unbounded knapsack breaks the single rule
Multiple copies make it a different tool
For each position check every item's worth
Add its value to remaining space's girth
No previous row needed in this game
Current row updates, result's the same
Each cell depends on cells to the left
Maximum value, that's the final gift
[Outro]
From zero-one constraint to unlimited flow
Dynamic programming solutions help us grow
Knapsack problems everywhere you see
Optimization's the master key
33. Matrix chain multiplication
[Verse 1]
Got matrices lined up in a chain reaction
Need to multiply but there's optimal action
Different ways to group them, parentheses matter
Cost can explode or be flat like a platter
A times B times C, how you gonna compute it?
Order of operations, which way you execute it
Rows and columns dancing, dimensions align
But the sequence you choose affects processing time
[Chorus]
Find the minimum, split and conquer the scene
Dynamic programming keeps the cost lean
M of i j equals the best you can get
Optimal substructure, place your bet
Break it down, build it up, memoize the way
Matrix chain multiplication saves the day
[Verse 2]
Bottom up approach, fill that table clean
Start with single matrices, build up the machine
Length two chains first, then three and four
Each cell holds the minimum, can't ask for more
Check every split point, left side plus right side
Plus the multiplication cost, let the math be your guide
Dimensions array holds the key to success
Rows of first, columns of last, avoid the mess
[Chorus]
Find the minimum, split and conquer the scene
Dynamic programming keeps the cost lean
M of i j equals the best you can get
Optimal substructure, place your bet
Break it down, build it up, memoize the way
Matrix chain multiplication saves the day
[Bridge]
Time complexity cubic, space is quadratic
But exponential brute force would be problematic
Overlapping subproblems, that's the golden sign
Dynamic programming makes the solution shine
Traceback through the table when you want the grouping
Parentheses placement, keep the algorithm looping
[Verse 3]
From position i to j, what's the minimum cost?
Without this optimization, efficiency is lost
Fill diagonal by diagonal, length by length
Bottom up construction shows the algorithm's strength
Each entry depends on smaller subproblems solved
Optimal principle keeps the method evolved
Matrix dimensions guide the computation weight
Choose the right split point, seal your optimal fate
[Chorus]
Find the minimum, split and conquer the scene
Dynamic programming keeps the cost lean
M of i j equals the best you can get
Optimal substructure, place your bet
Break it down, build it up, memoize the way
Matrix chain multiplication saves the day
[Outro]
When matrices multiply in a chain so long
Remember this algorithm, remember this song
Optimal parenthesization, that's the key
Dynamic programming sets your matrices free
34. Coin change
[Verse 1]
Got a problem with some change to make today
Minimize the coins but find the optimal way
Dynamic programming gonna save the day
Bottom up or top down, either way we slay
Start with base case, zero change is free
Build up solutions systematically
For each amount, check every coin we got
Take the minimum, that's the winning spot
[Chorus]
Break it down, build it up
Coin by coin, fill the cup
Memo table, save the state
Optimal sub, don't be late
Greedy fails but DP wins
Bottom up is where it begins
Coin change, rearrange
Dynamic programming in the game
[Verse 2]
Create a table, size amount plus one
Fill with infinity, except index zero's done
For every coin denomination in our set
Check if current amount can be met
If coin value's less than or equal to i
Take the min of what we got and what we try
One plus table at position i minus coin
That's the recurrence, let the solutions join
[Chorus]
Break it down, build it up
Coin by coin, fill the cup
Memo table, save the state
Optimal sub, don't be late
Greedy fails but DP wins
Bottom up is where it begins
Coin change, rearrange
Dynamic programming in the game
[Bridge]
Time complexity's amount times coins
Space complexity's linear, no random joins
If the final answer's still infinity
No solution exists, that's the reality
Trace back the path if you need to know
Which coins to use, let the algorithm show
[Verse 3]
Top down approach with memoization
Recursive calls with optimization
If we've seen this amount before today
Return the cached result right away
Base case zero, recursive relation
One plus minimum of each combination
Both approaches give the same result
Choose your style, that's the default
[Chorus]
Break it down, build it up
Coin by coin, fill the cup
Memo table, save the state
Optimal sub, don't be late
Greedy fails but DP wins
Bottom up is where it begins
Coin change, rearrange
Dynamic programming in the game
[Outro]
From pennies to quarters, dollars to cents
Dynamic solutions, no time to repent
Master coin change, you've learned the art
Optimal substructure, that's the smart part
35. Edit distance
[Verse 1]
Two strings sitting side by side, how different can they be?
Count the moves to make them match, that's our mystery
Insert a char, delete one too, or substitute instead
Minimum operations needed, that's the path ahead
Dynamic programming helps us find the optimal way
Building up a table, row by row, we're here to stay
[Chorus]
Edit distance, Levenshtein's the name
Three operations in this transformation game
Insert, delete, substitute - remember these three
D-P table holds the key, minimum cost guarantees
Edit distance, measure similarity
From string A to string B, what's the complexity?
[Verse 2]
Start with empty string as base, build the matrix grid
First row counts insertions, first column what we did
For each cell we calculate three possible routes
Take the minimum plus one, that's how the algorithm computes
If characters match exactly, diagonal stays the same
No cost added to that path, we're winning at this game
[Chorus]
Edit distance, Levenshtein's the name
Three operations in this transformation game
Insert, delete, substitute - remember these three
D-P table holds the key, minimum cost guarantees
Edit distance, measure similarity
From string A to string B, what's the complexity?
[Bridge]
Applications everywhere, spell check and DNA
Fuzzy matching algorithms use this every day
Text comparison engines, finding closest match
Edit distance calculation helps them make the catch
Time complexity quadratic, space can be optimized
Linear space solution, that's how we get surprised
[Verse 3]
Backtrack through the table when you need the actual path
Show the operations sequence, do the edit math
Wagner-Fischer algorithm, classic D-P approach
From computational linguistics, this method we can coach
String alignment problems, this is what we need
Edit distance computation plants the coding seed
[Chorus]
Edit distance, Levenshtein's the name
Three operations in this transformation game
Insert, delete, substitute - remember these three
D-P table holds the key, minimum cost guarantees
Edit distance, measure similarity
From string A to string B, what's the complexity?
[Outro]
Minimum edit distance, measure how strings relate
Dynamic programming power, algorithms so great
From kitten to sitting, three moves is all you need
Edit distance mastery, plant that coding seed
36. Binary search tree operations
[Verse 1]
Start with a root, that's where we begin
Every node's got a left and right within
Left side smaller, right side's getting greater
Binary search tree, data navigator
Insert a value, compare as you go
Less than current, left is where you flow
Greater than current, right side's the way
Find an empty spot, that's where it'll stay
[Chorus]
Left Less Right More, that's the core
Search Insert Delete, operations we explore
Left Less Right More, keep it in your head
Balanced or not, that's how the tree is fed
In-order traversal gives you sorted gold
Left Root Right, story that's been told
[Verse 2]
Searching for a value, start from the top
Compare with current, decide when to stop
Target's smaller, go left down the line
Target's bigger, right side is fine
Found the match, return success today
Reached a null, value's gone away
Time complexity, log n when it's balanced
Linear worst case, when the tree's not managed
[Chorus]
Left Less Right More, that's the core
Search Insert Delete, operations we explore
Left Less Right More, keep it in your head
Balanced or not, that's how the tree is fed
In-order traversal gives you sorted gold
Left Root Right, story that's been told
[Bridge]
Deletion's tricky, three cases to know
No children present, just remove and go
One child waiting, promote it up high
Two children there, successor's the guy
Find the minimum in the right subtree
Replace the value, then delete carefully
Or predecessor from the left side chain
Either method keeps the structure sane
[Verse 3]
Traversal patterns, three ways to explore
Pre-order visits root before
In-order gives you sorted sequence clean
Post-order processes children first scene
Height of tree affects every operation
Balanced structure needs consideration
Self-balancing types like AVL and red-black
Keep performance tight, never look back
[Chorus]
Left Less Right More, that's the core
Search Insert Delete, operations we explore
Left Less Right More, keep it in your head
Balanced or not, that's how the tree is fed
In-order traversal gives you sorted gold
Left Root Right, story that's been told
[Outro]
Binary search tree, foundation so strong
Master these operations, you can't go wrong
Left Less Right More, remember the rule
Data structures power, developer's tool
37. AVL tree rotations
[Verse 1]
When your binary search tree gets unbalanced and slow
Heights differ by more than one, that's when you know
Left-heavy or right-heavy, the structure's gone wrong
Time to rotate and fix it, let's sing this song
Check the balance factor, negative two or plus two
That's your signal to rotate, here's what you do
Single rotation first, then we'll learn the double
Keep your tree balanced, avoid search trouble
[Chorus]
Left-left case, rotate right
Right-right case, rotate left tonight
Left-right case, double spin around
Right-left case, balance can be found
AVL rotations keep the height in check
Log n performance, no more tree wreck
Remember the patterns, sing it loud and clear
Balanced search trees, efficiency's here
[Verse 2]
Single right rotation when the left side's too tall
Take the left child, make it parent of all
Original root becomes the right child now
Left subtree stays, right subtrees somehow
Switch positions cleanly, maintain BST order
Height gets balanced, crossing the border
From unbalanced chaos to structured delight
One simple rotation makes everything right
[Chorus]
Left-left case, rotate right
Right-right case, rotate left tonight
Left-right case, double spin around
Right-left case, balance can be found
AVL rotations keep the height in check
Log n performance, no more tree wreck
Remember the patterns, sing it loud and clear
Balanced search trees, efficiency's here
[Verse 3]
But sometimes single rotation just won't do the trick
Left-right pattern means you need a double fix
First rotate left on the left child node
Then rotate right on root, follow the code
Right-left pattern works the mirror way
Right rotate first, then left saves the day
Two rotations working as a team
Keeping AVL trees living the dream
[Bridge]
Balance factor is the key to know
Left height minus right, watch how it goes
Minus one, zero, plus one, you're fine
Outside that range, it's rotation time
Insert and delete, check every node up
Propagate changes, fill balance cup
AVL invariant, height difference bound
Logarithmic search time, performance sound
[Chorus]
Left-left case, rotate right
Right-right case, rotate left tonight
Left-right case, double spin around
Right-left case, balance can be found
AVL rotations keep the height in check
Log n performance, no more tree wreck
Remember the patterns, sing it loud and clear
Balanced search trees, efficiency's here
[Outro]
Four cases total, learn them by heart
Single and double, playing their part
AVL trees keep your data in line
Rotations working, performance divine
38. Red-black tree balancing
[Verse 1]
Started with a binary tree but it got unbalanced
Height was growing wild, performance got damaged
Left side heavy, right side light
Searches taking way too long into the night
Then I learned about a better way to code
Red and black nodes following a special mode
Self-balancing structure keeps things in line
Every operation running in logarithmic time
[Chorus]
Red node parent must be black, that's the rule
Uncle red means recolor, that's the tool
Uncle black means rotation time
Left or right to keep the tree in prime
Black height equal on every path
Red-black balancing, do the math
No red-red connections allowed
Keep it balanced, make it proud
[Verse 2]
Insertion starts just like BST
Find the spot and place the node carefully
Color it red, that's the starting way
But now the properties might break today
Check the parent, is it red as well?
If so we got violations to dispel
Look up to grandpa, check the uncle too
Their colors determine what we're gonna do
[Chorus]
Red node parent must be black, that's the rule
Uncle red means recolor, that's the tool
Uncle black means rotation time
Left or right to keep the tree in prime
Black height equal on every path
Red-black balancing, do the math
No red-red connections allowed
Keep it balanced, make it proud
[Bridge]
Left-left case, rotate right
Right-right case, rotate left tonight
Left-right case, double rotation flow
Right-left case, let the spinning go
Root stays black, that never changes
Nil nodes black through all the ranges
[Verse 3]
Deletion's trickier than insert for sure
Find the node and check what to endure
If it's red just remove with ease
Black node gone puts balance in freeze
Double black bubble needs fixing fast
Sibling cases make the solution last
Push the blackness up the tree
Till the root absorbs it finally
[Chorus]
Red node parent must be black, that's the rule
Uncle red means recolor, that's the tool
Uncle black means rotation time
Left or right to keep the tree in prime
Black height equal on every path
Red-black balancing, do the math
No red-red connections allowed
Keep it balanced, make it proud
[Outro]
Five properties keep the structure tight
Logarithmic height is our delight
From root to leaf the black count's same
Red-black trees mastered in this game
39. B-tree insertion/deletion
[Verse 1]
B-tree standing tall, balanced and wide
Keys in order, children by their side
Minimum degree determines the flow
Half full nodes, that's how we grow
Split when full, merge when sparse
Every path same length, near and far
Root to leaf, same distance down
B-tree keeps our data sound
[Chorus]
Insert and split, merge and borrow
Keep it balanced for tomorrow
Half full rule, never break
Split the full, for balance sake
Insert and split, merge and borrow
Keys in order, straight and narrow
[Verse 2]
Insertion starts at the leaf node level
Find the spot where new key settles
If the leaf is not yet full
Drop it in, simple pull
But when the node hits maximum capacity
Split in half with median key
Middle rises to parent above
Left and right nodes show their love
[Chorus]
Insert and split, merge and borrow
Keep it balanced for tomorrow
Half full rule, never break
Split the full, for balance sake
Insert and split, merge and borrow
Keys in order, straight and narrow
[Verse 3]
Deletion gets a bit more complex
Find the key, what happens next
If it's in a leaf that's not too small
Just remove it, that's all
But if the leaf goes under minimum
Borrow from sibling, redistribute them
Or merge with neighbor, combine as one
Parent key comes down, the deed is done
[Bridge]
When internal nodes lose their keys
Replace with predecessor with ease
Or successor from the right subtree
Maintain order, keep it free
Cascading splits climb up the tree
Root might split, new root we see
Height increases, balanced still
B-tree bends to our data will
[Chorus]
Insert and split, merge and borrow
Keep it balanced for tomorrow
Half full rule, never break
Split the full, for balance sake
Insert and split, merge and borrow
Keys in order, straight and narrow
[Outro]
Logarithmic time for every operation
Search and insert, perfect relation
B-tree standing, strong and true
Balanced data structure through and through
40. Trie operations
[Verse 1]
Start with a root that holds no key
Empty node that's foundation free
Children pointing down the tree
Twenty-six slots for A to Z
Insert begins with simple start
Character by character we chart
Following paths that letters make
New nodes born for each step we take
[Chorus]
Trie it out, branch by branch we go
Search and insert, watch the structure grow
Prefix power, common paths we share
Find all words that start with letters there
Trie it out, memory we save
Autocomplete, that's how we behave
Root to leaf, the journey's clear
Trie operations, algorithm gear
[Verse 2]
Search begins at root node base
Follow children at each place
If the path leads to dead end
Not found message we will send
Reach a leaf with end word flag
Success result is in the bag
Time complexity we can see
Linear with the length key
[Chorus]
Trie it out, branch by branch we go
Search and insert, watch the structure grow
Prefix power, common paths we share
Find all words that start with letters there
Trie it out, memory we save
Autocomplete, that's how we behave
Root to leaf, the journey's clear
Trie operations, algorithm gear
[Bridge]
Delete gets tricky, step by step
Mark the end flag, memory kept
If node has children, leave it there
Just unmark it, handle with care
No more children, no more use
Cut the branch, memory loose
Backtrack up until you find
Shared prefix, peace of mind
[Verse 3]
Space complexity trade we make
Memory for speed we take
Common prefixes we share
Storage savings everywhere
Applications run so deep
Spell checkers, contacts we keep
IP routing, genome search
Trie structure does the research
[Outro]
From root to leaf we navigate
Character paths we create
Trie operations, fast and true
Prefix matching, me and you
Branch by branch, we build the way
Trie algorithms here to stay
41. Huffman coding
[Verse 1]
Data compression on my mind, gotta make it small
Huffman coding is the way, gonna beat them all
Start with frequency counting, every symbol gets a score
Characters appearing more get the codes that are short for sure
Build a tree from bottom up, merge the smallest two
Assign the zeros to the left, ones go to the right side too
Most frequent at the top, rare ones buried deep
Variable length encoding, that's the secret that we keep
[Chorus]
Huff-man code, short for more, long for less
Fre-quen-cy first, build the tree, compress
Left goes zero, right goes one, path shows the way
Optimal prefix, no confusion, that's how we save the day
Huff-man code, short for more, long for less
Fre-quen-cy first, build the tree, compress
[Verse 2]
Priority queue in action, minimum heap is king
Pop the smallest frequencies, let the merging begin
Create internal nodes, sum the weights as you go
Left child, right child, watch the binary tree grow
No code is prefix of another, that's the golden rule
Unique decodability guaranteed, David Huffman's tool
Greedy algorithm working, optimal every time
Lossless compression power in this rhythmic rhyme
[Chorus]
Huff-man code, short for more, long for less
Fre-quen-cy first, build the tree, compress
Left goes zero, right goes one, path shows the way
Optimal prefix, no confusion, that's how we save the day
Huff-man code, short for more, long for less
Fre-quen-cy first, build the tree, compress
[Bridge]
From root to leaf, trace the path
Binary digits do the math
E gets one bit, Q gets seven
Frequency distribution heaven
Average length minimized
Information theorized
[Verse 3]
JPEG uses it, ZIP files too
Everywhere you look, this algorithm's true
Shannon's entropy sets the bound
Huffman gets close to what's been found
Static tables or adaptive trees
Dynamic coding with such ease
Canonical forms for transmission
Perfect data compression mission
[Chorus]
Huff-man code, short for more, long for less
Fre-quen-cy first, build the tree, compress
Left goes zero, right goes one, path shows the way
Optimal prefix, no confusion, that's how we save the day
Huff-man code, short for more, long for less
Fre-quen-cy first, build the tree, compress
[Outro]
When you need to save some space
Huffman coding wins the race
Variable length, optimal scheme
Data compression living dream
42. Chaining
[Verse 1]
When your data needs connecting in a logical flow
Chain the operations together, watch the magic grow
Start with one collection, transform it step by step
Map and filter, fold and reduce, no shortcuts we accept
Iterator patterns linking up like cars upon a track
Each method feeds the next one, there's no looking back
Lazy evaluation means we don't compute too soon
Until you call collect or count, we're building up the tune
[Chorus]
Chain it up, chain it down
Link your methods all around
One by one they pass it through
Chaining makes your code flow true
Chain it up, chain it down
Functional style, profound
Transform your data clean and neat
Chaining makes your code complete
[Verse 2]
Method chaining syntax keeps your logic crystal clear
Dot notation connects each step from here to there
Instead of intermediate variables cluttering your space
Chain the transformations in one elegant embrace
Fluent interfaces guide you with a natural feel
Each method returns an object, keeping the chain real
Builder patterns use this concept to construct with ease
Chaining operations together like a gentle breeze
[Chorus]
Chain it up, chain it down
Link your methods all around
One by one they pass it through
Chaining makes your code flow true
Chain it up, chain it down
Functional style, profound
Transform your data clean and neat
Chaining makes your code complete
[Bridge]
Optional chaining handles nulls without a crash
And then and or else keep your logic from being rash
Promise chains in async code prevent the callback hell
Sequential operations running smooth and running well
But watch for performance costs when chains get too long
Sometimes breaking up is right when chaining feels wrong
[Verse 3]
Stream processing libraries built on chaining design
Apache Spark and RxJS make data pipelines shine
Immutable transformations preserve your original state
Each link in the chain creates something new and great
Testing becomes simpler when each step is defined
Debugging flows more smoothly when operations align
From functional programming to reactive streams
Chaining turns your algorithms into powerful dreams
[Outro]
Chain your thoughts and chain your code
Let the data freely flow
One connection leads to more
Chaining opens every door
43. Open addressing (linear probing, quadratic probing, double hashing)
[Verse 1]
When your hash table's full and collisions arise
You need a strategy that's clever and wise
Open addressing keeps everything tight
In one single array, no pointers in sight
Linear probing takes the simplest route
When a slot is taken, just step to compute
Move one step forward, then one step more
Until you find space or you've checked the floor
Primary clustering starts to appear
When data groups up, performance you'll fear
But it's easy to code and understand clear
Linear probing gets the job done here
[Chorus]
Open addressing, three ways to go
Linear steps forward, nice and slow
Quadratic jumps with distance that grows
Double hash when the table overflows
Probe and search until you find space
Keep your load factor in the right place
Open addressing, memory's embrace
One array holds the whole database
[Verse 2]
Quadratic probing tries to break the chain
Distance equals i-squared, reducing the pain
One, four, nine, sixteen, spreading it wide
Secondary clustering still can't hide
But it's better than linear for most of your needs
Avoiding those clusters where linear feeds
The formula's simple, implementation's clean
Best middle ground that you've ever seen
[Chorus]
Open addressing, three ways to go
Linear steps forward, nice and slow
Quadratic jumps with distance that grows
Double hash when the table overflows
Probe and search until you find space
Keep your load factor in the right place
Open addressing, memory's embrace
One array holds the whole database
[Verse 3]
Double hashing brings two functions to play
First one maps your key to start the way
Second one gives you the step size right
Uniform distribution, clustering takes flight
Hash one mod table size for position start
Hash two mod table size for stepping part
Make sure that second hash is relatively prime
Or you'll loop forever, wasting your time
[Bridge]
When deletion comes around
Mark the slot as deleted found
Don't leave it empty, that breaks the chain
Tombstone markers keep searches sane
Load factor matters, keep it low
Point seven five is the way to go
Higher loads mean longer probe sequences grow
Performance drops and searches slow
[Chorus]
Open addressing, three ways to go
Linear steps forward, nice and slow
Quadratic jumps with distance that grows
Double hash when the table overflows
Probe and search until you find space
Keep your load factor in the right place
Open addressing, memory's embrace
One array holds the whole database
[Outro]
Choose your method based on your need
Linear's simple if clustering you can feed
Quadratic's balanced for general case
Double hashing for uniform space
Open addressing, memory efficient and clean
Best hash table method you've ever seen
44. Consistent hashing
[Verse 1]
Started with a simple hash table dream
But when servers crash the whole thing screams
Rehashing everything when nodes go down
Million keys relocating all around
Need a better way to scale this right
Keep the data moving smooth and tight
Enter consistent hashing on the scene
Most elegant solution ever seen
[Chorus]
Ring around the hash space, pocket full of keys
Map them to the circle, distribute with ease
Clockwise to the server, first one that you meet
Consistent hashing magic, makes your system complete
Ring around, ring around, hash space never lies
Add or drop a server, just a few keys fly
[Verse 2]
Picture hash space as a giant ring
Zero to two-fifty-six the range we bring
Servers get their spots upon the wheel
Hash their names and make positions real
Keys get hashed and placed around the band
Clockwise search to find where they will land
First server that you hit becomes the home
No more chaos when your cluster's grown
[Chorus]
Ring around the hash space, pocket full of keys
Map them to the circle, distribute with ease
Clockwise to the server, first one that you meet
Consistent hashing magic, makes your system complete
Ring around, ring around, hash space never lies
Add or drop a server, just a few keys fly
[Bridge]
Virtual nodes solve the balance game
One server claims multiple names
Spread them out across the ring so wide
Even distribution side by side
When machines fail only neighbors care
Adjacent keys need homes somewhere
Hot spots cool and load gets shared
Perfect scaling engineered
[Verse 3]
Amazon Dynamo showed us the way
Cassandra and Riak use it today
Chord and BitTorrent peer-to-peer
Consistent hashing makes it clear
No central point to coordinate
Just local knowledge calculates
Which replica holds your precious data
Fault-tolerant illuminata
[Chorus]
Ring around the hash space, pocket full of keys
Map them to the circle, distribute with ease
Clockwise to the server, first one that you meet
Consistent hashing magic, makes your system complete
Ring around, ring around, hash space never lies
Add or drop a server, just a few keys fly
[Outro]
From rehashing chaos to stability
Consistent hashing sets your data free
O of n down to O of log
Beautiful math cuts through the fog
Ring topology keeps the peace
Scalability will never cease
45. Bloom filters
[Verse 1]
Picture this scenario, data flowing through
Million queries hitting, what you gonna do?
Traditional hash tables eating up your space
But I got a solution gonna pick up the pace
Bloom filter magic, probabilistic game
Uses bit arrays to play the membership claim
Hash functions working, maybe three or four
Mapping your data to positions galore
[Chorus]
Bloom it up, filter down, never false negative
Might get false positive but that's definitive
Space efficient, lightning quick, that's the way we roll
Hash it once, hash it twice, let the bits take control
B-L-O-O-M, memory saved today
F-I-L-T-E-R, false positives okay
[Verse 2]
Start with zeros filling every single bit
Insert an element, here's how we commit
Run it through each hash function in the set
Turn those zeros into ones, place your bet
When you query later, check each position
If any bit is zero, definite decision
Element's not there, that's a guarantee
But all ones just means possibility
[Chorus]
Bloom it up, filter down, never false negative
Might get false positive but that's definitive
Space efficient, lightning quick, that's the way we roll
Hash it once, hash it twice, let the bits take control
B-L-O-O-M, memory saved today
F-I-L-T-E-R, false positives okay
[Bridge]
Trade-off wisdom, space for accuracy
Can't delete items, that's the tragedy
More hash functions, fewer false alarms
But computation cost might cause you harm
Size your filter based on expected load
Math equations help you crack the code
Optimal ratio keeps the error low
Engineering balance, now you know
[Verse 3]
Web crawlers use them, avoiding duplicate links
Databases check existence before the system thinks
CDN networks, cache hit prediction
Distributed systems, data contradiction
Bitcoin wallets, checking transaction history
Password crackers, solving the mystery
Anywhere you need that quick membership test
Bloom filters prove they're among the best
[Chorus]
Bloom it up, filter down, never false negative
Might get false positive but that's definitive
Space efficient, lightning quick, that's the way we roll
Hash it once, hash it twice, let the bits take control
B-L-O-O-M, memory saved today
F-I-L-T-E-R, false positives okay
[Outro]
Probabilistic power in your algorithm arsenal
Bloom filters blazing, making systems optimal
Remember the trade-off, remember the gain
Space-time efficiency, that's the refrain
46. Strassen's matrix multiplication
[Verse 1]
Standard matrix multiplication got you feeling slow
N cubed complexity making algorithms crawl below
Volker Strassen had a vision back in sixty-nine
Seven multiplies instead of eight to cross the line
Breaking down the matrices into blocks of four
Each quadrant gets a name from A eleven to two four
Same thing for matrix B and result matrix C
Now we're ready for the magic, follow closely with me
[Chorus]
Seven products, not eight, that's the Strassen way
M one through M seven, remember what they say
Divide and conquer matrices, recursive all day
From N cubed down to N log two point eight one way
Seven products, not eight, complexity goes down
Strassen's algorithm wears the efficiency crown
[Verse 2]
M one equals A eleven plus A twenty-two times
B eleven plus B twenty-two, that's our first design
M two takes A twenty-one plus A twenty-two and then
Multiply by B eleven, second product in the pen
M three grabs A eleven times B twelve minus B twenty-two
M four uses A twenty-two times B twenty-one minus B eleven too
Each multiplication strategically placed with care
Seven products total, that's the genius we share
[Chorus]
Seven products, not eight, that's the Strassen way
M one through M seven, remember what they say
Divide and conquer matrices, recursive all day
From N cubed down to N log two point eight one way
Seven products, not eight, complexity goes down
Strassen's algorithm wears the efficiency crown
[Bridge]
When N gets large enough to make it worth the switch
Overhead of recursion won't put you in a ditch
Base case still classic when the size gets small
Hybrid implementation gives you best of all
Memory access patterns, cache efficiency
Strassen shows us how to beat the N cubed spree
[Verse 3]
M five combines A eleven plus A twelve with B twenty-two
M six takes A twenty-one minus A eleven times B eleven plus B twelve too
M seven uses A twelve minus A twenty-two times B twenty-one plus B twenty-two
Now combine these seven to get C in our view
C eleven gets M one plus M four minus M five plus M seven
C twelve equals M three plus M five, that's matrix heaven
C twenty-one takes M two plus M four
C twenty-two gets M one minus M two plus M three plus M six, nothing more
[Outro]
Seven multiplications broke the cubic bound
Divide and conquer principles where efficiency is found
Strassen's legacy lives on in algorithms today
When matrices get massive, this is how we play
47. Karatsuba multiplication
[Verse 1]
When numbers get massive and multiplication's slow
There's a clever algorithm that'll steal the show
Karatsuba's the name, divide and conquer's the game
Split your digits in half, reduce the computational strain
Traditional method takes n-squared time to complete
But this Russian technique makes the process more sweet
Take your number A-B, and your number C-D
Where A and C are high, B and D are low, you see
[Chorus]
Split and multiply, three times not four
Karatsuba's magic opens up the door
A-C times ten-squared plus A-plus-B times C-plus-D
Minus A-C minus B-D, that's the key
Split and multiply, watch the time decrease
From quadratic down to log-linear peace
[Verse 2]
Let me break it down with numbers, make it crystal clear
Take five-six-seven-eight times one-two-three-four here
Split them both in half, fifty-six and seventy-eight
Twelve and thirty-four, now calculate their fate
First multiplication: fifty-six times twelve
Second one: seventy-eight plus fifty-six, delve
Into ninety-six times forty-six, that's our middle term
Third: seventy-eight times thirty-four, watch the pattern firm
[Chorus]
Split and multiply, three times not four
Karatsuba's magic opens up the door
A-C times ten-squared plus A-plus-B times C-plus-D
Minus A-C minus B-D, that's the key
Split and multiply, watch the time decrease
From quadratic down to log-linear peace
[Bridge]
Recursive calls stack up like building blocks
Each level cuts the problem, beating traditional clocks
Base case hits when digits get too small
Switch to grade school method, answer them all
The beauty lies in asymptotic gain
When input size grows, we break the chain
Of exponential growth, efficiency's our aim
Three multiplies per level, that's Karatsuba's claim
[Verse 3]
Implementation-wise, pad your numbers with zeros
Handle odd-length digits like computational heroes
Recursion depth is log-n, memory overhead's light
Threshold optimization makes performance tight
For small numbers stick to basic multiplication
But when dealing with cryptographic calculation
Million-digit integers, polynomial arithmetic too
Karatsuba's your weapon, tried and true
[Chorus]
Split and multiply, three times not four
Karatsuba's magic opens up the door
A-C times ten-squared plus A-plus-B times C-plus-D
Minus A-C minus B-D, that's the key
Split and multiply, watch the time decrease
From quadratic down to log-linear peace
[Outro]
From Moscow nineteen-sixty, this algorithm came
Revolutionizing how we play the multiplication game
So remember the formula when big numbers collide
Karatsuba multiplication, let efficiency be your guide
48. Closest pair of points
[Verse 1]
Got a thousand points scattered on the plane
Need to find the closest pair, driving me insane
Brute force checking every single combination
That's n squared time, computational frustration
But there's a better way, divide and conquer style
Split the points in half, make the problem worthwhile
Recursively solve each side, then merge with care
The closest pair's hiding somewhere in there
[Chorus]
Divide the space, conquer the race
Sort by x coordinate, find your place
Minimum distance from left and right
Check the strip with delta insight
Closest pair, closest pair
Logarithmic time if you prepare
Closest pair, closest pair
Divide and conquer gets you there
[Verse 2]
First we sort by x, then split down the middle
Left side, right side, solving the riddle
Find the minimum distance in each partition
Now comes the tricky part, the merge condition
Draw a vertical line right down the center
Points within delta distance, that's where we enter
The strip contains the candidates we need to check
But smart pruning keeps our algorithm in spec
[Chorus]
Divide the space, conquer the race
Sort by x coordinate, find your place
Minimum distance from left and right
Check the strip with delta insight
Closest pair, closest pair
Logarithmic time if you prepare
Closest pair, closest pair
Divide and conquer gets you there
[Bridge]
In the strip, sort points by y coordinate
Seven points max to check, don't subordinate
The geometry proves this magical bound
Each point checks at most seven around
Base case with three points or less
Brute force works when the size's a mess
But for larger sets, our method's the best
N log n time puts us ahead of the rest
[Verse 3]
Implementation details, let's break it down
Pre-sort by y to avoid slow-down
Pass the sorted arrays through recursion deep
Merge efficiently, our runtime to keep
Handle edge cases, points that coincide
Floating point precision, nowhere to hide
Test your algorithm on random sets
This classic problem, no regrets
[Chorus]
Divide the space, conquer the race
Sort by x coordinate, find your place
Minimum distance from left and right
Check the strip with delta insight
Closest pair, closest pair
Logarithmic time if you prepare
Closest pair, closest pair
Divide and conquer gets you there
[Outro]
From computational geometry's core
This algorithm opens up the door
Applications everywhere you look
Clustering, graphics, by the book
Remember the pattern, remember the flow
Divide and conquer, watch your code grow
49. Activity selection
[Verse 1]
Got a list of tasks with start and finish times
Need to pick the max without conflicts in lines
Greedy algorithm coming through with the flow
Sort by finish first, that's the way to go
Activity selection, optimization game
Choose the most you can, no overlap shame
Each task has a window, beginning to end
Smart choices matter when resources we spend
[Chorus]
Sort by finish, pick the earliest end
Greedy choice, let the algorithm blend
No overlap, keep the schedule clean
Maximum activities in the time machine
Sort by finish, pick the earliest end
Greedy choice, let the algorithm blend
Activity selection, the optimal way
Pack the most into your busy day
[Verse 2]
Start with first activity when finish time's least
Then scan through the list, find the next feast
Compatible means start time comes after
Previous finish, no scheduling disaster
Linear scan through the sorted array
Each valid choice leads the optimal way
Proof by contradiction shows it's the best
Greedy stays ahead, outperforms the rest
[Chorus]
Sort by finish, pick the earliest end
Greedy choice, let the algorithm blend
No overlap, keep the schedule clean
Maximum activities in the time machine
Sort by finish, pick the earliest end
Greedy choice, let the algorithm blend
Activity selection, the optimal way
Pack the most into your busy day
[Bridge]
Exchange argument proves the method right
If another solution seems just as bright
We can swap activities one by one
Our greedy choice gets the same result done
O of n log n for the sorting phase
O of n for selection in the maze
Optimal substructure, greedy choice too
Dynamic programming alternative view
[Verse 3]
Interval scheduling, meeting rooms to book
Conference planning, take a deeper look
Resource allocation in the real world scene
Activity selection keeps the schedule lean
Recursive solution builds from the ground
But greedy iteration is more profound
Bottom-up thinking with the optimal play
Maximum value in minimum time today
[Chorus]
Sort by finish, pick the earliest end
Greedy choice, let the algorithm blend
No overlap, keep the schedule clean
Maximum activities in the time machine
Sort by finish, pick the earliest end
Greedy choice, let the algorithm blend
Activity selection, the optimal way
Pack the most into your busy day
[Outro]
When intervals conflict and choices arise
Sort by finish, that's the compromise
Greedy algorithms, they lead the way
Activity selection saves the day
50. Huffman coding
[Verse 1]
Data compression is the name of the game
When file sizes got you feeling shame
Fixed-length codes waste precious space
Variable-length brings us saving grace
Start with frequencies, count each letter
Common symbols coded better
Build a tree from bottom up
Priority queue fills your cup
[Chorus]
Huffman coding saves the day
Frequent symbols shorter way
Less frequent get longer strings
Binary tree optimization brings
Left is zero, right is one
Greedy algorithm gets it done
Optimal prefix codes we make
Data compression for goodness sake
[Verse 2]
Two smallest frequencies combine
Create internal nodes every time
Parent holds the sum of both
Children sworn by sacred oath
No code word is prefix of another
That's the rule we can't ignore brother
Unique decoding guaranteed
Self-synchronizing is what we need
[Chorus]
Huffman coding saves the day
Frequent symbols shorter way
Less frequent get longer strings
Binary tree optimization brings
Left is zero, right is one
Greedy algorithm gets it done
Optimal prefix codes we make
Data compression for goodness sake
[Bridge]
Start with leaves at the bottom floor
Merge the smallest two or more
Until one root remains standing tall
That's the tree that rules them all
Read the path from root to leaf
Compression ratio brings relief
Forty percent or more you'll save
When Huffman's algorithm you crave
[Verse 3]
ASCII uses eight bits flat
But English text ain't balanced like that
Letter E appears the most
Gets the shortest code to boast
Letter Z shows up real rare
Longer code is only fair
Adaptive versions change on the fly
As new statistics pass on by
[Chorus]
Huffman coding saves the day
Frequent symbols shorter way
Less frequent get longer strings
Binary tree optimization brings
Left is zero, right is one
Greedy algorithm gets it done
Optimal prefix codes we make
Data compression for goodness sake
[Outro]
From JPEG to zip files too
Huffman's legacy shines through
Lossless compression at its best
Put your data to the test
Back to Home