Datastrukt

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

A binary heap can be fully represented in an array. How does the relationship between the item at index i and its children work?

The items left child will be at index 2i and its right child at index 2i + 1

How does a queue work?

It follows FIFO (first in first out), it works similar to a linked list however we also need to keep a pointer to the last element in the list. So that we can update which one is the last one when we enqueue.

Why is it a bad idea to select the first element as a pivot for quicksort?

It would take quadratic time to sort an already sorted list because all of the items would already be to the right of the pivot. So you would just loop through the whole array for all n items.

What is clustering?

When a lot of items have been sent forward due to collision in open addressing, creating a chain of objects not in their corresponding index that would have been created by the hash code + compression. It can cause the data structure to slow down to linear time complexity.

Is a binary heap a BST?

No. >:(

At what load factor does the hash table begin to slow down in open addressing?

0.75. The array should now be resized.

How does the Uniform Cost Search work? (Dijkstra) What is its complexity?

1. Add a starting node to your priority queue 2. Remove the minimum (in terms of weight) item from your priority queue and add this edge to your shortest path tree. 3. Add all outgoing edges from that node to your priority queue where the weights are the distance from your starting node 4. Repeat until you have visited all nodes.

What are the three operations we can do on a priority queue?

1. Add an item 2. Find the smallest item 3. Remove the smallest item

How do we insert into a binary heap?

1. Insert the item at the next available spot at the bottom of the tree (from left to right) 2. If the child is less than its parent, swap them 3. Keep swapping until the invariant is fixed.

What are the three invariants in a red black tree?

1. It must have the BST invariant 2. A red node may only occur as a left child of a black node 3. On any path from the root to a null, we pass through the same number of black nodes.

What makes a hash function good?

1. It should distribute objects evenly 2. It should be efficient to compute 3. It should use the entire object and a similar object should result in a very different hash code.

What are the most important complexity classes? Starting from best to worst.

1. O(1) - Constant 2. O(log(N)) - logarithmic 3. O(N) - linear 4. O(Nlog(N)) - linearithmic 5. O(N^2) - quadratic 6. O(2^N) - exponential (yikes)

How does Prim's algorithm work and what is its complexity?

1. Pick an arbitrary node as starting node, this is now the start to your MST. 2. Pick the minimum weighted edge with only one connection to the MST, this is your next edge in the MST. 3. Repeat until you found all N - 1 edges. The complexity is O(Elog(V)) <---- ren gissning

How does the quick sort partitioning work?

1. Select the pivot (can be done in various ways) 2. Swap the pivot with the first item. 3. Set two indices, "lo" to the right of the pivot and "hi" at the end of the array. 4. Move lo right until you find something greater than the pivot. 5. Move hi left until you find something less than the pivot. 6. Swap the elements at lo an hi, then repeat step 4 and 5 until the array is divided into one part less than the pivot, and the other greater than the pivot. 7. Swap the pivot with the last element in the "less than" part of the array. Done!

How do we remove the minimum element in a binary heap?

1. Swap the root and the last element (the right most element in the bottom row). 2. Remove the last element (The previous root) 3. Fix the invariant by swapping down the item at the root with its least child.

What are the three notations for complexity and what do they mean?

1. big O - O(g(n)) : f(n) will eventually have an upper bound of a multiple of g(n) 2. big Omega - Omega(g(n)) : f(n) will eventually have a lower bound of a multiple of g(n) 3. big Thetha - Thetha(g(n)) : f(n) will eventually have a lower and upper bound of multiples of g(n). (Tight bound).

What does it mean for a tree to be unbalanced / balanced?

A balanced tree has a height proportional to the logarithm of the number of nodes in the tree.

What is a Binary search tree?

A binary tree where all nodes are comparable and the key of the left child is less than the key of the parent, and the key of the right child is greater than the key of the parent.

What is a strongly connected component?

A component of a graph where every node can reach each other.

What is a crossing edge?

A crossing edge connects a node in one set with a node in the other after a cut has been made.

What is a binary search tree?

A hierarchical data structure built up from nodes where each node can only have two children. Each node must have a unique parent, and to store the tree we only need to remember the root.

How does a stack work in general?

A stack is a data structure where LIFO (Last in first out) holds, which means that the element added last is the first (and only) element that can be removed.

How does a linked list work?

All elements in a linked list are nodes that point to exactly one other unique node through its "next" (two nodes cannot point to the same one). The linked list is stored by having a pointer "head" point to the first node in the list. Since it would be inefficient to loop through all elements to calculate size, it also holds a size field.

What is the complexity of search, insert and delete in a BST?

All operations have the same time complexity that all depends on the balance of the tree. The worst case complexity is that the tree is completely skewed, then all operations that linear time O(N), however if the tree is balanced then all operations take logarithmic time O(log(N)).

What is the complexity to push, pop, enqueue and dequeue?

Always constant O(1) since we never iterate over any items, we only do lookups.

What is a dynamic array and how does it work?

An array which can vary in size. When full, it doubles in size. It does have to copy each element to a new array, however it only have to do so whenever the array has doubled its elements. It also shrinks when it is quarter full.

How do you dequeue in a queue implemented as a linked list?

Assign a variable to the value of the first node (that is to be returned), redirect first to the "next" of the current first and decrease the size.

How does binary search work and what is its complexity?

Binary search looks at the median element in a sorted array, and if the item does not equal the one we are searching for, we can cut away half of the array, then again look at the median of the remaining array and so on until we either find the element or exhaust the array. The complexity of binary search is O(log(N)). When implementing, you use three indices: lo, hi and mid (mid = (lo + hi)/2). Where you set the lo to mid + 1 if the element is greater than the median and hi + mid - 1 if the element is less than the median and so on.

What is the complexity of the three operations in a binary heap: add, delete min and lookup min

Both add and delete take logarithmic time O(log(N)) while looking up the minimum item is constant O(1) (it is the first item of the array, or the root of the binary heap).

What is the main idea behind finding the shortest path?

Breadth first search with a priority queue instead of a queue.

How do we fix the invariant when inserting into an AVL-tree?

By making rotations, similar to skew.

How does Kurskals algorithm work and what is its complexity?

Consider all of the edges in ascending order of weight. Add the next edge to the MST unless it would create a cycle. Continue until you have found all N - 1 edges. The time complexity is O(Elog(V)) <---- ren gissning

How do you enqueue to a queue implemented as a linked list?

Create a new node, point "next" of the current last element to our new node, redirect last to the new node and increase the size.

How does one push from a stack implemented as a linked list?

Create a new node, point the new nodes "next" to the current head, redirect the head to the new node and increase the size.

What is the difference between a sparse and dense graph?

Dense graph is a graph in which the number of edges is close to the maximal number of edges. Sparse graph is a graph in which the number of edges is close to the minimal number of edges.

What is the difference between depth first search and breadth first search?

Depth first search always goes down a path as long as it can before backtracking while breadth first search always searches one level at a time.

How does insertion sort work, and what is its complexity?

Divide array into two parts, left part is your sorted hand and the right part is an unordered deck. Take a "card" (an element), find its insertion point in your hand (using either binary or linear search), shift all items to the right of insertion point and insert card. Complexity is often O(N^2), however for an already ordered deck, you do not need to shift the cards and so the complexity is best case O(N).

How does the greedy algorithm work?

Find cuts which crosses no already found MST edges, the crossing edge with the lowest cost is chosen as an MST edge. Do this until you have found all N - 1 edges.

What is the additional invariant you can add to a binary search tree so that it becomes an AVL-tree?

For each node, the heights of its left and right subtrees must differ by at most 1.

How can we store a stack in a dynamic array?

If we use the dynamic array (in reverse) by using its size. That is we add and pop to the last element in the array, not the first.

How do you recurse the merge sort algorithm?

In the mergeSort(int[] arr, int lo, int hi) 1. Check size of arrays (hi - lo) 2. Calculate the mid index (where to split) 3. create the left subarray as mergeSort(arr, lo, mid) 4. create the right subarray mergeSort(arr, mid + 1, hi) 5. return merge(left array, right array)

What is a spanning tree?

It is a subgraph T such that it 1. is connected 2. is acyclic 3. connects all of the nodes

What is a cut in a graph?

It is when you partition the graph into two non-empty sets.

What is a DAG?

It stands for directed acyclic graph. And it is a directed graph containing no cycles, it is also called a topological order.

How does linear search work and what is its complexity?

Linear search loops through the entire array looks for the requested item. The complexity is O(N).

How does merge sort work? What is the complexity of merge sort?

Merge sort is a divide and conquer algorithm, meaning that it will split the array into smaller and smaller subarrays and then merge the arrays into ordered arrays. It takes logarithmic time to divide the array into the subarrays, then linear time for the actual merge algorithm. Hence O(Nlog(N)), always.

Is merge sort an in place algorithm?

No, merge sort copies elements to new arrays.

Does the hash function produce the actual index of the hash table array?

No, the objects create hash codes which are then compressed to be able to fit in the table, usually through simple modulus of the array size.

What is the difference between preorder, inorder and postorder?

Preorder: Do thing, visit left child, visit right child Inorder: Visit left child, do thing, visit right child Postorder: visist left child, visit right child, do thing

How does the quick sort algorithm work (In general, not the actual partitioning).

Quick sort is a divide and conquer algorithm. You select a pivot and place all items less than the pivot to its left, and all items greater than the pivot to its right. Then recursively to the same process for the subarrays to the left and right of the pivot.

How does one pop from a stack implemented as a linked list?

Save the value of the head (that is to be returned), redirect the head to the "next" of the current head, return the value and decrease the size.

What is the time complexity of the operations in a red black tree?

Since the tree is always perfectly balanced, all operations take logarithmic time O(log(N)).

What is open addressing? (linear probing)

The hash table can only hold one item per index, and when collision occurs, the item will be put in the next available spot.

What is separate chaining?

The hash table is divided into buckets, where each bucket can hold a collection of items (usually through a linked list).

How do we insert an item into a red black tree.

The idea is to insert a red node like in a BST and then fix the invariant that we probably broke. 1. Insert a red node with the standard BST algorithm. 2a. We inserted a red node to the right of a node, we then skew these nodes. Meaning that if y was the right child of x, then x will now be the left child of y. Any element between will switch parent. 2b. We have inserted a red node as the left child to another red node. The middle node becomes the red parent to the other two. 2c. We inserted a red node to the right of a black node which already has a red node as a left child. Swap the colors of all three nodes. 2d. If the red node is the root, swap its color.

What is load factor?

The ratio between the number of elements in the hash table and the size of the array.

What is a minimum spanning tree?

The spanning tree that minimizes the sum of the weights along its edges.

How do we delete an item in a binary search tree?

There are three cases: 1. If the node is a leaf (has no children), just delete it 2. If the node has one child, replace it with its child. 3. If the node has 2 children, replace it with its right most node in its left subtree, then delete the swapped item. If the right most node in the left subtree has a left child, delete that node by replacing it with its child.

What is the most common way to store a graph when implementing?

Through an adjacency list. Each entry in the array is an unordered list containing the neighbors to the node i.

How do we search in a binary search tree?

We compare the key of the node with the key we are searching for, if the node's key is less than our target we recursively search the right subtree and vice versa if the key is greater to our target.

How can we implement a queue as a circular dynamic array?

We set two indices, head and tail. To enqueue insert element at tail position, and to dequeue delete the element at head. When resizing always copy the elements beginning from the head and then wrapping around.

How do we insert an item into a binary search tree?

We use the search algorithm to find the correct spot. If we arrive at a null, create a new node in that place. If the key already exists, update the value.

How do you perform a breadth first search for a graph?

We will visit nodes at the same depth from a given node by adding them to a queue, instead of traversing down a path for as long as we can. When implementing you do 1. Add a starting node to the queue 2. When we visit a node, remove it from the queue and then add all of its adjacent nodes to the queue if they have not yet been visited. 3. Repeat until queue is empty.

How does the array merge algorithm work? (In merge sort)

You have two pointers starting at the first item in both arrays. Compare the items under the pointers, take the lesser item and put it into an array, then shift that pointer one step to the right. Repeat until both arrays are exhausted.

How does selection sort work, and what is its complexity?

You loop through the list, add the smallest item to the beginning of the array, repeat for all items. Complexity is always O(N^2).

How do you delete an item in open addressing?

You mark the spot with a deleted term. Otherwise simple lookups could fail because it had previously been pushed to a certain slot, which it would not have been now if there was a free spot. One would also have to keep track of the number of deleted items in order to recalculate the array if it is overfilled with deletions.

How do you perform a depth first search for a graph?

You start a a node, traverse down some path as long as you can before backtracking. The difference between this and when searching through a tree is that we have to store a list of visited nodes, since graphs can by cyclic.

What is the heap property?

the min-heap property: the value of each node is greater than or equal to the value of its parent, with the minimum-value element at the root. the max-heap property: the value of each node is less than or equal to the value of its parent, with the maximum-value element at the root.

Why is it an advantage to have an in-place algorithm?

they take up less space in memory


संबंधित स्टडी सेट्स

Purine Nucleotide Synthesis Regulation, Pyrimidine Synthesis, Pyrimidine Nulceotide Synthesis Regulation, Formation of Deoxyribonucleotides

View Set