COMP 215 Test 3 Prep

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

To get a random number in [lower, upper)

- Shift by lower and scale by (upper - lower) - randNum = (Math.random() * (upper - lower)) + lower

When an element is appended to an ArrayList, and the capacity is full, what occurs?

- a new larger array is created (e.g. 1.5x old capacity) - all of the elements in data get copied into the new array - data is updated to point to the new larger array

What are the two fields of an ArrayList?

- an Array (T[]) that holds its elements - an integer (int size) that tracks the number of filled slots

Workflow for testing a randomized algorithm

1. Build the expected distribution in the form of a map of each potential outcome to the probability of it occurring 2. Run the algorithm a large number of times, aggregating the actual results in a map of outcome to frequency of occurrence 3. Normalize the actual results 4. Compare the actual and expected distribution s○ Each potential outcome's actual rate of occurrence should be within a small margin of error of its expected occurrence rate

compare(T o1, T o2) returns...

<0 if o1 is less than o2 0 if o1 is equal to o2 >0 if o1 is greater than o2

compareTo(T object) returns...

<0 if this is less than o 0 if this is equal to o >0 if this is greater than o

How does a PRNG work?

A PRNG typically takes a seed = a number used to initialize it, to generate "random" numbers ○ Same algorithm + same seed → same sequence of numbers ● PRNG algorithms are typically periodic = if you generate enough numbers, the sequence will eventually repeat itself

What is a heuristic?

