(1.2) Which of the following data structures is more appropriate as an implementation of the queue ADT: singly linked list or doubly linked list?

Similar documents
CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Exam study sheet for CS2711. List of topics

Data Structures and Algorithms Written Examination

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Sample Questions Csci 1112 A. Bellaachia

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Data Structure [Question Bank]

Binary Heap Algorithms

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Social Media Mining. Graph Essentials

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Converting a Number from Decimal to Binary

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

Symbol Tables. Introduction

Analysis of Algorithms, I

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Data Structures Fibonacci Heaps, Amortized Analysis

V. Adamchik 1. Graph Theory. Victor Adamchik. Fall of 2005

IE 680 Special Topics in Production Systems: Networks, Routing and Logistics*

PES Institute of Technology-BSC QUESTION BANK

Ordered Lists and Binary Trees

Sequential Data Structures

CompSci-61B, Data Structures Final Exam

To My Parents -Laxmi and Modaiah. To My Family Members. To My Friends. To IIT Bombay. To All Hard Workers

Algorithms and Data Structures

Cpt S 223. School of EECS, WSU

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

Why? A central concept in Computer Science. Algorithms are ubiquitous.

6 March Array Implementation of Binary Trees

DATA STRUCTURES USING C

Data Structures. Chapter 8

Analysis of a Search Algorithm

GRAPH THEORY LECTURE 4: TREES

Algorithms and data structures

International Journal of Software and Web Sciences (IJSWS)

6. Standard Algorithms

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Cost Model: Work, Span and Parallelism. 1 The RAM model for sequential computation:

Binary Search Trees CMPSC 122

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

Efficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case.

Chapter 8: Bags and Sets

Euclidean Minimum Spanning Trees Based on Well Separated Pair Decompositions Chaojun Li. Advised by: Dave Mount. May 22, 2014

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Lecture Notes on Binary Search Trees

CS711008Z Algorithm Design and Analysis

Data Structures and Algorithms

Lecture 1: Course overview, circuits, and formulas

Any two nodes which are connected by an edge in a graph are called adjacent node.

International Journal of Advanced Research in Computer Science and Software Engineering

Analysis of Algorithms I: Binary Search Trees

Lecture Notes on Binary Search Trees

CS473 - Algorithms I

Now is the time. For all good men PERMUTATION GENERATION. Princeton University. Robert Sedgewick METHODS

THIS CHAPTER studies several important methods for sorting lists, both contiguous

Minimum Spanning Trees

Introduction to Data Structures and Algorithms

Bicolored Shortest Paths in Graphs with Applications to Network Overlay Design

CS5310 Algorithms 3 credit hours 2 hours lecture and 2 hours recitation every week

Algorithms and Data Structures

GENERATING THE FIBONACCI CHAIN IN O(log n) SPACE AND O(n) TIME J. Patera

5. Binary objects labeling

Simplified External memory Algorithms for Planar DAGs. July 2004

From Last Time: Remove (Delete) Operation

Scheduling Shop Scheduling. Tim Nieberg

Algorithms Chapter 12 Binary Search Trees

Algorithms. Margaret M. Fleck. 18 October 2010

Chapter 14 The Binary Search Tree

Rotation Operation for Binary Search Trees Idea:

Persistent Binary Search Trees

Java Software Structures

Zachary Monaco Georgia College Olympic Coloring: Go For The Gold

Mathematics for Algorithm and System Analysis

Dynamic Programming. Lecture Overview Introduction

Finding and counting given length cycles

Heaps & Priority Queues in the C++ STL 2-3 Trees

Approximation Algorithms

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

Part 2: Community Detection

Fast Sequential Summation Algorithms Using Augmented Data Structures

Load balancing Static Load Balancing

AP Computer Science AB Syllabus 1

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Lecture Notes on Linear Search

DATABASE DESIGN - 1DL400

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Outline 2.1 Graph Isomorphism 2.2 Automorphisms and Symmetry 2.3 Subgraphs, part 1

INDISTINGUISHABILITY OF ABSOLUTELY CONTINUOUS AND SINGULAR DISTRIBUTIONS

