You are on page 1of 23

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 Nguyen Hai Anh Student ID BH00125

Class IT0502 Assessor name Ta Quang Hieu

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 Anh

1
Grading grid

P1 P2 P3 M1 M2 M3 D1 D2

2
 Summative Feedback:  Resubmission Feedback:

Grade: Assessor Signature: Date:


Internal Verifier’s Comments:

IV Signature:

3
Table of Contents
Introduction...................................................................................................................................................................5
P1 Create a design specification for data structures explaining the valid operations that can be carried out on
the structures.................................................................................................................................................................5
Using an imperative definition, speccify the abstract data type for a software stack ....................................................5
- Abstract Data Type (ADT):...............................................................................................................................5
Stack ADT:..........................................................................................................................................................6
Compare the performance of two sorting algorithms...............................................................................................8
Analyse the operation, using illustrations, of two network shortest path algorithms, providing an example of each.
............................................................................................................................................................................... 14
P2 Determine the operations of a memory stack and how it is used to implement function calls in a computer........16
Memory Stack:......................................................................................................................................................16
Operations.............................................................................................................................................................17
Exception...............................................................................................................................................................18
Application:...........................................................................................................................................................18
Method calls and the implementation by using stack.............................................................................................18
Implement Stack by Array in Java:........................................................................................................................19
P3. Using an imperative definition, specify the abstract data type for a software stack.............................................20
Definition of software stack:..................................................................................................................................20
Parts of a software stack........................................................................................................................................20
Five Software Stack Examples:.............................................................................................................................21
References................................................................................................................................................................. 23

4
Introduction
Hello everyone, my name is Nguyen Hai Anh, and I work as an in-house software developer at Softnet
Development Ltd. Today, I'm excited to share some insights into the world of Abstract Data Types
(ADTs) and how they can significantly impact the design, development, and testing of software solutions.
Our company is currently engaged in a collaborative service provisioning development project, and we
have been entrusted with a crucial role in designing and developing a middleware solution to interface
with various computer provisioning interfaces and the telecom provisioning network. One of the
fundamental concepts we'll be focusing on is the use of ADTs in this project.

P1 Create a design specification for data structures explaining


the valid operations that can be carried out on the structures.
Using an imperative definition, speccify the abstract data type for a
software stack.
- Abstract Data Type (ADT):
Abstract Data type (ADT) is a type (or class) for objects whose behavior is defined by a set of values and
a set of operations. The definition of ADT only mentions what operations are to be performed but not how
these operations will be implemented. It does not specify how data will be organized in memory and what
algorithms will be used for implementing the operations. It is called “abstract” because it gives an
implementation-independent view. [objects?, W., 2023]

The process of providing only the essentials and hiding the details is known as abstraction.

Figure 1: Abstract Data Type (ADT)

5
The user of data type does not need to know how that data type is implemented, for example, we have
been using Primitive values like int, float, char data types only with the knowledge that these data type can
operate and be performed on without any idea of how they are implemented.

So a user only needs to know what a data type can do, but not how it will be implemented. Think of ADT
as a black box which hides the inner structure and design of the data type. Now we’ll define three ADTs
namely List ADT, Stack ADT, Queue ADT. [objects?, W., 2023]
Stack ADT:
- A stack is a linear data structure in which the insertion of a new element and removal of an
existing element takes place at the same end represented as the top of the stack. [objects?, W., 2023]

- LIFO (Last In First Out):


This strategy states that the element that is inserted last will come out first. You can take a pile of plates
kept on top of each other as a real-life example. The plate which we put last is on the top and since we
remove the plate that is at the top, we can say that the plate that was put last comes out first. [objects?, W.,
2023]

- Basic Operations on Stack:


 Push () to insert an element into the stack
 Pop () to remove an element from the stack
 Top () Returns the top element of the stack.
 IsEmpty() returns true if stack is empty else false.
 Size () returns the size of stack.

