1. Trang chủ
  2. » Luận Văn - Báo Cáo

Btec level 5 hnd diploma in computing unit number and title unit 19 data structures and algorithms

43 1 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Unit 19: Data Structures And Algorithms
Tác giả Nguyen Hai Anh
Người hướng dẫn Ta Quang Hieu
Trường học BTEC
Chuyên ngành Computing
Thể loại Bài tập
Định dạng
Số trang 43
Dung lượng 2,99 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Table of ContentsIntroduction...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 de

Trang 1

ASSIGNMENT 1 FRONT SHEET

Unit number and title Unit 19: Data Structures and Algorithms

Student declaration

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

Trang 2

Grading grid

1

Trang 3

P1 P2 P3 M1 M2 M3 D1 D2

Trang 4

Summative Feedback: Resubmission Feedback:

Internal Verifier’s Comments:

IV Signature:

Trang 5

3

Trang 6

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

Trang 7

4

Trang 8

Hello everyone, my name is Nguyen Hai Anh, and I work as an in-house software developer at SoftnetDevelopment 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 Ourcompany is currently engaged in a collaborative service provisioning development project, and we havebeen entrusted with a crucial role in designing and developing a middleware solution to interface withvarious computer provisioning interfaces and the telecom provisioning network One of the fundamentalconcepts 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 howthese operations will be implemented It does not specify how data will be organized in memory and whatalgorithms will be used for implementing the operations It is called “abstract” because it gives animplementation-independent view [objects?, W., 2023]

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

Trang 9

Figure 1: Abstract Data Type (ADT)

5

Trang 10

The user of data type does not need to know how that data type is implemented, for example, we havebeen using Primitive values like int, float, char data types only with the knowledge that these data type canoperate 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 ADTsnamely 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 existingelement 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 plateskept on top of each other as a real-life example The plate which we put last is on the top and since weremove the plate that is at the top, we can say that the plate that was put last comes out first [objects?, W.,2023]

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

Trang 11

6

Trang 12

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

Trang 13

Figure 3: Code LIFO (Last In First Out)

7

Trang 14

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 comparisonoperator on the elements The comparison operator is used to decide the new order of elements in therespective 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 dramaticallyreduce search time compared to unsorted data

- Improved Retrieval: In various applications, including databases and information retrieval systems, sorteddata can be retrieved faster This is crucial for systems handling large datasets that require rapid access tospecific information

- Algorithm Performance: Many algorithms perform optimally on sorted data Sorting enhances theefficiency 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 Itsimplifies 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 ifthey are in the wrong order This algorithm is not suitable for large data sets as its average and worst-casetime 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:

Trang 15

8

Trang 16

Figure 4: Buble sort code

Trang 17

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

9

Trang 18

- if arr[j] is greater than arr[j + 1] If this condition is true, it means that the elements are out of order, andthey 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 thispass If swapped is still false, it means that no swaps were made in this pass, and the array is alreadysorted 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 yourhands The array is virtually split into a sorted and an unsorted part Values from the unsorted part arepicked and placed at the correct position in the sorted part

- The algorithms:

Compare the current element (key) to its predecessor,

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:

Trang 19

Figure 7: Code insertion sort

10

Trang 20

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 theelement 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 variablewill be used to compare the key with elements in the sorted portion of the array and shift elements to makespace 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 thekey

- Inside the “while” loop, arr[j + 1] = arr[j]; move the element at index “j” one position to the right to makespace for the key

Trang 21

- 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 itsleft are smaller than it

Figure 9: Code insertion sort

11

Trang 22

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 theirperformance characteristics Here's a comparison table summarizing the performance characteristics ofbubble sort and insertion sort:

Bubble sort compares the adjacent elements

and move accordingly

Selection sort selects the smallest elementfrom the unsorted list and moves it at thenext position of the sorted list

Time complexity: O(n^2) in the worst and

average cases, O(n) in the best case (when

the input array is already sorted)

Time complexity: O(n^2) in all cases (worst,average, and best)

Bubble Sort may perform better than

Insertion Sort for larger datasets or datasets

that are partially sorted

Insertion Sort tends to perform better thanBubble Sort for small datasets

Bubble sort is relatively slower Selection sort is faster as compared to

bubble sortO: Order of

Trang 23

N: number of elements

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 Thesealgorithms 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 toanother while minimizing some cost or distance metric.Some common shortest path algorithms are:

- Bellman Ford’s Algorithm

12

Trang 24

- 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 theircritical role in ensuring efficient data transmission, resource optimization, fault tolerance, and overallnetwork 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 takesfor data to traverse the network

- Resource Efficiency: Shortest path algorithms assist in distributing traffic across the network'sinfrastructure optimally, ensuring that resources are not wasted and network capacity is fully utilized

- Network Design and Optimization: When designing network topologies or making changes toexisting networks, shortest path algorithms help find optimal routes and assess the effects of networkmodifications on performance and efficiency

- Network Monitoring and Diagnostics: Shortest path algorithms are used in network monitoring anddiagnostic tools to identify bottlenecks, anomalies, or routing problems

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

Trang 25

have a path that connects the source node to all other nodes following the shortest path possible to reacheach node.[ Afteracademy.com.2023]

Using illustrations, providing an example of each:

13

Trang 26

Figure 10: Dijkstra’s Algorithm

Trang 27

Bellman-Ford Algorithm

- Bellman-Ford is a single source shortest path algorithm that determines the shortest path between agiven source vertex and every other vertex in a graph This algorithm can be used on both weighted andunweighted graphs

- Although Bellman-Ford is slower than Dijkstra’s algorithm, it is capable of handling graphs withnegative edge weights, which makes it more versatile [ Afteracademy.com.2023]

14

Trang 28

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 inthe CPU by allocating a chunk of memory to a stack operation and using a processor register as a stackpointer The stack pointer is a processor register that specifies the stack's starting memory location

[GeeksforGeeks 2023]

Trang 29

15

Trang 30

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 areperformed at a single end of the stack, which is the top of the stack, and the following operations arepossible:

•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]

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

Trang 31

- 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)

16

Trang 32

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 followingthe 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 thecaller'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, thefirst AR on the stack is the last one to be removed [Tutorialspoint.com, 2023]

Ngày đăng: 12/04/2025, 22:40

Nguồn tham khảo

Tài liệu tham khảo Loại Chi tiết
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] Sách, tạp chí
Tiêu đề: What is the difference between Abstract Data Types and objects
Tác giả: W. objects
Nhà XB: Computer Science Stack Exchange
Năm: 2023
3. Afteracademy.com.2023.Comparison of Sorting Algorithms.[online] Available at:https://afteracademy.com/blog/comparison-of-sorting-algorithms [Accessed 2 October 2023] Sách, tạp chí
Tiêu đề: Comparison of Sorting Algorithms
Tác giả: AfterAcademy
Nhà XB: AfterAcademy
Năm: 2023
4. Programiz.com.2023.Bellman Ford's Algorithm.[online]Available at :<https://www.programiz.com/dsa/bellman-ford-algorithm> [Accessed 2 October 2023] Sách, tạp chí
Tiêu đề: Bellman Ford's Algorithm
Tác giả: Programiz.com
Năm: 2023
5. Tutorialspoint.com. (2023). Data Structure and Algorithms - Stack - Tutorialspoint. [online] Available at: https://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm Sách, tạp chí
Tiêu đề: Data Structure and Algorithms - Stack - Tutorialspoint
Nhà XB: Tutorialspoint.com
Năm: 2023
6. [3]. GeeksforGeeks. 2023. Abstract Data Types - GeeksforGeeks. [online] Available at:< https://www.geeksforgeeks.org/abstract-data-types/> [Accessed 2 October 2023] Sách, tạp chí
Tiêu đề: Abstract Data Types - GeeksforGeeks
Tác giả: GeeksforGeeks
Nhà XB: GeeksforGeeks
Năm: 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] Sách, tạp chí
Tiêu đề: Data Structure and Algorithms - Stack
Tác giả: Tutorialspoint.com
Nhà XB: Tutorialspoint
Năm: 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] Sách, tạp chí
Tiêu đề: What is Software Stack? - Definition from Techopedia
Nhà XB: Techopedia.com
Năm: 2023
9. GeeksforGeeks.2023.Queue Data Structure-GeeksforGeeks. [online]Available at:<https://www.geeksforgeeks.org/queue-data-structure/> [Accessed 2 October 2023] Sách, tạp chí
Tiêu đề: Queue Data Structure
Tác giả: GeeksforGeeks
Nhà XB: GeeksforGeeks
Năm: 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] Khác