CS 2420 Final Exam Review!

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Huffman's Algorithm

1. Compute the frequency of each character in the original file. 2. Construct the Huffman tree (binary trie). It takes N - 2 steps. A tiebreaker is needed for characters with the same frequency. 3. Write the encoding info (characters and frequencies) to the beginning of the compressed file, so the same tree can be reconstructed for decompression. 4. For each character in the original file, write its encoding to the compressed file.

Counting Sort Algorithm (Maybe unnecessary)

1. Create an integer array C of size R and set all C[i] to 0. 2. For each key in the original array A, update the appropriate counter in C (i.e. C[ key(A[i]) - min]). 3. Accumulate the counts in C. i.e. Add the value at C[i - 1] to the value at C[i]. 4. Fill in a new sorted array S, backwards. Each A[i] is copied to S[C[ key(A[i]) - min ]]. To ensure duplicate keys get their own cell, decrement C[key(A[i]) - min] before each copy.

Insertion Sort

1. The first array item is the sorted portion 2. Take the second item and insert it in the sorted portion 3. Repeat steps 1 and 2 to sort the rest of the array Best case: O(N). Average, worst case: O(N^2) * * Big-O behavior is O(N + I), where I is the number of inversions. Useful for when the list is small and when the list is close to being sorted. A stable sort.

Type bounds

<? extends Class>: Allows a type that is a child of Class <? super Class>: Allows a type that is a parent of Class

Why is a BST's balance important for its runtime?

A BST works by comparing the element to be inserted/found/removed with the root/subroot of the tree/subtree. When the BST is balanced, half of the elements of the tree are removed from consideration, and with this repeated halving principle, the optimal runtime of O(logN) can be achieved. However, if the tree is unbalanced (left or right heavy), then the traversal works more like a linked list, which causes a runtime of O(N) instead.

AVL Tree

A balanced tree where the height difference between the left and right subtree of any node is less than or equal to 1.

How to create a binary max heap using a binary min heap

A binary min heap can work as a binary max heap if a Comparator that compares an element with the reverse natural ordering of that element is used. The opposite is also true.

Binary Heap Structure

A binary tree that has is balanced (complete structure) with an order property (min, max, min/max, max/min). For example, a binary min heap is a tree where the root/subroot is the minimum of that tree/subtree. Uses an array to store elements rather than nodes.

Binary Trie

A binary trie in which a left branch represents a 0 and a right branch represents a 1. The path to a character represents its encoding.

Bucket Sort

A category of non-comparison based sorts. Requires a list that is or can reduce to ints or Strings. 1. Set up an array of initially empty buckets. 2. Iterate through the array, placing each item in its bucket. O(1) 3. Sort each non-empty bucket. 4. Visit the buckets in order and put item in the sorted array.

Wrapper class

A class designed to contain a primitive data type so that the primitive type can behave like a reference type. Ex: Integer for int

Array Basic Structure

A collection of items in which each item can be accessed by using a unique index. Elements are stored contiguously in memory.

Linked List Basic Structure

A collection of nodes arranged so that each node contains a link to another node in the sequence. Nodes are stored noncontiguously in memory.

Cyclic vs acyclic graphs

A cyclic graph contains a node/nodes that has a traversal that leads back to same node/nodes. An acyclic graph does not.

Reference type

A data type that is stored on the heap, but a reference to that heap location is stored on the stack. When this data is passed into a method, a copy of the reference to that data is passed into the method. The method that was called can change the value stored.

Primitive type

A data type that is stored on the stack. When this data is passed into a method, a copy of the data is passed into the method. The method that was called cannot change the value stored.

Circularly Linked List Basic Structure

A doubly linked list where the first and last items hold references to each other. Same Big-O behavior as a doubly linked list.

Sparse Graph

A graph where |E| << |V|^2 Most common type of graph. Uses a list of adjacency lists to store connections at each vertex.

Dense Graph

A graph where |E| ≈ |V|^2. Uncommon type of graph. Uses an adjacency matrix to store the connections between the vertices.

