Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 32

Data Structure and Algorithms for Call

Center
Unit19

Implementation for Call Center----------------------------------------------------------------------------------------------------------------- 3


Creation Package for Call Center------------------------------------------------------------------------------------------------------------- 3
Creation Node Class for Call Center--------------------------------------------------------------------------------------------------------- 3
Creation Singly Linked List Class for Call Center------------------------------------------------------------------------------------------ 4
Creation Queue Class for Call Center-------------------------------------------------------------------------------------------------------- 7

Reporting Results---------------------------------------------------------------------------------------------------------------------------------- 10
Result-1------------------------------------------------------------------------------------------------------------------------------------------- 10
Result-2------------------------------------------------------------------------------------------------------------------------------------------- 10
Result-3------------------------------------------------------------------------------------------------------------------------------------------- 11
Result-4------------------------------------------------------------------------------------------------------------------------------------------- 12
Result-5------------------------------------------------------------------------------------------------------------------------------------------- 12
Result-6------------------------------------------------------------------------------------------------------------------------------------------- 13

Error Handlings------------------------------------------------------------------------------------------------------------------------------------- 13
Error Handling-1-------------------------------------------------------------------------------------------------------------------------------- 13
Error Handling-2-------------------------------------------------------------------------------------------------------------------------------- 13

Well-Defined Problems-------------------------------------------------------------------------------------------------------------------------- 14
Solving the First Problem to Be Show Options------------------------------------------------------------------------------------------ 14
Solving the Second Problem to Be Choose the Options-------------------------------------------------------------------------------15
Solving the Third Problem to be Store the Receive Calls------------------------------------------------------------------------------16
Solving the Fourth Problem to be Review All Calls-------------------------------------------------------------------------------------19
Solving the Fifth Problem to Be Dial A Number According to Received Order---------------------------------------------------20
Solving the Sixth Problem to be Exit from the System---------------------------------------------------------------------------------20

Complexity of An Implemented ADT/Algorithm------------------------------------------------------------------------------------------ 21


Complexity of IsEmpty Method------------------------------------------------------------------------------------------------------------- 21
Time Complexity of IsEmpty Method-------------------------------------------------------------------------------------------------- 21
Space Complexity of IsEmpty Method------------------------------------------------------------------------------------------------- 21
Complexity of AddLast Method------------------------------------------------------------------------------------------------------------- 21
Time Complexity of AddLast Method-------------------------------------------------------------------------------------------------- 22
Space Complexity of AddLast Method------------------------------------------------------------------------------------------------ 22
Complexity of Traverse Method------------------------------------------------------------------------------------------------------------ 22
Time Complexity of Traverse Method------------------------------------------------------------------------------------------------- 22
Space Complexity of Traverse Method------------------------------------------------------------------------------------------------ 23
Complexity of Last Method------------------------------------------------------------------------------------------------------------------ 23
Time Complexity of Last Method------------------------------------------------------------------------------------------------------- 23
Space Complexity of Last Method------------------------------------------------------------------------------------------------------ 23

Asymptotic Analysis------------------------------------------------------------------------------------------------------------------------------ 24
Best-Case Analysis----------------------------------------------------------------------------------------------------------------------------- 24
Worst-Case Analysis--------------------------------------------------------------------------------------------------------------------------- 24
Average-Case Analysis------------------------------------------------------------------------------------------------------------------------ 24
Asymptotic Notations------------------------------------------------------------------------------------------------------------------------- 25
Big Oh Notation, Ο------------------------------------------------------------------------------------------------------------------------- 25
Omega Notation, Ω------------------------------------------------------------------------------------------------------------------------ 25
Theta Notation, θ-------------------------------------------------------------------------------------------------------------------------- 26

Algorithm Efficiency------------------------------------------------------------------------------------------------------------------------------ 27
Space Efficiency--------------------------------------------------------------------------------------------------------------------------------- 27
Time Efficiency---------------------------------------------------------------------------------------------------------------------------------- 27
Example for Time and Space Efficiency--------------------------------------------------------------------------------------------------- 27
Example for Space Efficiency in Java------------------------------------------------------------------------------------------------------- 28
Example for Time Efficiency in Java-------------------------------------------------------------------------------------------------------- 28

