Download presentation
Presentation is loading. Please wait.
Published byClement Morgan Modified over 9 years ago
1
CS112A1 Spring 2008 Practice Final
2
ASYMPTOTIC NOTATION: a)Show that log(n) and ln(n) are the same in terms of Big-Theta notation b)Show that log(n+1) = O(log(n))
3
SORTING: Show how quicksort works on the following array, by showing its contents after each recursive call, and marking the pivot element’s final location. Assume the pivot is always chosen to be the last element in the array. 032131203021 Now show how mergesort would work on the same array, by showing the two sub-arrays merged at each iteration and the resulting merged array.
4
SIMPLE ABSTRACT DATA TYPES (ADTs): a)True/False: Stack, Queue, and List can be implemented using an array, or a linked list. b)True/False: Stack is a FIFO (first in, first out) data structure, while Queue is a LIFO (last in, first out data structure). c) True/False: Stack and Queue can be implemented using a LIST. HEAPS: a)True/False: A Binary Heap is one way to implement a priority queue. b)True/False: Like with Binary Search Trees, we can produce the elements in a Heap in sorted order in O(n). c)In a Min-Heap, where can the largest element be found? How long would it take to find it? AVL TREES: Show the result of inserting the following keys into an empty AVL tree: KONSTANTIN (assume that duplicate keys are not inserted)
5
3 108 6 127 LEFTIST HEAPS: Show the result of merging the two leftist heaps below: Evo Morales, president of Bolivia, is known for his colorful native sweaters (which he wears to formal occasions). Give an argument why the merge operation preserves the leftist and the heap order property of the resulting heap.
6
HASHING: Illustrate the insertion of the keys below into a hash table with the hash function h(k) = (2k + 5) mod 11, using an array of size 11, collisions are handled with a second hash function h'(k) = 7 - k mod 7. 12 22 13 88 23 94 11 39 20 15 5
7
ab d e GRAPH REPRESENTATION: Show an adjacency matrix and an adjacency list representation of the graph below: c Which representation will use less memory for this graph?
8
BREADTH FIRST SEARCH: while BFS is still running, what is the smallest and largest possible size of the queue? DEPTH FIRST SEARCH: Show how DFS is run on the graph below. For each vertex, mark its discovery and finishing time. Draw the resulting depth first spanning tree forest. Assume that vertices are processed in alphabetical order. a bc d e f g h i TOPOLOGICAL SORT: Use the output of your DFS to give a topological ordering of the vertices (if one is possible).
9
DEPTH FIRST SEARCH: a)True/False: If v is discovered from u, then f(u) > f(v), where f(u) and f(v) denote the finishing times of u and v in a DFS. b)True/False: If v is a descendant of u in a depth first spanning tree, then there is a path from u to v. c)True/False: If d(u) < d(v) < f(v) < f(u), then v is a descendant of u in a depth first spanning tree, where d and f denote discovery/finishing times in a DFS. d) Argue that if there is a path from u to v and f(v) > f(u), there must be a path from v to u. Why does correctness of dfs-based topological sort follow?
10
STRONGLY CONNECTED COMPONENTS (SCC): Show how the DFS-based SCC algorithm works on the following graph: a b c d e
11
MINIMUM SPANNING TREE (MST): Show the MST returned by Prim’s and Kruskal’s algorithm for the following graph: a b c def g 2 3 1 4 5 6 7 9 8 Do they find the same MST? Will this always be the case?
12
SHORTEST PATHS: What algorithm will you use to find shortest paths in the graph in each situation: a)Graph is undirected, unweighted b)Graph is directed, unweighted c)Graph is weighted, no negative weight edges d)Graph is weighted, with negative weight edges Why are shortest paths not defined for networks with negative weight cycles? We have seen that the RELAX subroutine is part (of an efficient implementation) of Prim, Dijkstra, and Bellman-Ford algorithms. If we relax edge (u,v) of weight 3, and v has a distance of 6, and u has a distance of 2, will the parent of v be set to u?
13
SHORTEST PATHS: For the graph below, find shortest paths from a to all other vertices using Dijkstra’s algorithm. List the order in which shortest paths of the vertices will be discovered, and the shortest path distances. ab cd e fg 4 2 10 6 1 5 8 4 2 31 2
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.