What must happen when adding an item into a hash table?

A hash code is produced by the key's hashCode() function. The absolute value of the hash code is then clamped (%) to the size of the hash table, and the item's placement is attempted there.

Method Overriding

A method of the same name and same parameters as a method in a parent class. This effectively replaces the parent method.

Method Overloading

A method of the same name but different parameters as a method in a parent class.

Recursive method

A method that calls itself, working toward a base case. The method can be split into two parts: the base case (which ends the recursive call), and the recursive step.

What must happen when a hash table must be rehashed?

A new table with a larger size is created. Because the index of an item is dependent on the size of the hash table, all of the items must be added back into the hash table with the hash code clamped to the new table size.

Leaf of a Tree

A node with no children

For a hash table using quadratic probing, what must the length of the backing array be?

A prime number

What should be done when using a recursive method to avoid redundancy and streamline code?

A public driver helper method should be created, which calls the private recursive method. The driver method helps reduce the number of parameters that a user must populate. Any checks that only need to be done once (such as throws) should be in the driver method.

Terminal symbol

A symbol in a grammar that is a part of the language. Ex. <start> => a a is a terminal

Nonterminal symbol

A symbol in a grammar that names a phrase. Ex. <start> => a <start> is a nonterminal

Why does topological sort not work with cyclic graphs?

A topological sort will fail because there will be a node/nodes that will not be considered in the topological sort. This is due to the indegree of such nodes not reaching 0 and being put into the FIFO queue.

What must happen when adding a node into a BST?

A traversal starts at the root of the tree. The traversal goes to the left child of the current if the new node is smaller than the current node, and the right child if it is more. If that child does not exist for the node being accessed, then the traversal stops, and the node is assigned to that node's correct child.

How to measure the number of inversions in a given list

Access the first element and compare it to the elements right of it. If the current element you are checking is "out of place" with the element you are accessing from the right, add one to the number of inversions. After the first element has been fully accessed, access the second element. Repeat this until the entire list has been accessed.

Queue Big-O Behavior of: Add Find Remove findMin

Add - O(1) Get - O(1) Remove - O(1) findMin - O(N)

Stack Big-O Behavior of: Add Find Remove findMin

Add - O(1) Get - O(1) Remove - O(1) findMin - O(N)

Priority Queue via Binary Max Heap Big-O of: Add extractMax Peek buildHeap

Add - O(1) Worst case: O(logN) extractMax - O(logN) Peek - O(1) buildHeap - O(N)

Hash Table with Linear/Quadratic Probing Big-O Behavior for: Add Get Remove findMin

Add - O(1) when 𝛌 < 0.5 else O(N) Get - O(1) when 𝛌 < 0.5 else O(N) Remove - O(1) when 𝛌 < 0.5 else O(N) findMin - O(Size of the backing array)

Hash Table with Separate Chaining Big-O Behavior for: Add Get Remove findMin

Add - O(1) when 𝛌 is not very high (max 10) Get - O(1) when 𝛌 is not very high (max 10) Remove - O(1) when 𝛌 is not very high (max 10) findMin - O(N + size of the backing array) (I believe)

Singly Linked List Big-O Behavior of: Add Get Remove findMin

Add - O(N). O(1) at beginning Get - O(N). O(1) at beginning Remove - O(N). O(1) at beginning findMin - O(N)

Doubly Linked List Big-O Behavior of: Add Get Remove findMin

Add - O(N). O(1) at beginning and end Get - O(N). O(1) at beginning and end Remove - O(N). O(1) at beginning and end findMin - O(N)

Heapsort

