COSI 21a Final Exam

Ace your homework & exams now with Quizwiz!

Tree edge

(u,v) is a tree edge if v was first discovered by exploring edge (u,v)

What is the runtime of inserting into the Linked List?

- inserting at the beginning: O(1) - inserting at the end: O(1), but only if you keep track of the tail - inserting in the middle: O(1) only if you're given the pointer to where you need to insert

Four Edge Classifications

- tree edge - back edge - cross edge - forward edge

What are the four types of trees? What are their traits?

1.) Binary Trees Binary tree nodes can have at most two children. These are NOT ordered in any way. An AVL/BST have the properties of a Binary tree, but a tree can be a binary tree without being AVL or BST. 2.) Binary Search Trees Each node has 0, 1, or 2 children. The left node will always be smaller or equal to the parent, and the right node will always be greater than the parent. Searching is O(logn) if the tree is bushy, but O(n) if the tree is very long, like a Linked List. 3.) AVL Trees Same as Binary Search Trees where each node has 0, 1, or 2 children and the left child will be smaller or equal to the parent, and the right child will always be greater than or equal to the parent. However, balance factor is also kept track of in order to keep the tree balanced within 1. 4.) Splay Trees

What are the two policies used to handle collisions in a hash table?

1.) Chaining - store all elements that collide in a linked list - store a pointer to the head of the linked list 2.) Open addressing - everything is stored in the hash table itself - when collisions occur, use some type of procedure to store elements in free slots (i.e probing)

What are the four splay cases?

1.) Node is the root. 2.) Node is a left or right child of the root - so we do a zig operation 3.) Node is a left child of a right child, or a right child of a left child - so we do a zig-zag operation 4.) Node is a left child of a left child or a right child of a right child - so we do a zig-zig operation

What is a Queue?

A Queue is a First-In, First-Out data structure.

Cycle

A cycle is a path beginning and ended at a vertex

What is a heap?

A heap is a tree-like data structure that can be implemented with an array. It must satisfy the heap properties.

What is a linked list?

A list of nodes or elements of a data connected by pointers

How is a queue implemented?

A queue is implemented with an array.

Simple cycle

A simple cycle is a cycle which does not pass through any vertex more than once

Balanced Tree

A tree is balanced if the heights of the subtrees are within 1.

Perfectly Balanced Tree

A tree is perfectly balanced if a left and right subtree at any node have the same height.

What are the advantages and disadvantages of open addressing?

Advantage: - Simplicity and fixed memory usage Disadvantage: - The size of the array puts an upper limit on the number of elements that can be stored - Deletions are problematic

What are the advantages and disadvantages of chaining?

Advantages: - Simpler insertion and removal - array size is not a limitation Disadvantages: - Memory overhead is large if entries are small

What is the time of the three operations in a Splay Tree (insertion, deletion, searching)?

Amortized time of O(logn). Worst case is O(n).

Dijkstra's algorithm

Assumptions: - directed or undirected graph - all edges connected must have a nonnegative weight - connected graph Input: Weighted graph and a source vertex Output: Length of shortest paths from a given vertex to all other vertices (with a slight modification, we can obtain a path) It is considered a greedy algorithm because it always chooses the "lightest" vertex.

How to calculate balance factor

BF = Height of left subtree - Height of right subtree. If balance factor > 0, the tree is left heavy. If the balance factor < 0, the tree is right heavy.

What is heapify-down?

Basically allows us to fix the heap property. Check the left and right children, and swap the one that's greater than the parent. Then make the swapped element the new parent, and call Heapify-Down again until all is fixed and the heap property is satisfied.

What does cuckoo hashing guarantee?

Constant time search and insertion.

What is depth in a tree?

Depth refers to how deep we are in the tree. The root has a depth of 0, and increases by each level we go down in the tree.

Directed edge vs undirected edge

Directed edge: - ordered pair of vertices - first vertex, u, is the origin - second vertex, v, is the destination Undirected edge: - unordered pair of vertices (u, v)

Quick Sort

Divide and Conquer algorithm similar to Merge Sort, but sorts in place. Pick a partition, and swap elements. At the end, swap i and your first element. Worst-Case: O(n^2) Best-Case: O(nlogn) Average-Case: O(nlogn) In-Place: Yes Stable: No

Merge Sort

Divide and Conquer algorithm. Worst-Case: O(nlogn) Best-Case: O(nlogn) In-Place: No Stable: Yes

Weighted graph

Each edge has a "weight" (or "cost").

Priority Queue

Each element of a priority queue has an associated priority. Some operations of a priority queue (for both min and max priority queues) include: - extract-min/extract-max (get the min or max and remove it) - minimum/maximum (get the min or max without removing it) - insert (insert into the queue) - increase-key or decrease-key (increase the key of an element)

