Data Structure and Algorithm Homework #5 Due: 2:00pm, Thursday, May 31, 2012 TA === Homework submission instructions ===

Similar documents
Data Structure and Algorithm Homework #2 Due: 2:00pm, Thursday, March 29, 2012 TA

Data Structure and Algorithm Homework #3 Due: 2:20pm, Tuesday, April 9, 2013 TA === Homework submission instructions ===

Algorithm Design and Analysis Homework #2 Due: 2:20pm, Thursday, October 17, 2013 TA

Fundamental Algorithms CSCI-GA /Summer Solution to Homework 8

Data Structure and Algorithm Homework #6 Due: 5pm, Friday, June 14, 2013 TA === Homework submission instructions ===

Data Structure and Algorithm I Homework #4 Due: 5pm, Sunday, December 12, 2010

Introductory Computer Programming: Shortest Path Algorithms

Problem Set 5. Part A

Chapter 8 Sorting in Linear Time

Data Structure and Algorithm Homework #1 Due: 1:20pm, Tuesday, March 21, 2017 TA === Homework submission instructions ===

CS350: Data Structures Dijkstra s Shortest Path Alg.

Data Structure and Algorithm II Homework #2 Due: 13pm, Monday, October 31, === Homework submission instructions ===

Dynamic Programming. CSE 101: Design and Analysis of Algorithms Lecture 19

Algorithm Design and Analysis Homework #4

Design and Analysis of Algorithms PART III

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

CSC2100-Data Structures

CS261: Problem Set #2

Sample Solutions to Homework #4

Single Source Shortest Path: The Bellman-Ford Algorithm. Slides based on Kevin Wayne / Pearson-Addison Wesley

UC Berkeley CS 170: Efficient Algorithms and Intractable Problems Handout 7 Lecturer: David Wagner February 13, Notes 7 for CS 170

Exam 3 Practice Problems

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

The Shortest Path Problem. The Shortest Path Problem. Mathematical Model. Integer Programming Formulation

CSE 101. Algorithm Design and Analysis Miles Jones Office 4208 CSE Building Lecture 8: Negative Edges

Homework 2 Solutions

Homework Assignment #3

Design and Analysis of Algorithms

CS490 Quiz 1. This is the written part of Quiz 1. The quiz is closed book; in particular, no notes, calculators and cell phones are allowed.

Introduction to Algorithms and Data Structures. Lecture 12: Sorting (3) Quick sort, complexity of sort algorithms, and counting sort

Data Structure and Algorithm Homework #3 Due: 1:20pm, Thursday, May 16, 2017 TA === Homework submission instructions ===

1 Shortest Paths. 1.1 Breadth First Search (BFS) CS 124 Section #3 Shortest Paths and MSTs 2/13/2018

6.1 Skip List, Binary Search Tree

Homework 2. Sample Solution. Due Date: Thursday, May 31, 11:59 pm

CS61BL. Lecture 5: Graphs Sorting

Lecture 5: Sorting in Linear Time CSCI Algorithms I. Andrew Rosenberg

Ling 473 Project 4 Due 11:45pm on Thursday, August 31, 2017

Homework #2 Assembly Programming

CS2223: Algorithms D-Term, Assignment 5

CMPSCI 187 / Spring 2015 Sorting Kata

Question Points Score TOTAL 50

Homework No. 4 Answers

Data Structures Brett Bernstein

These are not polished as solutions, but ought to give a correct idea of solutions that work. Note that most problems have multiple good solutions.

Part 1: Written Questions (60 marks):

Chapter 8 Sort in Linear Time

Quiz 2 CS 3510 October 22, 2004

Chapter 6. Dynamic Programming

Homework 1 Due Monday April 24, 2017, 11 PM

Ma/CS 6a Class 27: Shortest Paths

Lecture 6 Sorting and Searching

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

CSCI544, Fall 2016: Assignment 1

Minimum Spanning Trees

Minimum Spanning Tree

Dijkstra s algorithm for shortest paths when no edges have negative weight.

