Docsity

ASM 2_1649_Data Structures and Algorithms (PASS), Slides of Data Structures and Algorithms

Partial preview of the text

Download ASM 2_1649_Data Structures and Algorithms (PASS) and more Slides Data Structures and Algorithms in PDF only on Docsity!

Higher National in Computing

ASSIGNMENT 2

Name: Dong Sy Nhat Thanh

ID: GCS

Class: GCS1003A

Subject code: 1649

Assignment due: Assignment submitted:

ASSIGNMENT 2 FRONT SHEET

Qualification BTEC Level 5 HND Diploma in Computing

Unit number and title Unit 19: Data Structures and Algorithms

Submission date Date Received 1st submission

Re-submission Date Date Received 2nd submission

Student Name Student ID

Class Assessor name

Student declaration

I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism. I understand that

making a false declaration is a form of malpractice.

Student’s signature

Grading grid

P4 P5 P6 P7 M4 M5 D3 D

Assignment Brief 2 (RQF)

Higher National Certificate/Diploma in Business

Student Name/ID Number: Unit Number and Title: Unit 19: Data Structures and Algorithms Academic Year: 2021 Unit Assessor: Assignment Title: Implement and assess specific DSA Issue Date: Submission Date: Internal Verifier Name: Date:

Submission Format: Format: ● The submission is in the form of an individual written report and a presentation. This should be written in a concise, formal business style using single spacing and font size 12. You are required to make use of headings, paragraphs and subsections as appropriate, and all work must be supported with research and referenced using the Harvard referencing system. Please also provide a bibliography using the Harvard referencing system. Submission

● Students are compulsory to submit the assignment in due date and in a way requested by the Tutor.

● The form of submission will be a soft copy posted on http://cms.greenwich.edu.vn/.

● Remember to convert the word file into PDF file before the submission on CMS.

Note:

● The individual Assignment must be your own work, and not copied by or from another student. ● If you use ideas, quotes or data (such as diagrams) from books, journals or other sources, you must reference your sources, using the Harvard style. ● Make sure that you understand and follow the guidelines to avoid plagiarism. Failure to comply this requirement will

result in a failed assignment.

Unit Learning Outcomes:

Learning Outcomes and Assessment Criteria (Assignment 2)

Pass Merit Distinction

LO3 Implement complex data structures and algorithms

D3 Critically evaluate the

complexity of an implemented ADT/algorithm

P4 Implement a complex ADT

and algorithm in an executable

programming language to solve

a well defined problem.

P5 Implement error handling and

report test results.

M4 Demonstrate how the

implementation of an ADT/algorithm solves a well-defined problem

LO4 Assess the effectiveness of data structures and algorithms

D4 Evaluate three benefits of using

implementation independent data structures

P6 Discuss how asymptotic analysis

can be used to assess the effectiveness of an algorithm

P7 Determine two ways in

which the efficiency of an

algorithm can be measured,

illustrating your answer with

an example.

M5 Interpret what a trade-off is when

specifying an ADT using an example to support your answer

Table of Contents

1. Introduction

An algorithm is a set of guidelines that spells out how to solve a given issue or carry out a

particular operation. It is a sequential process that accepts inputs, handles them, and generates an

output. In several disciplines, including computer science, mathematics, and engineering,

algorithms are used to solve complicated problems effectively.

On the other hand, data structures are strategies to arrange and keep data in a computer program

so that it may be accessed and used effectively. They offer a structure for data organizing that

enables quick retrieval and effective storage. Data structures like arrays, lists, stacks, queues,

trees, and graphs are frequently used.

In this report, I wrote a program to send messages using ADT components: "Queue and Stack".

And after all I will analyze them.

2. Implementation of the data structure (P4)

2.1. Stack.java

Figure 1 : Stack (1)

I first declares the package name as "stack", then imports the Node class from a linkedlist

package. And I import a built-in Java exception class that is used to indicate when there is no

element in the stack to be popped.

Figure 2 : Stack (2)

Declares the Stack class and its attributes. A Stack is initialized with a head and a count, both set

to null and zero respectively and also checks if the stack is empty by checking if the count is

