Tải bản đầy đủ (.pdf) (31 trang)

1649 assignment 1 (pass)

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.08 MB, 31 trang )

Higher Nationals in Computing

Unit 19: Data Structures and Algorithms
ASSIGNMENT 1

Assessor name: PHAN MINH TAM
Learner’s name: DAO VINH KHANG
ID:GCS200222
Class:GCS0905B
Subject code: 1649

Assignment due:

Assignment submitted:


ASSIGNMENT 1 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

DAO VINH KHANG

Student ID

GCS200222

Class

GCS0905B

Assessor name

TAMPM

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

KHANG

Grading grid
P1

P2


P3

M1

M2

M3

D1

D2


 Summative Feedback:

 Resubmission Feedback:

3.1

3.2

Grade:

Assessor Signature:

Internal Verifier’s Comments:

Signature & Date:

Date:



ASSIGNMENT 1 BRIEF
Qualification

BTEC Level 5 HND Diploma in Business

Unit number

Unit 19: Data Structures and Algorithms

Assignment title

Examine and specify ADT and DSA

Academic Year

2022

Unit Tutor

TamPM

Issue date

12 – September – 2022

Submission date

IV name and 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 />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:
LO1 Examine abstract data types, concrete data structures and algorithms
LO2 Specify abstract data types and algorithms in a formal notation
Assignment Brief and Guidance:
Assignment scenario
You work as in-house software developer for Softnet Development Ltd, a software body-shop providing network
provisioning solutions. Your company is part of a collaborative service provisioning development project and your
company has won the contract to design and develop a middleware solution that will interface at the front-end to
multiple computer provisioning interfaces including SOAP, HTTP, JML and CLI, and the back-end telecom
provisioning network via CLI .
Your account manager has assigned you a special role that is to inform your team about designing and implementing
abstract data types. You have been asked to create a presentation for all collaborating partners on how ADTs can be
utilised to improve software design, development and testing. Further, you have been asked to write an introductory
report for distribution to all partners on how to specify abstract data types and algorithms in a formal notation.

Tasks
Part 1
You will need to prepare a presentation on how to create a design specification for data structures, explaining the
valid operations that can be carried out on the structures using the example of:
1. A stack ADT, a concrete data structure for a First In First out (FIFO) queue.
2. Two sorting algorithms.
3. Two network shortest path algorithms.
Part 2
You will need to provide a formal written report that includes the following:

1. Explanation on how to specify an abstract data type using the example of software stack.
2. Explanation of the advantages of encapsulation and information hiding when using an ADT.
3. Discussion of imperative ADTs with regard to object orientation.


Learning Outcomes and Assessment Criteria (Assignment 1)
Pass

Merit

Distinction

LO1 Examine abstract data types, concrete data structures and
algorithms
P1 Create a design
specification for data structures
explaining the valid operations
that can be carried out on the
structures.

M1 Illustrate, with an example, a
concrete data structure for a First
In First out (FIFO) queue.
M2 Compare the performance of
two sorting algorithms.

D1 Analyse the operation,
using illustrations, of two
network shortest path
algorithms, providing an

example of each.

P2 Determine the operations of
a memory stack and how it is
used to implement function
calls in a computer.
LO2 Specify abstract data types and algorithms in a formal notation
P3 Using an imperative
definition, specify the
abstract data type for a
software stack.

M3 Examine the advantages of
encapsulation and information
hiding when using an ADT.

D2 Discuss the view that
imperative ADTs are a basis
for object orientation and,
with justification, state
whether you agree.


Table of Contents
Contents
Unit 19: Data Structures and Algorithms ASSIGNMENT 1 ......................................................................................... 1
P1/ Create a design specification for data structures explaining the valid operations that can be carried out on
the structures. .......................................................................................................................................................... 3
1/ Introduction to Abstract Data Type .................................................................................................................... 3
1.1/ What is ADT? ............................................................................................................................................ 3

