Data Structures Final Exam (2nd half of semester)

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Yes The solution can be verified in polynomial time: calculate length of cycle and see if < k. Verify that cycle is also simple

*P vs NP vs NP-complete* Example NP-complete problem: Traveling salesperson (TSP) Given a complete graph, every vertex has an edge to every other vertex. The goal is to form a simple cycle of all vertices (visit everywhere only once other than the start/end vertex) whose length < k Can this solution be verified in polynomial time?

NP

*P vs NP vs NP-complete* If given the solution (most are in 2^n time), you can verify that solution in polynomial time

NP; P = NP

*P vs NP vs NP-complete* If you solve an NP-complete problem, you've automatically found the solution to all other ____ problems. The conversion is in polynomial time. If you find an NP-complete solution in polynomial time, you would prove _______

unknown! (but unlikely)

*P vs NP vs NP-complete* does P = NP?

P

*P vs NP vs NP-complete* problems that CAN be solved in polynomial time

NP

*P vs NP vs NP-complete* problems that can be solved in "nondeterministic polynomial time"

insert: O(N) remove: O(1)

*Priority Queue* Using a SORTED linkedList, what is the cost of an insert? What is the cost of remove?

insert: O(log N) remove: O(log N) **remember min is the leftmost node **for insert, the worst case is if you have to percolate up the insert all the way to the root

*Priority Queue* Using an AVL tree, what is the cost of an insert? What is the cost of remove? ** you are sorting based on the priority number

insert: O(1) remove: O(N)

*Priority Queue* Using an UNSORTED linkedList, what is the cost of an insert? What is the cost of remove?

0

*Priority Queue* We put a reference to the inserted item, x, in position ___ of the array in order to make the loop terminate. We elect to place x into position 0 in our implementation

yes

*Priority Queues* Can the same priority be assigned to multiple items?

there is a priority function to assign the importance of an item. This influences the dequeue order (regardless of the order they arrived into the queue)

*Priority Queues* How do priority queues differ from regular queues?

insert; deleteMin

*Priority queue* ________: presents item to be stored in the priority queue _________: remove the highest priority element

3 first, last, and mid push min and max values of the three to the VERY edge (even past the pivot on the right) and just analyze the unsorted values against the pivot

*Quicksort* After using median-of-three pivot selection, the relative positions of the other 2 values are known and they can be placed in their respective halves Therefore, _____ elements are already sorted before you begin the quicksort

3 first vs last first vs mid mid vs last

*Quicksort* How many comparisons must be made to determine the pivot from median-of-three

O(N^2) -- it just turns into selection sort!

*Quicksort* If the min/max is picked for the pivot each time, what is the cost?

last; <; >; swap; i

*Quicksort* Swap pivot with ________ element in array i is the first unsorted element, j is the last unsorted element if i ____ pivot, increment i if j ____ pivot, decrement j if i and j are both not in their correct positions, ______ them! Once i and j cross, insert the pivot by swapping with ____

in the middle -- result in even halves

*Quicksort* To get NlogN cost, the pivot must be where?

median of 3 Look at first, middle, and last element and pick the median out of those to be the pivot

*Quicksort* What is the best way to select a pivot?

Choose a pivot and put smaller elements tot he left and larger elements to the right Recursively select pivots on each half Base case: section of length 3ish (--> insertion sort)

*Quicksort* What is the general idea of quicksort?

6

*Quicksort: Median of three* What would be the pivot? 5 1 7 9 8 2 4 6

double; prime; reinsert

*Rehashing hash table* To lower the load factor, ________ the size of the hash table and expand the size up to the next _______ number _______ all the elements in their respective spots to lower the load factor ex: rehash size 11 --> 23 --> 47

adjacency list

*Representing Graphs* Each vertex has a linked list of vertices that it is connected to

adjacency matrix

*Representing Graphs* This is helpful for weighted graphs. You can fill in the boxes with the respective weights of edges. For unweighted graphs, use 0 or 1

Locate the minimum value in the unsorted portion and swap it with the front of the list, move the sorted pivot one position over

*Selection Sort* Describe selection sort

front

*Separate Chaining Hash Table* A new element is inserted at the _____ of the list

INFINITY; false; currDist + 1; v

*Shortest Path Algorithm: UNWEIGHTED shortest-path: nonqueue-based* if starting at Vertex s: //set up the graph to start for each Vertex v { -->v.dist = ________; -->v.known = _______; } s.dist = 0; for(int currDist = 0; currDist < NUM_VERTICES; currDist++) -->for each Vertex v -->-->if(!v.known && v.dist == currDist) -->-->-->v.known = true; -->-->-->for each Vertex w adjacent to v -->-->-->-->if(w.dist == INFINITY) { -->-->-->-->-->w.dist = ___________; -->-->-->-->-->w.path = ______; }}}

