Coursera 1 (https://class.coursera.org/algs4partI-007/lecture) Flashcards
why algorithms? why is n square so bad?
rough standard is 10 to power 9 ops per second. that means touch all such words (billion) in memory in 1 sec.
But 10 to power 18 will take 30 years.
dynamic connectivity (is there a path between 2 pts) is referred to as?
Union - Find. Pixels is photo. computers in network.
- create groups of connected elements
- better is to create a tree as unions happen and 2 elems connected if root same
- further improve to keep tree balanced by moving a smaller size tree within larger one
- further improve by balancing lazily when find operation is triggered
What is Monte Carlo method?
Computational (not mathematical) algos that rely on repeated random sampling. eg sprinkle random points in square and a circle inside to guess PI.
Percolation probability in a square of connected points used this plus Union-find algo to find that cutoff probability when percolation will happen.
what is 3-sum problem and how to do better than n cube?
In an array of numbers, find cases where sum of 3 = zero.
instead of 3 loops, sort the array and then for every 2 numbers binary search for -(n+m)
even if sort is n square, binary search is log n
so total is n square log n still better
What are commonly used notations for complexity measure?
Tilde, Big Theta, Big Oh, Big Omega
Big Oh develops upper bounds while Big Omega develops lower bounds. Tilde is approximate model
Memory sizes for Java arrays?
In bytes:
char[] = 2N + 24
int[] = 4N + 24
Memory sizes for some objects in Java, like Date and String?
Object overhead = 16 bytes
and optional padding to round to multiple of 8 = ~4 bytes
Date object = 16 + (3 ints for year, month, day) + 4 padding = 32 bytes
String object = 16 + (char[] = 2N + 24) + char[] reference (8 bytes) + (3 ints for offset, count, hash) + 4 padding = 2N + 64 bytes
(Note: Some JVMs support compressed pointers of 4 bytes)
What two data structures can be used to create a Stack or Queue impl?
LinkedList and array[]
LinkedList uses more memory due to object overhead, however all operations are O(1)
With array impl, you have to resize array (both up and down) by factor of 2. When resizing down though, better to wait till 25% of array is full before reducing array by half.
What is Djiktra’s 2 stack algo?
For arithmetic operations, solve them via 2 stacks. One stack for values, other for operators. Whenever closing bracket encountered, pick top values from both stack and operate and but value on value stack.
(1 + (2 + 4) * (5-1))
It adds to basis of computation, as well as compilers!
Selection sort?
Most basic. Move one index at a time, and swap with min beyond that index. Brings forward the min item one by one by comparisons.
Num compares = N square / 2
Max Num swaps = N
Insertion sort?
Very basic. Grabs an extra element (like insert a new element in array) and brings it into position with existing sorted elements.
Num compares = ~ N square / 4
Best case compares = N if already sorted. Worst case is N square / 2
However for partially sorted arrays this still is linear!
Shell sort?
Shell == inventor’s name.
Instead of moving in increments of 1, increments of say 3x+1 … better performance but hard to put in O(n) terms.
3,6,2,5,1
First iteration use increment = 4, compare (and swap if needed) 3 and 5 and then 6 and 1 etc
Second iteration use increment = 1 and start again
How to implement shuffling?
One way is to assign random float (0-1) to each entry and sort. Performance is as good as sort is.
Better is knuth shuffle: In linear traversal, for every ith card, generate random between first card and ith card and swap the cards. Works like a charm!
Convex hull?
It is perimeter on points in 2D where we can make polygon with least vertices. Helps in finding smallest distance around a cluster of points.
first sort points by y coord, then by polar angle. Start traversing and ignore points that involve counter-clockwise turn.
Merge sort?
Divide and conquer from middle point. Merge function merges two sorted arrays.
Disadv: Pure version needs an auxillary array of N size. In-place merge becomes tough.
NlogN perf though.