1.2/ The benefits of ADT. ................................................................................................................................. 3
1.3/ ADT example Suppose we have an index array of size 4. We have an index location starting from 0, 1,
2, 3. Array is a data structure where the elements are stored in a contiguous location. The memory address
of the first element is 1000, second element is 1004, third element is 1008, and the fourth element is 1012.
Since it is of integer type so it will occupy 4 bytes and the difference between the addresses of each
element is 4 bytes. The values stored in an array are 10, 20, 30 and 40. These values, index positions and
the memory addresses are the implementations. ........................................................................................... 4
1.4/ Comparison between ADT and OOP ........................................................................................................ 4
2/ Linear Data Structures ..................................................................................................................................... 4
2.1/ Definition .................................................................................................................................................. 4
2.2/ Linked List ................................................................................................................................................. 5
2.3/ Stack .......................................................................................................................................................... 5
2.4/ Queue ........................................................................................................................................................ 8
3/ Sorting Algorithms ............................................................................................................................................ 8
4/ Searching Algorithms ..................................................................................................................................... 11
4.1/ Definition ..................................................................................................................................................... 12
4.2/ Linear search ........................................................................................................................................... 12
4.3/ Binary Search .......................................................................................................................................... 13
P2/ Determine the operations of a memory stack and how it is used to implement function calls in a computer.
................................................................................................................................................................................ 14
1/ Memory stack ................................................................................................................................................ 14
2. The Push Operation........................................................................................................................................ 15
3. The Pop Operation ......................................................................................................................................... 16
P3 Using an imperative definition, specify the abstract data type for a software stack. ....................................... 17
1/ Formal specification ..................................................................................................................................... 17
2/ What are the pre-condition, post-condition, and error-condition? ............................................................ 18
2.1/ Pre-condition .............................................................................................................................................. 18
2.2/ Post-condition ............................................................................................................................................ 18
Page |1



2.3/ Error-condition ........................................................................................................................................... 18
3/ Specify stack Operation using this formal language. ................................................................................... 19

Page |2


ASSIGNMENT 1 Answer
P1/ Create a design specification for data structures explaining the valid operations
that can be carried out on the structures.
1/ Introduction to Abstract Data Type
1.1/ What is ADT?
In computer science, an abstract data type (ADT) is a mathematical model for a given class of data
structures that behaves similarly; or for certain data types of one or more semantically similar
programming languages. An abstract data type is defined indirectly, only by the operations that can be
performed on it and by mathematical constraints on the effects (and possibly costs) of those
operations.
To be more specific, you can take Java's List interface as an example. The interface does not define any
behaviour at all because there is no concrete List class. The interface just defines a set of methods that
other classes (e.g., Array List and LinkedList) must implement to be considered a List.

1.2/ The benefits of ADT.
The following are the advantages of using the data structure:


These are the essential ingredients used for creating fast and powerful algorithms.



They help us to manage and organize the data.

Page |3




Data structures make the code cleaner and easier to understand.

1.3/ ADT example
Suppose we have an index array of size 4. We have an index location starting from 0, 1, 2, 3. Array is a
data structure where the elements are stored in a contiguous location. The memory address of the
first element is 1000, second element is 1004, third element is 1008, and the fourth element is 1012.
Since it is of integer type so it will occupy 4 bytes and the difference between the addresses of each
element is 4 bytes. The values stored in an array are 10, 20, 30 and 40. These values, index positions
and the memory addresses are the implementations.
The abstract or logical view of the integer array can be stated as:


It stores a set of elements of integer type.



It reads the elements by position, i.e., index.



It modifies the elements by index

• It performs sorting
The implementation view of the integer array:


1.4/ Comparison between ADT and OOP
ADT

OOP

Abstract

ADT is abstraction that define set of
value and set of operations on these
values

Definition

It is a type for objects whose behaviour It is basic unit of OOP.
is defined by a set of value and a set of
operation.

Using

User-defined data type.

It is an instance of class.

Data type

ATD is not definitely an OOP concept.

Objects is an OOP concept.

Example


List, Stacks, Queue, Sets, etc.

Test T=new Test.

Use

Allocate memory when data is stored.

When we instantiate an object then memory is
allocated.

2/ Linear Data Structures
2.1/ Definition
Page |4

It is a sell-contained component which consist
of methods and properties to make certain
type of data useful.


Data structures are divided into two types: linear data structures and non-linear data structures. In a
linear data structure, the elements are arranged in sequential order. Since the elements are arranged
in a specific order, they are easy to implement. However, as the complexity of the program increases,
a linear data structure may not be the best choice. Examples of linear data structures include arrays,
stacks, queues, and linked lists.