Pseudo code Tutorial and Exercises Teacher s Version

Transcription:

QUESTION 1 (25 marks) Short answer questions you do not need to provide any justifications for your answers. Just fill in your answer in the space provided. For each correct answer, 1 mark will be awarded. (1.1) True or false: Testing can never be used to prove that a program is correct. (1.2) Which of the following data structures is more appropriate as an implementation of the queue ADT: singly linked list or doubly linked list? Answer: singly linked list (1.3) True or false: Prim s algorithm is asymptotically faster than Dijkstra s algorithm when running on connected graphs. (1.4) True or false: The worst-case running time of selection sort, when applied to an array that is already sorted, is in Θ(n). (1.5) True or false: In divide-and-conquer algorithms, a global optimization problem is solved by making a sequence of local greedy choices. (1.6) True or false: Breadth-first search algorithm works on both cyclic and acyclic graphs. Answer: true 3 of 15

(1.7) True or false: Dijkstra s algorithm discussed in class does not work correctly if some edge weights are negative. Answer: true (1.8) Using the most accurate asymptotic notation, give the worst-case running time of randomized partitioning (in quick sort) on an input array of length n. Answer: Θ(n) (1.9) Using asymptotic notation, give the worst-case running time of testing whether two vertices are neighbors in a graph G = (V, E) if the graph is implemented with adjacency lists. Answer: Θ( V ) (1.10) True or false: Compared to worst-case analysis, amortized analysis provides a more accurate upper bound on the performance of an algorithm. (1.11) Is black box testing a type of dynamic or static testing? Answer: dynamic testing (1.12) True or false: In some exceptional cases a loop invariant for a while loop may not be true before the first execution of the loop. (1.13) Using asymptotic notation, what is the memory space required to represent a graph G = (V, E) using an adjacency-list? Answer: Θ( V + E ) 4 of 15

(1.14) Consider an undirected connected acyclic graph G that has n vertices. How many spanning trees can be identified in G? Answer: one (1.15) Consider a graph G = (V, E). Let deg(v) denote the degree of vertex v V. What is the result of expression v V deg(v) in terms of E and V? Answer: 2 E (1.16) True or false: Every connected graph has a unique predecessor subgraph. (1.17) A telephone company plans to connect its switching centers together using fiber optics. When connecting switching centers, it is required to have a path between every pair of them. The telephone company is looking for an interconnection topology that minimizes the amount of fiber required to connect its switching centers. Which algorithm discussed in class can be used to help the telephone company find the optimal topology? Answer: Prim s Algorithm Minimum Spanning Tree (1.18) An array A is used to implement a binary heap of size n. Give the left child of the heap element stored in location i of the array. Answer: 2i + 1 null if 2i + 1 n 1 (1.19) Which type of hash table is susceptible to primary clustering: hash table with open addressing or hash table with chaining? Answer: hash table with open addressing 5 of 15

(1.20) Which of the following data structures is more appropriate as an implementation of the priority queue ADT: linked list, stack or heap? Answer: heap (1.21) (5 marks) Consider functions f (n) and g(n) as given below. Use the most precise asymptotic notation to show how function f is related to function g in each case (i.e., f?(g)). For example, if you were given the pair of functions f (n) = n and g(n) = n 2 then the correct answer would be: f o(g). To avoid any ambiguity between O(g) and o(g) notations due to writing, use Big-O(g) instead of O(g). f (n) g(n) Relation n 3 + 2n + 1 1 100 n3 + n log n f Θ(g) 2 n n 1000 f ω(g) log 2 n log 3 n f Θ(g) 2 n 3 n f o(g) 0.5 n 1 f o(g) 6 of 15

