Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

GROUP ASSIGNMENT (PART B - COMPLEXITY)

TECHNOLOGY PARK MALAYSIA

CT111-2-3-COMT

COMPUTING THEORY

UC2F2008CS-CS(DA)-CS(DF)-SE

HAND OUT DATE: 07 SEPTEMBER 2020

HAND IN DATE: 16 November 2020

WEIGHTAGE: 20%

LECTURER NAME : Prof. Dr. R. Logeswaran N. Rajasvaran


TEAM MEMBER :
Kalvin Valiant (TP053982)
Abdurakhmonov Siddikjon (TP057592)
Emerald Tan Mei Ing (TP056649)
Eng Tyng Shuan (TP056629)
Rawas Reza (TP053586)

This study source was downloaded by 100000841823167 from CourseHero.com on 08-30-2022 04:12:58 GMT -05:00

https://www.coursehero.com/file/73479137/COMT-Group09-2docx/
TABLE OF CONTENTS

1.0 Code Snippet and Time Complexity 2


1.1 Time Complexity 2
1.2 Big O notation 2
1.3 Code Snippet of For Loop 3
1.4 Best Case 3
1.5 Worst Case 4
1.6 Corresponding Big O Notation for the Code Snippet 4
2.0 Computability 5
2.1 Computability 5
2.2 Conclusion on Computability 5

References 6

1
This study source was downloaded by 100000841823167 from CourseHero.com on 08-30-2022 04:12:58 GMT -05:00

https://www.coursehero.com/file/73479137/COMT-Group09-2docx/
1.0 Code Snippet and Time Complexity
1.1 Time Complexity
The time complexity of an algorithm, which expressed as T(n), represents the
asymptotic behavior of the run time and it is most generally estimated by calculating the
number of elementary operations required to complete the execution. (Studytonight, 2020)
Commonly, the time complexity is expressed using the Big O notation. In this assignment, we
will also use Big O notation to demonstrate the time complexity of for loop.

1.2 Big O notation


Big O notation is a mathematical symbol invented by Paul Bachmann and Edmund
Landau, thus it’s also called as Landau's symbol. Big O notation is widely used in computer
science and mathematics to identify the complexity or efficiency of an algorithm. Practically,
it shows you how does the runtime of a particular function grow as the size of the input
grows.
Typically, there are three tiers to solve for, which are best case, average case and
worst case. These cases are known as asymptotic notations. (educative, 2019). The best case
of the algorithm, which is Big Omega, means the minimum number of steps taken or
minimum running time for an algorithm to complete the execution. Average case, which is
Big Theta, means the most likely number of steps taken or most likely running time for an
algorithm to complete the execution. The worst case of the algorithm, which is Big O
notation that we are discussing, means the maximum number of steps taken or maximum
running time for an algorithm to complete the execution. For this assignment, we will follow
the question provided and identify the Big O notation for a particular code snippet by listing
best case and worst case.

2
This study source was downloaded by 100000841823167 from CourseHero.com on 08-30-2022 04:12:58 GMT -05:00

https://www.coursehero.com/file/73479137/COMT-Group09-2docx/
1.3 Code Snippet of For Loop

Figure 1.3.1 Code Snippet of Nested For Loop by using the C Programming Language

1.4 Best Case


After experimenting with different scenarios, the best-case scenario is where the test
expression returns false at the first time execution, which means N is 0 or any negative
number.

Statement Number of execution for each statement

int i = 0 1

i<N 1

int x = 0 0

x<N 0

printf(“%d”,x); 0

x++ 0

printf(“%d”,i); 0

i++ 0

Total number of Execution 2

Table 1.4.1 Number of execution in the code snippet when N is a negative number
According to (Cormen, T., et. al., 2009), the running time of an algorithm on a
particular input is the number of “steps” executed. From Table 1.4.1, we can see that the total

3
This study source was downloaded by 100000841823167 from CourseHero.com on 08-30-2022 04:12:58 GMT -05:00

https://www.coursehero.com/file/73479137/COMT-Group09-2docx/
number of execution is 2 when N is 0 or a negative number. The number of execution will
still remain 2 as long as N is 0 or a negative number. As the number of execution is a
constant, the running time when N is 0 or a negative number is also a constant, we can say
that the time complexity is O(1).
In short, the best-case time complexity is O(1).

4
This study source was downloaded by 100000841823167 from CourseHero.com on 08-30-2022 04:12:58 GMT -05:00

https://www.coursehero.com/file/73479137/COMT-Group09-2docx/
1.5 Worst Case
The worst-case scenario is where N is a positive number. In this case, the outer loop
runs N times. Meanwhile, each time the outer loop runs, the inner loop runs N times.

Statement Number of execution for each statement

int i = 0 1

i<N N+1

int x = 0 N

x<N N*(N+1) = N2 + N

printf(“%d”,x); N*N = N2

x++ N*N = N2

printf(“%d”,i); N

