CS2420 - Midterm Exam 2 Review

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

Which is a valid topological sort of the graph below? (Select all that apply.) A. v1 v3 v4 v6 v0 v2 v5 B. v0 v1 v3 v2 v4 v6 v5 C. v0 v2 v1 v4 v3 v5 v6

B. v0 v1 v3 v2 v4 v6 v5

What is true for this example graph in figure 2? (Only one choice is correct) A. |E| = |V| B. |E| = 1.5|V| C. |E| = 6, |V| = 6 D. |E| = 6, |V| = 9

B. |E| = 1.5|V| |V| = 6, |E| = 9, so |E| / |V| = 1.5

This linear-probing hash table holds integers. The hash function used is simply the identity function. Notice that the capacity of the backing array is 50. A. λ is less than 0.5. B. λ is 0.5 or greater

B. λ is 0.5 or greater Recall that the load factor λ indicates how full the table is. For this table λ = 30 / 50 = 0.6. For good performance, we should have already resized and rehashed.

Which of these algorithms solves the weighted, single-source shortest path problem? (Select all that apply.) A. Breadth-first search B. Depth-first search C. Dijkstra's algorithm D. Topological sort

C. Dijkstra's algorithm

What is the running-time behavior of a toArrayList() method for a linear-probing hash table with N entries and backed by an array with capacity N2? A. O(1) B. O(log N) C. O(N) D. O(N log N) E. O(N2)

E. O(N2) Every cell of the backing array must be checked to determine if it contains an item to be added to the list. Without prior knowledge of items in the backing array we cannot use hashing to locate those items with O(1) behavior each.

Number sequences generated by computational algorithms are considered to be random because the nature of an algorithm is that its outcome can be predicted. True False

False

The graph represented by the following DOT specification is a DAG (i.e., directed acyclic graph). digraph D { 1 -> 2 2 -> 3 3 -> 4 4 -> 5 5 -> 3 1 -> 6 6 -> 7 7 -> 5 2 -> 7 } True False

False

What is the Big-O behavior of the get(int index) method in Java's ArrayList for a list of size N, when the value of index is N/2? O(1) O(log N) O(N) O(N log N)

O(1)

What is the Big-O behavior of the get(int index) method in Java's LinkedList class for a list of size N, when the value of index is N/2? O(1) O(log N) O(N) O(N log N)

O(N)

For a balanced BST, what is the behavior of insertion in the average case? O(1) O(log N) O(N) O(N log N)

O(log N)

The height of the tree is __. The depth of node 5 is __.

The height of the tree is 3. The depth of node 5 is 2.

• The root is __. The height is __. • The parent of v3 is __. • The depth of v3 is __. • The children of v6 are __, __, and __. • The ancestors of v1 are __ and __. • The descendants of v6 are __, __, __, and __. • The leaves are __, __, __, __, and __. • There are subtrees rooted at __, __, __, __, __, __, __, and __.

• The root is v4. The height is 3. • The parent of v3 is v2. • The depth of v3 is 2. • The children of v6 are v5, v7, and v8. • The ancestors of v1 are v2 and v4. • The descendants of v6 are v5, v7, v8, and v9. • The leaves are v1, v3, v5, v7, and v9. • There are subtrees rooted at v1, v2, v3, v5, v6, v7, v8, and v9.

In a rooted tree, - - -

• there is one node distinguished as the root, • every non-root node c is the target of exactly one directed edge (p, c) where p is c's parent and c is p's child, and • a unique path traverses from the root to each node.

Evaluate this postfix expression: 4 5 7 2 + - * (HINT: Use a stack.)

-16 4 5 7 2 + - * 4 5 9 - * 4 -4 * -16

Describe the Topological Sort algorithm.

1. Compute the indegree for each vertex. 2. Queue up any vertices with indegree 0. 3. While Q is not empty, x = dequeue() and add x to sorted ordering. Decrement indegree of each vertex connected to x. If the indegree of any vertex becomes 0, enqueue. Note. - Time Complexity > O(V+E) when Adjacency List > O(V^2) when Adjacency Matrix - Digraph, weighted edges

Evaluate this postfix expression: 1 2 3 2 ^ * + (Hint: Use a stack.)

19 1 2 3 2 ^ * + 1 2 9 * + 1 18 + 19