O(|V|^2) ================================================== Set all vertex distances to infinity initially = O(|V|) outer for loop currDist < NUM_VERTICES : for each vertex, check if it is unknown and at current distance: O(|V|^2) O(|V| + |V|^2) = O(|V|^2)

*Shortest-path algorithm: nonqueue-based: UNWEIGHTED* What is the runtime for the UNWEIGHTED shortest-path problem NOT using a queue?

INFINITY; enqueue(s); v.dist + 1; enqueue

*Shortest-path algorithm: queue-based: UNWEIGHTED* Queue<Vertex> q = new Queue<Vertex>(); for each Vertex v --> v.dist = __________; s.dist = 0 q._____________; while(!q.isEmpty()) { -->Vertex v = q.dequeue(); -->for each Vertex w adjacent to v -->-->if(w.dist == INFINITY) { -->-->-->w.dist = _______; -->-->-->w.path = v; -->-->-->q.________(w); //enqueue all newly marked vertices }}}

weakly connected graph

*Graphs: directed graph* If you ignore the direction of the edges, this type of graph is connected (all vertices have a path to all other vertices)

strongly connected graph

*Graphs: directed graph* When considering the direction of the edges, this graph is connected too (all vertices have a path to all other vertices)!

O(|V|)

*Graphs: sparse graph* |E| = what in terms of |V|?

1

*Hash Table Probing Functions* For linear and quadratic probing, how many values can you put into each slot of the hash table?

load factor < 1/2 If load factor is pushed about 0.5, rehash!

*Hash Table Probing Functions* What must the load factor be to keep the average cost of insert/contains to O(1)

f(i) = i

*Hash Table Probing Functions* hi(x) = (hash(x) + f(i))% table size What is f(i) for linear probing?

f(i) = i^2

*Hash Table Probing Functions* hi(x) = (hash(x) + f(i))% table size What is f(i) for quadratic probing?

nothing

*Hash Table* During insert, if the item to be inserted is already present in the table, what do you do?

collision (you ultimately want to minimize collisions)

*Hash Table* This is when you have multiple different inputs with the same hash value

Fill to next probe spot

*Hash Table: Probing Functions* When you hit a slot in the hash table that is already filled, what do you do?

# elements / table size

*Hash table* How to calculate the load factor?

x just use the raw integer!

*Hash table* What is hash(x) if x is an int?

N (need to check through entire linkedlist)

*Hash table* What is the WORST case cost for insert/contains in a separate chaining hash table?

prime

*Hash table* the table size of a hash table is usually what type of number?

index 4: 4-->64 index 7: 57 use linkedlist!

*Hash table: separate chaining* What does the table look like? hash(57) hash(64) hash(4)

load factor

*Hash table: separate chaining* This is equivalent to the average size of each linked list

binary heap; deleteMin/Max

*Heap Sort* Put unsorted array into a ________ ________ (can cost O(N) or O(NlogN) depending on approach) To return sorted, keep calling __________ (NlogN) **A single deleteMin costs log(N) and you do this N times

take first element of unsorted portion and INSERT it into the sorted portion

*Insertion Sort* Describe how insertion sort works

own; heap; deleteMin

*Kruskal's Algorithm: minimum spanning tree* Each vertex is initially in its _______ set. As vertices become connected they are put in the same set as all other vertices in their connected tree A new edge is accepted as long as u and v are NOT in the same set --> union of set containing u and set containing v build a ________ in linear time with the edge costs. __________ calls give the edges to be tested in order

Rather than removing nodes, you have a boolean indicator called "isValid"

*Lazy Deletion: BST* What modification to a regular node must you add for lazy deletion to work?

dividing the array in half to sort until you get to the base case. For each recursion, merge the two sorted halves

*MergeSort* What is the general scheme of MergeSort?

index 1 (index 0 is left blank)

*Min-heap* Where is the minimum item in a min-heap array representation?

root

*Min-heap* Where is the minimum item in a min-heap tree representation?

end; <=; >

*Min-heap: insert* Must keep parent <= children Reserve a spot in the tree for the inserted value to follow completeness (@ ______ of array) and check if the insert would be valid if parent ____ x, it is valid if parent ____ x, swap the parent and new child node

acyclic; every

*Minimum spanning tree: undirected graph* The minimum spanning tree is a tree because it is ________ (tree = acyclic graph). It is spanning because it covers _______ vertex. It is minimum for the obvious reason LOL

yes

*P vs NP vs NP-complete* Are all P problems NP?

1

A balanced BST means that the heights of the L and R subtree at every level do not differ by more than _____

undirected

A binary heap is an example of a ___directed/undirected___ graph

complete! every level of the tree is entirely filled other than the last line which is filled L --> R

A binary heap must be: full? complete? perfect?

directed

A binary tree: parent --> child is an example of a ___directed/undirected___ graph

vertices; edges

A graph is a set of ______ and _______

Minimum spanning tree

A tree formed from graph edges that connects all the vertices of G at LOWEST total cost.

you have a ton of vertices but only a few edges!

Adjacency matrices waste a lot of space under what conditions?

0 children: remove node 1 child: node = node.child node.child = null; 2 children: node = node.(smallest node in right subtree) smallest node in right subtree = null

BST Remove: What are the three conditions of the node to be removed you must consider?

parent: i/2 left child: 2i right child: 2i + 1 Note: we see that the parent of the root = 1/2 which rounds down to 0 in java

Binary Heap: For node at index i of the binary heap array: what is the index of the parent? index of the left child? index of the right child?

yes

Can a parent be equivalent to its child in a min-heap or max-heap?

int hole = ++currentSize; //set up hole for(array[0] = x; x.compareTo(array[hole/2]) > 0; hole /= 2) --> array[hole] = array[hole/2]; array[hole] = x

Code for max-heap insert: public void insert (AnyType x) { //error checking if (currentSize == array.length -1) { ---> enlargeArray; } //your code here }

int hole = ++currentSize; //set up hole for(array[0] = x; x.compareTo(array[hole/2]) < 0; hole /= 2) --> array[hole] = array[hole/2]; array[hole] = x

Code for min-heap insert: public void insert (AnyType x) { //error checking if (currentSize == array.length -1) { ---> enlargeArray; } //your code here }

enqueue(v); counter++; enqueue(w);

Complete the code: topological sort using queue Queue<Vertex> q = new Queue<Vertex>(); int counter = 0; for each Vertex v --->if (v.indegree == 0) --->---> q.__________________ while (!q.isEmpty()) { ---> Vertex v = q.dequeue(); --->v.topNum = _________; //assign next number ---> for each Vertex w adjacent to v --->--->if(--w.indegree == 0) --->--->--->q._____________; } if(counter != NUM_VERTICES) --->throw new CycleFoundException();

table; linkedList

Hash Table: Separate chaining: Remove Go to the spot where the value should be in the hash ______ Iterate through the ___________ to see if it is there Remove if found

index 4: 44 index 5: 54 index 6: 65

Hash Table: linear probing example (table size = 10): insert (44) insert(54) insert(65)

lazy deletion You must use lazy deletion or else the contains() method will fail for elements that were probed in various indices

Hash Table: probing function: How do you perform a remove?

doubled; last

Heap Sort usually uses an extra array to store all of the deleteMins in order. This means that memory space is ________. Extra time is only O(N) which is negligible compared to the overall heapsort O(NlogN) To avoid this extra use of space, you can swap the first (minimum) and _______ elements in the UNSORTED heap in the array and then percolate down. The end result will be a backward sorted output (pg 279 of textbook)

O(log N)

Height of binary heap in tree representation is always _______

set it to null

How do you remove a leaf node?

Identical to Dijkstra's but rather than updating distance with overall path cost, you just update with the smallest adjacent cost from a given node -- do a few more practice problems on this on paper! Initial values all set to unknown and infinity distance

How does Prim's algorithm compare to Dijkstra's algorithm? graph --> minimum spanning tree

go right until you hit leaf

How to find largest value in BST?

go left until you hit leaf

How to find smallest value in BST?

yes

If an edge is represented as a pair of ORDERED vertices, is the edge directed?

no

If an edge is represented as a pair of UNORDERED vertices, is the edge directed?

replace data value with the smallest node in the right subtree (or largest node in the left subtree)

If the node to be removed in a BST has 2 children, what do you do?

replace the node to be removed with its child

If the node to be removed in a BST only has 1 child, what do you do?

minimum spanning tree

If you are trying to wire a house with a minimum of cable, what type of problem is this?

cycle

If you haven't visited all vertices, BUT there are no vertices with indegree zero, then the topological sort can't be solved because there is a ________ in the graph

9 | 2 5 7 2 9 | 5 7 2 5 9 | 7 2 5 7 9 |

Insertion Sort practice: 9 2 5 7

yes

Is Kruskal's algorithm a greedy algorithm?

yes

Is Prim's algorithm (forming a minimum spanning tree) a greedy algorithm?

NO! But it is a type of binary tree

Is a binary heap necessarily a BST?

yes |E| = O(|V|) -- linear nature of graph

Is an adjacency list good to represent a sparse graph?

no |E| = O(|V|)

Is an adjacency matrix good to represent a sparse graph?

yes (because of how AVL trees work by definition)

Is the height of an AVL always log N?

no

Is there only one minimum spanning tree per graph? Is the minimum spanning tree of a graph necessarily unique?

1

Shortest path of unweighted graph is the same as doing Dijkstra's but each edge is weighted = ______

lazy deletion

Standard deletion cannot be performed in a probing hash table because the cell might have caused a collision to go past it. Thus, probing hash tables require what type of deletion?

lower

The cost of the spanning tree is lowered if a n edge is replaced with a _______ cost edge

root node

The height of the tree is equivalent to the height of what?

NP-hard

The optimization version ("shortest path") of the traveling salesperson problem CANNOT be verified in polynomial time because you have *nothing to compare the solution to* for an optimal solution. This is called "________". If we manage to solve this, it can be used to solve all NP-complete and NP problems!

|E|

The sum of the size of all adjacency lists in a graph of all vertices =

bottom -- work up from the bottom If inValid, you can ADJUST the tree in constant time to make a valid AVL

To check the validity of an *AVL tree*, you start checking where?

all; |V| - 1

To create a minimum spanning tree from a given graph: _______ vertices of the given graph must be in the spanning tree # edges is equivalent to _________ in terms of |V|

heights

To implement an AVL tree, you must know the _______ of the left and right subtrees

Mergesort: O(NlogN) Quicksort: O(N^2)

What are the 2 "divide and conquer" sorting algorithms and what are their WORST case costs?

1) binary search tree 2) all nodes satisfy the AVL condition: diff in height of L and R subtree of every node is <= 1

What are the two requirements of an AVL tree?

table size is prime load factor < 0.5

What are the two rules of a quadratic probing hash table?

Prim's and Kruskal's

What are two algorithms for minimum spanning tree?

insert; deleteMin

What are two methods of a *priority* queue?

infinity

What cost should you put in to represent a nonexistent edge of a weighted graph in an adjacency matrix?

You stop the marker (i/j) Ex: if you have an array of all equal elements, you swap at every position until i and j cross. This eliminates the risk of wildly uneven subarrays where the pivot is replaced at the beginning/end of the array.

What do you do with elements that are even to the pivot in quicksort?

DAG

What is a requirement of the graph to be topologically sorted?

O(NlogN) using buildHeap: N + NlogN = O(NlogN) inserting one at a time into empty array: NlogN + NlogN = O(NlogN)

What is the Big O cost of heap sort?

O(NlogN)

What is the average case cost for quicksort?

O(N^2)

What is the average case cost of insertion sort?

O(NlogN)

What is the best case cost of quicksort?

O(log N) if a balanced BST

What is the best case for an insert in BST?

sparse: O(|V| log |V|): use priority queues dense: O(|V|^2): without priority queues **same costs as Dijkstra's algorithm

What is the best run time cost of Prim's algorithm for dense/sparse graphs? What is the implementation like?

O(log N)

What is the big O runtime for a balanced BST?

O(log N)

What is the cost of all AVL operations?

O(N) The sum of the heights of the nodes is O(N) = maximum number of swaps to sort -- don't need to know proof

What is the cost of buildHeap and why?

nonqueue: For each numerical distance away from start, iterate through all vertices to check if they are unknown and at the distance away from s being currently analyzed in the pass (sequential scanning): O(|V|^2) queue: store all visited vertices. Each vertex goes in and out of queue only once: O(|V| + |E|)

What is the difference between the queue and nonqueue based UNWEIGHTED shortest-path algorithm representations?

hash map

What is the equivalent of a dictionary in Python? (key, value) pairs

Parent node is >= its children

What is the heap order condition for a max-heap?

Parent node is <= its children

What is the heap order condition for a min-heap?

O(log N)

What is the height of an AVL tree in BigO?

left is smaller than root, right is larger than root

What is the identifying characteristic of a BST?

buildHeap operation: O(N) **compare to inserting elements 1 at a time: O(NlogN)

What is the most efficient way to build a binary heap from N elements?

N-1

What is the number of edges of a path of N vertices

O(N)

What is the runtime of rehashing a hash table?

quadratic probing

What is the solution to avoid primary clustering in linear probing hash tables?

O(|V|^2)

What is the space complexity of an adjacency matrix?

O(|E| + |V|)

What is the space cost of an adjacency list (general)?

O(|V|^2) O(|E| + |V|) = O(|V|^2)

What is the space cost of an adjacency list of a dense graph?

O(|V|) O(|E| + |V|) = O(|V|)

What is the space cost of an adjacency list of a sparse graph?

O(|E| log |E|) Use a priority queue based on edge costs: deleteMin: O(log |E|) worse case, you will have to go through all edges

What is the worse-case running time of Kruskal's algorithm? **assume isCycle and union occur in constant time

load factor <= 1

What must the load factor be to have a guaranteed average cost of O(1) for contains/insert in a separate chaining hash table?

topological sorting

What sorting implementation did we observe for organizing the prerequisites for courses?

priority queue deleteMin removes the unknown minimum vertex (never unknown again and is removed from further consideration)

What type of queue is used for the queue-based representation of Dijkstra's algorithm?

BST

What type of tree prints a SORTED list with an inorder traversal?

load factor exceeds 1

When do you rehash a separate chaining hash table?

sparse graph: O(|V|) dense graph: O(|V|^2) In either case, there space cost is no worse than an adjacency matrix, but if you have a sparse graph you do even better! So this is generally a better choice over an adjacency matrix because the matrix is always O(|V|^2) regardless of dense/sparse

Why is an adjacency list (LinkedList) better than an adjacency matrix?

Anagrams hash to the same value (does not take position of characters into account) = lots of collisions!

Why is this a bad hash function? Assigning hash value according to the overall sum of ASCII values of each char for a given key

although position is taken into account, words with the same first three letters will result in collisions ex: premade, prefix, pregame

Why is this a bad hash function? return (key.charAt(0) + 27*key.charAt(1) + 729*key.charAt(2))% TS

imbalanced (you have to do a rotation to adjust!)

Worst case insert into AVL tree results in an ___________ tree

NlogN

Worst case of any general purpose sorting is ALWAYS >= _________

Node k2 = k1.left; k1.left = k2.right; k2.right = k1; return k2; //new node

Write code for Case 1 AVL rotation that returns new node: Node rotateLeft(Node k1) { //your code here }

Node k2 = k1.right; k1.right = k2.left; k2.left = k1; return k2; //new node

Write code for Case 4 AVL rotation that returns new node: Node rotateRight(Node k1) { //your code here }

//base case if (root == null || root.data == key) { return root; } //key > root if (key > root.data) { return search(root.right, key); } return search(root.left, key);

Write code to insert an element into a BST: public Node search(Node root, int key) { //your code here }

midpoint; left; root

buildHeap operation: for a minHeap 1) find ________ of array (rightmost leaf in second to last row) 2) percolate down the midpoint 3) go one index ______ in the array and percolate that node down 4) when you reach the _______, the entire tree is a valid minHeap

yes ex: free flight

can cost of an edge be 0?

yes

can the weight/cost of an edge be negative in a weighted graph?

no

can there be duplicates in binary search trees (BST)?

N (you have to search for a potential duplicate in the linkedlist at the index)

cost of *insert* into separate chaining hash table using

O(log N)

cost of deleteMax() of max-heap

O(log N)

cost of deleteMin() of min-heap

O(1)

cost of get min for minheap

O(log N) -- the farthest an insert can be pushed down is the height of the tree

cost of min/maxHeap insert

number of edges to the root node

define DEPTH in the context of trees

length of longest path from given node to a leaf node

define HEIGHT in the context of trees

no (hash tables are sets)

do hash tables contain duplicates?

undirected (there are 2 independent directed edges to SIMULATE an undirected graph)

doubly linked lists are simulations of a ____directed/undirected___ graphs

percolate up

for a minHeap, if there was an decreaseKey (increase priority) operation, what would the change in the array representation look like?

percolate down

for a minHeap, if there was an increaseKey (decrease priority) operation, what would the change in the array representation look like?

small (deleteMin comes out first)

highest priority elements in a PQ have a __small/large___ priority value?

indegree

how many directed edges (arcs) are going into a node

yes

is Dijkstra's a greedy algorithm?

yes |E| = O(|V|^2)

is an adjacency matrix good to represent a dense graph?

queue! O(|V| + |E|) sparse: O(|V|) dense: O(|V|^2) never worse than the array representation, but even better for sparse graphs!

is it better to use a queue or array for topological sort

no

is this a valid AVL tree?

yes

is this a valid AVL tree?

return array[1];

minHeap code example: findMin() { //single line of code }

root; hole; last; smallest

minHeap: deleteMin() 1) store _______ value in a tmp variable. Put a ______ in the array/tree at the root 2) Put rightmost bottom leaf back into the hole (______ element in array), size-- 3) Swap parent with ________ child if parent > smallest child (continue swaps down the tree) **edge case: if there is only 1 child and the child < parent, swap! 4) return tmp if asked for

