Data Structures and Algorithms Written Examination



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

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

Binary Search Trees CMPSC 122

Warshall s Algorithm: Transitive Closure

CompSci-61B, Data Structures Final Exam

Data Structure [Question Bank]

PES Institute of Technology-BSC QUESTION BANK

Fast Sequential Summation Algorithms Using Augmented Data Structures

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

Exam study sheet for CS2711. List of topics

The number of marks is given in brackets [ ] at the end of each question or part question. The total number of marks for this paper is 72.

Outline. NP-completeness. When is a problem easy? When is a problem hard? Today. Euler Circuits

Cpt S 223. School of EECS, WSU

Data Structures Fibonacci Heaps, Amortized Analysis

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

Dynamic Programming. Lecture Overview Introduction

Problem Set 7 Solutions

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

Ordered Lists and Binary Trees

MATHEMATICS Unit Decision 1

Max Flow, Min Cut, and Matchings (Solution)

Sequential Data Structures

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

Discuss the size of the instance for the minimum spanning tree problem.

Class One: Degree Sequences

Lecture 7: NP-Complete Problems

Algorithms and Data Structures Written Exam Proposed SOLUTION

Minimum cost maximum flow, Minimum cost circulation, Cost/Capacity scaling

Converting a Number from Decimal to Binary

Mathematical Induction. Lecture 10-11

Dynamic programming. Doctoral course Optimization on graphs - Lecture 4.1. Giovanni Righini. January 17 th, 2013

Computer Algorithms. NP-Complete Problems. CISC 4080 Yanjun Li

Data Structures. Level 6 C Module Descriptor

Section IV.1: Recursive Algorithms and Recursion Trees

Scheduling Shop Scheduling. Tim Nieberg

Sample Questions Csci 1112 A. Bellaachia

Data Structures in Java. Session 15 Instructor: Bert Huang

Decision Mathematics 1 TUESDAY 22 JANUARY 2008

Zabin Visram Room CS115 CS126 Searching. Binary Search

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

CS 103X: Discrete Structures Homework Assignment 3 Solutions

Analysis of Algorithms, I

Handout #Ch7 San Skulrattanakulchai Gustavus Adolphus College Dec 6, Chapter 7: Digraphs

Data Structures and Algorithms

Algorithms and Data Structures

136 CHAPTER 4. INDUCTION, GRAPHS AND TREES

VISUAL ALGEBRA FOR COLLEGE STUDENTS. Laurie J. Burton Western Oregon University

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

Part 2: Community Detection

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

Lecture 1: Course overview, circuits, and formulas

Graph Theory Problems and Solutions

Approximation Algorithms

Question 1. [7 points] Consider the following scenario and assume host H s routing table is the one given below:

Social Media Mining. Graph Essentials

One last point: we started off this book by introducing another famously hard search problem:

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

ML for the Working Programmer

B AB 5 C AC 3 D ABGED 9 E ABGE 7 F ABGEF 8 G ABG 6 A BEDA 3 C BC 1 D BCD 2 E BE 1 F BEF 2 G BG 1

Pseudo code Tutorial and Exercises Teacher s Version

Lecture Notes on Binary Search Trees

Euler Paths and Euler Circuits

COMPUTER SCIENCE. Paper 1 (THEORY)

Full and Complete Binary Trees

Solutions to Homework 6

DATA STRUCTURES USING C

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

GRAPH THEORY LECTURE 4: TREES

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:

CMPSCI611: Approximating MAX-CUT Lecture 20

Discrete Mathematics Problems

Data Structures. Chapter 8

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Lecture Notes on Binary Search Trees

Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally

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

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

A Review And Evaluations Of Shortest Path Algorithms

WRITING PROOFS. Christopher Heil Georgia Institute of Technology

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

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

Algorithm Design and Analysis

Algorithms and data structures

Network (Tree) Topology Inference Based on Prüfer Sequence

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

Analysis of a Search Algorithm

MATHEMATICS Unit Decision 1

Introduction to Logic in Computer Science: Autumn 2006

Computational Geometry. Lecture 1: Introduction and Convex Hulls

Answer: (a) Since we cannot repeat men on the committee, and the order we select them in does not matter, ( )

Linear Programming. March 14, 2014

Area and Perimeter: The Mysterious Connection TEACHER EDITION

Data Structures. Algorithm Performance and Big O Analysis

CS211 Spring 2005 Final Exam May 17, Solutions. Instructions

Decision Mathematics D1 Advanced/Advanced Subsidiary. Tuesday 5 June 2007 Afternoon Time: 1 hour 30 minutes

Reductions & NP-completeness as part of Foundations of Computer Science undergraduate course

Lecture 17 : Equivalence and Order Relations DRAFT

Transcription:

Data Structures and Algorithms Written Examination 22 February 2013 FIRST NAME STUDENT NUMBER LAST NAME SIGNATURE Instructions for students: Write First Name, Last Name, Student Number and Signature where indicated. If not, the examination can not be marked. Do not speak to any other student during the examination. If you speak to another student, your examination will be cancelled. Use a pen, not a pencil. Write neatly and clearly.