QUESTION 2 (7 marks) The following questions deal with binary search trees. (2.1) (4 marks) Give pseudocode for a recursive algorithm that searches for a given key in a binary search tree. If the key is found in the tree, your algorithm should return the value associated with that key; otherwise it should throw a NotFoundException exception. The following definitions are assumed: public class BST<E,V> { protected bstnode<e,v> root; protected class bstnode<e,v> { E key; V value; bstnode<e,v> left; bstnode<e,v> right; } } public V search(e key) throws NotFoundException {return search(root, E);} private V search(bstnode<e,v> T, E key) throws NotFoundException; where, you are being asked to complete the following recursive function: V search(bstnode<e,v> T, E key) throws NotFoundException { V search(bstnode<e,v> T, E key) throws NotFoundException { if (T == null) throw new NotFoundException(); else if (key.compareto(t.key) == 0) return T.value; else if (key.compareto(t.key) < 0) return search(t.left, key); else return search(t.right, key); } 7 of 15

(2.2) (3 marks) Let T(h) denote the number of steps used to search in a binary search tree of height h in the worst case. Then there are positive constants c 1 and c 2 such that c 1 if h = 1 T(h) (1) c 2 + T(h 1) if h 0 Use this relation to prove (via mathematical induction) that T(h) c 2 ( h + 1 ) + c 1 (2) (a) Base Case: h = 1 = T( 1) c 1 (b) Inductive Step: assume that for h = 1,, k. Prove that T(h) c 2 ( h + 1 ) + c 1 T(k + 1) c 2 ( k + 2 ) + c 1 Since k + 1 0, using (1), we have T(k + 1) c 2 + T(k) Using (2) for T(k), it is obtained that This completes the proof. T(k + 1) c 2 + T(k) c 2 + c 2 (k + 1) + c 1 = c 2 (k + 2) + c 1 8 of 15

QUESTION 3 (16 marks) The following questions deal with sorting algorithms. (3.1) (5 marks) Consider the merge operation used in mergesort. Give an algorithm using pseudocode that merges two ordered linked lists L 1 and L 2 into one ordered linked list L and returns the combined list. Your algorithm must run in time O(n). The following definitions are assumed: public class List<E> { } public bool isempty(); // returns true if empty; otherwise false public E head(); // returns the head element, but does not remove it public void insert(e e); // inserts `e' at the tail of the list public E delete(); // returns and removes the head element where, you are being asked to complete the following recursive function: List<E> merge(list<e> L 1, List<E> L 2 ) { List<E> merge(list<e> L1, List<E> L2) { L = create an empty list; while (!L1.isEmpty() and!l2.isempty()) { if (L1.head() <= L2.head()) L.insert(L1.delete()); else L.insert(L2.delete()); } while (!L1.isEmpty()) L.insert(L1.delete()); } while (!L2.isEmpty()) L.insert(L2.delete()); 9 of 15

(3.2) (4 marks) Using asymptotic notation, describe the running time of the following algorithms to sort an array of length n. Provide answers for two cases: (i) the input array is sorted in the correct order ( sorted array ), and (ii) the input array contains n copies of the same value ( duplicate array ). sorting algorithm sorted array duplicate array insertionsort Θ(n) Θ(n) mergesort Θ(n log n) Θ(n log n) heapsort Θ(n log n) Θ(n) quicksort Θ(n 2 ) Θ(n 2 ) (3.3) (7 marks) Consider the deterministic partitioning operation used in deterministic quick sort. We modify the partitioning algorithm so that it always partition an input array of length n to two partitions in such a way that the length of the left partition is n K and the length of the right partition is K 1 (for some constant K > 0). Let us refer to this partitioning algorithm as KPartition. Next, consider the following variation of the quick sort algorithm called KQuickSort: KQuickSort(int[] A, int low, int high) n = high low + 1 if n < K then insertionsort(a, low, high) else q = KPartition(A, low, high) KQuickSort(A, low, q-1) // sort the left partition of length n K insertionsort(a, q+1, high) // sort the right partition of length K 1 end if (a) (3 marks) Let T(n) denote the worst-case number of steps to run KQuickSort on input arrays of size n. Complete the following recurrence relation for T(n). Assume that KPartition takes Θ(n) steps to partition an array of size n. c 1 n < K, T(n) c 2 + c 3 n + T(n K) n K for some constants c 1, c 2, c 3 > 0. 10 of 15

(b) (2 marks) Use the substitution technique to find an upper bound on T(n). For simplicity, assume that n is a multiple of K, i.e., n = m K (m 0 is an integer). You may find the following relation useful: n + (n K) + (n 2K) + + K = m (n + K) 2 For n K: T(n) c 2 + c 3 n + T(n K). c 2 + c 3 n + [c 2 + c 3 (n K) + T(n 2K)] = 2c 2 + c 3 [n + (n K)] + T(n 2K) 2c 2 + c 3 [n + (n K)] + [c 2 + c 3 (n 2K) + T(n 3K)] = 3C 2 + c 3 [n + (n K) + (n 2K)] + T(n 3K) mc 2 + c 3 [n + (n K) + (n 2K) + + K] + T(0) = mc 2 + c 3 1 2 m(n + K) + c 1 = c 2 K n + c 3 2K n(n + K) + c 1 (c) (1 mark) Using the Big-O notation, give an asymptotic expression for the worst-case running time of KQuickSort (no proof is required). O(n 2 ) (d) (1 mark) How does the worst-case asymptotic running time of KQuickSort compares with that of quicksort? Both have the same worst-case running time O(n 2 ). 11 of 15

QUESTION 4 (12 marks) The following questions deal with graphs and their algorithms. (4.1) (4 marks) Recall that the depth-first search algorithm presented in class assumes an adjacencylist representation of the input graph. We modify the algorithm to work with an adjacencymatrix representation of the input graph. The modified algorithm is presented below in which the for loop in DFS-Vist has been modified to iterate over the adjacency matrix Adj. The rest of the algorithm remains intact. DFS(G = (V, E), s) 1: for each vertex u V do 2: colour[u] = white 3: π[u] = NIL 4: end for 5: DFS-Visit(s) 6: return π DFS-Visit(u) 1: colour[u] = grey 2: for each vertex v V do 3: if Adj[u, v] == 1 and colour[v] == white then 4: π[v] = u 5: DFS-Visit(v) 6: end if 7: end for 8: colour[u] = black (a) (2 marks) What is the worst-case asymptotic running time of the modified DFS algorithm on a graph G = (V, E)? Briefly justify your answer. i. initialization: Θ( V ) ii. no. of calls to DFS-Visit: Θ( V ) iii. no. of steps in each call of DFS-Visit: Θ( V ) Therefore, the total running time is in Θ( V 2 ) (b) (2 marks) How does the worst-case asymptotic running time of the modified DFS compare with that of the unmodified DFS presented in class? Briefly explain. The worst-case running time of unmodified algorithm is in Θ( V + E ). In general, E < V 2. Thus, the modified algorithm is always slower. Specifically, for dense graphs where E = O( V 2 ), both algorithms have the same asymptotic running time. However, for sparse graphs where E = O( V ), the unmodified version is asymptotically faster. 12 of 15

(4.2) (4 marks) Consider the following directed graph and assume that the neighbors of any vertex are always accessed in their respective alphabetical order by label. b e a d g c f (a) (2 marks) Give the final contents of the array π (i.e., the predecessor function) after running depth-first search on this graph, using vertex a as the source. Draw the resulting depth-first tree. b e a d g a b c d e f g π a f e b e f c f (b) (2 marks) Give the final contents of the array d (i.e., the distance function) after running breadth-first search on this graph, using vertex a as the source. Draw the resulting breadth-first tree. b e a d g a b c d e f g d 0 1 1 2 2 3 3 c f 13 of 15

(4.3) (4 marks) Consider the following weighted undirected graph and assume that the neighbors of any vertex are always accessed in their respective alphabetical order by label. 3 a 2 c b 2 1 1 f e 3 d 4 (a) (2 marks) Draw the shortest-paths tree produced by running the Dijkstra s algorithm on this graph, using vertex a as the source. 3 a 2 c b 1 1 f e 3 d (b) (2 marks) Draw the minimum-cost spanning tree produced by running the Prim s algorithm on this graph, using vertex a as the source. a 2 c b 2 1 1 f e 3 d 14 of 15

extra page 15 of 15