2.2/ Linked List
A linked list is a linear data structure that includes a series of connected nodes. Here, each node stores
the data and the address of the next node. For example,


You must start somewhere, so we give the address of the first node a special name called HEAD. Also,
the last node in the linked list can be identified because its next portion points to NULL.
Linked lists can be of multiple types: singly, doubly, and circular linked lists. In this article, we will focus
on the singly linked list. To learn about other types, visit Types of Linked List.
Advantages:


Memory saving and dynamic allocation: Unlike arrays, which need an allotted number of cells
in memory right after creation. Linked list only uses memory for storage when the data is
stored in the linked list.



It can also store elements anywhere in memory without the need for contiguous memory cells
like arrays.



Quick insertion (Add very quickly with complexity only O (1))

• Quick deletion
Defect


Slow search: Searching is slow due to having to traverse many nodes to get to the node you are
looking for

2.3/ Stack
Page |5



In Java programming, we use Stack to store elements in stack form. For example, let's say I arrange a
stack of 10 plates. Then the plate that is arranged first will be at the bottom, and the plate that is
arranged last will be at the top. So, when we take each plate, we will take the top one first. Stack
elements are operated on the principle that the element added last will be removed first. It differs
from a Queue in that the element added first is retrieved first.

2.3.1/ Push
The process of putting (push) a new data element onto the stack is also known as a PUSH operation.
The push operation includes the following steps:
Step 1: check if the stack is full or not.
Step 2: if the stack is full, the process fails and exits.
Step 3: if the stack is not full, increment top to point to the next piece of free memory.
Step 4: add the data element to the position where top is pointing on the stack.
Step 5: return success.

Page |6


2.3.2/ Pop
Accessing the element content while removing it from the stack is also known as a POP Operation. In
the Array implementation of the pop() operation, the data element is not actually deleted, instead
top is reduced to a lower position on the stack to point to the next value. But in the Linked List
implementation, the pop() operation deletes the data element and removes it from memory space.
POP operations may include the following steps:
Step 1: check if the stack is empty or not.
Step 2: if the stack is empty, the process fails and exits.
Step 3: if the stack is not empty, access the data element at which top is pointing.
Step 4: reduce the value of top by 1.

Step 5: return success.

Page |7


2.4/ Queue
In Java programming, Queue collection is used to store elements in an order, it is like List, but the
mechanism of Queue operation is First in First Out. That is, the element that is added first will be
removed first when removed. Imagine when you go to the supermarket and wait in line to pay.
Whoever gets in line first gets paid first. Whoever is last will pay later.

3/ Sorting Algorithms
Sort is to arrange data in a specific format such as ascending/descending alphabet,
ascending/descending numeric order. In computer science, sorting algorithms determine how to
arrange data in a certain order. Sorting here is sorting in numerical or alphabetical order as in a
dictionary.

Sorting algorithms are described in the following table along with the description.
Page |8


SN

Sorting

Description

Algorithms
1


Bubble Sort

It is the simplest sort of method that performs sorting by repeatedly moving
the largest element to the highest index of the array. It comprises comparing
each element to its adjacent element and replacing them accordingly.

2

Bucket Sort

Bucket sort is also known as bin sort. It works by distributing the element into
the array also called buckets. In this sorting algorithm, Buckets are sorted
individually by using a different sorting algorithm.

3

Comb Sort

Comb Sort is the advanced form of Bubble Sort. Bubble Sort compares all the
adjacent values while comb sort removes all the turtle values or small values
near the end of the list.

4

Counting Sort It is a sorting technique based on the keys i.e., objects are collected according
to keys which are small integers. Counting sort calculates the number of
occurrences of objects and stores its key values. A new array is formed by
adding previous key elements and assigning them to objects.

5


Heap Sort

In the heap sort, the Min heap or max heap is maintained from the array of
elements depending upon the choice, and the elements are sorted by deleting
the root element of the heap.

6

Insertion Sort As the name suggests, insertion sort inserts each element of the array to its
proper place. It is a very simple sort of method that is used to arrange the
deck of cards while playing bridge

7

Merge Sort

Merge sort follows the divide and conquer approach in which, the list is first
divided into sets of equal elements and then each half of the list is sorted by
using merge sort. The sorted list is combined again to form an elementary
sorted array.

8

Quick Sort