i++ N

Total number of execution 3N2 + 5N + 2

Table 1.5.1 Number of execution in the code snippet when N is a positive number

From Table 1.5.1, we can see that the total number of execution is (3N 2 + 5N + 2). To
figure out the time complexity, we have to find the fastest growing term and take out the
coefficient. In this case, the fastest growing term is 3N 2. After taking out the coefficient, the
time complexity is O(N2).
In short, the worst-case time complexity is O(N2).

1.6 Corresponding Big O Notation for the Code Snippet


As Big O notation represents the worst case of the function, the Big O notation for the
code snippet shown in Figure 1.3.1 is O(N2). In other words, the time required to execute the
nested loop is directly proportional to the square of N.

5
This study source was downloaded by 100000841823167 from CourseHero.com on 08-30-2022 04:12:58 GMT -05:00

https://www.coursehero.com/file/73479137/COMT-Group09-2docx/
2.0 Computability

2.1 Computability

If a problem can be worked out in principle using a computing device, the problem is
said to be computable which can also be said as “decidable” or “solvable” (Immerman, 2004)
and the code snippet of the nested loop shown above is computable as it can be determined
by a finite procedure from state to state, and the algorithms are simple as well. If the problem
is computable, it is decidable if the problem has an algorithm that decides it and meets the
criteria of any of these 3 characteristics: complete, mechanistic and deterministic otherwise,
the problem is undecidable.
If a clean code can be executed without any interruptions, the code is complete and
the code snippet above is complete as upon execution, the code will be checked sequentially
to ensure that the conditions are true and based on that, an output is displayed without faults.
The code is not deterministic as output during best-case, we will get same output if we
have same input.

Figure 2.1.1 Test for the deterministic characteristic

6
This study source was downloaded by 100000841823167 from CourseHero.com on 08-30-2022 04:12:58 GMT -05:00

https://www.coursehero.com/file/73479137/COMT-Group09-2docx/
After experimenting for multiple case, we found that the code shown in Figure2.1.1 is
deterministic. We will prove this by giving several examples. To prove the code snippet is
deterministic, we need to prove the same input always bring the same output. To test this, we
added an infinite loop to execute the code snippet in Figure 2.1.1 for multiple times. Also, we
made the console to print "End of code snippet" every time the code snippet done execution
so that we know when the code snippet done execution.
In the first case, we considered the N as -1000. As mentioned in previous part, when
N is a negative number, the test expression i<N will return false and the code snippet in
Figure 2.1.1 will be ended. Hence, the console shows “End of code snippet". When we input
N as -1000 again, the console still show "End of code snippet". This means that when we
input N as -1000, the output is always the same.
In the second case, we considered N as 0 for three times. The console shows the same
output when N is 0.
In the third case, we considered N as 2. As we can see from Figure2.1.1, when we
input N as 2, the console shows "000111End of code snippet". When we input N as 2 again,
the console still shows the same output "000111End of code snippet".
From these three cases above, we can see that the same input always lead to the same output.
Hence, we can say that the code is deterministic.
Furthermore, another characteristic of computation is mechanistic. It means that the
code need to reach the requirement of finite sequences and each can be run without
ambiguity. (Chowdhary, 2015)The steps and answer of the code should be precise when we
proceed it. The error and unclear condition should not appear to meet the requirement of
mechanistic. To related to our case, we have to make sure the input of code snippet should be
clear and correct and we will get a clear and right answer without any error.

An effective procedure can be called if it met three properties above, complete, mechanistic
and deterministic.

7
This study source was downloaded by 100000841823167 from CourseHero.com on 08-30-2022 04:12:58 GMT -05:00

https://www.coursehero.com/file/73479137/COMT-Group09-2docx/
References
Chowdhary, K., 2015. Lecture 19: Decidability, and Church-Turing Thesis. [ebook] pp.19-1.
Available at: http://www.krchowdhary.com/toc/19-decide-lect.pdf [Accessed 12 November
2010].
Cormen, T., et. al., 2009. Introduction To Algorithms. 3rd ed. Cambridge, Mass.: MIT Press,
p.25.
Educative, 2019. Big O Notation: A primer for beginning devs. [Online] Available at:
https://www.educative.io/blog/a-big-o-primer-for-beginning-devs [Accessed on 1 November
2020]
Immerman, N., (2004), “Computability and Complexity”, [online]. Available from:
https://plato.stanford.edu/entries/computability/ [Accessed: 10 Nov 2020]
Studytonight, 2020. Time Complexity of Algorithms. [Online] Available at:
https://www.studytonight.com/data-structures/time-complexity-of-algorithms [Accessed on 1
November 2020]

8
This study source was downloaded by 100000841823167 from CourseHero.com on 08-30-2022 04:12:58 GMT -05:00

https://www.coursehero.com/file/73479137/COMT-Group09-2docx/
Powered by TCPDF (www.tcpdf.org)

You might also like