directed

singly linked lists are examples of ___directed/undirected____ graphs

priority queue

the hospital waiting room is similar to what data structure?

|V| - 1

the number of edges in the minimum spanning tree is what in terms of |V|?

max heap

this is an example of what type of binary heap?

min heap

this is an example of what type of binary heap?

vertex; known; distance; previous/path

what are the 4 parameters in the table for shortest path/ Dijkstra's algorithm?

directed acyclic (directed acyclic graph)

what are the two requirements of a DAG

worse: O(N^2) best: O(N) -- 1 comparison each

what are the worst case and best case costs of insertion sort

Worst: O(N^2) Best: O(N^2) N + (N-1) + (N-2) + ... + 1 = O(N^2)

what are the worst case and best case costs of selection sort

data boolean for isValid (lazy deletion)

what does each hash node in a hash table (probing function) contain?

arc

what is another name for an edge

sparse: queue: O(|V| log |V|) dense: nonqueue: O(|V|^2)

what is the best way to implement Dijkstra's algorithm for SPARSE graph? for DENSE graph?

for both, use queue

what is the best way to implement single-source UNWEIGHTED shortest path in a SPARSE graph? In a DENSE graph?

For both, use queue

what is the best way to implement topological sorting for SPARSE graph? for DENSE graph?