Add each item into a binary heap without regard to order. Apply buildHeap(). Call deleteMin() N times. We can save memory by using the cell that was last in the heap to store the item just deleted. Elements are in reverse order of what the binary heap is ordered (binary min heap's heapsort => elements sorted from max to min) O(NlogN), but it is slower than quicksort and merge sort To find the k smallest items: O(N + k*logN)

Abstract Class vs Interface

An abstract class is a class that cannot be implemented directly, but it can contain normal methods and abstract methods (methods that are not fully implemented). To use an abstract class, one must use the "extends" keyword. An interface can have only abstract methods, and all methods and variables must be public, and all variables must be final. To use an interface, one must use the "implements" keyword.

Abstract Data Type v Data Structure

An abstract data type is defined from the user's perspective. It affects possible values, possible operations, and the behavior of these operations. e.g. Priority Queue, List, FIFO Queue, Stack, etc. A data structure is data organization, management, and storage format that enables access and modification. e.g. Basic array, Linked list, Hash table, Tree, Heap, etc.

Lambda expression usage and syntax

An anonymous function that is used as shorthand to create a basic Comparator ex (o1, o2) -> o2 - o1

Header Node in a Linked List

An empty fake node at the beginning of the list that helps with edge cases like inserting and removing the first element. Also ensures every node has a previous node.

Define functor / function object

An object representing a single, generic function

How to construct a Binary Trie

Associate each character to be encoded with its frequency (its weight). Create a single node for each character, each a separate tree. Repeatedly merge trees until we get one final tree. To merge trees, create a new node with two existing nodes as its left and right children. The new tree's weight is the sum of its children's weights. (See Week 13 for a visualization.)

Autoboxing and unboxing

Automatic conversion between reference and primitive types.

Why are BSTs not typically considered for sorting?

Binary search trees cannot contain more than one of the same item.

Queue Basic Structure

Can only access least recently added element: first in first out (FIFO)

Stack Basic Structure

Can only access most recently added element; last in first out (LIFO)

Quicksort

Choose an item from the array as a pivot. Partition the array so that all items less than the pivot are to the left and items greater than the pivot are to the right. Recursively sort these partitions. Best and average case: O(NlogN) Worst case: O(N^2) - Using a bad pivot (a max or min) Useful for when a backing array cannot be used and the runtime must be fast.

Finding the runtime of consecutive loops vs nested loops

Consecutive: add the runtimes together. The dominant term is the Big-O behavior. O(N^2 + N) => O(N^2) Nested: multiply the runtimes together. O(N * N) => O(N^2)

Graph Basic Structure

Consists of a set of vertices and a set of edges that connect the vertices. Can be weighted or unweighted, and directed or undirected.

Finding the Maximum Contiguous Subsequence Sum (MCSS)

Create a subarray of an integer array such that when all of the integers of the subarray are added, it is the largest sum of the array.

Directed vs undirected graphs

Directed graphs are ordered. With an edge, one vertex is the origin and another vertex is the destination. This is denoted by an arrow indicating the direction. Undirected graphs are unordered and traversals can go either direction.

Radix Sort

Each bucket corresponds to the rightmost digit of the key. Repeated for each digit until reaching the leftmost digit. O(kn) where k is the number of digits 1. Set up an array of ten initially empty buckets. 2. Iterate through the original array, putting each item in its bucket. 3. Visit the buckets in order and put items back into the original array. 4. Repeat 1-3 for every other digit in the keys. A stable sort.

Doubly Linked List Basic Structure

Each node holds data and a reference to the next and previous node.

Singly Linked List Basic Structure

Each node holds data and a reference to the next node.

Complete Binary Tree

Every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible.

Min-Max Heap Order

Every node in at even depth is smaller than its two children. Every node at odd depth is larger than its two children.

Min/Max Heap Order

Every node is the minimum/maximum of its subtree.

Why can .toString() and .equals() be called on any object in Java?

Every object, Java's or user-created, IS-A Object, which contains .toString() and .equals() methods.

Comparator interface

Ex: cmp.compare(obj1, obj2) Allows for multiple and different comparisons for ordering of sorting. Uses a functor (function object)

Comparable and its interface

Ex: obj1.compareTo(obj2) Compares by using the natural ordering of sorting of Java classes. Returns: negative = smaller, 0 = equals, positive = larger The interface allows one .compareTo() method that defines the natural ordering of sorting of user-created classes.

T/F Huffman's algorithm can produce more than one tree from the same data

False

Determining the amount of space used in an encoded file that used Huffman's algorithm

Find the amount of space used for the header: 5 bytes * the number of unique characters Find the amount of space used for the encoded characters in the Huffman tree: total number of digits used / 8 The sum of these two is the file size.

Dijkstra's Algorithm

Finds the SHORTEST PATH from the source to each node on a WEIGHTED or unweighted graph. Uses a priority queue. O(|V| + |E| log|V|)

Selection Sort

Finds the smallest then next smallest item from the original list and adds it to the sorted list. O(N^2) Uses the same amount of steps regardless if the array is close to being sorted Useful for when the number of swaps must be kept to a minimum.

Shellsort

First, compare array items far apart, then less far apart, shrinking toward a basic insertion sort using gap sequences. Average case: O(N^(5/4)), Worst case: (O(N^(3/2)) The runtime is dependent on the gap sizes used. Simplest sub-quadratic sorting algorithm.

What is stability in a sorting algorithm?

If two objects with equal keys appear in the same order in sorted output as they appear in the input array to be sorted. Informally, stability means that equivalent elements retain their relative positions, after sorting. For example, (3, 2b, 1, 2a) → (1, 2b, 2a, 3)

How to create an ArrayList with N spaces

Initialize an Arraylist, then use a for loop to add N nulls to the ArrayList. The size of the ArrayList is now N.

List the stable sorting algorithms we have studied. (There are 5.)

Insertion sort Merge sort Pigeonhole sort Radix sort Counting sort

Where is the minimum item in a BST?

It is located at the leftmost node in the tree

Where is the maximum item in a BST?

It is located at the rightmost node in the tree

Where is the maximum item in a binary max heap?

It is located at the root of the tree

Where is the minimum item in a binary max heap?

It is located somewhere at the bottom of the tree (leaf nodes)

Lazy Deletion is required for a hash table with which collision-resolving strategy(s)? And why?

Linear and Quadratic Probing When searching for an item, it searches until finding an empty index. If you fully remove an item, you create an empty index that may confuse the search algorithm.

How to calculate 𝛌 (load factor) for linear/quadratic probing and separate chaining

Linear/Quadratic Probing: Filled indexes / array length Linear: 0.0 ≤ 𝛌 < 1.0 Quadratic: 0.0 ≤ 𝛌 ≤ 0.5 Separate Chaining: Number of items / size of the backing array list 0.0 ≤ 𝛌

Hash Code Function Requirements

Must return the same integer for equal objects. DOES NOT have to return different integers for different objects, but it is better if it does.

What is the disadvantages of Big-O notation?

Not useful for small N Difficult to tell which is larger when constants are involved (50N vs NlogN)

What is the upper bound for comparison-based sorts for the best case?

O(N) (Insertion sort)

What is the lower bound for comparison-based sorts for the average and worst case?

O(NlogN)

BST Big-O of: Add Find Remove findMin

O(logN) in the best and average case (balanced-ish) or O(N) in the worst case (unbalanced) In general (for the average BST): O(h), where h is the height of the BST

Priority Queue via Binary Max Heap Basic Structure

Only allows access to the top item in the heap. The heap (best visualized by a complete binary tree) is backed by a basic array. The root is generally at index 1 to avoid edge cases. The left child of any node is at index (i * 2). The right child is at (i * 2 + 1). Every node is the maximum of its subtree (greater than its children).

How to find the computation of a postfix expression

Operands are pushed to a stack (access the last item) When an operator is encountered, the right then left operand are popped from the stack (ex. 2 3 ^ => 2^3) Expression is then evaluated then pushed back into the stack This is repeated until there is a single item in the stack, which is the computation.

Why is the pivot selection for quicksort important for the performance of quicksort?

Optimally, the pivot should be the median of the array, which would partition the array into two subarrays of equal lengths (reducing the total amount of partitions that must take place and keeping quicksort's performance optimal). However, finding the median is too costly, but the median-of-three pivot selection is viable as it is not. Choosing the first or last item to be the pivot is not recommended because if the array is close to being sorted, then the pivot choice is would split the array into two subarrays into two very unequal lengths (increasing the total amount of partitions that must take place and reducing quicksort's performance).

Binary Search Tree (BST) Basic Structure

Orders data with nodes, which have up to 2 children. The left child is smaller than the parent. The right child is bigger.

Topological Sort

Orders vertices in an ACYCLIC graph so that if there is a path from v_j to v_k, then v_k appears after v_j in the ordering. (Like a sequence of jobs which can only be done one after the other.) Uses a FIFO queue. O(|V| + |E|)

Merge Sort

Recursively split an array in half until there is only one item per subarray. Then, combine the subarrays in a sorted order to a separate array. Then, copy the sorted items back into the original array. Best, average, and worst case: O(NlogN) Uses a backing array of O(N) Uses the same amount of steps regardless if the list is close to being sorted. Useful when the runtime of sorting must be consistent and fast. A stable sort.

Inorder Tree Traversal

Recursively visits nodes. Affects the left child, then parent, then right child.

Postorder Tree Traversal

Recursively visits nodes. Affects the left child, then right child, then parent.

Preorder Tree Traversal

Recursively visits nodes. Affects the parent, then left child, then right child.

How to perform binary max heap methods: Add extractMax Peek buildHeap

Remember: Structure first, order second. Add - Place the new item at the end of the tree and percolate up until the item is smaller than its parent. extractMax - Replace the root with the last item in the tree. Percolate down until the item is larger than both children. Peek - Return the root buildHeap - Add every item into the heap with no regard to order. Call percolate up on every node with a child from the bottom of the heap to the top.

What extra step is required to remove a node with two children in a BST?

Replace the node to be removed with the smallest item from its right subtree (or greatest item from its left subtree). Resume deletion by checking the node to be removed's new position.

Binary Search

Searches a sorted list. Compares the middle element of the sublist to the key. Calls binary search on the left or right half of the list recursively until the key is found or not found. O(logN)

Static/compile time type

The "left-side" portion of the assignment operator (=) Ensures that the assignment can be done by checking if this type is the "right-side" type or a parent of the "right-side" type. Ensures that a method called must be contained in this type, regardless if this method exists in the "right-side" type (broad over specific).

Dynamic/runtime type

The "right-side" portion of the assignment operator (=) When the program runs, the most available method is used (specific over broad).

Encapsulation

The act of grouping and hiding the data, implementation, and operations used in an object.

Determining the amount of space used in the original file.

The file size: the number of characters used in the file

Hash Table Linear/Quadratic Probing Basic Structure

The hash table stores objects in a backing array. The index of each object is decided by that object's hash function and using linear/quadratic probing when there is a collision.

Hash Table Separate Chaining Basic Structure

The hash table uses an array list of linked lists. The linked list that an object is added to (the index of the array list) is decided by that object's hash function. Collisions exist. Items with the same hash code are added to the same linked list.

Tree Height

The length of the path from the root to the furthest node.

What must happen when inserting a node into a singly-linked list at a specific index?

The new node's next must be set to the previous node's next. Then, the previous node's next can be set to be this new node.

What must happen when deleting a node with one child in a BST?

The node's parent must bypass this node by reassigning it's left/right child's reference to be this node's child.

What must happen when deleting a node with no children in a BST?

The node's parent's left/right child's reference must be set to null.

Red-Black Tree

The nodes are either red or black. The root is black. If a node is red, its children MUST BE BLACK. Every path from a node to a leaf must contain the same number of black nodes. New insertions will always be red and always left leaning. Insertions must satisfy the conditions that red nodes have black children and that they have the same number of black nodes in all paths.

Tree Depth

The number of edges from the root to the specified node

What is inversion count?

The number of swaps necessary for an array to be sorted. (0 if it's already sorted)

What must happen when deleting a node from a singly-linked list at a specific index?

The previous node's next must be set to this node's next.

Why shouldn't an adjacency matrix be used for sparse graphs?

The use of an adjacency matrix would waste too much space compared to the amount of space required for a sparse graph.

T/F A binary heap (min, max, min/max, or max/min) can have different orderings depending on the ordering of insertions

True

T/F Topological sorts can produce more than one valid ordering from the same graph

True

T/F a binary heap can contain duplicate items

True

If T(N) / F(N) → ∞, then F(N) is a ____ of the growth rate. If T(N) / F(N) → 0, then F(N) is a ____ of the growth rate.

Underestimate Overestimate

Counting Sort

Uses key values as indexes into an array. Keys must be ints. Must find/know the range of keys R. R is used as the size of a new array C. C[i] holds the count of the number of elements in A that have a key equal to i. The counts in C can be used to put A in sorted order. O(N + R) A stable sort.

Pigeonhole Sort

Uses one bucket for every possible key value. 1. Create an array of empty "pigeonholes" (one pigeonhole for each key in the range of values in the original). O(max{range, N}) If range <≈ N, O(N); else O(range). 2. Iterate through the original array, putting each item into the corresponding pigeonhole. 3. Iterate over the pigeonhole array in order, putting items from non-empty pigeonholes back in the original array. A stable sort.

Depth-First Search

Visits all connected nodes recursively (emulating a stack). Visits every node connected to the current node, then does the same for each of those nodes if they're unvisited. DOES NOT find the shortest path. O(|V| + |E|)

Breadth-First Search

Visits nodes in layers (except those already visited) from the source node in order to find the SHORTEST PATH to each node on an UNWEIGHTED graph or a graph where all edges have equal weights. Uses a FIFO queue. O(|V| + |E|)

Level Order Tree Traversal

Visits nodes starting at the root and going top to bottom and left to right.Uses a FIFO queue to store unvisited nodes. When a node is visited, its children are placed at the end of the queue.

Linear Probing

When a collision occurs, checks if the next index is open. Can result in clusters.

Quadratic Probing

When a collision occurs, checks increasingly distant indices until finding an open one. If the item maps to index i, it checks i, i + 1^2, i + 2^2, i + 3^2, ...

When is having public member variables acceptable?

When it is created inside a private class that is inside another class.

Say an object is defined Object obj = new Bicycle(); where Bicycle defines two methods toString() and ride(). a) obj.toString() will call toString from which class? b) obj.ride() would do what?

a) Bicycle since it is the most locally defined version b) Compiler error since ride() is not defined in Object