6
Figure 2: LIFO (Last In First Out)
- Example: Browser History Management: Web browsers often use a stack to track the browsing
history of users. Each URL visited by the user is added to the stack when they navigate to a new webpage.
When the user requests to go back or navigate to a previous page, the browser uses the stack to manage
and display the previously visited URLs.

Figure 3: Code LIFO (Last In First Out)

7
Compare the performance of two sorting algorithms.
1. Introduction to sorting algorithms.
- A Sorting Algorithm is used to rearrange a given array or list of elements according to a comparison
operator on the elements. The comparison operator is used to decide the new order of elements in the
respective data structure. [GeeksforGeeks. 2023]
1.2. Explanation of the importance of sorting in data processing.
- Efficient Searching: Sorting enables quicker and more efficient searching of data. When data is ordered,
searching in data structure provides algorithms like binary search that can be employed to dramatically
reduce search time compared to unsorted data.
- Improved Retrieval: In various applications, including databases and information retrieval systems, sorted
data can be retrieved faster. This is crucial for systems handling large datasets that require rapid access to
specific information.
- Algorithm Performance: Many algorithms perform optimally on sorted data. Sorting enhances the
efficiency of algorithms like merge sort and quicksort, resulting in faster processing times.
- Identifying Patterns: Sorted data makes patterns and trends more apparent, aiding in data analysis. It
simplifies tasks like identifying outliers, understanding distributions, and drawing meaningful insights.
[GeeksforGeeks. 2023]

1.3. Brief mention of two sorting algorithms .


Buble sort: [GeeksforGeeks. 2023]
- Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if
they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case
time complexity is quite high.
- give the algorithms:
 traverse from left and compare adjacent elements and the higher one is placed at right side.
 In this way, the largest element is moved to the rightmost end at first.
 This process is then continued to find the second largest and place it and so on until the data is sorted.
- the codes:

8
Figure 4: Buble sort code

9
Figure 5: Buble sort code

- The first “for” loop: beginning with i = index position 0, i is less than the length of arr - 1
- Assume Swapped = false to check whether the array is completely sorted or not
- The second “for” loop: with j = index position 0, i is less than the length of arr - i - 1
- if arr[j] is greater than arr[j + 1]. If this condition is true, it means that the elements are out of order, and
they are swapped using a temporary variable “temp”.
- After swapping, swapped is set to true to indicate that at least one swap occurred during this pass.
- After the inner loop finishes, there is a check if (!swapped) to see if any swaps were made during this
pass. If swapped is still false, it means that no swaps were made in this pass, and the array is already
sorted. In that case, the sorting process terminates early with the break statement.

Figure 6: Buble sort code


- Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards in your
hands. The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are
picked and placed at the correct position in the sorted part.
- The algorithms:
 Compare the current element (key) to its predecessor,

10
 If the key element is smaller than its predecessor, compare it to the elements before
 Move the greater elements one position up to make space for the swapped element.
- The codes:

Figure 7: Code insertion sort

11
Figure 8: Code insertion sort

- The “for” loop: This loop iterates over the array from i = 1 to i = n - 1. It represents the index of the
element that we want to insert into the sorted portion of the array.
- Inside the loop, int key = arr[i]; stores the value of the current element at index “i” in the variable key.
- int j = i - 1; This line initializes a variable “j” to the index just before the current element “i”. This variable
will be used to compare the key with elements in the sorted portion of the array and shift elements to make
space for the key.
- The “while” loop: This loop runs as long as “j” is >= 0 and the element at index arr[j] is greater than the
key.
- Inside the “while” loop, arr[j + 1] = arr[j]; move the element at index “j” one position to the right to make
space for the key.
- j = j - 1;: This decrements “j” to move further left in the sorted portion of the array.
- After the while loop, arr[j + 1] = key; inserts the key into its correct sorted position, as all elements to its
left are smaller than it.

12
Figure 9: Code insertion sort

1.4. Compare the performance of bubble sort and selection sort