A consistent strategy used to determine the locally optimal choice in greedy algorithms; Once a choice has been made, it is irrevocable (even if you later realize it's globally sub-optimal)

What is a binary tree

A constrained tree that can have at most 2 children (nodes can also have just 1 or no children)

What is a recursive data structure?

A data structure that is composed of smaller instances of the same type (i.e. trees)

What is a doubly linked list?

A doubly-linked list. Elements have two references, one to each of the preceding and following neighbors.

Benefits of an array backed list over a linked list?

Array-backed lists are easier to implement → less chance of bugs; Each element in a linked list incurs an overhead: in addition to the data, need to store 1-2 references (prev, next) (problems with space efficiency); locality benefits to storing elements contingually

What are ArrayLists?

Arraylists are Java's implementation of an array backed list, which used an array as their underlying representation. This entire array is allocated as one contiguous block, which allows us to use a fast offset based lookup scheme.

Dangers with PRNGs

Because of this lack of true randomness, PRNGs are less secure: if the seed is known, the entire sequence can be reproduced

Why, when we create a new ArrayList to account for greater capacity, should we not just allocate one more spot?

Because that would mean that the next time we try to add to our list, we'd have to go through this same process of creating a new array and copying. This copy operation is expensive from a time perspective. We want to make sure that we don't have to perform it every single time and element gets added after the initial capacity is filled

Workflow for debugging random algorithms

During testing & debugging, try to make things as deterministic as possible in order to improve reproducibility ○ Take advantage of seeds: Random rng = new Random(5); ○ rng will output the same sequence of values on each execution ○ Remove the seed after debugging

Cons of doubly linked lists over singly linked lists

Each node consumes more space (one extra reference)

What if we want to remove the element at position i?

Elements at positions > i must be shifted left by one

What if we want to insert an element at position i?

Elements at positions > i must be shifted right by one

When to use PRNGs over HRNGs

For situations where we true randomness isn't necessary (e.g. security is not at stake), PRNGs are the typical choice

What are Greedy algorithms?

Greedy algorithms are used to find approximate solutions by performing a series of local optimizations using a heuristic (once an optimization choice is made, it is irrevocable). Sometimes, greedy algorithms can actually be used to find exact solutions.

How do HRNGs work?

HRNGs take "digital snapshots" of real world phenomena and convert them into random numbers (e.g. Lava lamps)

Why should we include a tail field in our linked list even though it's optional?

Having a tail gives significant performance benefits at the cost of storing only one additional reference (such as convenient append operations)

What are LinkedLists?

In a linked list, elements are allocated individually. However, the list structure is maintained by references or links between those elements.

What is a singly linked list?

In a singly linked list, each element has one reference to the neighbor that immediately follows it in the list.

Under what circumstances would it be a good design to use a greedy algorithm?

It is a good choice to use greedy algorithms when dealing with optimization problems that are NP-hard and/or intractable.; If it's a case where you care more about time efficiency than accuracy; Sometimes, greedy algorithms gives us exact solutions

Pros of PRNGs

It's fast to implement!

For array backed lists, why is removing an element by reference operation is slightly worse than removing an element by position operation in terms of time complexity?

It's worse since it needs to first scan the list to find the element to be removed and then left shift.

When should you use an array backed list over a linked list?

Many lookups → array-backed list

What is NP in terms of algorithms

NP = set of problems for which solutions can be verified with algorithms that are polynomial time in the worst case (P is a subset of NP because we can "verify" a solution by directly solving)

What is the time complexity of looking up an element at index for Array-Backed Lists?

O(1)

What is the time complexity of append for Array-Backed Lists?

O(1) amortized (usually O(1), but occasionally O(n), yet O(1) on average)

What is the time complexity of prepend for doubly linked lists?

O(1), assuming that our design has a reference to its head; no scanning is needed

What is the time complexity of prepend for singly linked lists?

O(1), assuming that our design has a reference to its head; no scanning is needed

What is the time complexity of append for doubly linked lists?

O(1), assuming that our design has a reference to its tail; no scanning is needed

What is the time complexity of append for singly linked lists?

O(1), assuming that our design has a reference to its tail; no scanning is needed

What is the time complexity of remove by reference to node for doubly linked lists?

O(1), since we already have a reference to the node to be removed thanks to the prev field

What is the best-case time complexity of insert, contains, and remove in a BST?

O(logn) (for reasonably balanced trees where the layers are considerably filled and we only have to traverse on branch)

What is the worst-case time complexity of insert, contains, and remove in a BST?

O(n) (when the BST ends up looking like a linked list and we have to scan this "list" starting from the head)

What is the time complexity of lookup for doubly linked lists?

O(n) because it requires a scan, but can be optimized for in-order access

What is the time complexity of lookup for singly linked lists?

O(n) because it requires a scan, but can be optimized for in-order access

What is the time complexity of prepend for Array-Backed Lists?

O(n), since we have to shift every element to the right to place a new element at the first index

What is the time complexity of insert for doubly linked lists?

O(n), since we need to scan the list to find the appropriate position to insert

What is the time complexity of insert for singly linked lists?

O(n), since we need to scan the list to find the appropriate position to insert

What is the time complexity of remove by reference to object for doubly linked lists?

O(n), since we need to scan the list to find the appropriate position to remove an element

What is the time complexity of remove by reference to object for singly linked lists?

O(n), since we need to scan the list to find the appropriate position to remove an element

What is the time complexity of removing elements for Array-Backed Lists?

O(n), since we need to shift on average, n/2 elements to the left

What is the time complexity of inserting elements at arbitrary positions for Array-Backed Lists?

O(n), since we need to shift on average, n/2 elements to the right

What is the time complexity of remove by reference to node for singly linked lists?

O(n), since we require a scan to find the parent node

Randomness

Randomness = a lack of pattern or predictability ○ Lack of predictability on the granularity of an individual event ○ However, randomness often follows a probability distribution

How do we perform a lookup in a linked list?

Scan from head*, keeping track of the current index, until the ith node is reached (however, we can optimize this operation if we add an extra field to the LinkedList to track the most recently looked up Node & start the scan from there; if elements are looked up in order, the next lookup will be O(1).)

What determines how big data is for an ArrayList?

The constructor! ArrayList has three constructors: - ArrayList() - initial capacity is 10 - ArrayList(Collection<? extends T> c) - initial capacity is the size of c; resultant ArrayList is a copy of c - ArrayList (int initCapacity) - initial capacity is as specified

What is NP-complete in terms of algorithms

The set of problems that are both NP and NP-Hard; there are the most difficult problems in NP! However, if we can solve one NP-complete problem efficiently, we can solve all NP problems efficiently

Why is debugging random algorithms hard?

Unpredictability → lack of reproducibility → bugs that appear intermittently

Second hybrid approach to strike a balance of between non-determinism and efficiency

Use a PRNG, but periodically re-seed it with a number generated by an HRNG

First hybrid approach to strike a balance of between non-determinism and efficiency

Use an HRNG by default, but fall back on a PRNG when the desired rate of generation exceeds the HRNG's capabilities

Why are linked lists recursive?

Within the definition of a linked list node class, we have references to other objects of type node. You can think of the next node in the chain as being the head of a smaller linked list.

Big-O Notation

Written in the form O(<expr>), where <expr> is a math expression in terms of the sizes of the inputs (often n)

What is a self-balancing BST?

a BST that periodically performs transformations to keep its height close to O(log n)

What is the underlying representation of an ArrayList?

an array

What is a BST

binary tree + ordering constraint

Pseudo random number generators (PRNGs)

deterministic random number generators (DRNGs) ○ Numbers only appear to be random

In a BST, how do we remove a node that doesn't exist?

do nothing & return false

The Comparator interface

enables external comparisons; a Comparator object compares two separate objects to each other

The Comparable interface

enables internal comparisons: a Comparable object can directly compare itself to another object (similar to equals method)

What is the problem with regular binary trees in the sense that any given value can be stored in any location

if you put something into the tree and you later one to find it again, you have no guidance as to where it's stored. So you have to traverse the entire tree looking for it. O(n) lookup time

In-order traversal

left → node → right (make sure to go all the way down to the tree's leftmost node first!!)

Post-order traversal

left → right → node(first return the subtrees and then visit the node)

In a BST, how do we remove a node with one child?

let node's parent point to node's child & return true

pre-order traversal

node → left → right (first return the node and then visit the subtrees)

Pros of HRNGs

non-deterministic

Deterministic algorithms

one set of inputs (+ program state) → one expected output; test by comparing actual and expected

Random algorithms:

one set of inputs → many possible outputs; an expected distribution rather than an expected result

What are tractable problems?

problems = problems whose solutions are efficient enough to be used in practice ○ Problems in P are generally considered to be tractable

What are intractable problems?

problems that have known solutions, but whose solutions are too inefficient to be used in practice ○ NP-hard problems (including NP-complete problems) are often considered to be intractable

What are optimization problems?

problems that seek the best solution from the collection of all possible solutions ● Many optimization problems are NP-hard & intractable, since there are a large number of possible solutions to consider

Pros of a self-balancing BST?

quick lookups!

In a BST, how do we remove a leaf node?

remove node & return true

What is NP-hard in terms of algorithms

set of problems that are at least as hard as the hardest problem in NP

What is P in terms of algorithms

set of problems that can be solved with algorithms that are polynomial time in the worst case

Hardware random number generators (HRNGs)

true random number generators (TRNGs) ○ Numbers are genuinely random

What happens if we over-allocate memory for data with ArrayLists?

we also don't want to allocate an excessively large array, since that would be expensive from a space perspective

Pros of doubly linked lists over singly linked lists

■ Deletion by reference to node is now O(1) rather than O(n), because we don't have to scan the list to find its parent ■ Reverse iteration can be made substantially more efficient

Cons of Array-Backed Lists

■ Every once in a while, appending an element is very slow, because we need to copy the entire array

In a BST, how do we remove a node with two children?

■ Find the minimum value (val) in node's right subtree ■ Delete the node containing val ■ Update node's contents to contain val & return true

What does it mean for a BST to have total order?

■ Left child's data < node's data ■ Right child's data > node's data ■ No duplicates are allowed

What does it mean for a BST to have total preorder?

■ Left child's data <* node's data ■ Right child's data >* node's data ■ Duplicate elements are allowed (*it doesn't matter on which side duplicates fall as long as it's consistent, i.e. could alternatively say < and >)

Pros of Array-Backed Lists

■ We keep the simple and fast offset-based lookup scheme: myArray[i] = addr(myArray) + (i * sizeof(T)) ■ Most of the time, appending elements is fast, because the space is pre-allocated

How do we append in a linked list?

○ Create a Node (node) encapsulating n ○ If tail isn't null, update tail.next to point to node ○ Update tail; update head if the list was empty before ○ Update size

How do we insert in a linked list?

○ Create a Node (node) encapsulating n ○ Scan from head to find correct parent for node; set node's next to its parent's old next; update parent's next to point to node ○ Update size

How do we prepend in a linked list?

○ Create a Node (node) encapsulating n ○ Set node's next to be the old head ○ Update head; update tail if the list was empty before ○ Update size

A PRNG is a CSPRNG iff it's been proven that:

○ If an attacker knows all of the generated bits up to a point ○ But the attacker does not know the seed ○ Then the attacker cannot guess the next bit with probability significantly higher than 50%

How do we remove in a linked list?

○ If n is not encapsulated by the head: scan from head to find parent of node encapsulating n and set parent's next to node's old next ○ Update head and/or tail, as appropriate ○ Update size

When should you use a linked list over an array backed list?

○ Many prepends or deletions by node reference → linked list ○ Implementing a queue → linked list

Cons of PRNGs

○ Not truly random; a poorly-designed PRNG may exhibit: ■ A short period ■Correlation of successive values ■ Lack of uniformity of distribution

What is the compromise between array backed lists and linked lists?

○ Segmented list = a series of arrays linked together ○ A compromise that can be tuned: ■ Longer segments → behavior approaches array-backed lists ■ Shorter segments → behavior approaches linked lists

Cons of HRNGs

○ The rate at which entropy can be harvested from a natural source depends on the underlying physical phenomena ○ Some algorithms may request random numbers faster than they can be generated; this causes the HRNG to block

Use Comparable if:

○ You want to define a single default order for objects ○ You want to put your objects in a sorted data structure that requires elements to be Comparable to each other

Use Comparator if:

○ You want to define multiple custom orders for objects ○ You want to compare objects of a class you did not define, and the default order is not what you want

What is a tree

● A tree is a data structure comprised of linked nodes ○ Each node encapsulates a piece of data ● Can be thought of as an acyclic graph (no cycles)


Ensembles d'études connexes

"Θα σου κάνω μερικές ερωτήσεις και θα πρέπει να μου απαντήσεις με σωστή σύνταξη. Θα σου έχω πίσω από την ερώτηση πως θα πρέπει να είναι η απάντησή σου αν δυσκολευτείς."

View Set

pi kappa phi chapter 1 study notes

View Set

ECON 200 chapter 11: Technology, Production, and Costs

View Set

Growth & Development 2/End of Life

View Set