CS 251 Midterm Set
Prime number
A whole number that has exactly two factors, 1 and itself.
priority queue
Each element of a priority queue has an associated priority.
Dynamic Programming Example
Fibonacci numbers with array
System dependent effects
Hardware (CPU, memory), Software (compiler, interpreter), System (OS, network, other apps). Determine the constant a in power law
How to prove correctness of greedy algorithm?
you have to prove that choosing the local optimum based on that greedy choice will always produce a global optimum. Proof by contradiction
Bubble Sort Average Case
N^2
Bubble Sort Worst Case
N^2
Selection Sort Average Case
N^2
Selection Sort Best Case
N^2
Selection Sort Worst Case
N^2
Ternary Tree
every node has at most 3 children
Full binary tree
every node other than the leaves has two children
Brute Force
exhaustively search all the possibilities. Often useful as a starting point
Order of growth example
1, logN, N, NlogN, N^2, N^3, 2^N
given a complete ternary tree of height 7 how many internal nodes are there are level 3
27
What is the maximum number of leaves a tree can have
2^h
if there are e external nodes how many nodes total are there
2e - 1
given a ternary tree of height h what is the max number of leaves it can have
3^h
Max heap
A binary tree that maintains the simple property that a node's key is greater than or equal to the node's children's keys
Log-log plot
A graph where both the x-axis and the y-axis represent the logarithm of the respective data points. Straight line identifies power law.
What makes a good algorithm?
Accuracy: you want to be sure there is a correct output for given input Efficient: you don't want to waste time or space
Bubble Sort
Moving through a list repeatedly, swapping elements that are in the wrong order.
Prune and Search example
Binary Search
What do we care about with sorting methods?
Certification, runtime, extra memory, types of data
Divide and Conquer
Done recursively, divides a problem into subproblems and combine solutions to subproblems to form solution to original problem
Bubble Sort Best Case
N
Brute Force Example
Linear Search
Divide and Conquer Example
MergeSort, Quicksort
Bubble Sort Extra Space
O(1)
Selection Sort Extra Space
O(1)
array look up operation time
O(1)
linked list look up tim
O(N)
amortized cost of push in array
O(N) for total cost for N pushes -> O(1) for average cost per push
Reasons to Analyze Algorithms
Predict performance, compare algorithms, provide guarantees, understand theoretical basis
Selection sort
Repeatedly select the smallest item and put it into the next unsorted spot
Greedy Algorithm example
Scheduling problem
Average case
expected cost for random input. Need a model for "random" input. provides a way to predict performance
Binary search recurrence relationship
T(N) <= T(N/2) + 1 for N > 1 with T(1) =1 . T(1) is for te first compare or if there is only one element. The N/2 is because you divide the array into halves
Power law
T(N) = a * N^b where a = 2^c
Worst Case
Upper bound on cost. Determined by "most difficult" input. provides a guarantee for all inputs
Arrays
a collection of items stored in a contiguous memory location and addressed using one or more indices
Abstract Data Type
a description of operations on a data type that could have multiple possible implementations. Specifies data, operations, and error conditions
Doubly Linked list
a linked list in which each element has both forward and backward pointers.
Algorithm
a process or set of rules to be followed (instructions) typically with an end goal
Linked List
a sequence of records, where each record contains a link to the next one.
Comparable Interface
a useful Java interface for writing generic sorting methods
Bag operations
add, isEmpty, size, (iterate)
frequency depends on...
algorithm, input data
System independent effects
algorithm, input data. Determine the exponent b in power law
Bags
allow any type of data in any order
key
an abstraction, must be something that can be ordered
Postorder
an order of processing a tree in which the children before parents
Preorder
an order of processing a tree in which the parent node is processed before its children.
How to implement a stack
array or linked list. array you may need to resize
amortized analysis
average running time per operation over a worst case sequence of operations
Dynamic Programming
combines optimal solutions to form an optimal solution and keeps track of subsolution to avoid repetitive statements
Cost of operations depend on...
compiler, machine
data structure
data organization, management, and storage format that enables efficient access and modification. More precisely, a data structure is a collection of data values, the relationships among them, and the functions or operations that can be applied to the data.
Queue
data stored in First in first out order
Stacks
data stored in first in last out order
if a program uses queue...
dealing with data stored and processed in fifo order
if a program uses stack...
dealing with data that needs to be put in FILO/LIFO order
lazy approach
do work until later
eager approach
do work up front
Barnes-Hut Algorithm
enables new research
FFT Algorithm
enables new technology
queue operations
enqueue(e), dequeue(), size(), isEmpty()
Big-Theta notation
establishes a "TIGHT" bound. (both upper and lower) f(n) is Theta(g(n)) if there is a constants c` and c`` > 0 such that c`g(n) <= f(n) <= c``g(n)
Big-Omega Notation
establishes a LOWER bound. f(n) is Omega(g(n)) if there is a constant c> 0 such that f(n) >= cg(n)
Binary Tree
every node has at most 2 children
swimming
go from bottom up and exchange values based on what type of heap it is
sink
go from top to bottom exchanging values based on what type of heap it is
Rooted Tree
hierarchical structure that has a parent-child relationship
Value of defining a narrow adt
how an operation is implemented matters, encourages discipline, thinks about what is essential
if there are i internal nodes, how may external nodes are there
i + 1
Array resizing in stacks and queues
if it is full double the array size, if less than 1/4 of array is used decrease size by half
Heap as an array
if node is at index k (zero based). Left child is 2k+1, right child is 2k+2, parent is the floor of (k-1)/2
Tilde notation
ignore lower order terms, and keep the constant for the largest time and highest term
priority queue operations
inser, removeMax, removeMin
Insertion Sort
insert current item into its proper place in the sorted part of the array (on the left)
inorder
left, root, right
if there are e external nodes what is the minimum height
log (e) base 2
Best case
lower bound on cost. determined by "easiest" input. Provides a goal for all inputs
Why do we study data structures with algorithms?
need to understand how data structures operate because this will effect runtime of an algorithm
Runtime Cost Model for Sorting
number of compares and exchanges, if there are no exchanges, use array accesses
Discrete Fourier Transform Success
previously solved in N^2 steps, with FFT algorithm, now solvable in NlogN
stack operations
push, pop, peek
doubling hypothesis
quick way to estimate b in a power law relationship
Properties of selection sort
runtime is insensitive to input, data movement is minimal (O(N) exchanges), anything left of the current index is sorted and in final position
Properties of insertion sort
runtime is sensitive to input, works well for tiny arrays and partially sorted arrays (number of inversions is low)
Prune and Search
similar to divide and conquer but recursively prune the search space (reduce the size) by a constant factor
Greedy
solving an optimization problem where we attempt to find a global optimal solution by choosing the optimal value locally
N body simulation
stimulate gravitational interactions among N bodies previously in N^2 steps, now NlogN.
Total running time
sum of cost * frequency for all operations
if f(n) is theta(g(n))...
then f(n) is O(g(n)) and f(n) is omega g(n) meaning g(n) is o(f(n)) and g(n) is omega f(n))
If f(n) is theta(g(n))...
then g(n) is theta(f(n))
if a program uses a bag...
then the order of data stored doesn't matter
3-sum problem
total possible number of triplets C(N,3)
Big-Oh Notation
used to classify UPPER bound. f(n) is O(g(n)) is there exists a positive constant c such that f(n) <= cg(n)
Algorithm paradigm
way of approaching a problem. No paragidm is better than another, success is depending on the context of the problem you are trying to solve