Data Structures and Algorithms using Python
Copyright © 2023 by Rance Necaise
Table Of Contents
Data Structures and Algorithms using Python
Copyright © 2023
by Rance Necaise

Chapter Exercises

Review Questions

R 1.
True or False. Complexity analysis can provide the exact running time of an algorithm.
R 2.
True or False. Complexity analysis is performed independent of the hardware and language used.
R 3.
What does a big-O value represent?
R 4.
Why is the constant of proportionality not important when specifying the time-complexity of an algorithm.
R 5.
True or False. All algorithms have a best and worst case time-complexity.
R 6.
True or False. An algorithm containing a loop whose iterations are based on the input size must have a time-complexity larger than O(1).
R 7.
True or False. The average case time-complexity algorithm will be the same as its amortized cost.
R 8.
What is the difference between a general matrix and a sparse matrix?
R 9.
True or False. The list based implementation of the Sparse Matrix ADT provides a better time-complexity for the addition operation than the 2-D array implementation of the general matrix.
R 10.
True or False. All operations of the list based implementation of the Sparse Matrix ADT have better worst case time-complexities than the 2-D array implementation of the general matrix.
R 11.
How does a Set differ from a Bag?
R 12.
What is the difference between the set union and the set intersection operations?
R 13.
What is a variable-length argument? Give an example of a built-in function that defines a variable-length argument.

Exercises

E 1.
Arrange the following expressions from slowest to fastest growth rate.

n log 2 n 4 n k log 2 n 5 n 2 40 log 2 n log 4 n 12 n 6

E 2.
Determine the O() for each of the following functions, which represent the number of steps required for some algorithm.

  1. T ( n ) = n 2 + 400 n + 5
  2. T ( n ) = 67 n + 3 n
  3. T ( n ) = 2 n + 5 n log n + 100
  4. T ( n ) = log n + 2 n 2 + 55
  5. T ( n ) = 3 ( 2 n ) + n 8 + 1024
  6. T ( n , k ) = k n + log k
  7. T ( n , k ) = 9 n + k log n + 1000
E 3.
Evaluate each of the following code segments and determine the O() for the best and worst cases. Assume an input size of n.
  1. sum = 0
    for i in range( n ) :
      if i % 2 == 0 :
        sum += i
  2. sum = 0
    i = n
    while i > 0 :
      sum += i
      i = i // 2
  3. for i in range( n ) :
      if i % 3 == 0 :
        for j in range( n // 2 ) :
          sum += j
      elif i % 2 == 0 :
        for j in range( 5 ) :
          sum += j
      else :
        for j in range( n ) :
          sum += j
E 4.
Determine the O() for the following Set operations:difference, intersect, and remove.
E 5.
Complete the Set class by implementing the clear, intersect, and difference methods.
E 6.
Modify the Set constructor to accept an optional variable argument to which a collection of initial values can be passed to initialize the set. The prototype for the new Set constructor should look as follows:
def __init__(self, *initElements)

It can then be used as shown here to create a set initialized with the given values:

s = Set(150, 75, 23, 86, 49)
E 7.
Add a new operation to the Set ADT to test for a proper subset. Given two sets, A and B, A is a proper subset of B, if A is a subset of B and A does not equal B.
E 8.
Add the __str__ method to the Set implementation to allow a user to print the contents of the set. The resulting string should look similar to that of a list, except you are to use curly braces to surround the elements.
E 9.
What is the time-complexity of the proper subset test operation?
E 10.
Prove or show why the worst case time-complexity for the insert and remove vector operations are .
E 11.
Implement the remaining methods of the SparseMatrix class: __sub__, __mul__, transpose, and __getitem__.
E 12.
Determine the worst case time-complexities for the SparseMatrix methods implemented in the previous question.
E 13.
Add Python operator methods to the SparseMatrix class that can be used in place of the named methods for several of the operations: __add__, __sub__, and __mul__.

Programming Projects

P 1.
In this chapter, we implemented the Set ADT using a list. Implement the Set ADT using a bag created from the Bag class. In your opinion, which is the better implementation? Explain your answer.
Page last modified on August 26, 2023, at 04:48 PM