Uploaded by Junwoo Min

Programming Quiz 2 cop3530

advertisement
Programming Quiz 2 - Algorithm Analysis
Note: This quiz has two questions.
Q1 Algorithm Design
7 Points
DungeonGator is a repository containing n*m books, each of which has a title length of at most t
characters. All the books are stored in a 2-D container or collection, A[n][m] which has n rows and m
columns. Each slot in the container contains a random book title. Thus the collection is not sorted and is
purely random. For example,
A[n][m]
0
.
.
n-1
0
pride and prejudice
.
.
the great gatsby
...
...
.
.
...
m-1
don quixote
.
.
ulysses
Write a function using pseudocode or C++ code that takes in as input this repository and returns a new
1-D container with unique book titles. The returned repository must keep one copy of multiple repeated
book titles.
Note: You can assume that the input collection or container is a 2-D array. Other containers like vectors
or lists are fine as long as you state what you are using.
Q2 Algorithm Analysis
3 Points
Describe and justify the worst-case time and space complexity of your designed algorithm (the
one you wrote in Q.1) in Big O notation.
Answer :
In Big O Notation of our designed algorithm,
vector<string> bookTile is O(1)
For First Loop
int i = 0 is O(1), i < n is O(n + 1), i++ is O(n)
int j = 0 is O(1), j < m is O(m + 1), j++ is O(m)
bookTile.insert(repository[i][j]) is O(n * m)
vector<string> uniqueBookTtile is O(1), book check is O(1)
bool check is O(1)
For Second Loop
int i = 0 is O(1), i < n *m is O(n *m + 1), i++ is O(n*m)
check = false is O(1)
int j = 0 is O(1), j < i is O(n*m + 1), j++ is O(n*m)
In checking unique book titles,
We denote the max length of book title as “s”, and the length of target book tile as “t”.
So, O((n*m)^2*min(t, s))
return uniqueBookTitle is O(1)
Therefore, time complexity in Big O T(n) = (n*m)^2*min(t, s) + 5(n * m) + 2n + 2m + 13.
When time complexity T(n) is applied with “Drop Lower Order Terms”, T(n) = (n*m)^2*min(t,s).
Time complexity in Big O notation is an upper bound on performance, and thus, order of growth
is less than or equal to time complexity in Big O. Therefore, the worst-case time complexity is
O((n*m)^2*min(t,s)).
Likewise, space complexity is also expressed asymptotically in Big O notation. Space complexity
consists of input size and auxiliary space. Considered less amount of memory space are dropped
in Big O Notation, the biggest amount of memory space, 2-D string array memory space in our
designed algorithm, is denoted by Big O Notation. Thus, the worst-case space complexity is
O(n*m)
Download