O(|V|)

what is the cost of findNewVertexOfIndegreeZero (used for topological sort to find the next unvisited vertex of indegree zero) if scanning through an array?

O(NlogN) one insert costs log(N) and you are doing this N times

what is the cost of inserting N elements ONE AT A TIME into an initially empty heap?

Sparse/Dense: O(|V|^2) **Overall algorithm is O(|V|^2 + |E|) For each vertex, we run O(|V|) of findNewVertexOfIndegreeZero to start the scan through = O(|V|^2) All adjacent vertices are decremented by 1 at some point in the traversal = O(|E|)

what is the runtime for topological sort if scanning through an array? Sparse vs dense graph

directed acyclic graph

what type of graph is this an example of?

inorder

what type of tree traversal (preorder/inorder/postorder) must you perform on a BST to get a SORTED list?

O(N)

worst case run time for insert in BST

O(N)

worst case run time for search in BST

O(|V|^2)

*Graphs: dense graph* |E| = what in terms of |V|?

int leftEnd = rightPos - 1; int tmpPos = leftPos; int i = leftPos; int j = rightPos; while (i <= leftEnd && j <= rightEnd) { -->if(a[i].compareTo(a[j]) <= 0) -->-->tmpArray[tmpPos++] = a[i++]; -->else -->-->tmpArray[tmpPos++] = a[j++]; while(i <= leftEnd) //copy rest of left -->tmpArray[tmpPos++] = a[i++]; while(j <= rightEnd) //copy rest of right -->tmpArray[tmpPos++] = a[j++]; //copy tmpArray back in for (int k = leftPos; k <= rightEnd; k ++) --> a[k] = tmpArray[k]; }

**PAY ATTN -- CODING PROBLEM** Code the merge algorithm of mergesort: AnyType[] tmpArray = (AnyType[]) new Comparable[a.length]; void merge(AnyType[] a, AnyType[] tmpArray, int leftPos, int rightPos, int rightEnd) { //your code here }

smaller; increment

**PAY ATTN THIS IS A CODING PROBLEM** Merge algorithm of mergeSort: on arrays a1 and a2 i = a1[0]; j = a2[0]; compare i to j and pick _________ of the 2 to add to output array _________ i or j depending on which one you just used

Case 4: single rotation swap 1 and 2 so 2 is root with 1 and 3 as L/R children respectively

*AVL modification* insert 1 insert 2 insert 3 How do you balance this tree?

Case 3: double rotation Swap 2 and 3 Case 4 rotation: 2 in root position with 1 and 3 as L/R children respectively

*AVL modification* insert 1 insert 3 insert 2 How do you balance this tree?

Case 2: double rotation Swap 1 and 2 Case 1 rotation to have 2 in root position with 1 and 3 as L/R children respectively

*AVL modification* insert 3 insert 1 insert 2 How do you balance this tree?

Case 1: single rotaiton put 2 in the root position with 1 and 3 as L/R children respectively

*AVL modification* insert 3 insert 2 insert 1 How do you balance this tree?

binary search tree; 1

*AVL tree* 1) what type of tree? 2) all nodes satisfy the AVL condition: diff in height of L and R subtree of every node is <= _____