Bubble sort and insertion sort are both simple sorting algorithms, but they differ in terms of their
performance characteristics. Here's a comparison table summarizing the performance characteristics of
bubble sort and insertion sort:

BUBLE SORT SELECTION SORT


Bubble sort compares the adjacent elements Selection sort selects the smallest element
and move accordingly. from the unsorted list and moves it at the
next position of the sorted list.
Time complexity: O(n^2) in the worst and Time complexity: O(n^2) in all cases (worst,
average cases, O(n) in the best case (when average, and best)
the input array is already sorted)
Bubble Sort may perform better than Insertion Sort tends to perform better than
Insertion Sort for larger datasets or datasets Bubble Sort for small datasets
that are partially sorted
Bubble sort is relatively slower. Selection sort is faster as compared to
bubble sort
O: Order of.
N: number of elements

13
Analyse the operation, using illustrations, of two network shortest path
algorithms, providing an example of each.
2. Introduction to network shortest path algorithms.

Network shortest path algorithms are a fundamental part of graph theory and network analysis. These
algorithms are used to find the shortest path or the minimum cost path between two nodes in a network,
which can represent a wide range of real-world scenarios such as road networks, computer networks,
social networks, and more. The goal is to determine the most efficient route or path from one point to
another while minimizing some cost or distance metric.Some common shortest path algorithms are:
- Bellman Ford’s Algorithm
- Dijkstra’s Algorithm
There are two main types of shortest paths:
- Single-Source Shortest Path: Finding the shortest path from a single source node to all other nodes
in the network. The most common algorithm for this is Dijkstra's algorithm.
- Single-Pair Shortest Path: Finding the shortest path between a specific pair of nodes in the network. This
is typically achieved using algorithms like Dijkstra's algorithm or the Bellman-Ford algorithm.
2.2 Explanation of their relevance in networking and routing.
Network shortest path algorithms are highly relevant in the field of networking and routing due to their
critical role in ensuring efficient data transmission, resource optimization, fault tolerance, and overall
network performance
- Optimal Routing: Shortest path algorithms are fundamental for finding these optimal routes,
minimizing delays, congestion, and resource utilization.
- Latency Reduction: By selecting the shortest path, these algorithms help reduce the time it takes
for data to traverse the network
- Resource Efficiency: Shortest path algorithms assist in distributing traffic across the network's
infrastructure optimally, ensuring that resources are not wasted and network capacity is fully utilized.
- Network Design and Optimization: When designing network topologies or making changes to
existing networks, shortest path algorithms help find optimal routes and assess the effects of network
modifications on performance and efficiency
- Network Monitoring and Diagnostics: Shortest path algorithms are used in network monitoring and
diagnostic tools to identify bottlenecks, anomalies, or routing problems