PAGE-1
Unit19

An Abstract Data Type Representation of My Program---------------------------------------------------------------------------------29


A Trade-Off My Program--------------------------------------------------------------------------------------------------------------------- 29

Using Implementation Independent Array------------------------------------------------------------------------------------------------- 30

Using Implementation Independent Linked List------------------------------------------------------------------------------------------- 30

Using Implementation Independent Stack-------------------------------------------------------------------------------------------------- 30

Using Implementation Independent Tree--------------------------------------------------------------------------------------------------- 30

Using Implementation Independent Graph------------------------------------------------------------------------------------------------ 30

REFERENCES--------------------------------------------------------------------------------------------------31

FIGURE 1 RESULT-1...............................................................................................................................................................10
FIGURE 4 RESULT-2...............................................................................................................................................................10
FIGURE 5 RESULT-3...............................................................................................................................................................11
FIGURE 6 RESULT-4...............................................................................................................................................................12
FIGURE 7 RESULT-5...............................................................................................................................................................12
FIGURE 8 RESULT-6...............................................................................................................................................................13
FIGURE 2 ERROR HANDLING 1..............................................................................................................................................13
FIGURE 3 ERROR HANDLING 2..............................................................................................................................................13
FIGURE 9 BIG OH NOTATION, O...........................................................................................................................................25
FIGURE 10 OMEGA NOTATION, Ω........................................................................................................................................25
FIGURE 11 THETA NOTATION, Θ..........................................................................................................................................26

PAGE-2
Unit19

P4 Implement a complex ADT and algorithm in an executable programming language to


solve a well-defined problem.

Implementation for Call Center


Future Way company need to request to implement the data structure for call
center. In implementation, if a client call, her call must be stored until there is a free
operator to pick it up. Calls should be processed in the same order they are received. The
operator can choose:
1. Storing the received calls.
2. Review all calls.
3. Dial a number according to received order.
Creation Package for Call Center
package CallCenter;