O(1) = ? O(logN) = ? O(N) = ? O(NlogN) = ? O(N^2) = ? O(N^3) = ?

constant logarithmic linear NlogN (superlinear) quadratic cubic

Write a header for a generic class called "Classy" whose generic type T must implement Comparator. Classy extends the class "CS".

public class Classy<T extends Comparator<? super T> extends CS {

Write a static generic method header for a function called "funcy" that takes in any ArrayList called list whose data is Comparable. The generic placeholder is "T". The method is public and returns nothing.

public static <T extends Comparable<? super T>> void funcy(ArrayList<T> list) {


Kaugnay na mga set ng pag-aaral

Biochemistry unit test section 1

View Set

Chapter 58: Drugs for Thyroid Disorders

View Set

"Psychiatric/Mental Health Nursing - Psychobiological Disorders + Foundations"

View Set

What is a Mutual Fund?/Whats is risk? 1

View Set

Patho/Pharm 4 Week 1, 2, 5 Combined (Does not include week 3-Mental Health)

View Set

Seizure Pediatric Practice Questions (Test #2, Fall 2020)

View Set

Science - Lunar Cycle from crossword

View Set

real estate principles chapter 2

View Set

L1.8: Andropov, Chernenko, Gorbatchev and Yeltsin Era (1982-91)

View Set