Mention of two algorithms (e.g., Dijkstra's Algorithm and Bellman-Ford Algorithm).


Dijkstra's Algorithm:
- This algorithm was created and published by Dr. Edsger W. Dijkstra, a brilliant Dutch computer
scientist and software engineer.
- Dijkstra's Algorithm basically starts at the node that you choose (the source node) and it analyzes
the graph to find the shortest path between that node and all the other nodes in the graph.
- The algorithm keeps track of the currently known shortest distance from each node to the source
node and it updates these values if it finds a shorter path.
- Once the algorithm has found the shortest path between the source node and another node, that
node is marked as "visited" and added to the path.
- The process continues until all the nodes in the graph have been added to the path. This way, we
have a path that connects the source node to all other nodes following the shortest path possible to reach
each node.[ Afteracademy.com.2023]
14
Using illustrations, providing an example of each:

Figure 10: Dijkstra’s Algorithm

Bellman-Ford Algorithm
- Bellman-Ford is a single source shortest path algorithm that determines the shortest path between a
given source vertex and every other vertex in a graph. This algorithm can be used on both weighted and
unweighted graphs.
- Although Bellman-Ford is slower than Dijkstra’s algorithm, it is capable of handling graphs with
negative edge weights, which makes it more versatile. [ Afteracademy.com.2023]

15
Figure 11: Bellman-Ford Algorithm

P2 Determine the operations of a memory stack and how


it is used to implement function calls in a computer.
Memory Stack:
A stack can be implemented in a computer's random-access memory (RAM). A stack is implemented in
the CPU by allocating a chunk of memory to a stack operation and using a processor register as a stack
pointer. The stack pointer is a processor register that specifies the stack's starting memory location.
[GeeksforGeeks. 2023]

16
Figure 12: Memory stack

Operations
A Stack is a collection of elements of the same type that are arranged in a logical order. All operations are
performed at a single end of the stack, which is the top of the stack, and the following operations are
possible:
•push() – Inserts a new element at the top of the stack.

•pop() – If the stack isn't empty, remove and return the element at the top.

•peek() – If the stack is not empty, return the element at the top of the stack without removing it.

•size() – Returns the stack's size in elements.

•isEmpty() – If the stack is empty, return true; otherwise, return false.

•isFull() – If the stack is full, return true; otherwise, return false [Tutorialspoint.com, 2023]

17
Exception
-The operations pop and top cannot be performed if the stack is empty. -A StackEmptyException should
be thrown if you try to execute pop or top on an empty stack.
- Due to a lack of memory, push operations are occasionally unable to complete.
- When memory is insufficient, attempting to execute push should result in an OutOfMemoryError.
[Tutorialspoint.com, 2023]

Application:
Nesting of any kind (such as parentheses)

 Determining the value of arithmetic expressions (and other sorts of expression)


 Implementing method or function calls
 Maintaining a record of previous decisions (as in backtracking)
 Keeping track of decisions that have yet to be made (as in creating a maze)
 In a text editor, undo sequence
 An algorithm's auxiliary data structure
 Part of a larger data structure [Tutorialspoint.com, 2023]

Method calls and the implementation by using stack


An activation record (AR) is assigned to each method that is called.
-The following information is contained in this record:

 The called method's parameters and local variables


 Dynamic link: a link to the activation record for the caller
 Return address to allow the caller to regain control (address of instruction immediately following
the call)
 Value returned by a method that isn't declared as void.
 Because the size of the AR varies from call to call, the returned value is placed directly above the
caller's AR.
 Each new AR is stacked on top of the previous one.
 A method's AR is removed from the top of the run-time stack when it terminates. As a result, the
first AR on the stack is the last one to be removed. [Tutorialspoint.com, 2023]

18
Implement Stack by Array in Java:

Output

19
P3. Using an imperative definition, specify the
abstract data type for a software stack.
Definition of software stack:
An application is made up of a set of functions that work together in a defined architecture to provide the
user with specific services. Three layers make up the most basic application architecture:

 Layer of Presentation: When a client accesses an application through a website or web-based


application portal, they see the presentation layer.
 Logic Layer: The logic layer stores application logic and business rules, which aids in the
fulfillment of application requests. This layer performs calculations and makes decisions about
how to handle requests while supervising data flow between the data layer and the presentation
layer.
 Data Layer: The data layer is a server-side system that sends data to the logic layer when it's time
to complete a computation or deliver it to the presentation layer, where users can view it.
Each of these layers has its own set of programming languages and software tools that it needs to set up
and maintain its functionality. HTML5, JavaScript, and CSS are examples of languages that can be used to
create a web-based presentation layer. Java, C#, Python, or C++ could be used to create the application
layer. Back-end servers could be maintained using applications like MySQL and MongoDB.
The term "Software Stack" refers to the collection of components that work together to support application
execution. Some software components are used to power back-end processes, while others are used in the
presentation layer to enable user interface. In any case, the components of a software stack work together
to deliver application services to the end-user as quickly as possible. [Techopedia.com, 2023]

Parts of a software stack


There are four tiers in an application, three of which are server-side. This diagram depicts how a stack
works: the client is where it all begins and ends.
•In the browser, the client tier is the only component.

•The web tier is comprised of the web server (also known as an HTTP server).

•The application server (which includes the development platform, frameworks, and server-side
programming languages) is the business tier.

•The database tier—the database server you select, which is frequently influenced by the business tier.

20
An operating system, server, database, and server-side scripting language are all included in each tier.
You're not restricted to the components in a stack; they're interchangeable and customizable based on your
needs. [Techopedia.com, 2023]

Figure 13: software stack

Five Software Stack Examples:


Other developers may use a software stack that has proven to be beneficial or preferable for delivering a
particular type of application on occasion. A popular software stack may take on a life of its own as an
increasing number of software companies choose the same set of software components to create an
application. Software companies may bundle and sell specific components as a single software stack for a
specific purpose. Five of the most popular software stacks for developers to use as an application platform
are listed below:
1. LAMP - LAMP is a web services-oriented software stack that can be used to build dynamic web
pages and cloud applications. The stack includes the Linux operating system, Apache web server,
MySQL relational database management system, and PHP programming language.
2. To create dynamic websites and web apps, the MEAN software stack is used, which consists of
four free and open-source components: a database tool called MongoDB, the Express.js web

21
application server framework, and a front-end web framework called Angular.js, and the Node.js
runtime environment.
3. WIMP - The Windows operating system, IIS web servers, MySQL or MS Access for data
administration, and PHP, Perl, or Python for programming make up the WIMP software stack.
4. NMP - NMP is a collection of software stacks that include Nginx web servers, MySQL, and PHP
programming language. This technology suite has been packaged separately for Linux, Windows,
and macOS and is compatible with all major operating systems.
5. MAMP - The MAMP framework allows you to build websites that run on both Windows and Mac
computers. The operating system (macOS or Windows), the Apache web server, MySQL for
relational database administration, and PHP, Perl, or Python for web programming make up the
software stack.
Each software stack has its own set of advantages and disadvantages for developers. Before deciding on
the best set of software solutions to support the delivery of application services to end-users, application
architects must first comprehend and anticipate the application's unique requirements. [ Techopedia.com,
2023]