What is open addressing in a hash table?

Each hash table entry only contains a single key.

Why are splay trees different from AVL and BST trees?

Every time you do an operation, you splay to the root.

Breadth First Search (BFS)

Given a graph, systematically explore the edges to discover every vertex that is reachable from s. Runtime is O(V+E)

What is load factor?

Given a hash table, its the number of elements divided by its size. A good load factor is about 0.5. In a hash table with chaining, the load factor can be greater than 1. The expected length of a linked list is the load factor. In a hash table with collisions, load factor is between the range of 0 to 1.

Depth First Search (DFS)

Idea: - follow the path until you get stuck - backtrack until you reach an unexplored neighbor - don't repeat a vertex Running Time: O(V+E)

What is rehashing?

If the load factor exceeds a certain value, we rehash with a table twice the size, so the hash table is basically being rebuilt.

What does it mean for a sorting algorithm to be stable?

If the order of duplicates is preserved.

Successor in a Binary Search Tree

If the right subtree of v is nonempty, then the successor of v is the left most most node in the right subtree. If the right subtree of v is empty, then the successor is the right most node in the left subtree.

Strongly Connected Components

If there is a path from u to v and from v to u, then the nodes are strongly connected. A single node can be strongly connected with itself.

Kruskal's Algorithm

Initially, each vertex is on its own. Consider each edge in turn, where edges are ordered by increasing weight. If an edge connects two different trees, then the two trees are merged. If an edge connects two vertices already in the same tree, that edge is discarded.

What is the advantage of open addressing over chaining?

It avoids pointers, and you don't have to worry about O(n) searches as much, because you can utilize probing rather than searching a linked list.

How is a left child stored in an array implementation of a heap?

Left(i) = 2i

Counting Sort

Literal black magic. The way it works is you count the number of unique elements in Array A. The elements in this array have numbers from a range of 1 to k. Array C contains the times each element appears in A, in its corresponding index (so the number of 4's appears at index 4 in C). We then go through C, and change the value of the index i, to be the value of i + the value of the element before 1. Then, we go through A. We find the index of C to look at from index A, and then use that value in C, and insert the value from A into the value of index C. This is really confusing to type out, so here is a great youtube video. Honestly, just black magic: https://www.youtube.com/watch?v=7zuGmKfUt7s Worst-Case: O(n) In-Place: No Stable: Yes

What is the runtime of deletion in a hash table that uses open addressing?

O(1)

What is the runtime of insertion in a hash table that uses open addressing?

O(1)

What is the runtime of searching in a hash table that uses open addressing?

O(1)

What is the runtime of extracting the minimum element in a min-priority-queue and the maximum in a max-priority-queue?

O(1) for the removal, O(logn) for the deletion.

What is the runtime of deletion in a hash table that utilizes chaining?

O(1) if the linked list is doubly linked

What is the runtime of insertion in a hash table that utilizes chaining?

O(1) if we are inserting into the head of the linked list.

What is the runtime of deletion in an AVL Tree?

O(logn)

What is the runtime of heapify-down?

O(logn)

What is the runtime of heapify-up?

O(logn)

What is the runtime of inserting into a heap?

O(logn)

What is the runtime of insertion in an AVL Tree?

O(logn)

What is the runtime of searching in an AVL Tree?

O(logn)

What is the runtime of increasing the key in a heap?

O(logn) because we may need to heapify the node if it no longer satisfies the heap property.

What is the running time for searching a linked list?

O(n)

What is the runtime of searching in a hash table that utilizes chaining?

O(n) worst case, where n is proportional to the size of our linked list.

What is the runtime of building a heap?

O(nlogn)

Adjacency matrix

Out-degree is shown by the rows, in-degree is shown by the columns. The storage required is O(V^2). Insertion, searching, and deletion can all take O(1).

AVL Rotation Cases

Outside Cases (Single Rotation): 1.) Insertion into left child of a left child 2.) Insertion into right child of a right child Inside Cases (Double Rotation) 1.) Insertion into left child of a right child 2.) Insertion into a right child of a left child

How is a parent stored in an array implementation of a heap?

Parent(i) = i/2

Degree

Refers to the number of edges starting or ending at that vertex (a cycle counts as two - one in and one out)

How is a right child stored in an array implementation of a heap?

Right(i) = 2i + 1

Operations on a LinkedList

Some operations on a LinkedList are: - isEmpty (check if the Linked List is empty) - insertNode (insert a node into a particular position) - findNode (find the node with a given value) - deleteNode (delete the node with a given value)

Directed graph

The edges are directed from one vertex to another

Shell Sort