Data Structures and Algorithms Exam Werner Nutt 22 Februar 2013 The exam comprises 4 questions, which consist of several subquestions. You will have 2 hours time to answer the questions. There is a total of 150 points that can be achieved in this exam. Marking will be out of 120. That is, to achieve a final mark of 30, however, it will suffice to obtain 120 points. Please, write down the answers to your questions in the exam booklet handed out to you. For drafts use the blank paper provided by the university. If the space in the booklet turns out to be insufficient, please use the university paper for additional answers and return them with the booklet. No questions will be answered during the exam. If you are not sure about interpreting a question, you may write down additional assumptions you made in order to proceed with your solution. 1

1 Divide And Conquer Consider an array A of integers. Suppose that A.length returns the length of such an array. (i) Write down an iterative algorithm in pseudocode that returns true if the array is sorted and false otherwise. (8 points) 2

(ii) Write a recursive algorithm following the divide-and-conquer approach that tests whether A is sorted. Start from the fragment below. boolean sorted (array A, int l, int r) if l = r then return true else if r = l + 1 then... else... (12 points) 3

(iii) Briefly explain why your algorithm is correct. (3 points) (iv) Analyze the running time T(n) of the algorithm. Formulate a recursion for T(n) and apply the Master Theorem. (3 points) 4

2 Sorting with Lists (i) Describe in your own words the idea of the Insertion Sort algorithm for arrays. (6 points) (ii) Write down a Java class declaration for doubly linked lists consisting of elements with an integer as key. (6 points) 5

(iii) Write down code (in Java or in pseudocode) for an operation insert that takes as input a sorted doubly linked list an element like those in the list and inserts the element in the list such that the resulting list is sorted. (8 points) 6

(iv) Write down code (in Java or in pseudocode) for a version of Insertion Sort that sorts doubly linked lists. (8 points) 7

3 Binary Tree Algorithms We consider binary trees that have integers as key values. (i) Write down a Java class declaration for such binary trees. Hint: There is no need to distinguish in the data structure between internal nodes and leaves. All are nodes of the same kind. (6 points) We want to compute for a given binary tree the sum of all key values. (ii) Write pseudocode for an algorithm NodeSum that takes a tree as input and returns the sum of the keys of all nodes of that tree. Hint: You may want to write the algorithm as a recursive function. (10 points) 8

(iii) Explain why your algorithm NodeSum is correct. (3 points) (iv) What is the asymptotic running time of NodeSum for a tree with n nodes? Explain your answer. (3 points) 9

Next, we want to compute for a given binary tree the maximum of all key values. Note that we do not assume that the input trees are search trees and remember that all keys are positive. (v) Write pseudocode for an algorithm NodeMax that takes a tree as input and returns the maximum of the keys of all nodes of that tree. Hint: You may want to write the algorithm as a recursive function. (10 points) 10

(vi) Explain why your algorithm NodeMax is correct. (3 points) (vii) What is the asymptotic running time of NodeMax for a tree with n nodes? Explain your answer. (3 points) 11

4 Graph Algorithms A graph is a pair G = (V, E), where V is the set of vertices (or nodes) and E the set of edges of the graph. In a directed graph, or digraph, the edges have a direction. In a weighted directed graph, a number is attached to each edge. (i) Write down Java class definitions to implement weighted directed graphs. (10 points) 12

Dijkstra s algorithm finds shortest paths in weighted directed graphs. Consider the directed graph G 1 with the set of vertices { A, B, C, D, E } and eight weighted edges, given by the following triples: (A, B, 9), (A, C, 2), (A, D, 6), (C, D, 3), (C, E, 1), (D, B, 2), (E, B, 3), (E, D, 1). For example, the triple (A, B, 9) says there is an edge from A to B with weight 9. (ii) Make a drawing of the graph with sufficient space among the vertices. Run Dijkstra s algorithm on this graph with start vertex A and target vertex B. Visualize the execution of the algorithm, like we did in the lecture. It should become clear in which order the different steps were executed. In particular, it should be clear how many steps were needed and what is the cost of the shortest path from A to B. (8 points) 13

From now on we consider graphs without edge weights. Moreover, we concentrate on graphs without cycles, that is, directed acyclic graphs (DAGs). (iii) Give an example of a directed graph that is a DAG and another example of a directed graph that is not a DAG. (3 points) For a DAGs, one can topologically sort the graph vertices. (iv) Explain what is a topological sort of the vertices of a DAG. (4 points) 14

Consider the directed graph G 2 with the set of vertices { A, B, C, D, E, F, H } and the edges (A, B), (A, C), (A, D), (A, E), (C, F), (D, E), (D, H), (F, H), (H, B), (H, E) (v) Write down an ordering of the vertices that is a topological sort for the graph G 2. (4 points) 15

Since we do not consider weights anymore, the length of a path is now the number of edges on the path. (Clearly, this is the same as assuming that all edges have weight 1.) In some applications it is important to find the longest path in a DAG. The longest path problem is, given a DAG G = (V, E) and a start vertex s in V, to label every vertex v in V with the length v.len of the longest path from s to v. (vi) Explain what it means that a problem has the optimal substructure property! Does the longest path problem have the optimal susbstructure property? Explain your answer. (4 points) (vii) Describe an algorithm that, given a DAG and a start vertex s, returns for each vertex v the length of the longest path from s to v. You need not write pseudo-code and can describe the steps of the algorithm in words. Hint: You may want to first topologically sort the vertices of the graph. (20 points) 16

17

(viii) Explain how your algorithm finds the length of the longest path in the graph G 2 above from A to B, from A to C, and from A to D. (5 points) 18