Department of Computer Science and Engineering COSC 2011: Assignment 1 (v.4) Due Date - Friday, October 10 by Noon For any of the following exercises, you may assume that you have access to an efficient sort routine, say mergesort or heapsort, which sorts an array of comparable objects in worst-case running-time of O(n log n). 1. Algorithm Design: Algorithm for Breaking a Secret Key (15 %) 1 Consider a set of n words (S). Each word (w) in S has a unique value of asciisum(w), which is obtained by summing up the ASCII values of all characters in w. The access to a database is protected with a secret-key consisting of two words, i.e. a secret-key in the form of [ID, Password] pair. Each of the two components has been chosen from S (ID, Password S). Together, the selected ID and Password satisfy the following m is a positive integer constant. asciisum(id) + asciisum(password) = m Lock ID ***** Password ******** Figure 1 Secret Lock Now, assume that all words from S and the value of m are known to us. Design an O(n log(n))-time algorithm that searches through all possible pair combinations [ID, Password] of words from S, until the right one (i.e. the actual secret-key) is found. Describe your algorithm both in English and in pseudo-code or Java. Mention any additional data structure used, and explain in detail why the timecomplexity is O(n log(n)). If you decide to describe your algorithm in java, you can assume that there is a class Lock, with a method public boolean unlock(string ID, String Password). (Lock L).unlock(word1, word2) returns true if the right combination of word-arguments is used; otherwise it returns false. This method runs in O(1) time. You can also assume asciisum(string word) is public. 1 15% of the overall mark for this assignment.
2. Algorithm Design: Sorting Lego Objects (15 %) Consider an array of n Lego objects. Each Lego has a method colour() that returns the value red, blue, or white. Write in pseudo-code a linear-time algorithm that rearranges the array of Legoes so that the red Lego objects come first, the blue Lego objects come next, and the white Lego objects some last. Explain briefly but convincingly why your algorithm is O(n). 3. Algorithm Design and Analysis: 2-D Array Manipulation (15 %) (a) Suppose that each row of an nxn array A consists of positive and negative real numbers such that in any row of A, all the negative numbers come before any positive number in that row. Assuming A is already in memory, describe a method running in O(n) time for finding the row of A with the most positive numbers. Give a pseudo-code description of your algorithm. (b) Suppose further that the number of positive numbers in row i is at most the number of positive numbers in row (i+1) for i=0,1,..,(n-1). Assuming A is already in memory, describe a method running in O(n) time for counting negative numbers in the array A. Give a pseudo-code description of your algorithm. (c) Finally, assume that the array from (b) also satisfies the following: there are m negative and (n-m) positive numbers in the first row (0 m n); there are k negative and (n-k) positive numbers in the last row (where 0 k m must be satisfied, based on (b)); A(i,j)=A(i+1,j) if both positive, or both negative. Propose a minimum running-time algorithm for sorting array A row-wise, that is which sorts each row. Give a pseudo-code description of your algorithm and its running time in Big-O notation. Your algorithm should be O(n 2 ) for full credit. 4. Algorithm Analysis: Big-Θ Notation (15 %) Determine the Θ running time of the following algorithm in terms of n: for i = 1 to n for j = 1 to i*i if (j%i == 0) for k=1 to j sum ++ Show clearly the steps of how you have derived the solution.
5. Project Question (40 %) Background information Self-Organizing Lists: Search in a linked list requires that a half of the nodes be visited on average, if the search (i.e., request) probabilities are uniformly distributed among the list elements. If however, the most frequently searched elements get placed near the front of the list, the average search time improves. (Linked) lists with such property are called selforganizing. There are many different strategies for placing the most frequently searched items near the front, one of them being simple move-to-front. According to simple move-to-front, every time your search for an element (and find it), you move the element to the front of the list. In general, this strategy is a gamble, since some items may be searched for once, and never again. However, after a series of subsequent searches, such items would slowly sink to the end of the list, while the ones that have been searched for more frequently would remain close to the front. Through this project, you will be required to design and implement an ordinary and a selforganizing list, and compare their performance in terms of complexity of their search operations. Project Description A movie-rental company has decided to create a database of all its movies-titles, using a simple List ADT. The interface of this ADT should look as follows: public interface MovieList { public int size(); public boolean isempty(); public boolean searchmovie(string title); public String remove(movie p); public Movie insert(string title); The company is committed to providing the best possible service to its customers, so it wants to know which actual implementation of the database (i.e., MovieList interface) would result in the minimum average search-time for a movie. The company possesses files with the access records from previous years, which can be used for test purposes. Two such files are available at: http://www.cs.yorku.ca/course/2011/project/movie/movieaccessyear2001.dat http://www.cs.yorku.ca/course/2011/project/movie/movieaccessyear2002.dat (The above two files can be downloaded together from: http://www.cs.yorku.ca/course/2011/project/movie/movieaccessdatfiles.zip) Version 3 Note: We replaced MovieAccessYear200X with larger files of 100,000 titles each, rather than the 1,000 titles each that we had before. This is because some people did not see a time difference between the two MovieLists (ordinary and self-organizing) with the smaller files. You can still access the original files (if you need to for some reason) at: http://www.cs.yorku.ca/course/2011/project/movie/movieaccessyear2001_1k.dat http://www.cs.yorku.ca/course/2011/project/movie/movieaccessyear2002_1k.dat
You are asked to conduct this analysis and propose the best possible List design to the company. The analysis should involve these steps: (1) create Movie class, analogous to Position class - as discussed in class, and 5.2.2 of the textbook. Besides element() method, which returns (String) movietitle, the interface of this ADT should include another method public int accesscount(), which returns the number of times than an instance of Movie class (i.e., its movietitle) has been accessed. (1) create OrdinaryMoveList class, a simple straight-forward implementation of MovieList interface. (2) create SelfOrganizingMovieList class, an implementation of MovieList interface that includes the basic functionality of self-organizing lists, as described in the Background Information. (3) create MovieListComparator class, this class contains the titles of all available movies, stored in a form of an array, and main method. (An outline of this class and its required functionality is given below, and can also be accessed at the following URL: www.cs.yorku.ca/course/2011/project/movie/movielistcomparator.html ) (4) create SearchTimer class, an auxiliary class responsible for the run-time measurement of a series of search operations on a MovieList. (An outline of this class and its required functionality is also given below.) public class MovieListComparator { / * class variables */ public final static String[ ] moviecollection = { Casablanca, Cast Away, Forrest Gump, Ghost, Gladiator, Midnight Cowboy, One Flew Over the Cuckoo's Nest, The Dead Poet Society, The Godfather, Tootsie ; OrdinaryMovieList list1; SelfOrganizedMovieList list2; String[ ] accessedmovies = new String[100000]; /* constructor */ public MovieListComparator() { /* store moviecollection in an empty movielist */ public void storeinlist(movielist list) { ; /* from a given file, read a list of previously accessed movies (one title per line), and store the list in accessedmovies array */ public void readaccessedmovies(string filename) { ; /* within main method of MovieListComparator the following should be executed: (1) create instances of list1 and list2, and store the content of MovieCollection array in each; (2) prompt the user for the name of the file with a previous access record, and store this list in accessedmovies array; (3) by using SearchTimer class, measure the times required to search movie titles from accessedmovies in list1 and list2; (4) print out the results from (3) and state which list provides a better overall performance for the given access record; (5) print out each title from moviecollection and the number of times it has been accessed during (3). */ public static void main(string[ ] args) {
public class SearchTimer { / * class variables */ MovieList ML; int executiontime; /* constructor */ public SearchTimer(MovieList L) { ML = L; /* perform searches for testtitles on ML, and return the overall execution time in milliseconds */ public int measure(string[] testtitles, int numberoftesttitles) { HINT: Implement measure() method of SearchTimer class using Java s System.currentTimeMillis() method.