Referring again to the graph in Question 3, how many paths exist from vertex v0 to vertex v6?

5

What is a leaf?

A leaf is a node with no children.

What is a tree?

A tree is an acyclic connected digraph.

Which of the following algorithms is guaranteed to find the shortest path? (More than one choice may be correct.) A. BFS B. DFS C. Dijkstra's (assume all edge weights = 1) D. Toposort

A. BFS C. Dijkstra's (assume all edge weights = 1) BFS finds the shortest paths in an unweighted graph (or a weighted graph, if all edge weights are equal). Dijkstra's finds the shortest paths in a weighted graph, where all edges have non-negative weights.

Which of the following best describes the running time of finding the minimum item in a right-heavy BST (i.e., a BST with no left children)? A. Constant B. Logarithmic in the number of nodes C. Linear in the height of the tree D. Linear in the number of nodes

A. Constant The node with the minimum item is the root node.

Level order traversal of a binary tree uses which of the following? A. FIFO queue B. priority queue C. stack D. none of the above

A. FIFO queue

Which of the following snippets of Java code does not belong in a method to traverse a linked list? (Select all that apply.) A. Node start = temp; B. Node temp = start; C. start = start.next; D. temp = temp.next; E. temp.next = temp; F. while(temp != null)

A. Node start = temp; C. start = start.next; E. temp.next = temp;

Which of the following is true for BFS? (Multiple choices may be correct) A. Uses Queue B. Always finds shortest path C. Visits all neighbors first (connected vertexes) D. Uses Recursion

A. Uses Queue B. Always finds shortest path C. Visits all neighbors first (connected vertexes)

Is this a BST? Compare alphabetically. A. Yes B. No

A. Yes

Same table and hash function. Does inserting item 55 result in a collision? A. Yes B. No

A. Yes 55's hash code is 55. Clamp(55) = |55| % 50 = 5. There is an integer stored at index 5 (collision), probe at index 6 (collision), probe at index 7 (collision), ..., probe at index 14 and insert there. Nine collisions total.

We are running BFS on this Graph starting from vertex 2. Green represent visited edges. What are possible vertexes and its order in the BFS queue? (Multiple choices may be correct) A. [5, 7, 4] B. [4, 5, 7] C. [5, 4, 7] D. [4, 7]

A. [5, 7, 4] C. [5, 4, 7] See, 2 have visited its neighbors 1, 6 but yet to visit its neighbor 5 (since 2 is visited before its neighbor should be start of list) Since, 1 & 6 is already visited their neighbors should be in the list > 1 has no unvisited neighbors > 6 has two neighbors 4, 7

What is the running-time behavior of the get(int i) method of Java's ArrayList, assuming the size is N? (This is equivalent to arr[i] in a basic array.) A. constant B. logarithmic C. linear D. N log N E. quadratic

A. constant Execution goes directly to the elements location in memory.

What is the running-time behavior of the add(int i, E item) method of Java's LinkedList, where i is 0 and assuming the size is N?

A. constant Requires only the creation of a new node (with a "next" reference set to the start/head of the existing linked list) and resetting the the start/head of the linked list to this new node.

Which tree traversal will give us the mathematical formula 2.2 - x / 11 + 7 * y cos? A. inorder B. level-order C. postorder D. preorder E. none of the above

A. inorder Logically, by "processing" the parent node's data in between that of the left and right children, we get nested binary expressions with the operator in between operands.

We are running BFS on this Graph from vertex 2. Green represent visited edges. What are possible return from a dequeue operation? Dequeue() (Multiple choice may be correct) A. 4 B. 5 C. 1 D. 7

B. 5 C. 1 When 2 is visited {6, 1, 5} are enqueued [6, 1, 5] or [6, 5, 1] (6 is visited next, first entry). Now, 6 is visited next so it already dequeue therefore, the first entry in the queue would be either 1 or 5. BFS all neighbors are visited first.

What is the length of the longest path from vertex 2 to vertex 7 in the graph in Figure 3? (Visit every vertex only once, no cycle) A. 5 B. 6 C. 4 D. 3

B. 6 Longest Path: 2 > 5 > 3 > 1 > 6 > 4 > 7

