Data Structure And Algorithm 2
What is a graph?
A set of nodes connected in some way by a set of edges
What's a simple cycle?
All edges and vertices are distinct except start & finish
What is a directed graph?
All edges are directed
What is the ith level?
All nodes of depth i
What does BFS use to find the shortest path?
A Heap
Write the line of code you'd need at the top of your source code file to use STL template functions e.g., sort
#include
What's the equation of MAD?\nWhat are the components?
((ay + b) mod P)\n\n\nP = prime number\na and b = non-negative integers [0, P-1]
When ordering complete binary tree nodes, what number is the root?
-
What is Stack topIndex when the stack is empty?
-1
λ should definately be less than or equal to this with quadratic probing
.5
λ should be approximately equal to this for separate chaining
1
What are the 3 conditions that linear probe searching stops at?
1. An item is found with the key\n2. an empty cell is found\n3. N Cells have been unsuccessfully probed
What are the two parts of a hash function?
1. Generate hash code (keys -> integers)\n2. Apply a compression fuction (integers -> [0, N-1])
What's Dijkstra's algorithm
1. Start at a node and calculate the lowest distance to opposite nodes\n2. Choose Lowest node to visit\n3. Repeat using all nodes
What is the algorithm for rotating a binary tree root to the right
1. copy root.left --> temp\n2. set root.left --> temp.right\n3. set temp.right --> root\n4 set root --> temp
What's the maximum number of nodes at level i?
2^i
Length of path ABCD in an unweighted graph
3
QuickSort
3 parts: less than pivot, pivot, greater than pivot\n\nrecursively sort\n\nrandom pivot selection: expected runtime nlogn\nworst is still n^2
The minimum number of items a leaf node of a B-Tree of order 7 can store
4
What does a comparator object return?
>0 if more than\n<0 if less than\n0 if equal to
What is an AVL tree?
A Binary tree with a height balance property so that the height of children of any node differs at most by 1
What is a cycle?
A circular path beginning & ending on the same vertex
What is a binary heap?
A complete binary tree ordered from top to bottom so that the children keys are larger than their parents
What is the edge-list structure?
A list of nodes\nA list of edges\nEach edge has a pointer to two nodes
What is a connected component?
A maximum connected subgraph
What is a map?
A model of a searchable collection of key-value entries
What does "Connected" mean?
A path between every pair of vertices
What is a path?
A sequence of alternating vertices and edges
What's a spanning tree?
A spanning subgraph that is a tree
This graph representation is space-efficient for sparse graphs
Adjacency Lists
This graph representation allows the presence or absence of an edge to be checked in constant time
Adjacency Matrix
What does Method Overloading do?
Allows there to be two methods with the same name but different signatures
What is an Interface?
An interface contains only the signatures of methods properties
Define a directed edge
An ordered pair of vertices
Why are we taking this course?
Because Bill said so.
Why are hashtables better at implementing maps than arrays?
Better performance
Strings algs
Brute Force\nBM - Last occurence function\nKMP - Failure Function
DAG
directed, acyclic graph\n\ncan find topological ordering
What's a spanning subgraph?
Contains all vertices of a bigger graph
A directed graph with no directed edges
DAG
List
DLL or SLL\nawareness of "before" and "after"\nfast to manipulate, slow to access
A way of organizing information in memory
Data Structure
What is a Hash Table
Data structure that maps keys to values
What does DAG stand for?
Directed Acyclic Graph
How does the hashing of a Hash Table work?
Distributes key/value pairs across an array of buckets\n\n\nhash = hashfunc(key)\nindex = hash % array_size\n\n\n`hash` is independent of array size, and is reduced to an index (number between 0 and array_size - 1) using modulo operator
What is separate chaining?
Each bucket stores a list-based map
Structures
Edge List\nAdjacency List\nAdjacency Matrix
What is an incident edge?
Edges belonging to a vertex
What's another name for a key-value pair?
Entry
(T/F) For a B-Tree stored on a disk, the maximum number of items stored in a leaf node should be chosen to be as small as possible
FALSE
(T/F) The subtrees of a B-Tree can differ in height, but by no more than 1
FALSE
(T/F) With collision resolution strategy f(i) = i, insertions can fail if λ = 0.95
FALSE
How does a Queue work? What are its main methods?
FIFO: first in, first out\nOR\nLILO: last in, last out\n\n\nMethods:\nOffer : adds element to back of queue\nPeek: returns element at front of queue\nPoll: takes element at front of q
Queue
FIFO\nO(1)
Stack
FILO\nO(1)
KMP
Failure Function:\n- length of prefix that is suffix of current position\n\ni and j counter, j updated by F(j-1) if j > 0 otherwise j = j + 1\n\nO(m + n)
A set of vertices and a set of edges
Graph
If you wanted a non-recursive sort with\nO(n log n) worst-case running time, which one would you use?
Heapsort
An algorithm for finding optimal prefix codes is named after him
Huffman
What extra steps need to be taken for an AVL tree during insertion?
If the unbalance is right-right/left-left then you rotate the first node that becomes unbalanced and it's unbalanced child and shift the child node to balance\nIf the unbalance is left-right/right-left then you rotate the first unbalanced node and it's unbalanced child and then use the left-left/right-right balance
How does up-heap-bubbling work?
Insert as last node\nSwap node and parent\nRepeat until order is restored
If an array is nearly sorted, the best sorting algorithm to use is _____________
Insertion Sort
An algorithm for finding minimu spanning trees is named after him
Kruskal
How does the Stack work? What are its main methods?
LIFO: last in, first out \nOR \nFILO: first in, last out \n\n\nMethods: \nPush(T item): adds item to top of stack \nPop(): grabs item from top of stack \nPeek: returns top item of stack
A node with no children in a tree
Leaf
When ordering complete binary tree nodes, what are the values of the left and right children?
Left: 2*parent+1\nRight: 2* parent+2
What does MAD stand for?
Multiply, Add, Divide
The number of edges in a tree with N nodes
N-1
What's the problem with edge list structure?
Nodes can't find what edges they're connected to without scanning all edges
What are endpoints?
Nodes of an edge
Average-case run time for insert in a hash table
O(1)
What's the order of growth for insertions, removals, and searches of hash tables?
O(1)
DAG sorting
O(V+E)
DFS / BFS running time
O(V+E)
k-D tree building time
O(knlogn)
Average-case run time for contains/find in a binary search tree
O(log n)
What is the order of growth for up-heap and down-heap bubbling?
O(log n)
Worst-case run time for find in an AVL tree
O(log n)
Worst-case run time for find in an STL set
O(log n)
Bucket sort
O(n + N)!!!\n\nplace all elements into buckets\nthen remove from buckets
What is the order of growth for heap building when implementing a priority queue using a heap?
O(n log n)
What is the order of growth for heap sorting when implementing a priority queue using a heap?
O(n log n)
Worst-case run time for clone or makeEmpty (both are traversals) in a binary search tree
O(n)
Worst-case run time for insert in a binary search tree
O(n)
Worst-case run time for insert in a hash table
O(n)
Worst-case run time for insert in a hash table using linear probing
O(n)
standard trie
O(n) space where n is the total length of all strings\n\nheight is longest string\n\nfinding string: O(dm) - note not dependent on n
What is the Order of Growth for PQ-Sort?
O(n^2)
If the running time of an algorithm increases by a factor of 8 whenever the size of the input increases by a factor of 2, the growth rate of the algorithm is ___________
O(n³)
What is the adjacency list structure?
On top of the edge-list structure, edges have 2 more pointers: to their references in the sequences for both of the nodes\nEach node has a sequence of edges (Incidence Sequence)\nNodes also have a pointer to their edge collections
What is Polymorphism?
Permits parent class to dynamically take behaviour of descendent class
What is linear probing?
Placing colliding items in the next available cell (circular array)
The fastest sort using C++ on a large, randomly-ordered array (taking into account both growth rate and constant factors) is ______________
Quicksort
Solves an instance of a problem by first solving one or more smaller instances
Recursive Algorithm
How does down-heap bubbling work?
Replace root with last node on heap\nSwap node with smallest key child until order is restored
What are the main operations for maps?
Searching for things\nInserting new things\nDeleting things
What are the two types of PQ-Sort?
Selection Sort\nInsertion Sort
What are the two ways of handling collisions?
Separate Chaining\nLinear Probing
The mathematical entity a queue is based on
Sequence
What is the Adjacency Matrix Structure?
Similar to the edge list structure\nThere's also another table labelled with nodes that points to edges\nEdges do not point to these references
What is graph traversal?
Systematic procedure visiting all nodes
Give the formal definition of Big-Oh, i.e., what does T(n) = O(f(n)) mean precisely?
T(n) = O(f(n)) if there exists a constant c & another n₀ that when used with T(n) allows f(n) to be an upper bound for all n ≥ n₀.\ni.e. T(n) ≤ c ∗ f(n)
If T(n) is Big-Oh of n² but not Big-Theta of n², what can we conclude?
T(n) = o(n²)
(T/F) The insert operation takes O(log n) worst-case but only constant time on average in a heap.
TRUE
(T/F) The subtrees of an AVL Tree can differ in height, but by no more than 1
TRUE
(T/F) With collision resolution strategy f(i) = i², insertions can fail if λ = 0.51
TRUE
What happens if you insert a key value pair and an entry is found that already has the key in a map?
That entry's data is replaced and the old data is returned
What is the load factor?
The expected number of entries in a bucket\nDescribed the probability of a collision
What is height?
The longest path from the root to a leaf
What is depth?
The number of ancestors
What does the override keyword mean?
The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method property
What is Insertion Sort?
The variant of PQ-Sort which uses a sorted sequence
What is Selection Sort?
The variant of PQ-Sort which uses an unsorted sequence
How are arrays like maps?
They have an integer key for the data they hold. THey are special cases of maps
What's an important part of keys in maps?
They must be unique
What is the term for insertion in a binary heap?
Up-Heap Bubbling
What is PQ-Sort?
Uses priority queue to sort
What is the simple implementation of a map?
Using a doubly linked list
What is a subgraph?
Vertices and edges that are a subset of a bigger graph
When is a search table the best implementation for an ordered map?
When searches are the primary operation and the maps are small
When is the simple implementation of a map good?
When the map is small
suffix trie
all suffixes of a potential trie, compactable\n\npattern matching O(dm) with d being length of alphabet
What is a simple path?
all vertices and edges are distinct
What does the remove function require for linear probing hash tables?
an AVAILABLE item
What is a tree?
an undirected connected graph with no cycles
What is a forest?
an undirected graph that has no cycles
Vector
array\nhas rank\ncircular?\nslow to change, fast to access
What is a bucket array?
arrays assigned to each array index and hold key value pairs
2D range search
auxiliary structures are subtrees root v sorted with y\n\nRange search: O(log^2 n + s)\n\nSpace nlogn (getting worse)
AVL
balanced tree\n\nlogn access
DFS BFS
both take O(V + E) time
DP example change of coins
building network from the bottom up\n\nstoring solutions and using them later
Adjacency List
builds on Edge list, \n\nVertex now contains container of edges to link to, and edges link to that container\n\nruntime now dependent on deg(v) or minimum of deg(u),deg(v)
Adjacency matrix
builds on edge list, \n\nedges are stored in a vertex matrix, each vertex has an index\n\nnull = no link, object = link present\n\nareAdjacent no O(1), rest in n and space O(n^2)
AVL deletion
delete: basic if leaf node, otherwise left most node of right subtree becomes node \n\nz is first unbalanced, y and x are children of subtree with LARGER HEIGHT\n\nlogn steps worst case
This STL container class supports random access with an index, and can grow/shrink at either end
deque
What can BFS do?
determine if a graph is connected\nComputer connected components\nCompute a spanning forest
What can DFS do?
determine whether G is connected\nfind and report connected components\ncompute a spanning forest
What are examples of maps?
dictionaries\nassociative memory
This collision resolution strategy uses a second hash function to probe at intervals that depend on the value being inserted
double hashing
What is the term for removal in a binary hep?
down-heap bubbling
topological ordering of DAG
enqueue with in-degree 0\n\ndequeue with 0, reduce all linked vertices in-degree and store its rank. Repeat with new vertices that are no 0\n\nO(V+E)
What is the ceiling entry in an ordered map?
entry with the smallest key >= k
1D range tree
findall in O(logn + s)\n\nO(n) space
Euler tour
general tour for traversing a tree
A function that converts a key value into an array index
hash
What two components do hash tables have?
hash function\nbucket array
Heapsort
implementing the sequence as a heap.\n\neach insertion is logn, and retrieving is also logn. For n items, becomes nlogn\n\nin place in an array shifting the "barrier" between heap and unsorted sequence
AVL insertion
insert in correct location\nfind z if unbalanced, y and z are children\ndetermine abc, switch around\nconstant time
A class MUST implement all methods defined in the
interface
A class can implement multiple...
interfaces
Graph important methods
isAdjacent(v,w), adjacentVertices(v)\n\ndeleteVertex(v)\n\nthese will be compared frequently
Tree implementation (vector)
layers from top to bottom, children are 2xrank and + 1 for right child
Binary Search Trees
left < parent < right\nsearch is O(h)\n\ninsertion: trivial\nremoval: take left most node of right-subtree and replace (if has leaf node, just delete)
Radix Sort
lexicographically sorting pairs\n\nfrom back to front\n\nruns O(d(n+N)) with d being the length or depth of sorting
This ADT manages data by position
list
The number of items in a hash table divided by the table size
load factor
AVL proof of height
looking at number of nodes stored when balanced with respect to height.\n\nn(h) > 2 * n(h-2) (always more nodes than this)\n\nso logn
This STL container class always stores key-value pairs ordered by key
map
This STL container class can use a string like an array index (with [ ]) for retrieving a value
map
The main advantage of more sophisticated (than a heap) priority queue implementations like binomial queues and fibonnacci heaps is that they support the ____________ operation efficiently (less than linear time).
meld
Sequences
methods of List and vector, implementation can vary
Spanning tree
of a connected, undirected graph is a subgraph with all vertecies but no cycles and is fully connected
a class can only have...
one parent class
A function object (a.k.a functor) is an instance of a struct or class that must define which member function?
operator()
Heap
parent < children\nalways balanced -> O(logn)\nbinary tree implementation or array\nbottom up construction takes O(n) because of maximum path
in place quicksort
pivot and flipping elements over the pivot if smaller
Insertion Sort (for PQ)
places each item sortedly in the sequence\n\nruns in O(n^2) but not always
k-D tree
point or region based. point is balanced, height logn\n\nnearest neighbour is O(n) worst but O(logn) expected
This phenomenon makes linear probing take longer than random probing as λ increases
primary clustering
QuadTree
region in quads, can be greatly impalanced\n\nO(sqrt(n) + s) for 2D
Selection Sort (for PQ)
retrieves the smallest item from a sequence. \n\nRuns in O(n^2)
What does remove return in a map?
returns the value if it's found\nreturns null if not
Boyer Moore
scanning from back of pattern\n\nlast Occurence function for each letter in alphabet \n\njumpy by according m to align or entire length if not in pattern or 1 if neither\n\nstill O(mn)
The mathematical entity the dictionary ADT is based on
set
This STL container class can store any type for which an ordering (e.g., operator<) is defined and traverse the container in sorted order using an iterator
set
This STL container class lets you efficiently insert, find, and remove items based on their value, not position, and requires them to be comparable using operator
set
Dynamic programming
similar to DC bt more powerful\n\nsubproblems can overlap - store solutions\n\n3 properties:\n- must be able to break problem down\n- optimal subproblems must give optimal solution\n- overlapping is GOOD
Sets (merging)
smallest element from the front of both sorted parts, when one runs out, just let the other one finish
What is the higher entry?
smallest key > k
divide and conquer
solving distinct sub-problems and then combining
Lookup Table
sorted array\nwith binary search: logn access
priority queue
sorted queue with rank or weight\ntotal order relation\ncan return smallest key
compact trie
space is O(s) where s is the number of strings
Merge Sort
splitting into two parts, recursively sorting, then merging\n\nMerging: smallest object of each, proceed, merge takes O(n1+n2)\n\ntotal nlogn
integer multiplication (D&C)
splitting large and small orders\n\nexpanding the multiplication\n\nusing a substitution for the mixed part, then storing answers and O(n^1.585)
Trie
standard -> compressed -> compact\n\nuse special character to deal with prefix
What does the static keyword mean?
static means that the method or class belongs to the TYPE and not the INSTANCE of a class. A static member cannot be referenced through an instance. Instead it is referenced through the type name
a child class exhibits AT LEAST...
the same behaviour as the parent class
Dictionary
umbrella for sorted search methods\nKEYS\nhas methods for key before and key after
Which STL class lets you store key-value pairs using primitive types or strings as keys in a hash table without needing to define a structured type or your own hash function.
unordered_map
Graph
vertices and edges\n\ndirected\n\ncycles
Edge List
vertices are objects in list\n\nedges are objects in container, link to vertices\n\nruntime mostly O(E)
What are 3 BFS properties?
visits all vertices and edges\ntraverse edges frmo a spanning tree\nthe shortest path from a node to another node
Why is does the hash table use modulo with prime numbers?
when you're repeating over a set space, you're going to provide an even distribution across your hash space.
If an algorithm can reduce the size of a problem by a constant factor (e.g. divide it by 2) in constant time, the growth rate of the algorithm is __________
θ(log n)
A tight, worst-case Big-Oh time bound for the buildHeap operation is ____________
θ(n)
What is the worst-case running time of Quicksort? Is there a simple ordering of the data that could cause this worst case, using the book's implementation of Quicksort?
θ(n²), No
What is the average-case growth rate for Shellsort using Hibbard's increments?
θ(n⁵/⁴)