-1

*AVL tree* what is the height of an empty subtree? (no node whatsoever)

if min is invalid, you must back track through the tree to find the next valid minimum

*BST with lazy deletion* How does findMin work?

Search for element: if found, set isValid = true (this will work if isValid is already true or false) if not found, add to existing nodes like normal (if the node to add to is invalid, it becomes a bit more tricky because you have to look at previous nodes up tree)

*BST with lazy deletion* How does insert work?

search for element and check if isValid

*BST with lazy deletion* How does the contains method work?

connected graph

*Graphs* A graph where every vertex has a path to every other vertex Undirected graph

simple path

*Graphs* All vertices along the path are DISTINCT : no revisits **only exception: very last vertex can be the starting vertex to form a simple cycle

undirected; directed

*Graphs* Facebook is an example of a ___directed/undirected___ graph Instagram is an example of a ___directed/undirected___ graph

no

*Graphs* For a cycle, must the revisits to vertices necessarily be the endpoints of the path?

UNDIRECTED

*Graphs* Is a connected graph directed or undirected?

sparse graph

*Graphs* Number of edges is small relative to number of vertices

|V| and |E| **V is set of vertices, E is set of edges

*Graphs* The big-O cost is dependent on what two things?

path

*Graphs* The list of vertices that you traverse to get to a destination ex: v1, v3, v4

