0% found this document useful (1 vote)
2K views2 pages

Cs521 Midterm Cheatsheet

The document covers algorithms and data structures commonly used in computer science including insertion sort, quicksort, red-black trees, heaps, and binary search trees. It provides details on the time and space complexity analysis of operations for each as well as solutions to recurrences using techniques like the master theorem. Common asymptotic notations for analyzing algorithms are also defined.

Uploaded by

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

Cs521 Midterm Cheatsheet

The document covers algorithms and data structures commonly used in computer science including insertion sort, quicksort, red-black trees, heaps, and binary search trees. It provides details on the time and space complexity analysis of operations for each as well as solutions to recurrences using techniques like the master theorem. Common asymptotic notations for analyzing algorithms are also defined.

Uploaded by

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

CS521 Midterm Cheat-Sheet

Insertion Sort:
Each element is pushed as far as possible to the left (sorted) part of the array. Running time: ( ) worst-case (when sorted in reverse), ( ) best-case (when already sorted). Sorts in-place, space complexity ( ).

Quick Sort:
Partition( ): linear procedure that partitions in[ ] s.t.: place around and returns the index of the pivot. Preferable over merge-sort, as it sorts in-place. ( ) ( ) ( ) General recurrence: ( ) W.C.: array already sorted, partition at the beginning: ( ) ( ) ( ) ( ) ): Note: for any -proportional division we get ( ( ) ( ) ) ( ) (( ) For we will get a balanced tree. Expected running time: [ ( )] [ ] ( ) ( )) ( ) ( ( )

Red-Black Trees:
Every node is either red or black Root and leafs (NIL) are black If a node is red, its children will be black All black paths have same # of blacks (black height) Height: maximum Insertion: Create new red node with 2 black leafs and put in place. Corrections (a inserted): Case 1: as uncle is red: Color father and uncle black, and grandfather red. Problem moved up 2 levels to grandfather.

Asymptotic Notations:
Big-O notation: ( ) ( ( )) ( ) ( ) ( ) ( Or: Small-o notation: ( ) ( ( )) ( ) ( ) ( ) ( Or: Big- notation: ( ) ( ( )) ( ) ( ) ( ) ( Or: Note that ( ) ( Small- notation: ( ) ( ( )) ( ) ( ) ( ) ( Or: Note that ( ) ( Big- notation: ( ) ( ( )) ( ) ( ) ( ) ( Or: Notes: ( ) ( ( )) ( ) ( ( )) ( ( ) ( ( )) (

Heap:
Left filled structure that satisfies partial order (node its children, no particular relation between children). For node : left child at , right at , parent Height is the cost of insertion, extract-max, delete. Insert: put node at the end and heapify up (switch with parent( ) until parent( ) or is root. Remove-max: remove root and put last node of the heap in its place. Heapify it down, each level switching with the greater child of the two until satisfies heap properties. Build-heap: for node down to , heapify down. Amortized analysis each level but lowest will be visited ( ) ) . Total: ( ) ( ). at most ( The sum part is ( ) total: ( ). Case 2: as uncle is black, a is a right child: Rotate left around father and continue to case 3.

) ( ))

(non-negative constant) ( ) ( ( ))

) ( ))

( )

( ( ))

Case 3: as uncle is black, a is left child: Rotate right around grandfather, switch colors between father and new sibling.

) ( ) ) )

( ) (some constant) ( ( )) ( ( )) ( ( )) ( ( )) ( ) ( ( )) ( ) ( ( ))

Heap-sort:
Build a heap and then run times: Swap the max with the last element, decrease heap size by 1 and heapify the new root down. Result: the array is sorted. ), sorts in-place. Running time: ( Deletion: Delete as for any BST. If successor was black, leave behind extra black and correct: Case 1: as sibling is red: Rotate left around father, switch colors between father and grandfather and continue with circled subtree to other case.

Selection problems:
The Select algorithm for finding the th o.s.: ( ): Divide into groups of size 5. Find the median for each group and store in . ( ) to find Call median( ). ( ): Let If return . ( ) If return ( ) If return Running time: ( ). Selects recurrence: ( ) ( ( ) ( ) ( ) (proof ) by induction for upper bound. Simplified Master Theorem: ( ) ( ) For ( ) : ( ) ( ) 1. ( ) ( ) 2. ( ) 3. ( )

Divide and Conquer recurrences:


Break a problem into sub-problems, solve sub problems and merge solutions. ( ) ( ) Hanoi-tower: ( )

Merge-sort:
Divide the sequence into two sequences, apply the algorithm recursively and merge solutions. ( ) ( ) ( ) ( )

Solving Recurrences:
Substitution method: ( ) ( ) ( ) [ ( ) ( )] ( ) ( ) ( ) ( ) ( )
( ) ( ) ( )

Case 2: as sibling and nephews are black: Take blacks from a and c and move problem up.

<last row> + <all other rows> Recursion tree: Build a tree from the levels in the substitution: Level 0: ( ) ) Level 1: elements, each costs ( ) Level : elements, each costs ( Last level: elements , each costs ( ) The Master Theorem: For , ( ) function over non-negative integers and ( ) the recurrence: ( ) ( ) ( ) ( ) ( ) 1. ( ) ( ) 2. 3. and ( ) ( ) ( ) ( ) ( ( ) ( ) ) ( ) ( ) ( ( ))

Binary Search Trees:


Satisfy: left(x) key(x) right(x) Unbalanced trees: all operations cost ( ), where is the height of the tree, between and . Successor of : Left most leaf of s right subtree. If doesnt have one, then the lowest ancestor with left subtree. Deletion: Case 1,2: No left/right child. Put sole child instead. Case 3: Successor is right child. Put successor instead. Case 4: Successor is not direct right child. Swap successor with its sole (right) child, and then swap deleted node with successor.

Case 3: as sibling is black with left red and right black: Rotate right around sibling, switch colors between new sibling and old sibling and continue to case 4.

Case 4: as sibling is black with right red: Rotate left around father, color grandfather with fathers color, color father with extra black, color uncle black.

Notes for MT: The addition/subtraction denotes polynomial difference in running time between ( ) and . Cases (1) and (2) derive for some relation between .

Examples:
Some hierarchies: ( ( ) ( ) ( ) Some recurrences: ( Binary search: ( ) ( Linear search: ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) (bounds) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ) ( ) ) (MT case 1) ( ) (tree) ( ) (MT case 3) ( ) (bounds) ( ) (tree) ( ) ( ) ) ( ( is o, ) is ( ) ( )

( (

) (substitution) ) (bounds) ( ) (bounds) ) ( ) (tree)

Math stuff:
( (

) harmonic series

You might also like