public interface CallCenterIF<E>{


int Size( );
boolean IsEmpty( );
void Enqueue(E e);
void First( );
E Dequeue( );

Creation Node Class for Call Center


package CallCenter;

public class Node<E> {


private E element;
private Node <E> next;
public E getElement() {
return element;
}
public void setElement(E element) {
this.element = element;
}
public Node<E> getNext() {

PAGE-3
Unit19

return next;
}
public void setNext(Node<E> next) {
this.next = next;
}
}

Creation Singly Linked List Class for Call Center


package CallCenter;
public class SinglyLinkedList<E> {
private int size;
private Node<E> head;
private Node<E> tail;

public int getSize() {


return size;
}
public void setSize(int size) {
this.size = size;
}
public Node<E> getHead() {
return head;
}
public void setHead(Node<E> head) {
this.head = head;
}
public Node<E> getTail() {
return tail;
}
public void setTail(Node<E> tail) {
this.tail = tail;
}
public int size() {
return this.size;
}

PAGE-4
Unit19

public boolean isEmpty() {


boolean flag;
if(this.size==0) {
flag=true;
}
else flag=false;
return flag;
}
public void first() {
if(isEmpty()) {
System.out.println("List is Empty.Psl,Firstly input the data");
System.exit(0);
}
System.out.println(head.getElement());
}
public void last() {
if(isEmpty()) {
System.out.println("List is Empty.");
}
else{
System.out.println("Calling number is "+tail.getElement());
}
}

public void addFirst(String data) {


Node newest=new Node();
newest.setElement(data);
newest.setNext(head);
head=newest;
if(isEmpty())tail=head;
size++;
}
public void addLast(String data) {
Node newest=new Node();
newest.setElement(data);

PAGE-5
Unit19

if(isEmpty()) {
head=newest;
tail=newest;
}
else
{ newest.setNext(null);
tail.setNext(newest);
tail=newest;
}
size++;
}

public E removeFirst() {
if(isEmpty())
{
System.out.println("List is Empty");
}
E data=head.getElement();
head=head.getNext();
size-=1;
return data;
}
public void traverse() {
int count = 0;
if(isEmpty())
{
System.out.println("List is Empty");
}
while(head!=null) {
count += 1;
E data=head.getElement();
head=head.getNext();
System.out.println(count+". "+data+"");
}
}

PAGE-6
Unit19

Creation Queue Class for Call Center


package CallCenter;
import java.util.Scanner;

public class Queue<E> {

private SinglyLinkedList<E> sl = new SinglyLinkedList<>();

public void Check(int item) {


Scanner input = new Scanner(System.in);
Queue Q = new Queue();
if (item == 1) {
System.out.print("Enter receives phone number : ");
String recive = input.next();
for (int i =0;i<recive.length();i++) {
while (recive.charAt(i) != '0' &recive.charAt(i) != '1' &
recive.charAt(i) != '2' & recive.charAt(i) != '3' & recive.charAt(i) != '4' & recive.charAt(i) !=
'5'
& recive.charAt(i) != '6' & recive.charAt(i) != '7' &
recive.charAt(i) != '8' & recive.charAt(i) != '9') {
System.out.println("Must to enter only digit number.
Pleas enter again : ");
recive = input.next();
}
}
sl.addLast(recive);
System.out.println("-----------------------");
}
else if (item == 2) {
sl.traverse();
System.out.println("-----------------------");
}
else if (item == 3) {
sl.traverse();

PAGE-7
Unit19

if (sl.size() != 0) {
System.out.print("If you want to call last phone number, Enter
'3' : ");
int dial = input.nextInt();
sl.last();
System.out.println("-----------------------");
}
}
else if (item == 4) {
System.exit(0);
}
else {
while (item != 1 || item != 2 || item != 3) {
System.out.print("Pease enter number '1' or '2' or '3' or '4' : ");
int second = input.nextInt();
Q.Check(second);
}
}
}
public static void main(String[] args) {
Queue Q = new Queue();
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("Welcome From Call Center\n1. To be store the
received calls.\n"+"2. Review all calls.\n" +
"3. Dial a number according to received order.\n"+"4.
Exit");
System.console();
System.out.print("Enter with number : ");
int no = input.nextInt();
System.out.println("-----------------------");
while (no != 1 & no != 2 & no != 3 & no != 4) {
System.out.print("Must to enter only digit number '1' to '4'.
Pleas enter again : ");
no = input.nextInt();
}

PAGE-8
Unit19

Q.Check(no);
}
}
}

PAGE-9
Unit19

P5 Implement error handling and report test results.

Reporting Results
Result-1

Figure 1 Result-1

There are 4 type in program.


 No.1 represents to be store or save the received calls.
 No.2 represents to review all calls.
 No.3 represents to call a number according to received order.
 No.4 represents exit from system.

Result-2

Figure 2 Result-2

If user not stores the receive phone numbers by firstly and then user choose
number 2. At the same time, user can see “List is Empty” message.
Result-3

PAGE-10
Unit19

If user stores the receive phone numbers by firstly and then user choose number 2.
At the same time, user can see his stored phone number list in program.

Figure 3 Result-3

PAGE-11
Unit19

Result-4

Figure 4 Result-4

If user stores the receive phone numbers by firstly and then user choose number 3,
user can see receives phone number and user can call last receives phone numbers.
Result-5

Figure 5 Result-5

If user not stores the receive phone numbers by firstly and then user choose
number 3, the program will show message. That message is “List is Empty”.

PAGE-12
Unit19

Result-6

Figure 6 Result-6

If user want to quit or leave from the program, need to enter number 4. If user
enter number 4, the program will exit from the system.

Error Handlings
Error Handling-1

Figure 7 Error Handling 1

In this case, if user is not entered only digit 1 from 4, error message can see and
need to enter digit number 1 to 4.
Error Handling-2

Figure 8 Error Handling 2

If user choose number is 1, user can input the receives phone number. However,
use must to enter with only digit number because other characters are not allowed for
phone number in this case. If user enter with other character, user can see error
message. Error message is need to enter only digit number.

PAGE-13
Unit19

M4 Demonstrate how the implementation of an ADT/algorithm solves a well-defined


problem.

Well-Defined Problems
Future Way company need to request to implement the data structure for call
center. In implementation, if a client call, her call must be stored until there is a free
operator to pick it up. Calls should be processed in the same order they are received. The
operator can choose:
1. Storing the received calls.
2. Review all calls.
3. Dial a number according to received order.
There are five problems in the implementation. They are, first to be show options
for user can be see, second to be choose the option by user, third to be store or save the
received calls, fourth to review all calls, fifth to be call a number according to received
order and, sixth to be exit from the system. That why, I choose Queue ADT to be
implement for call center.
Solving the First Problem to Be Show Options
The fist problem is need to show 4 option (Storing the received calls, review all
calls, dial a number according to received order and exit) in the program by firstly.
Therefore, I created main method.
public static void main(String[] args) {
Queue Q = new Queue();
Scanner input = new Scanner(System.in);
while (true) {
System.out.println("Welcome From Call Center\n1. To be store the
received calls.\n"+"2. Review all calls.\n" +
"3. Dial a number according to received order.\n"+"4.
Exit");
System.console();
System.out.print("Enter with number : ");
int no = input.nextInt();
System.out.println("-----------------------");
while (no != 1 & no != 2 & no != 3 & no != 4) {
System.out.print("Must to enter only digit number '1' to '4'.
Pleas enter again : ");
no = input.nextInt();
}
Q.Check(no);
}

PAGE-14
Unit19

}
In the main method, I coded 3 option to be show and then to be accept user
request.

After finished implementing main method. The program can show 4 option to user
and then user can choice the option.
Solving the Second Problem to Be Choose the Options
When user input the data for options, the program needs to check what user
needed is. That why, I created check method.
public void Check(int item) {
Scanner input = new Scanner(System.in);
Queue Q = new Queue();
if (item == 1) {
System.out.print("Enter receives phone number : ");
String recive = input.next();
for (int i =0;i<recive.length();i++) {
while (recive.charAt(i) != '0' &recive.charAt(i) != '1' &
recive.charAt(i) != '2' & recive.charAt(i) != '3' & recive.charAt(i) != '4' & recive.charAt(i) !=
'5'
& recive.charAt(i) != '6' & recive.charAt(i) != '7' &
recive.charAt(i) != '8' & recive.charAt(i) != '9') {
System.out.println("Must to enter only digit number.
Pleas enter again : ");
recive = input.next();
}
}
sl.addLast(recive);
System.out.println("-----------------------");
}
else if (item == 2) {
sl.traverse();
System.out.println("-----------------------");
}

PAGE-15
Unit19

else if (item == 3) {
sl.traverse();
if (sl.size() != 0) {
System.out.print("If you want to call last phone number, Enter
'3' : ");
int dial = input.nextInt();
sl.last();
System.out.println("-----------------------");
}
}
else if (item == 4) {
System.exit(0);
}
else {
while (item != 1 || item != 2 || item != 3) {
System.out.print("Pease enter number '1' or '2' or '3' or '4' : ");
int second = input.nextInt();
Q.Check(second);
}
}
}
In the check method, I created four conditions. First condition is to be store or
save the received calls. Second condition is to be review all calls. Third condition is to be
call a number according to received order. Last condition is to be exit from the system.

After finished implementing check method. User can choice options in the
program.
Solving the Third Problem to be Store the Receive Calls
In this problem, user want to store the data into the program. That why, I choose
singly linked list ADT because the singly linked list is a way to store a collection of items.
Each item in a linked list is stored in the form of a node. Therefore, first I created node
class.
package CallCenter;

PAGE-16
Unit19

public class Node<E> {


private E element;
private Node <E> next;
public E getElement() {
return element;
}
public void setElement(E element) {
this.element = element;
}
public Node<E> getNext() {
return next;
}
public void setNext(Node<E> next) {
this.next = next;
}
}
In the node class, I declare element and next variables because all of node must to
have element and next in linked list. Element is to be store data from user and next is to
be point the next node.
When finish node class In the program, I created singly linked list class.
package CallCenter;
public class SinglyLinkedList<E> {
private int size;
private Node<E> head;
private Node<E> tail;

public int getSize() {


return size;
}
public void setSize(int size) {
this.size = size;
}
public Node<E> getHead() {
return head;

PAGE-17
Unit19

}
public void setHead(Node<E> head) {
this.head = head;
}
public Node<E> getTail() {
return tail;
}
public void setTail(Node<E> tail) {
this.tail = tail;
}
public int size() {
return this.size;
}
}
When finish creating singly linked list class, created addLast method in the class
because user want to save the receive class.
public void addLast(String data) {
Node newest=new Node();
newest.setElement(data);
if(isEmpty()) {
head=newest;
tail=newest;
}
else
{ newest.setNext(null);
tail.setNext(newest);
tail=newest;
}
size++;
}
In the addLast method, I declared newest node variable and coded a condition. In
condition, if the list is empty, newest is assigned to head and tail. If the list is not empty,
next of the newest is created null and next of the tail is assigned newest. After that,
newest is assigned to the tail.

PAGE-18
Unit19

When finish creating addLast method, user can store the received phone numbers
into the program.
Solving the Fourth Problem to be Review All Calls
I created traverse method in singly linked list.
public void traverse() {
int count = 0;
if(isEmpty())
{
System.out.println("List is Empty");
}
while(head!=null) {
count += 1;
E data=head.getElement();
head=head.getNext();
System.out.println(count+". "+data+"");
}
}
In traverse method, created a condition. In that condition, I check list is empty or
not. If list is empty, I coded output message (list is empty). If list is not empty, created
while loop. In the while loop, compared head is not equal to null. If head is not equal to
null, head of the element is assigned to data variable and next of head assigned to head.
That why, all element is assigned to data and then I displayed that data.

When finish creating traverse method, user can see the phone numbers from the
program.

PAGE-19
Unit19

Solving the Fifth Problem to Be Dial A Number According to Received


Order
I created last method in singly linked list class.
public void last() {
if(isEmpty()) {
System.out.println("List is Empty.");
}
else{
System.out.println("Calling number is "+tail.getElement());
}
}
In the last method, created a condition. In that condition, I check list is empty or
not. If list is empty, I coded output message (list is empty). If list is not empty, created
calling message. In the message, contain that last element.

When finish creating last method, user can call last phone from the program.
Solving the Sixth Problem to be Exit from the System
I coded system exit.
else if (item == 4) {
System.exit(0);
}

PAGE-20
Unit19

D3 Critically evaluate the complexity of an implemented ADT/algorithm.

Complexity of An Implemented ADT/Algorithm


Complexity of an algorithm is a rough approximation of the number of
steps necessary to execute an algorithm. That is calculation of the amount of time and
amount of space required by an algorithm.
The implementation of set as an abstract data type or a sorted singly linked list. I
have been created four functions in my implementation. They are isEmpty, addLast,
traverse and, last function. IsEmpty function like isEmpty method in ADT. The process of
the isEmpty method is if the set is empty that will return true or otherwise, that will
return false. AddLast function like push function in ADT. That will add (receive calls) to
set. Traverse function like pop function in ADT. That will tell whether a set contains a
given item such as review all calls number. Last function like peek function in ADT. That
will tell last element (for example, last call number) from the set.
Complexity of IsEmpty Method
public boolean isEmpty() {
boolean flag;
if(this.size==0) {
flag=true;
}
else flag=false;
return flag;
}
Time Complexity of IsEmpty Method

The worst-case time of isEmpty method is the slowest of the two possibilities.
They are maximum time of true state and else state. For example, if true state is O(1)
and false state is O(1) the worst-case time for the whole if-then-else statement would be
O(N). isEmpty method run in constant time. In my implementation other methods run in
linear time on the size of the set. A less naive implementation could reduce these to
logarithmic time.
Space Complexity of IsEmpty Method

In isEmpty method, there is one flag variable. Boolean value is 1 bit. The space
complexity of isEmpty method is O(1).
Complexity of AddLast Method
public void addLast(String data) {
Node newest=new Node();
newest.setElement(data);
if(isEmpty()) {
head=newest;
tail=newest;

PAGE-21
Unit19

}
else
{ newest.setNext(null);
tail.setNext(newest);
tail=newest;
}
size++;
}
Time Complexity of AddLast Method

The worst-case time of addLast method is the slowest of the two possibilities. They
are maximum time of true state and else state. For example, if true state is O(N) and
false state is O(1) the worst-case time for the whole if-then-else statement would be
O(N).
Space Complexity of AddLast Method

In addLast method, there are newest, head, tail and size variables. Assuming 4
bytes for that variable, total space occupied by the program is 16bytes. So, the space
complexity of addLast method is O(n) or linear.
Complexity of Traverse Method
public void traverse() {
int count = 0;
if(isEmpty())
{
System.out.println("List is Empty");
}
while(head!=null) {
count += 1;
E data=head.getElement();
head=head.getNext();
System.out.println(count+". "+data+"");
}
}
Time Complexity of Traverse Method

In traverse method, if true state is O(1) and then while loop is N times, so the
sequence of statements also executes N times. Since, assuming the statements are O(1),
the total time for the for loop is N * O(1), which is O(N) overall.

PAGE-22
Unit19

Space Complexity of Traverse Method

In traverse method, there are count, data and head variables. Assuming 4 bytes for
that variable, total space occupied by the program is 12bytes. So, the space complexity
of addLast method is O(n) or linear.
Complexity of Last Method

public void last() {


if(isEmpty()) {
System.out.println("List is Empty.");
}
else{
System.out.println("Calling number is "+tail.getElement());
}
}
Time Complexity of Last Method

The worst-case time of last method is the slowest of the two possibilities. They are
maximum time of true state and else state. For example, if true state is O(1) and false
state is O(1) the worst-case time for the whole if-then-else statement would be
O(N). Last method runs in constant time.
Space Complexity of Last Method

The space complexity of last method is O(0).

PAGE-23
Unit19

P6 Discuss how asymptotic analysis can be used to assess the effectiveness of an


algorithm.

Asymptotic Analysis
Asymptotic analysis’s algorithm means to defining the mathematical foundation of
its run-time performance. If used asymptotic analysis, can be well the best-case,
average-case, and worst-case state of an algorithm. Asymptotic analysis is one of the
inputting bound procedure. For example, there is nothing input to algorithm, if is
concluded to work in a constant time. Other than the “input” all other factors are
considered constant. (TutorialsPoint, 2019)
There are three type of the time in asymptotic analysis,
 Best-Case − Minimum time needed for processing.
 Average-Case − Average time needed for processing.
 Worst-Case − Maximum time needed for processing.
Best-Case Analysis
For the best-case analysis, the lower bound of a running time on an algorithm is
calculated. The case that can cause a minimum number of operations can be considered
as a best-case scenario. For example, in an operation like linear searching, the best case
is when the value searched is in the first position.
(TutorialsPoint, 2019)
Worst-Case Analysis
In worst-case analysis, can calculate upper bound on running time of an algorithm.
Need to know the problem that reasons maximum number of times to be executed. In
linear search, the worst-case occurs when the item to be searched (x in the above code)
is not current in the array. When x is not current, search () methods compares it with all
the items of array [] one by one. So, worst-case time complexity of linear search will be
Θ(n). (TutorialsPoint, 2019)
Average-Case Analysis
In average-case analysis, takes all possible inputs and calculate the time for all of
that inputs. Sum all the calculated values and divided the sum by total number of inputs.
Need to know distribution of problems. In linear search case, according to the algorithm,
all problems are uniformly distributed. That why, sum all the problems and divide the
sum by (n+1). Average-case time complexity is,

Average Case Time =

=
= Θ(n)
(Akanksha, 2019)

PAGE-24
Unit19

Asymptotic Notations
There are three asymptotic notations to calculate the running time complexity of an
algorithm.
1. Ο Notation
2. Ω Notation
3. θ Notation
Big Oh Notation, Ο

The notation Ο(n) is measuring the worst-case time complexity or the longest
amount of time an algorithm can possibly take to complete.

Figure 9 Big Oh Notation, O

For example, for a function f(n)


Ο(f(n)) = {g(n): there exists c > 0 and n such that f(n) ≤ c.g(n) for all n > n .}
0 0

(TutorialsPoint, 2019)
Omega Notation, Ω

The notation Ω(n) is measuring the best-case time complexity or the best amount
of time an algorithm can possibly take to complete.

Figure 10 Omega Notation, Ω

For example, for a function f(n)

PAGE-25
Unit19

Ω(f(n)) ≥ {g(n): there exists c > 0 and n such that g(n) ≤ c.f(n) for all n > n . }
0 0

(TutorialsPoint, 2019)
Theta Notation, θ

Figure 11 Theta Notation, θ

The notation θ(n) is simple way to quickly both the lower bound and the upper
bound of an algorithm's running time.
θ(f(n)) = {g(n) if and only if g(n) = Ο(f(n)) and g(n) = Ω(f(n)) for all n > n0.}
(TutorialsPoint, 2019)

PAGE-26
Unit19

P7 Determine two ways in which the efficiency of an algorithm can be measured,


illustrating your answer with an example.

Algorithm Efficiency
A measure of the average processing time essential for an algorithm to finish
perform on a set of information. Algorithm efficiency is characterized by its order.
Generally, a bubble sort algorithm will have complexity in sorting N elements
proportional to and of the order of N2, always written O(N2) because an average of N/2
comparisons are needed N/2 times, giving N2/4 total comparisons, later of the order of
N2. Otherwise, quicksort has an efficiency O (N log2N).
If 2 algorithms for the equal case are of the equal order then they are
approximately as efficient in terms of computation. Algorithm efficiency is useful for
counting the implementation try hard of sure cases. Two efficiency are very important for
performance in algorithm efficiency,
1. Space efficiency – space for memory, also known as space complexity.
2. Time efficiency – time for program, also known as time complexity.
(Encyclopedia, 2019)
Space Efficiency
Space efficiency is a method describing the total amount of memory space and
algorithm an algorithm gets in terms of the total amount of input to the algorithm. There
are some conditions where the space of memory used must be analyzed. For example, for
large amount of data. There are three kind of space efficiency,
1. Instruction space
2. Data space
3. Run-time stack space
Time Efficiency
Time efficiency is a method describing the total amount of time an algorithm gets
in terms of the total amount of input to the algorithm. Time can mean number of
memory accesses performed, the number of comparisons between integers, the number
of times some inner loop is executed, or some other natural unit related to the amount of
real time the algorithm will take. The actual running time depends on many factors,
 The speed of CPU.
 The compiler, compiler options.
 The amount of memory space.
 The actual data. Where the data is locating.
Example for Time and Space Efficiency
Let an array A and an integer x and then need to find x in the array. If x is there is
array A, the program will exit.
Simple solution to this case is traverse the whole array A and check if the any item is
equal to x.

PAGE-27
Unit19

for i : 1 to length of A
if A[i] is equal to x
return TRUE
return FALSE
Each of the operation in computer take approximately constant time. Let each
operation takes c time. The number of lines of code executed is actually depends on the
value of x. During analyses of algorithm, considering worst case scenario, for example,
when x is not present in the array A. In the worst-case, if condition will run N times
where N is the length of the array A. So, in the worst case, total execution time will be
(N ∗ c + c). N ∗ c for the if condition and c for the return statement (ignoring some
operations like assignment of i).
Example for Space Efficiency in Java
public static void main(String[] args) {
int a = 5, b = 5, c;
c = a + b;
System.out.println(c);
}
In this case, there are 3 variable a, b and c. The size of the integer is 2 or 4 bytes
which depends on the compiler. Current, the size as 4 bytes. So. The total space
occupied by the above-given program is 4*3 =12 bytes. Since no additional variables are
used, no extra space is needed
Example for Time Efficiency in Java
public static void main(String[] args) {
int i, n = 8;
for (i = 1; i <= n; i++) {
System.out.println("Hello Word !!!");
}
}
In this case, “Hello World!!!” will print N times. So, time complexity of above code
is O(N).

PAGE-28
Unit19

M5 Interpret what a trade-off is when specifying an ADT using an example to support your
answer.

An Abstract Data Type Representation of My Program


An abstract data type is a proto type for the data consisting of values and
processes, that real structure is hidden. For example, a set ADT is defined as having
operations like addFirst, removeFirst and traverse in my implementation. Each of that
processes mutates or returns a set value, however, the incomes by which they do so, or
by which a set is represented, is hidden from consumers of the set type. To be strictly
correct, the word “model” is an operative part of this definition. An abstract data type is
an idea, describing a data type that could be represented in different languages.
Colloquially, it’s common to use the term “abstract data type” to refer both to the
formal definition of the data type as well as to concrete implementations of it. I will use
the colloquial definition, and refer to software components which hide their internal
structure as abstract data types.
A Trade-Off My Program
Most of the developers have the ability to create and manipulate, but are not able
to access, modify, or extend their underlying representations. (Assuming we are limited
by the constraints of good sense and decency and don’t try to modify the prototypes of
things that we shouldn’t.) This leads to a fundamental tradeoff which characterizes
abstract data types: by making the implementation of the data opaque, we have gained
modularity at the expense of extensibility.
Implementation of an ADT in a program does have its own drawbacks and benefits
generally as the nature of a tradeoff. Firstly, as an obvious fact of a drawback, there will
be a lot of complication of design issues. For example, in my call center program, it was
confusing and time consuming to choose the type of abstract data that would be
implemented with the queue. Moreover, the process of implementing it with queue needs
to be done correctly using the right methods and functions or else the program would not
run successfully. On the contrary, these complicated implementations are hidden from
the client and so the client can use the program with the ease of use. Another drawback
is that, in big programs that contains a lot of coding, when an ADT is used, the
maintenance of the code will become quite harder and error handling and debugging can
also become more difficult. But then again on the brighter side, when the program
becomes an application successfully, the maintenance of the application is easier for the
user since they do not need to look into the details of implementation.

PAGE-29
Unit19

D4 Evaluate three benefits of using implementation independent data structures.

Using Implementation Independent Array


 When insert or remove the item, don’t have to shift subsequent item which slows
our computation down.
 No need a big enough block of memory more than an array because an array needs
a large enough block of memory.
 Easily corrupted.

Using Implementation Independent Linked List


 When access a specific item, no need to traverse the list from the head of the list
to find it which can take longer than an array access.
 No need memory more than linked list because linked lists need more block of
memory.
 Easily corrupted.

Using Implementation Independent Stack


 No have any limit of memory because memory of the stack is limited.
 Don’t have stack overflow because when creating a lot of object on the stack, that
can increase the changes of stack overflow.
 Random access not possible

Using Implementation Independent Tree


 Not high overhead.
 No have large waste of idle links because tree is big waste of idle links.
 No need to predetermined limit on number of a node’s children.

Using Implementation Independent Graph


 No need to find the path in efficient way.
 No have large waste of idle links because tree is big waste of idle links.
 No need to predetermined limit on number of a node’s children.
 

PAGE-30
Unit19

References
Akanksha, R., 2019. www.geeksforgeeks.or. [Online]
Available at: https://www.geeksforgeeks.org/analysis-of-algorithms-set-2-asymptotic-
analysis/
[Accessed 1 2 2020].
Encyclopedia, 2019. www.encyclopedia.com. [Online]
Available at: https://www.encyclopedia.com/computing/dictionaries-thesauruses-
pictures-and-press-releases/algorithm-efficiency
[Accessed 1 2 2020].
TutorialsPoint, 2019. TutorialsPoint. [Online]
Available at:
https://www.tutorialspoint.com/data_structures_algorithms/asymptotic_analysis.htm
[Accessed 22 2 2020].

PAGE-31

You might also like