Quick sort is the most optimized sort algorithm which performs sorting in O(n
log n) comparisons. Like Merge sort, quick sort also works by using the divide
and conquer approach.


9

Radix Sort

In Radix sort, the sorting is done as we do sort the names according to their
Page |9


alphabetical order. It is the linear sorting algorithm used for Integers.
10

Selection

Selection sort finds the smallest element in the array and places it in the first

Sort

place on the list, then it finds the second smallest element in the array and
places it in second place. This process continues until all the elements are
moved to their correct ordering. It carries running time O(n2) which is worse
than the insertion sort.

11

Shell Sort

Shell sort is the generalization of insertion sort which overcomes the
drawbacks of insertion sort by comparing elements separated by a gap of
several positions.


Bubble sort
Bubble Sort is a simple sorting algorithm, with the basic operation being to compare two adjacent
elements, if they are not in the correct order, swap them (swap). It can be done from top down (left to
right) or bottom up (right side). Bubble sort is also known as direct comparison sort. It uses element
comparison, so it is a comparison sort algorithm.

P a g e | 10


Selection sort
Selection Sort is a simple algorithm. This sorting algorithm is an algorithm based on in-place
comparison, where the list is divided into two parts, the sorted list on the left and the unsorted list on
the left. the right. Initially, the sorted part is empty, and the unsorted part is the entire original list.
The smallest element is selected from the unsorted array and swapped with the leftmost element, and
that element becomes the element of the sorted array. This process continues until all the elements in
the unsorted array have been moved to the sorted array.

4/ Searching Algorithms
P a g e | 11


4.1/ Definition
In computer science, a search algorithm is an algorithm that takes a problem as input and returns a
solution to that problem, usually after weighing a series of possible solutions. body. Most of the
algorithms studied by computer scientists to solve problems are search algorithms. The set of all
possible solutions to a problem is called the search space. Brute-force search or information less
"primary" search algorithms use the simplest and most intuitive method. Meanwhile, informed search
algorithms use heuristics to apply knowledge of the structure of the search space to reduce the time
required for searching.


4.2/ Linear search
Each element is checked, and if any match is found, that element is returned; If not found, the search
continues until all data is found

P a g e | 12


4.3/ Binary Search
Binary Search has a great advantage in terms of time complexity when compared to Linear Search.
Linear Search has a worst-case complexity of Ο(n) while Binary Search is Ο(log n).
Binary Search searches for a specific element by comparing the middle element in the data set. If a
match is found, the index of the element is returned. If the search element is greater than the middle
element value, the search element is searched in the subarray to the right of the middle element;
otherwise, it will look in the subarray to the left of the middle element. The process will continue like
this on the subarray until all elements in this subarray are found.

P a g e | 13


P2/ Determine the operations of a memory stack and how it is
used to implement function calls in a computer.
1/ Memory stack
Stack memory is a memory usage mechanism that allows the system memory to be used as temporary
data storage that behaves as a first-in-last-out buffer. One of the essential elements of stack memory
operation is a register called the Stack Pointer. The stack pointer indicates where the current stack
memory location is and is adjusted automatically each time a stack operation is carried out. PUSH and
POP are commonly used at the beginning and at the end of a function or subroutine. At the beginning
of a function, the current contents of the registers used by the calling program are stored in the stack
memory using PUSH operations, and at the end of the function, the data on the stack memory is
restored to the registers using POP operations. Typically, each register PUSH operation should have a

corresponding register POP operation; otherwise, the stack pointer will not be able to restore registers
to their original values. This can result in unpredictable behaviors, for example, function return to
incorrect addresses.

P a g e | 14


2. The Push Operation
The push operation involves inserting data items into a stack. Let's examine our restaurant plate
dispenser in our second figure. The push operation adds plates (data items) to the plate dispenser
P a g e | 15


(stack). The first plate is pushed to the bottom of the stack with all subsequent plates following in
order after it. The first data item inserted is the most inaccessible and is positioned at the bottom of
the stack.

3. The Pop Operation
The pop operation involves removing data items from a loaded stack. In our plate dispenser
illustration, the last plate (data item) added is positioned at the top of the stack. This data item is
popped out of the stack as the first item to be removed. Think of the spring loading system at the base
of the plate dispenser. It pushes the stack of plates upward each time a plate is removed. In memory,
items continue to be popped out in that order.