zero.

Figure 3 : Stack (3)

This adds an element to the stack by creating a new node with the data passed in the argument,

setting its next pointer to the current head, making it the new head, and incrementing the count of

the stack.

Figure 7 : Queue (3)

First part is the constructor for the Queue class. It initializes the front, rear, and count variables to

their default values, indicating an empty queue. Second is method returns true if the queue is

empty (i.e., count is equal to 0), and false otherwise.

Figure 8 : Queue (4)

This method adds a new element to the back of the queue. It creates a new node containing the

given ‘data’, and sets the ‘rear’ variable to point to the new node. If the queue is empty, front is

also set to point to the new node. Otherwise, the ‘next’ field of the current ‘rear’ node is set to

point to the new node. Finally, the ‘count’ variable is incremented.

Figure 9 : Queue (5)

Removes the element at the front of the queue and returns its 'data'. If the queue is empty, it

throws a 'NoSuchElementException'. Otherwise, it sets the 'front' variable to point to the next

node in the queue, decrements the 'count' variable, and checks if the queue is now empty. If it is,

the 'rear' variable is set to 'null'.

2.3. Main.java

Figure 10 : Main (1)

The program starts by importing several required classes, including 'NoSuchElementException'

for error handling, 'Scanner' for reading user input, and custom implementation classes for linked

lists and stacks. After that the 'main()' method initializes an empty 'Queue' object for storing

messages, an empty 'Stack' object for storing messages, and a 'Scanner' object for reading user

input. Then, the program then enters an infinite loop, prompting the user to enter a command to

enqueue, dequeue, push, pop, view the queue, view the stack, or exit.The program then enters an

infinite loop, prompting the user to enter a command to enqueue, dequeue, push, pop, view the

queue, view the stack, or exit.

Based on the user input, the program executes one of several blocks of code that enqueue a

message into the queue, dequeue a message from the queue, push a message onto the stack, pop a

message from the stack, or print the current contents of the queue or stack. If the queue or stack

is empty and the user attempts to dequeue or pop a message, the program catches a

'NoSuchElementException' and prints an error message.

3. Test Case (P5)

Test Case Input Data Expected Ouptut

Actual Output

Result

Enqueue Message enqueued

Message enqueued

Pass

View Queue

Showing the text that I just entered

Dequeue Message Dequeued

Message Dequeued

Pass

Push Message Pushed

Message Pushed

Pass

View Stack

Pop Message Popped

Message Popped

Pass

Exit Exit Exit Pass

Dequeue And Pop Without text

Show nothing

Show nothing

Pass

Use over 250 characters to Enqueue

Not response

Message Enqueued

Fail

Use over 250 characters to Push

Not response

Not response

Pass

Time test Show runtime

Show runtime

Pass

Everything is working pretty well. There is only one error that is when entering more than 250

words for Enqueue, it still receives, this error does not happen with Push.

4. Analyze Big O (P6)

Some of the useful properties of Big-O notation analysis:

Figure 14 : Big O notation analysis (Analysis of Algorithms | Big-O analysis - GeeksforGeeks)

The algorithms can be classified as follows from the best-to-worst performance (Running Time

Complexity):

Figure 15 : Classified the algorithms (Analysis of Algorithms | Big-O analysis - GeeksforGeeks)

Example of Big-O notation:

Figure 16 : Big O notation example

5. Analyze performances (P7)

5.1. Time complexity:

Definition:

Time complexity is the number of operations an algorithm performs to fulfill its task against the

input size (considering that each operation takes the same amount of time). The algorithm that

performs the task with the smallest number of operations is considered the most efficient. Time-

complexity can be expressed using the below three terms called as Asymptotic Notations.

But most times, we will use the Big O notation because it will give us an upper limit of the

execution time i.e., the execution time in the worst-case inputs. The Big O notation represents

the runtime of an algorithm in terms of its growth rate relative to the input (this input is called

"n").

In this way, if we say for example that an algorithm's runtime increases "in the order of the size

of the input", we would state that it is "O (n)". If we say that the runtime of an algorithm

increases "in order of the square of the size of the input", we will denote it as "O (n²)".