CS 161 Fall 2015 Final Exam

Data Structure and Algorithm, Spring 2017 Final Examination

Your favorite blog : (popularly known as VIJAY JOTANI S BLOG..now in facebook.join ON FB VIJAY

Name: 2 Useful Formulas Pn i=1 i = nèn+1è 2 Pn i=1 xi = xn+1,1 x,1 P Pn 1 =lnn + Oè1è 1 i=1 i i=0 = ixi x è1,xè 2 e x =1+x + x2 + x 3 2! 3! + ::: lim

Graph Algorithms (part 3 of CSC 282),

Sorting Algorithms. CSE21 Winter 2017, Day 2 (B00), Day 1-2 (A00) January 11, 2017

Problem 1. Which of the following is true of functions =100 +log and = + log? Problem 2. Which of the following is true of functions = 2 and =3?

CSE 21 Spring 2016 Homework 5. Instructions

CSE 21 Spring 2016 Homework 5. Instructions

Algorithms and Data Structures for Mathematicians

EECE.4810/EECE.5730: Operating Systems Spring 2018 Programming Project 3 Due 11:59 PM, Wednesday, 4/18/18 Monday, 4/30/18

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist.

CpSc 1111 Lab 9 2-D Arrays

Algorithms Chapter 8 Sorting in Linear Time

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

About Dropbox. Managing the Dropbox

EE/CSCI 451 Spring 2018 Homework 8 Total Points: [10 points] Explain the following terms: EREW PRAM CRCW PRAM. Brent s Theorem.

END-TERM EXAMINATION

Arbitrage. CS 320, Fall Dr. Geri Georg, Instructor 320 Arbitrage 1

CS 202 Fundamental Structures of Computer Science II

CSCI4211: Introduction to Computer Networks Fall 2017 Homework Assignment 2

CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16. In-Class Midterm. ( 11:35 AM 12:50 PM : 75 Minutes )

Can we do faster? What is the theoretical best we can do?

King Abdulaziz University Faculty of Computing and Information Technology Computer Science Department

CS 1803 Pair Homework 4 Greedy Scheduler (Part I) Due: Wednesday, September 29th, before 6 PM Out of 100 points

Homework #5 Algorithms I Spring 2017

Com S 227 Assignment Submission HOWTO

Introduction to Algorithms November 28, 2007 Massachusetts Institute of Technology Fall 2007 Professors Ron Rivest and Srini Devadas Quiz 2

Minimum Spanning Trees My T. UF

CSCE 310 Assignment 3 Summer 2018

Algorithms: Design & Practice

Introduction to Algorithms May 14, 2003 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser.

CS 125 Section #6 Graph Traversal and Linear Programs 10/13/14

Artificial Intelligence

Graph Algorithms (part 3 of CSC 282),

Dynamic-Programming algorithms for shortest path problems: Bellman-Ford (for singlesource) and Floyd-Warshall (for all-pairs).

Introduction to Algorithms April 16, 2008 Massachusetts Institute of Technology Spring 2008 Professors Srini Devadas and Erik Demaine Quiz 2

UNIT 3. Greedy Method. Design and Analysis of Algorithms GENERAL METHOD

CSC 373 Lecture # 3 Instructor: Milad Eftekhar

Instructions PLEASE READ (notice bold and underlined phrases)

CS161 - Final Exam Computer Science Department, Stanford University August 16, 2008

Introduction to Algorithms November 17, 2011 Massachusetts Institute of Technology Fall 2011 Professors Erik Demaine and Srini Devadas Quiz 2

CS261: Problem Set #1

CS 170 Spring 2000 Solutions and grading standards for exam 1 Clancy

Transcription:

Data Structure and Algorithm Homework #5 Due: 2:00pm, Thursday, May 1, 2012 TA email: dsa1@csie.ntu.edu.tw === Homework submission instructions === For Problem 1, submit your source code, a shell script to compile the source, and a brief documentation to the SVN server (katrina.csie.ntu.edu.tw). You should create a new folder hw5 and put these three files in it. The filenames of the source code, the shell script, and the documentation file should be arbitrage.c, compile.sh, and report.txt, respectively. The shell script should compile your source codes to generate an executable binary with the filename arbitrage. You will get some penalties in your grade if your submission do not follow the naming rule. The documentation file should be in plain text format (.txt file). In the documentation file you should explain how your code works, and anything you would like to convey to the TAs. For Problem 2 to Problem 4, submit the answers through the CEIBA system (electronic copy) or to the TA in R508 (hard copy). Each student may only choose to submit the homework in one way; either all as hard copies or all through CEIBA except the programming assignment. If you submit your homework partially in one way and partially in the other way, you might only get the score of the part submitted as hard copies or the part submitted through CEIBA (the part that the TA chooses). If you choose to submit the answers of the writing problems through CEIBA, please combine the answers of all writing problems into only ONE file in the doc/docx or pdf format; otherwise, you might only get the score of one of the files (the one that the TA chooses). In addition, do NOT forget to specify your name and student ID in your submitted file. Discussions with others are encouraged. However, you should write down your solutions by your own words. In addition, you have to cite the sources you consulted from or people you discussed with on the first page of your solution to that problem. The TA can deduct up to 100% of the score for any problem without the citation. No late submission of the homework will be given any score (for that portion). Problem 1. (0%) Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1

1 U.S. dollar buys 49 Indian rupees, 1 Indian rupee buys 2 Japanese yen, and 1 Japanese yen buys 0.0107 U.S. dollars. Then, by converting currencies, a trader can start with 1 U.S. dollar and buy 49 2 0.0107 = 1.0486 U.S. dollars, thus turning a profit of 4.86 percent. Suppose that we are given n currencies c 1, c 2,..., c n and an n n table R of exchange rates, such that one unit of currency c i buys R[i, j] units of currency c j. We would like to find a sequence of currencies c i1, c i2,..., c ik such that R[i 1, i 2 ] R[i 2, i ] R[i k 1, i k ] R[i k, i 1 ] is more than 1.01, where k n. If {R[i 1, i 2 ] R[i 2, i ] R[i k 1, i k ] R[i k, i 1 ]} > 1.01, we call this set of currencies profitable. To result in successful arbitrage, a sequence of exchanges must begin and end with the same currency, but any starting currency may be considered. Input: The first line contain n, 2 n 50. The next n lines represent the exchange rate table R in the following format: R[1, 1] R[1, 2] R[1, ]... R[1, n] R[2, 1] R[2, 2] R[2, ]... R[2, n]... R[n, 1] R[n, 2] R[n, ]... R[n, n] with all numbers ranging from 10 5 to 10 5. Output: For each input table you must determine whether a sequence of exchanges exists that results in a profit of more than 1 percent (0.01). If a sequence exists you must print the sequence of exchanges that results in a profit. If there is more than one sequence that results in a profit of more than 1 percent you must print a sequence of minimal length and minimal starting index, i.e., one of the sequences that uses the fewest exchanges of currencies to yield a profit. Furthermore, if there are more than one minimal length sequence that results in a profit of more than 1 percent such as 1,,2,1 and 1,2,,1, we can print either one of the sequences. All profiting sequences must consist of n or fewer transactions where n is the dimension of the table giving exchange rates. You should output the profitable sequence in the following format: i 1, i 2,..., i k, i 1 where i 1, i 2,..., i k, i 1 is the sequence of the indices of the currencies in the profitable exchange, where i 1 i j, for j = 2,..., k. If no profitable sequence that results in a profit of more than 1 percent (0.01) can be found within n transactions, output Not profitable. All numbers in this output line are separated by comma. Sample Input 1: 1 0.6 0.8 1.6 1 1.2 2

1.2 0.7 1 Sample Output 1: Not profitable Sample Input 2: 1 0.05 0.21 16 1 4 5.1 0.09 1 Sample Output 2: 1,,1 Please write a program to solve this problem. Please also submit a report in which you give a clear description of your code. Problem 2. Shortest Path Algorithms (22%) 1. (6%) Use Dijkstra s Algorithm to determine the shortest-path distance from vertex a to all other vertices in Figure 1. Please create a table similar to the one on page 9 of the lecture note graph to show how the algorithm is executed. Figure 1: Determine the shortest-path distance by Dijkstra s Algorithm 2. (6%) Use Bellman-Ford Algorithm to determine the shortest-path distance from vertex a to all other vertices in Figure 2. Please create a table similar to the one on page 19 of the lecture note graph to show how the algorithm is executed.

Figure 2: Determine the shortest-path distance by Bellman-Ford Algorithm. (5%) Explain why, in a directed graph G, we CANNOT use Dijkstra s Algorithm to find the longest simple path from one vertex to another. A simple path is a path in which each vertex appears at most once. 4. (5%) If we restrict the graph G in. to a Directed Acyclic Graph (DAG), can Dijkstras s Algorithm be applied to find the longest path? Why or why not? Problem. More on Graph (16%) 1. (8%) Give a linear-time algorithm to examine whether an undirected and connected graph G(V, E) contains any cycle. You do not have to list the vertices that the cycle is composed of. What is the time complexity of your algorithm? Note that if your algorithm is written in C code or pseudo code, you should explain it in detail. 2. (8%) Given a directed and weighted graph G(V, E) with ONE negative cycle, describe an efficient algorithm to find the vertices in this cycle. Note that if your algorithm is written in C code or pseudo code, you should explain it in detail. Problem 4. Sorting (2%) 1. (4%) Write down the sequence of the list (5,, 2, 7, 8, 6, 1, 9, 4) at the end of each iteration when performing an insertion sort. 2. (4%) Use quick sort to sort the list (5,, 2, 7, 8, 6, 1, 9, 4). Write down the sequence of the list after each iteration and mark the parts are processed in that iteration and are smaller and larger than the pivot, respectively. In this problem, you are asked to pick the left-most element as the pivot.. (4%) Give an example input with which quick sort may run in O(n 2 ), where n is the number of numbers in the input list to be sorted. 4

4. (4%) Write down the sequence of the list (5,, 2, 7, 8, 6, 1, 9, 4) at the end of each phase when performing a merge sort. 5. (4%) Insert the following list (5,, 2, 7, 8, 6, 1, 9, 4) into an initially empty max-heap in the given order and then use heap sort to sort this list. You have to show the content of the max-heap after inserting all the elements as well as the content of the max-heap at the end of each extraction (after the maximum value of the max-heap is returned and deleted). 6. (4%) The following pseudo code is the algorithm of counting sort. A is a given list and k is the range of all the elements in A, for example, if A = {2, 12, 10}, k = 12. B is an initially empty list for holding the sorted elements of A. (X[i] indicates the i-th element of a list X.) COUNTING-SORT(A, B, k) for i = 0 to k do C[i] = 0 for j = 1 to length[a] do C[A[j]] = C[A[j]] + 1 // C[i] now contains the number of elements equal to i. for i = 1 to k do C[i] = C[i] + C[i - 1] // C[i] now contains the number of elements less than or equal to i. for j = length[a] downto 1 do B[C[A[j]]] = A[j] C[A[j]] = C[A[j]] - 1 Use counting sort to sort the list (5,, 2, 7, 8, 6, 1, 9, 4). You have to show the content of B and C at the end of each iteration of the fourth for loop. 7. (4%) Use radix sort to sort the following list (501, 99, 117, 245, 666, 4, 218). Set the base r = 10 for radix sort and use the counting sort algorithm to sort the base elements of each pass of radix sort. Write down the sequence of the list at the end of each pass of radix sort. 8. (4%) Continuing the previous problem, please use n, k, and r to analyze the time complexity of radix sort with the case in which we only use counting sort to sort the given list (501, 99, 117, 245, 666, 4, 218). Then compare the computational cost between these two algorithms for solving this problem. 5