CmSc360 Algorithms

advertisement
CmSc250 Intro to Algorithms
Homework 01 due 09/07
Solution
Team problems:
1. Consider the following algorithm for finding the distance between the two closest
elements in an array of numbers
ALGORITHM MinDistance(A[0..n-1])
// Input: Array A[0..n-1] of integers
// Output: Smallest distance between two of its elements
dmin  
for i  0 to n-1 do
for j  0 to n-1 do
if i  j and |A[i] – A[j]| < dmin
dmin  |A[i] – A[j]|
return dmin
Make as many improvements as you can in this algorithmic solution to the
problem. List separately each change and explain how it improves the algorithm. (If
you need to, you may change the algorithm altogether; if not, improve the
implementation given.)
Write your improved algorithm in pseudocode (use the notation of pseudocode
algorithms in the textbook)
(p.18, #9)
Improvement: considers each pair only once, performs the abs function only once
dmin  
for i  0 to n-2 do
for j  i+1 to n-1 do
temp  |A[i] – A[j]|
if temp < dmin
dmin  temp
return dmin
Better algorithm: sort the array with O(nlogn) operations, then find the smallest
distance with O(n) operations.
2. Consider a variation of sequential search that scans a list to return the number of
occurrences of a given search key in the list. Will its efficiency differ from the
efficiency of classical sequential search? Explain your answer.
(p.51, #3)
1
Yes. This algorithm does not have a “best case”, because it has to scan to
list to the end.
3. Design an algorithm for checking whether two given words are anagrams,
i.e. whether one word can be obtained by permuting the letters of the
other. For example, the words ‘tea’ and ‘eat’ are anagrams.
Use pseudocode notation to write the algorithm, see examples in the
textbook (e.g. pp 23, 47 )
(p.38, #10)
Three different solutions were proposed, here they are:
Solution 1:
ALGORITHM Anagrams(word1, word2)
list1  word1.getletters
list2  word2.getletters
if list1.len != list2.len
return false
//if amount of letters differ, not anagram
Else:
list1  ASCIIsort(word1)
list2  ASCIIsort(word2)
//This will get all letters of both words in 2 separate lists
//Then we can sort these using the same sorting criteria
//Now we check to see if the letters match up exactly
//If they do, Anagram, if not, not anagram
for i  list1.len – 1
if list1[i] != list2[i]
return false
return true
Solution 2:
// Checks whether two words are anagrams
// Input: two different words x and y
// Output: returns array with no letters if it is an anagram, returns array
// with letters or “Arrays not of same length” if not an anagram
2
array1  x // split at each letter
array2  y // split at each letter
if len(array1) = len(array2)
for i  0 to n-1 do
for j  0 to n-1 do
if array1[i] = array2[j]
array2[j]  “ “
j  n-1 // if match is found, moves on to next letter
// in array1 to be compared
return array2
else
return “Arrays not of same length”
Solution 3:
//Checks if given inputs, as strings are anagrams
//Input: word1,word2
//string.size denotes the amount of characters inside a given string
//string.lower sets the string to all lower case letters
//string[] is a way to return a character of a given position in a string
//The word “to” denotes <
//string.remove[] denotes removing a character from a string at a given
index
//break denotes breaking away from current loop
//Output: areTheyAnagrams, as Boolean
areTheyAnagrams false
word1.lower
word2.lower
if word1.size == word2.size do
tempword2
for i0 to (word1.size) do
for jto (temp.size) do
if word1[i]==temp[j] do
temp.remove[j]
3
break
if temp.size==0 do
areTheyAnagramstrue
return areTheyAnagrams
4
Download