The idea is that you divide the array into subarrays, and sort each of the arrays using insertion sort. We then divide the array again into a number of smaller arrays, and sort this array using insertion sort. We repeat this until the number of subarrays being sorted is one. Worst-Case: O(n^2) Best-Case: O(n) In-Place: Yes Stable: No

What is height in a tree?

The number of edges on the longest path from the node to the root of the tree.

What is a binary heap?

The simplest kind of heap. A binary heap can be seen as a binary tree with two additional constraints: 1.) shape property - the tree is either a full tree, or if the last level is not full, the nodes are filled from left to right 2.) heap property - each node is greater than or equal to its children or less than or equal to its children (greater than means we have a max heap, less than means we have a min heap)

Adjacency list

The storage required is O(V+E). Insertion, searching and deletion can all take potentally O(V)

What operations can you perform on a queue?

There are several operations you can perform on a queue. - enqueue (add into a queue) - dequeue (remove from a queue) - isEmpty (check if the queue is empty) - size (get the size of the queue) - front (get the front element without removing it)

Undirected graph

There is no distinction between two vertices associated with an edge

Endpoints in a graph

These are the end vertices for an edge.

Selection Sort

This algorithm selects the smallest value in the array, and swaps it with the value in the first position. It then repeats this, with that first position incrementing. Worst-Case: O(n^2) Best-Case: O(n^2) In-Place: Yes Stable: Yes

Deletion from an AVL Tree

This is more complicated than insertion. If the deleted node was in v's right subtree, then z is the root of v's left subtree: - If z has a balance factor of 0 or 1 after deletion, do a single right rotation - if z has a balance factor of -1 after deletion, do a left-right rotation If the deleted node was in v's left subtree, then z is the root of v's right subtree: - If z has a balance factor of 0 or -1, do a single left rotation. - If z has a balance factor of 1 after deletion, do a right-left rotation

Bubble Sort

This is the one where you basically iterate through the array, and swap the left and right to put them in order. It is not efficient. You will be bullied for using this sorting algorithm. Worst-Case: O(n^2) Best-Case: O(n) In-Place: Yes Stable: Yes

Insertion Sort

This sorting algorithm is basically like sorting in a deck of cards. Worst-Case: O(n^2) Best-Case: O(n) In-Place: Yes Stable: Yes

Incidents in a graph

Two or more edges which share a vertex, v, are called incidents. For example: (2, 10), (5,10), and (7, 10) are incident to 10.

Adjacent vertices

Two vertices connected by an edge

Examples of solving recurrence

Very hard, but good pictures for an example:

In-Order Traversal

Visit each level from left to right

Post-Order Traversal

Visit left and right, then V.

Pre-Order Traversal

Visit v, then left and right.

Cuckoo Hashing

When we insert an element using a hash function, kick out what occupies the spot if there is collision, and move the kicked out element to another spot using a second hash function.

What is heapify-up?

While i is not the root and the node we are at is greater than its parent, swap the node and the parent, and then set i as the parent.

Heapsort

Worst-Case: O(nlogn) In-Place: Yes Stable: No

How do you rotate a right child of a right child, or a left child of a left child in a splay tree?

You do a zig-zig operation. First, you rotate the parent, then you rotate the node you are splaying.

How do you do a delete operation in a splay tree?

You splay the node to the root, and move it, which gives you two subtrees. You splay the max (the right most node) in the left subtree to the root, and then attach the right's root to the left subtree.

Directed Acyclic Graph (DAG)

a directed graph with no cycles.

Topological Sort

a linear ordering of all its vertices such that if DAG contains an edge (u, v), then u appears before v in ordering Analysis: Runtime: O(V+E) Space: O(V+E)

Loop

an edge whose endpoints are the same vertex

Forward edges

edges (u,v) connecting a vertex u to a descendant v in a depth-first tree - basically if we explored a path, and we have a direct path to a descendant, that is a forward edge

Back edge

edges (u,v) connecting a vertex v to an ancestor u. A self-loop is a back edge

What is the formula for linear probing?

h(k) = (hash(k) + i) mod m, where i is between 0...m-1

What is the formula for quadratic probing?

h(k) = (hash(k) + i^2) mod m, where i is between 0...m-1

What is the formula for double hashing?

h(k) = (hash1(k) + i * hash2(k)) mod m, where i is between 0...m-1

Cross edges

link nodes with no ancestor-descendant relation and point from "high" to "low" nodes.

In-degree

the number of edges ending at a vertex in a weighted graph

Out-degree

the number of edges starting at a vertex in a weighted graph


Related study sets

Chapter 6 Vocab - Bonds and Bond Valuation

View Set

Abnormal Psych Final- Revel Section 2

View Set

Health Assessment Chapter 16 & 17

View Set

Chapter 53: Caring for Clients with Disorders of the Female Reproductive System

View Set