cycle

*Graphs* contains revisits to vertices (that are not necessarily the endpoints of the path)

length

*Graphs* number of edges traversed

directed (given direction of streets -- bonus: can be weighted by traffic!)

*Graphs* street maps are examples of ___directed/undirected___ graphs

cost

*Graphs* sum of the weights of the edges traversed

acyclic graph

*Graphs* A graph that contains NO cycles

cyclic graph

*Graphs* A graph that contains cycle(s)

loop

*Graphs* A single edge that starts and ends at the SAME vertex

O(|V| + |E|) ========================= Set v.dist to infinity for all vertices initially: O(|V|) Dequeue while q is not empty and adjust and enqueue all adjacent vertices: O(|E|)

*Shortest-path algorithm: queue-based: UNWEIGHTED* What is the runtime for the UNWEIGHTED shortest-path problem using a queue?

sparse/dense: O(|V|^2) each scan is O(|V|) and you do this for each vertex. There is at most one update per edge for a total of O(|E|) --> runtime = O(|E| + |V|^2) = O(|V|^2)

*Shortest-path algorithm: single source shortest path: Dijkstra's algorithm: NONQUEUE-based algorithm* What is the cost of Dijkstra's algorithm if you sequentially scan through the vertices to find teh minimum dv? dense vs sparse

