1 / 13

CS112A1 Spring 2008 Practice Final

CS112A1 Spring 2008 Practice Final. ASYMPTOTIC NOTATION : Show that log(n) and ln(n) are the same in terms of Big-Theta notation Show that log(n+1) = O(log(n)). SORTING: Show how quicksort works on the following array, by showing its

eldora
Download Presentation

CS112A1 Spring 2008 Practice Final

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS112A1 Spring 2008 Practice Final

  2. ASYMPTOTIC NOTATION: • Show that log(n) and ln(n) are the same in terms of Big-Theta notation • 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. 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): • True/False: Stack, Queue, and List can be implemented using an array, or a linked list. • 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. AVL TREES: Show the result of inserting the following keys into an empty AVL tree: KONSTANTIN (assume that duplicate keys are not inserted) • HEAPS: • True/False: A Binary Heap is one way to implement a priority queue. • True/False: Like with Binary Search Trees, we can produce the elements in a Heap in sorted order in O(n). • In a Min-Heap, where can the largest element be found? How long would it take to find it?

  5. LEFTIST HEAPS: Show the result of merging the two leftist heaps below: 3 6 10 8 12 7 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. GRAPH REPRESENTATION: Show an adjacency matrix and an adjacency list representation of the graph below: a b c e d 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 e h b c f i d g TOPOLOGICAL SORT: Use the output of your DFS to give a topological ordering of the vertices (if one is possible).

  9. DEPTH FIRST SEARCH: • 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. • True/False: If v is a descendant of u in a depth first spanning tree, then there is a path from u to v. • 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 c e b d

  11. MINIMUM SPANNING TREE (MST): Show the MST returned by Prim’s and Kruskal’s algorithm for the following graph: a 5 c b 6 9 8 4 g 2 7 1 d e f 3 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: • Graph is undirected, unweighted • Graph is directed, unweighted • Graph is weighted, no negative weight edges • 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. 2 a b 10 4 1 3 2 e c 2 d 8 4 5 6 f g 1

More Related