We are running DFS on this Graph from vertex 2. DFS(2) > DFS(1) > DFS(6) > DFS(7) > DFS(?) What could be next recursion call (?) in the DFS(?) (Multiple choices may be correct) A. DFS(2) B. DFS(4) C. DFS(5) D. DFS(6)

B. DFS(4) C. DFS(5) No outgoing unvisited edge from 7 (the DFS(7, call), > so the DFS(7) call complete, > goes back to parent call node 6. Now check unvisited neighbors of 6, possible calls > DFS(5) or DFS(6) (both possible) Note. DFS several possible calls (paths) possible

The following is a correct hash function for String: A. True B. False

B. False A valid hash function must always return the same integer, given the same object.

Which of the following best describes the running time of finding any item in a balanced BST? A. Constant B. Logarithmic in the number of nodes C. Linear in the height of the tree D. Linear in the number of nodes

B. Logarithmic in the number of nodes The longest possible path required to find any item is from the root node to a leaf node. In a balanced BST, this path is guaranteed to be O(log N), where N is the number of nodes.

Is this a BST? Compare using <. A. Yes B. No

B. No !(6.99 > 9.96)

Is this a BST? Compare alphabetically. A. Yes B. No

B. No !(R before P)

Same table and hash function. Does inserting item 65 result in a collision? A. Yes B. No

B. No 65's hash code is 65. Clamp(65) = |65| % 50 = 15. There is no integer stored at index 15, so insert 65 (no collisions).

Is topological sort valid for this graph? A. Yes B. No

B. No Cycles: 3 > 4 > 5 > 3 0 > 2 > 1 > 7 > 0

In the worst case, how many comparisons are necessary to find any item in any BST? A. 1 B. the height of the tree + 1 C. N, the number of nodes in the tree

B. the height of the tree + 1

For each of the following components, indicate whether it is an example of a data structure or an ADT (abstract data type). Basic array - Binary search tree - FIFO queue - Map - Linked list - Stack -

Basic array - data structure Binary search tree - data structure FIFO queue - ADT Map - ADT Linked list - data structure Stack - ADT

What is the depth of the node containing 7? A. 0 B. 1 C. 2 D. 3 E. 4

C. 2 The path length (i.e., number of edges) from the root node containing + to the node containing 7 is two.

Construct a BST by adding 30, 50, 45, 40, 15, 43, 31 (one at a time, in that order) — SAME TREE as previous slide. How many leaf nodes are in the tree? A. 1 B. 2 C. 3 D. 4 E. None of the above

C. 3

Inserting items into a linear-probing hash table in sorted order will result in primary clustering. A. True B. False C. It depends on the hash function used.

C. It depends on the hash function used. Insertion order alone does not determine clustering. Assuming a good hash function, inserting in sorted order will not yield poor performance (unlike the BST in Assignment 8).

Which of the following best describes the running time of finding any item in any BST? A. Constant B. Logarithmic in the number of nodes C. Linear in the height of the tree D. Linear in the number of nodes

C. Linear in the height of the tree The longest possible path required to find any item is from the root node to a leaf node. In any BST, this path is the height of the tree. The height of any BST may range from log N to N, where N is the number of nodes.

Which of the following is true for DFS? (Multiple choices may be correct) A. When run multiple times always find same path B. Always gives shortest path C. Might give longest path D. Uses Recursion

C. Might give longest path D. Uses Recursion

What kind of graph is shown in figure 1? (Only one choice is correct) A. Sparse Directed Graph B. Dense Directed Graph C. Sparse Undirected Graph D. Dense Undirected Graph

C. Sparse Undirected Graph Sparse as |E| = |V| - 1 = 5

A. Node newNode = new Node('n', curr.next); B. curr = new Node('n', curr); C. curr.next = new Node('n', curr.next); D. curr.next = new Node(); E. none of the above

C. curr.next = new Node('n', curr.next);

What is the running-time behavior of the add(int i, E item) method of Java's ArrayList, where i is 0 and assuming the size is N? (This is equivalent to adding a new item at arr[0] in a basic array.) A. constant B. logarithmic C. linear D. N log N E. quadratic

C. linear Requires shifting all elements "up" from index i to index i +1, in order to make space for the new element.

Java's LinkedList is a doubly-linked list. What is the running-time behavior of the get(int i) method of Java's LinkedList, assuming the size is N? A. constant B. logarithmic C. linear D. N log N E. quadratic

C. linear Requires traversal from the beginning (or end) of the linked list to the ith node.

Which is a valid topological sort of the graph represented by the following DOT specification? (Select all that apply.) digraph D { 0 -> 000 00 -> 000 } A. 0 00 000 B. 0 000 00 C. 000 00 0 D. 00 0 000 E. 00 000 0 F. 000 0 00

D. 00 0 000

What is the length of the shortest path from vertex 2 to vertex 7 in the graph in figure 3? A. 5 B. 3 C. 1 D. 2

D. 2 Shortest Path: 2 > 4 > 7 or 2 > 5 > 7 Like this example, many graphs can have start and end vertex with more than one shortest path.

What is the height of the tree? A. 0 B. 1 C. 2 D. 3 E. 4

D. 3 The path length (i.e., number of edges) from the root node containing + to any of the deepest/lowest nodes is three.

Construct a BST by adding 30, 50, 45, 40, 15, 43, 31 (one at a time, in that order). What is the height of the tree? A. 1 B. 2 C. 3 D. 4 E. None of the above

D. 4

Which of the following is true about the height of a binary tree? A. The height of a tree is max(right child subtree height, left child subtree height) + 1. B. The height of a tree with a single node is 0. C. The height can be determined with a recursive method. D. All of the above are true.

D. All of the above are true.

Which of the following best describes the running time of finding the minimum item in a left-heavy BST (i.e., a BST with no right children)? A. Constant B. Logarithmic in the number of nodes C. Linear in the height of the tree D. Linear in the number of nodes

D. Linear in the number of nodes The node with the minimum item is the leaf at the end of the path from the root, and we know that all nodes in the tree are on this path. Linear in the number of nodes is more descriptive than linear in the height of the tree, in this case.

The subtrees are rooted at which nodes? A. 9 and 27 B. 9, 27, 5, and 16 C. 9, 27, 5, 16, and 23 D. all nodes except for the root

D. all nodes except for the root

For which common operation on a collection of data is a hash table less efficient than a binary search tree? A. void add(Type item) B. boolean contains(Type item) C. void remove(Type item) D. void sort() E. none of the above

D. void sort()

How to write the preorder traversal:

Establish base case: if (curr == null) return; add the current item to the list: list.add(curr.getData()); call the recursive method on the left side: preorderRecursive(curr.getLeftChild(), list); call the recursive method on the left side: preorderRecursive(curr.getRightChild(), list);

Dijkstra's algorithm is guaranteed to find the shortest weighted path in a graph for which some edge weights are negative values. True False

False

For a sparse graph, an adjacency matrix representation is an efficient use of space/memory. True False

False

In a BST, if we are removing a node with two children, we can replace it with the smallest item from its left subtree and it will still be a BST. True False

False

First in, first out Last in, first out Queue, Stack, Priority queue, Linked list

First in, first out - Queue Last in, first out - Stack

Consider a left-heavy BST (i.e., all right children are null). What is the running-time behavior of last()? Let N be the number of items in the BST. Let h be the height of the BST. Select the most accurate formula to describe the running-time behavior. O(1) O(log N) O(h) O(N)

O(1)

Java's LinkedList class represents a doubly-linked list. What is the Big-O behavior of its addFirst method for a list of size N? O(1) O(log N) O(N) O(N log N)

O(1)

What is the Big-O behavior of the add method in Java's ArrayList for a list of size N, when the value of index is N (i.e., when adding to the end of the array/list)? You should assume that, in general, the backing array does not need to grow during a call to add. O(1) O(log N) O(N) O(N log N)

O(1)

What is the Big-O behavior of the addLast method in Java's LinkedList class for a list of size N? O(1) O(log N) O(N) O(N log N)

O(1)

Consider a left-heavy BST (i.e., all right children are null). What is the running-time behavior of first()?Let N be the number of items in the BST. Let h be the height of the BST. Select the most accurate formula to describe the running-time behavior. O(1) O(log N) O(h) O(N)

O(N)

For any BST, what is the behavior of insertion in the worst case? O(1) O(log N) O(N) O(N log N)

O(N)

Java's ArrayList class represents a basic array. As a convenience for the user, when the capacity of the backing array is exceeded, the class handles creating a new larger array and copying over the existing items. Its add(int index, E element) method inserts the specified element at the specified position. What is the Big-O behavior of this method for a list of size N, when the value of index is 0 (i.e., when adding to the beginning of the array/list)? O(1) O(log N) O(N) O(N log N)

O(N)

Consider a randomly-constructed BST. What is the running-time behavior of add(x), where x is an item smaller than all of the items currently contained in the BST? Let N be the number of items in the BST. Let h be the height of the BST. Select the most accurate formula to describe the running-time behavior. O(1) O(log N) O(h) O(N)

O(h)

Consider a balanced BST. What is the running-time behavior of add(x), where x is an item smaller than all of the items currently contained in the BST? Let N be the number of items in the BST. Let h be the height of the BST. Select the most accurate formula to describe the running-time behavior. O(1) O(log N) O(h) O(N)

O(log N)

Preorder: _ _ _ _ _ _ _ _ _ Inorder: _ _ _ _ _ _ _ _ _ Postorder: _ _ _ _ _ _ _ _ _ Level-order: _ _ _ _ _ _ _ _ _

Preorder: 20 9 5 2 16 11 19 27 23 Inorder: 2 5 9 11 16 19 20 23 27 Postorder: 2 5 11 19 16 9 23 27 20 Level-order: 20 9 27 5 16 23 2 11 19

The pop operation for a stack has the same Big-O behavior as the dequeue operation for a queue. True False

True

The tree is a BST. True False

True

When a queue is implemented with a basic array, the Big-O behavior of the enqueue operation is the same as when it is implemented with a linked list. True False

True

When a stack is implemented with a basic array, the Big-O behavior of the push operation is the same as when it is implemented with a linked list. True False

True

How to write the inorder traversal

establish the base case: if (curr == null) return; call the recursive method on the left side: inorderRecursive(curr.getLeftChild(), list); add the middle node to the list: list.add(curr.getData()); call the recursive method on the right side: inorderRecursive(curr.getRightChild(), list);

The indegree of a vertex v is its number of _____. The outdegree of a vertex v is its number of _____.

incoming edges (u, v) outgoing edges (v, u)

Describe the Depth First Search algorithm.

starting vertex s: 1. For each vertex, set distance from s to infinity and mark as unvisited. 2. For s, set distance from s to 0 and invoke DFS(s). DFS(x): Mark x as visited. For each edge (x, w) such tat w is unvisited, update to distance from s to x + (x, w)'s weight. In the path from s to w, set previous to x. Invoke DFS(w). Note. - Digraph, weighted edges - Can use Stack - Time Complexity > O(V+E) when Adjacency List > O(V^2) when Adjacency Matrix

Describe the Breadth First Search algorithm.

starting vertex s: 1. For each vertex, set distance from s to infinity. 2. Create a queue of vertices to visit. Enqueue s and set distance from s to 0. 3. While Q is not empty, x = dequeue(). For each edge (x, w) such that the distance from s to w is infinity, update to distance from s to x + 1. In the path from s to w, set previous to x. Enqueue w to be visited. Note. - Digraph, unit edge - Uses Queue - Time Complexity > O(V+E) when Adjacency List > O(V^2) when Adjacency Matrix

Define the order of visiting nodes: Level-order traversal -

• Nodes are visited starting at the root going top to bottom and left to right (i.e., breadth-first). • A FIFO queue stores nodes that are yet to be visited. • When a node is visited, its children are placed at the end of the queue.

Traversals define the order of visiting nodes: • Preorder— • Inorder— • Postorder—

• Preorder—visit the node, then visit left child, then visit right child • Inorder—visit left child, then visit the node, then visit the right child • Postorder—visit left child, then visit right child, then visit the node


Ensembles d'études connexes

Chapter 36: Comfort and Pain Management

View Set

Chapter 47: DT for Myasthenia Gravis, Alzheimer's Disease and Urinary Retention

View Set

CH 17 - Mood Disorders and Suicide

View Set

NUR 111 Tissue Integrity/Pressure Ulcers Pearson Study Plan

View Set

BIOL 343: Genetics Exam 3 Study Guide

View Set

Health Assessment Chapter 1 Questions

View Set