dense: O(|V|^2 log |V|) sparse: O(|V| log |V|) O(|E| log |V|)

*Shortest-path algorithm: single source shortest path: Dijkstra's algorithm: QUEUE-based algorithm* What is the runtime for the weighted shortest-path problem if we assume there are no negative edges? dense vs sparse?

deleteMin from priority queue: O(log |V|) is performed for each vertex --> O(|V| log |V|) cost to update each edge cost is O(|E| log |V|) Total cost: O(|E| log |V|)

*Shortest-path algorithm: single source shortest path: Dijkstra's algorithm: QUEUE-based algorithm* why is the cost of Dijkstra's algorithm using a priority queue O(|E| log |V|)?

indegree; topNum (position in topological sort order), adjacency list (we iterate across this)

*Topological sort* What three variables must you have in the vertex class?

once (because of use of queue. Once it is dequeued it is never used again)

*Topological sort: Queue* The queue operations are done at most how many times per vertex?

0; not empty; 0

*Topological sort: Queue* To start, all vertices of indegree _____ are placed on an initially empty queue While the queue is _____________, a vertex is removed and all adjacent vertices' indegrees are decremented Vertex is put on the queue as soon as its indegree falls to ____

Array finds a new (unvisited) vertex of indegree zero for each pass. Adjacency list vertices are decremented. This repeats --> Overall O(|V|^2) Queue initially finds all indegree = 0 values and puts them on a queue. As vertices become indegree = 0, they are put on the queue. The queue is dequeued for each new pass --> Overall O(|V| + |E|)

*Topological sort: array vs queue based* How do the array and queue based algorithms differ?

Kruskal's

*_________ Algorithm: minimum spanning tree* This selects the edges in order of smallest weight and accepts an edge if it does not cause a cycle

load factor

*hash table* What is the average cost for insert/contains in a separate chaining hash table?

equally likely to remove either of them

*priority queue: deleteMin* if 2 items have the SAME priority, which is removed first?

true; false

*ternary operator* variable x = (----expression---) ? value if ______: value if ______

71

*ternary operator* what does x = 2 return? (x == 2) ? 71:85

85

*ternary operator* what does x = 5 return? (x == 2) ? 71:85

0; adjacent; 1

Describe the general approach for *topological sort* Start with node that has indegree = ______ Locate all _________ nodes that your start points to and decrement their indegrees by _____ Find the next 0 indegree continue until you've gone through the ENTIRE graph (this may require you to start a path from node 0 of a previous step)

no! it could fail if you have a negative cost But yes, it always works if the cost is nonnegative

Does Dijkstra's algorithm always work?

dividing the array in half recursively: log N For each of the logN times, you do linear merge = O(NlogN)

Explain the cost of mergeSort

only at leaf nodes

For BST, where can you insert?

no!

For Dijkstra's and Prim's algorithms: Once a vertex is KNOWN, can its distance value be updated in the future?

rightmost leaf

For a binary heap: The last element in the array is in what location in the tree representation?

indegree = 0

For a topological sort using a queue, you only put vertices on the queue if they fit what condition?

cycle

For any spanning tree T, if an edge e that is not in T is added, a _______ is created The subsequent removal of any edge on this _-_-_-_ reinstates the spanning tree property

comparable

For general purpose sorting, items must be _________

AnyType maxItem = array[1]; array[1] = array[currentSize--]; //percolate down @ 1 int child; AnyType tmp = array[1]; for(int hole = 1; hole*2 <= currentSize; hole = child) { -->child = hole*2; if(child != currentSize && array[child + 1].compareTo(array[child]) > 0) //if child is currentSize, we know we only have one child ---> child++; if(array[child].compareTo(tmp) > 0) ---> array[hole] = array[child]; else ---> break; //hole is in right place } array[hole] = tmp; return maxItem;