P a g e | 16


P3 Using an imperative definition, specify the abstract data type for a software
stack.
1/ Formal specification

In computer science, formal specifications are math-based techniques intended to aid in the
implementation of systems and software. They are used to describe a system, to analyze its behavior,
and to support its design by verifying key properties of interest through robust and efficient reasoning
tools. These specifications are formal in the sense that they have syntax, their semantics are in a
domain, and they can be used to infer useful information.
With each passing decade, computer systems become more and more powerful and, as a result, they
become more impactful on society. Therefore, better techniques are needed to support reliable
software design and implementation. Established engineering disciplines use mathematical analysis as
the foundation of product design creation and validation. The official specification is one of the ways
to achieve this in terms of software engineering reliability as has been predicted. Other methods such
as testing are more commonly used to improve code quality.

Benefit:
P a g e | 17




A higher level of rigor enables a better understanding of the problem



Defects are uncovered that would likely go unnoticed with traditional specification methods



identify defects earlier in the life cycle




Can guarantee the absence of certain defects

Limit:


Used as an adjunct to, not a replacement for, standard quality assurance methods



Formal methods are not a panacea, but can increase confidence in a product’s reliability if
applied with care and skill



Very useful for consistency checks, but cannot assure completeness of a specification

2/ What are the pre-condition, post-condition, and error-condition?
2.1/ Pre-condition
By convention, the precondition of a public method is required to check the parameter and throw the
specified exception. This is because : assert is designed to check the correctness of the program, not
for exception handling. When assert false, it will throw an Assertion Error, a generic and non-obvious
exception that the programmer can handle. As for the private method, we can use it to check the
correctness of a variable — no matter what the user enters. When we write assert right before
performing the operation, we are doing a precondition check

2.2/ Post-condition
A post Condition is a statement or set of statements describing the outcome of action if true when the
operation has completed its task. The Post Conditions statement indicates what will be true when the
action finishes its task.
A postcondition associated with a method invocation is a condition that must be true when we return

from a method. For example, if a natural logarithm method was called with input X, and the method
returns Y, we must have (within the limits of the level of precision being used).

2.3/ Error-condition
Status
ERROR is always enabled.
Result
An error message is issued if no ON-unit is active when the ERROR condition arises, or if the ON-unit
does not use a GOTO (to exit the block) to recover from the condition.
Cause and syntax

P a g e | 18


The ERROR condition is the implicit action for many conditions. This provides a common condition that
can be used to check for a number of different conditions, rather than checking each condition
separately.
The ERROR condition is raised under the following circumstances:


As a result of the implicit action for a condition, which is to raise the ERROR condition



As a result of the normal return action for some conditions, such as SUBSCRIPTRANGE
CONVERSION or when no retry is attempted



As a result of an error (for which there is no other PL/I-defined condition) during program

execution



As a result of a SIGNAL ERROR statement

In order to prevent a loop of ERROR conditions, the first statement in any ON ERROR block should be
ON ERROR SYSTEM.

3/ Specify stack Operation using this formal language.
After the function exits another set of conditions will apply. A statement of these postconditions is
needed to check that a particular implementation of the function works according to requirements.
For example, the pre and postconditions of a typical math square-foot function defined in the style of
VDM are:
SQRT(x:real)y:real
pre x >=0
post y*y = x and y >= 0
The input must be zero or positive; the output when multiplied by itself must equal the input and be
greater than or equal to zero because sqrt() always returns the positive square root. Alternatively, the
relationship between pre- and post-conditions can be represented as a Hoare triple {P}A{Q} where P
and Q are predicates and A is a program that terminates when it is run.
The Vienna Development Method (VDM) founded on Hoare Logic supplied a basis for the realization
of these concepts in software design, but various less formal specification languages are now
available. OCL for example, an adjunct to the Unified Modelling Language (UML), allows the use of
formal English to define conditions and invariants.
VDM, OCL, etc. are typified as being formal methods. Their representation as Design by Contract has
resulted in further denormalization although this may not have been the intention of Bertrand Meyer
who coined the metaphor to represent the relationship between client code and code supplying a
service.
Applying VDM to a stack ADT, a possible set of operations on a could be formulated as:

Stack(max_size: integer): r: Stack
P a g e | 19


Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×