M5 Interpret what a trade-off is when specifying an ADT using an example to support your answer
1. What is the cost when I use ADT
Formally, an ADT may be defined as a "class of objects whose logical behavior is defined by a set of values
and a set of operations” this is analogous to an algebraic structure in mathematics. What is meant by
"behavior" varies by author, with the two main types of formal specifications for behavior being axiomatic
(algebraic) specification and an abstract model these correspond to axiomatic semantics and operational
semantics of an abstract machine, respectively. Some authors also include the computational complexity
("cost"), in terms of both time (for computing operations) and space (for representing values). In practice,
many common data types are not ADTs, as the abstraction is not perfect, and users must be aware of issues
like arithmetic overflow that are due to the representation. For example, integers are often stored as fixed-
width values (32-bit or 64-bit binary numbers), and thus experience integer overflow if the maximum value
is exceeded.
1.1. Time Complexity
In computer science, the time complexity of an algorithm quantifies the amount of time taken by an
algorithm to run as a function of the length of the string representing the input. The time complexity of an
algorithm is commonly expressed using big O notation, which excludes coefficients and lower order terms.
Time complexity is commonly estimated by counting the number of elementary operations performed by the
algorithm, where an elementary operation takes a fixed amount of time to perform. Thus, the amount of time
taken and the number of elementary operations performed by the algorithm differ by at most a constant
factor.
We will consider the growth rate of some familiar operations, based on this chart; we can visualize the
difference of an algorithm with O (1) when compared with O (n2). As the input larger and larger, the growth
rate of some operations stays steady, but some grow further as a straight line, some operations in the rest part
grow as exponential, quadratic, factorial.
This study source was downloaded by 100000850558056 from CourseHero.com on 08-25-2022 15:03:57 GMT -05:00
https://www.coursehero.com/file/66820310/M5-Interpret-what-a-tradedocx/
1.2. Space Complexity
Space complexity of an algorithm represents the amount of memory space required by the algorithm in its
life cycle. The Space complexity will equal to the amount of memory required by an algorithm to run to
completion. Some algorithms may be more efficient if data completely loaded into memory
The space required by an algorithm is equal to the sum of the following two components:
A fixed part: a space required to store certain data and variables that are independent of the size of
the problem. For example, simple variables and constants used, program size, etc.
A variable part: a space required by variables; whose size depends on the size of the problem. For
example, dynamic memory allocation, recursion stack space, etc.
1.3. Example of using time and space complexity to measure the algorithm
As an illustration, depend on the time & space complexity table above, we can easily find out that the
Bubble Sort has Time Complexity (Best-case: O (n); Worst-case: O (n 2); Average-case: O (n2)) and Space
Complexity: O (1). However, we need to understand the reason why the table have that result. Thus, let us
get started with the implementation of bubble sort in Java programming language.
To calculate the complexity of the bubble sort algorithm, it is useful to determine how many comparisons
each loop performs. For each element in the array, bubble sort does (n−1) comparisons. In big O notation,
bubble sort performs O (n) comparisons. Because the array contains (n) elements, it has an O (n) number of
elements. In other words, bubble sort performs O (n) operations on an O (n) number of elements, leading to
a total running time of O (n2).
Note: O (n) is the best-case running time for bubble sort. It is possible to modify bubble sort to keep track of
the number of swaps it performs. If an array is already in sorted order, and bubble sort makes no swaps, the
algorithm can terminate after one pass. With this modification, if bubble sort encounters a list that is already
sorted, it will finish in O (n) time.
Though bubble sort is simple and easy to implement, it is highly impractical for solving most problems due
to its slow running time. It has an average and worst-case running time of O (n2), and can only run in its
best- case running time of O (n) when the input list is already sorted. In addition, Bubble sort is a stable sort
with a space complexity of O (1).
Let me consider some example:
This study source was downloaded by 100000850558056 from CourseHero.com on 08-25-2022 15:03:57 GMT -05:00
https://www.coursehero.com/file/66820310/M5-Interpret-what-a-tradedocx/
2. What are disadvantages of using ADT instead of other data types
Reasoning about ADTs
There is another major downside to using ADTs — they severely affect theorem proving. Consider the
following proof that 0 + n = n + 0 using Coq’s standard library natural numbers. After the induction, the
proof is almost automatic due to Coq’s ability to computationally simplify (cbn) the goal.
This study source was downloaded by 100000850558056 from CourseHero.com on 08-25-2022 15:03:57 GMT -05:00
https://www.coursehero.com/file/66820310/M5-Interpret-what-a-tradedocx/
Compare this to the proof, which uses the ADT, where we have to perform equational
reasoning instead of computational reasoning. This is extremely burdensome, especially for
more proofs that are complicated.
ADTs do not support fix/match syntax.
ADTs have to expose derived operations.
ADTs prohibit computational reasoning.
This study source was downloaded by 100000850558056 from CourseHero.com on 08-25-2022 15:03:57 GMT -05:00
https://www.coursehero.com/file/66820310/M5-Interpret-what-a-tradedocx/
Powered by TCPDF (www.tcpdf.org)