For maxHeap: public AnyType deleteMax() { //error checking if(isEmpty()) -->throw new UnderflowException(); //your code here }

AnyType minItem = array[1]; array[1] = array[currentSize--]; //percolate down @ 1 int child; AnyType tmp = array[1]; for(int hole = 1; hole*2 <= currentSize; hole = child) { -->child = hole*2; if(child != currentSize && array[child + 1].compareTo(array[child]) < 0) //if child is currentSize, we know we only have one child ---> child++; if(array[child].compareTo(tmp) < 0) ---> array[hole] = array[child]; else ---> break; //hole is in right place } array[hole] = tmp; return minItem;

For minHeap public AnyType deleteMin() { //error checking if(isEmpty()) -->throw new UnderflowException(); //your code here }

completeness (always do this first and then make adjustments accordingly)

For minHeap deleteMin() Fill the hole at the root so as to: A) maintain ___________ B) make adjustments to make parent <= children

prime number (or you may end up with cycles where an insert can't happen even if there are free spaces in the table)

For quadratic probing to work, the table size MUST be what?

Yes -- it accounts for teh relative position of each char -- so anagrams wont be an issue, prefixes wont be an issue, etc.

Is this a good hash function for a String key? Why? int hashVal = 0; for (int i = 0; i < key.length(); i++) --> hashVal = 37*hashVal + key.charAt(i); hashVal %= tableSize;

NO! Because all previous multiprobed items are not being accounted for You can only overwrite a lazy deleted spot if you are adding the same item that is already there (changing isValid)

Linear and quadratic probed hash tables use lazy deletions for removal. Can you overwrite a lazy deleted spot?

probing; empty; isValid

Linear and quadratic probed hash tables use lazy deletions for removal. For the contains() method, keep _________ until the item is found or you hit an _______ spot When the element is found, you must check if _______ too!

load factor includes lazy deletions. The lazy deleted items are removed only upon rehashing

Linear and quadratic probed hash tables use lazy deletions for removal. What happens to the load factor with these lazy deletions?

Primary clustering (happens with linear probing)

Many elements hashing to the same hash location (big chunks of data in the table) Big O suffers!

N-1 every comparison adds an element to merged array except the last comparison which adds 2 elements to the merged array

Merging two sorted lists requires at most how many comparisons?

array

Min/max heaps are typically represented as what data structure type?

yes!

Must a minimum spanning tree be connected in order to be a minimum spanning tree?

bet good job you got this!

Notice the differences

yark! ESKGETTIT

Notice the differences

solve; verify; NP-complete; NP

P problems are quick to _______ NP problems are quick to _______ but slow to solve NP-complete problems are also quick to verify, slow to solve and can be reduced to any other _____________ problem NP-hard problems are slow to verify, slow to solve and can be reduced to any other ______ problem

Case 2 double rotation at 20 Case 1 single rotation at 15 Case 1 single rotation at 5

Perform insert: 20, 10, 15 --> 5, 12, 1 --> 0

1 | 3 9 7 5 1 3 | 9 7 5 1 3 5 | 7 9 1 3 5 7 | 9 1 3 5 7 9 |

Practice selection sort: 9 3 1 7 5

index 4: 44 index 5: 54 index 8: 64 index 3: 4

Quadratic probing hash table example (table size 10 for example) Insert 44 Insert 54 Insert 64 Insert 4

weighted

_____weighted/unweighted___ graph: each edge has a weight/cost attached to it

unweighted

_____weighted/unweighted___ graph: every edge is considered the same

maxHeap

a complete binary tree in which the value in each internal node is larger than or equal to the values in the children of that node

minHeap

a complete binary tree in which the value in each internal node is smaller than or equal to the values in the children of that node

complete graph

a graph in which there is an edge between EVERY pair of vertices

priority queue

airplane boarding is similar to what data structure?

greedy algorithm

an algorithm that always tries the solution path that appears to be the best In the context of Dijkstra's, this algorithm chooses the vertex that appears to be best (which can cause you to run into issues if there is deceiving negative cost!)

vertices (v, w)

an edge can be written as a pair of __________

digraphs

another word for directed graphs

NO P is a subset of NP

are all NP problems P?


Set pelajaran terkait

Chapter 14 Clinical Presentation and Management of the Cardiac Patient

View Set

Chapter 6 recordation, abstracts, and title insurance Quiz

View Set

Unit 3: How do our neurons communicate with each other?

View Set

Glosario de términos -Redes de Computación

View Set

med surg my evolve practice questions chapter 21

View Set

Ch. 31: Pediatric Emergencies (Orange book & online)

View Set

EVERFI: Financial Literacy for High School

View Set