22
References
1. objects?, W., 2023. What is the difference between Abstract Data Types and objects?. [online] Computer
Science Stack Exchange. Available at: https://cs.stackexchange.com/questions/51847/what- is-the-
difference-between-abstract-data-types-and-objects [Accessed 2 October 2023].
2. W3schools.com. 2023. Java Encapsulation and Getters and Setters. [online] Available
at:<https://www.w3schools.com/java/java_encapsulation.asp> [Accessed 2 October 2023].
3. Afteracademy.com.2023.Comparison of Sorting Algorithms.[online] Available
at:https://afteracademy.com/blog/comparison-of-sorting-algorithms [Accessed 2 October 2023].
4. Programiz.com.2023.Bellman Ford's Algorithm.[online]Available
at :<https://www.programiz.com/dsa/bellman-ford-algorithm> [Accessed 2 October 2023].
5. Tutorialspoint.com. (2023). Data Structure and Algorithms - Stack - Tutorialspoint. [online] Available at:
https://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm.
6. [3]. GeeksforGeeks. 2023. Abstract Data Types - GeeksforGeeks. [online] Available at:<
https://www.geeksforgeeks.org/abstract-data-types/> [Accessed 2 October 2023].
7. Tutorialspoint.com. 2023. Data Structure and Algorithms - Stack - Tutorialspoint. [online]
Available at:< http://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm#
%3A~%3Atext%3DA%20stack> [Accessed 2 October 2023]
8. [6]. Techopedia.com. 2023. What is Software Stack? - Definition from Techopedia. [online] Available at:<
https://www.techopedia.com/definition/4356/software#:~:text=Software%20is%20often%20used
%20to,often%20used%20to%20describe%20software.> [Accessed 2 October 2023].
9. GeeksforGeeks.2023.Queue Data Structure-GeeksforGeeks. [online]Available at:
<https://www.geeksforgeeks.org/queue-data-structure/> [Accessed 2 October 2023].

23

You might also like