1. Trang chủ
  2. » Công Nghệ Thông Tin

Asm 02 data structure

48 77 2

Đ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

Data Structures and Algorithms Study’s Name Huỳnh Trần Anh Khoa – GCS200252 CLASS GCS0903A | SUBJECT CODE 1649 Data Structures and Algorithms ASSIGNMENT 02 ASSIGNMENT 2 FRONT SHEET Qualification BTEC.

Data Structures and Algorithms ASSIGNMENT 02 Study’s Name : Huỳnh Trần Anh Khoa – GCS200252 CLASS: GCS0903A | SUBJECT CODE: 1649 ASSIGNMENT FRONT SHEET Qualification BTEC Level HND Diploma in Computing Unit number and title Unit 19: Data Structures and Algorithms Submission date June 26th 2022 Date Received 1st submission June 26th 2022 Re-submission Date Date Received 2nd submission Student Name Huỳnh Trần Anh Khoa Student ID GCS200252 Class GCS0903A Assessor name Lê Văn Thành Student declaration I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism I u false declaration is a form of malpractice Student’s signature Grading grid P4 P5 P6 P7 M4 M5 D3 D4  Summative Feedback: Grade:  Resubmission Feedback: Assessor Signature: Date: Internal Verifier’s Comments: IV Signature: Assignment Brief (RQF) Higher National Certificate/Diploma in Business Student Name/ID Number: Huỳnh Trần Anh Khoa – GCS200252 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: June 26th 2022 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: LO3 Implement complex data structures and algorithms LO4 Assess the effectiveness of data structures and algorithms Assignment Brief and Guidance: Assignment scenario Continued from Assignment Tasks For the middleware that is currently developing, one part of the provision interface is how message can be transferred and processed through layers For transport, normally a buffer of queue messages is implemented and for processing, the systems requires a stack of messages The team now has to develop these kind of collections for the system They should design ADT / algorithms for these structures and implement a demo version with message is a string of maximum 250 characters The demo should demonstrate some important operations of these structures Even it’s a demo, errors should be handled carefully by exceptions and some tests should be executed to prove the correctness of algorithms / operations The team needs to write a report of the implementation of the data structures and how to measure the efficiency of related algorithms The report should also evaluate the use of ADT in design and development, including the complexity, the trade-off and the benefits Learning Outcomes and Assessment Criteria (Assignment 2) Pass Merit Distinction LO3 Implement complex data structures and algorithms P4 Implement a complex ADT and algorithm in an executable programming language to solve a well defined problem M4 Demonstrate how the implementation of an ADT/algorithm solves a welldefined problem D3 Critically evaluate the complexity of an implemented ADT/algorithm P5 Implement error handling and report test results LO4 Assess the effectiveness of data structures and algorithms 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 D4 Evaluate three benefits of using implementation independent data structures Table of Contents Table of Contents Assignment Brief (RQF) Higher National Certificate/Diploma in Business Table of Contents P4 Implement a complex ADT and algorithm in an executable programming language to solve a well defined problem 1) Introduction 2) Implement ADT The given scenario is: To address a well-defined problem, implement a complicated ADT and algorithm in an executable programming language .7 Design the ADT and algorithm P5 Implement error handling and report test results .17 1) Error Handling 17 2) Test Result 19 P6 Discuss how asymptotic analysis can be used to assess the effectiveness of an algorithm 23 Discuss how asymptotic analysis can be used to assess the effectiveness of an algorithm 24 1) The execution time of algorithm .24 2) Asymptotic analysis 27 3) General Rules for Estimation 31 P7 Determine two ways in which the efficiency of an algorithm can be measured, illustrating your answer with an example 34 1) Time complexity 35 2) Space Complexity .36 3) Evalutte .39 Stack: 39 Queue: 41 Conclusion 44 Reference 44 P4 Implement a complex ADT and algorithm in an executable programming language to solve a well defined problem 1) Introduction Algorithms with varying degrees of inefficiency can frequently solve the same task When an algorithm is presented for a problem and (somehow) proven to be accurate, one critical step is determining how much time or space the method would require This assignment includes the following components of my report: • First, I will apply a complicated ADT and technique to a well-defined problem in Java programming • Second, I'll implement error handling and report on the results of the tests • The success of an algorithm may be evaluated using asymptotic analysis, which I'll discuss in my last part Finally, I'll present two alternative approaches for assessing the success of an algorithm, along with real examples 2) Implement ADT The given scenario is: “One aspect of the provider interface for the middleware that is presently being developed is how messages may be transported and processed via layers Typically, a buffer of queued messages is used for transit, and a stack of messages is used for processing The team must now create these types of collections for the system They should create ADT / algorithms for these two structures and construct a sample version with a message of no more than 250 characters The demonstration should show some of these structures' most significant operations Even if it's only a demo, failures should be handled properly using exceptions, and certain tests should be run to demonstrate the validity of algorithms and operations.” The supplied scenario suggests that we require some relevant ADTs to handle this issue The words stack and queue are described in Assignment • The stack is used to save process messages • A queue is used to hold the messages that have been transported To address a well-defined problem, implement a complicated ADT and algorithm in an executable programming language A stack is a linear data structure that can only be accessed at one of its ends for storing and retrieving data New trays are put on top of the stack and taken from the top, much like a stack of trays in the cafeteria The first tray removed from the stack is the one that was most recently inserted The outcome is the Last in First Out (LIFO) or First in Last Out (FILO) list As a result, the process messages are stored in Stack A queue is just a collection of objects that may expand by adding items at the end and contract by deleting ones from the beginning In contrast to a stack, a queue is a structure in which components may be added and deleted from both ends As a result, the last element must wait until every element in the queue has been wiped before being erased As a result, it is often referred to as a Last on Last out (LILO) or a First in First Out (FIFO) list As a result, the transferred messages are queued The message will be added to the queue, but it will not be added if the queue is already full case scenario, the growth rate is g(n) = O (n) Consider the following definition of theta notation: If there are positive numbers c1, c2, and n0 such that c1g(n) f(n) c2g(n) for every n n0, then f(n) is (g(n)) In the long run, f has an order of magnitude g, f is on the order of g, or both functions increase at the same pace If f (n) is O(g(n)) and f (n) is (g(n)), then f (n) is (g(n)) g(n) represents an asymptotic tight constraint for f (n) (g(n)) is the set of functions that increase in the same order as g (n) Figure: Illustrating the Theta-Θ notation 3) General Rules for Estimation In this part, I'll demonstrate the analysis using some simple code fragments We begin with an examination of a basic assignment statement to an integer variable A = B; because the assignment statement is constant in time, it is (1) There are certain broad guidelines that can help us determine an algorithm's execution time Loops: A loop's running time is, at most, the sum of the running times of the statements within the loop (including tests) multiplied by the number of iterations.\ 31 Figure: Loops The first sentence is (1) The for loop is iterated over n times Because the third line takes constant time, the total cost of running the two lines that comprise the for loop is (n) The total cost of the code segment is also (n) Total time = a constant c × n = c n = O(n) Nested Loops: Analyze nested loops from the inside out The overall running time of a statement within a set of nested loops is the statement's running time multiplied by the product of all loop sizes We will now look at a code snippet that has numerous for loops, some of which are nested Figure: Nested Loop This code consists of three statements: the initial assignment instruction and two for loops Again, the assignment statement takes a fixed amount of time; we'll name it c1 The second for loop takes c2n = (n) time to complete We start from the interior of the loop and work our way out Call the equation sum++ c3 since it needs constant time The inner for loop has a cost of c3i since it is run I times The outer for loop is run n times, but the cost of the inner loop varies since it costs c3i, where I changes each time We can see that I is for the first execution of the outer loop and for the second execution of the outer loop Each trip around the outer loop, I grows by one, until the final time through the loop, when I = n As a 32 result, the total cost of the loop is c3 multiplied by the sum of the numbers through n We are aware that n2) is straightforward (n2) that is to say (n2) Total time = (c1 + c2 n + c3 Consecutive statement: Add each statement's temporal complexities As an example, the following program fragment is also O(n2) since it has O(n) work followed by O(n2) work: Figure: Consecutive statement Total time = c0+ c1n + c2n2 = O(n2) If-then-else statement The running time of an if/else statement in the fragment is never more than the running time of the test plus the greater of the running timings of S1 and S2 Method calls must be examined first if they exist There are numerous choices if there are recursive methods If the recursion is simply a disguised for loop, the analysis is generally straightforward For example, the following approach is simply a loop and is O(1) (n) 33 Figure: If-then-else statement P7 Determine two ways in which the efficiency of an algorithm can be measured, illustrating your answer with an example Computer resources are scarce and should be used wisely The quantity of computational resources consumed by an algorithm is defined as its efficiency To determine the resource utilization of an algorithm, it must be evaluated The usage of various resources can be used to assess an algorithm's efficacy The quantity of resources consumed by an algorithm may be used to determine its efficiency The speed, timing, and amount of the input are all factors that impact an algorithm's efficiency An algorithm's primary resources are as follows: • The amount of memory required by an algorithm during its execution as a space-time tradeoff • Time complexity: The amount of time it takes the CPU to run the program Different elements influence an algorithm's time efficiency Write a program for a certain algorithm, run it in any programming language, and record the total time it takes to run The execution time you measure in this scenario will be determined by a variety of factors, including: • Machine speed • Compiler software and other systems • Operating system • Programming language used • Data volume necessary Because time is frequently more essential than space, efficiency concerns often center on the amount of time elapsed when processing data However, the least efficient method on a Cray computer may execute considerably quicker than the most efficient 34 algorithm on a PC, therefore the runtime is always system dependent To compare 20 algorithms, for example, they must all be executed on the same system Furthermore, even if the tests are done on the same system, the results of runtime tests are affected by the language in which a particular algorithm is written Compiling programs allows them to run considerably quicker than interpreting them A program written in C or Ada can be up to 20 times quicker than a BASIC-encoded application 1) Time complexity The number of operations an algorithm does to complete its goal in relation to the input size is referred to as temporal complexity (considering that each operation takes the same amount of time) The most efficient algorithm is one that completes the task with the fewest amount of operations Asymptotic Notations are three terminologies that can be used to indicate time complexity • Use the Big - Oh or Big O Notation (BIG O) • Large - Omega • Theta - Big However, we will most often choose the Big O notation since it gives us an upper limit of the execution time, i.e., the execution time with the worst-case inputs The Big O notation indicates an algorithm's runtime in terms of its growth rate relative to the input (this input is denoted by "n") In this approach, if we declare that the runtime of an algorithm rises "in the order of the amount of the input," we may say that it is "O (n)." When we state that the runtime of an algorithm rises "in order to the square of the amount of the input," we're talking about "O (n2)." An algorithm's typical complexity are: Big O Notation Time Complexity Detail O (1) Constant Time Complexity O (1) happens when there are no loops, recursive functions, or calls to other functions in the program In this situation, the execution time will remain constant regardless of the input value O (n) Complexity of Linear Time When the execution time of the code rises by an order of magnitude proportional to n, the result is O(n) The size of the input is denoted by n O (log n ) Complexity of Logarithmic Time O (log n) happens when the time decreases inversely proportionally to N at each consecutive step in the algorithm This is common in the Binary Search Algorithm 35 O (n log n) O (n2) O (2n) Linearities Time Complication Quick Sort, Heap Sort, and Merge Sort are three algorithms that execute with this time complexity Time Complexity in Quadratic Form Time Complexity that is Exponential Figure: A graph illustrating how different temporal complexity respond as the number of elements increases The coefficients were chosen in such a way that the worst temporal complexity were the biggest by time n = 1000 2) Space Complexity The overall amount of memory utilized by an algorithm or program, including the space of the input data for execution, is referred to as spatial complexity To determine space-complexity, just compute the space consumed by the variables utilized in an algorithm or program Space Complexity, like Time Complexity, is frequently stated asymptotically in Big O Notation, such as O (1), O (log n), O(n), O(n log n), O(n2), O(n3), O(2n), O(n!) where n is an input feature impacting space complexity and O is the worst-case scenario growth rate function 36 Memory is needed for the following purposes in any algorithm: • To save variables (constant and transient) • To save program instructions (or steps) • To make the software run If we enter inequation, the space complexity may be described mathematically as, Auxiliary Space + Input Space = Space Complexity Auxiliary Space is frequently mistaken with Space Complexity The Auxiliary Space, on the other hand, is the additional or temporary space used by the algorithm during its execution When a program is running, it requires compute device memory for three reasons: * Instruction Space: When code is compiled, it must be stored in memory someplace before it can be run The Instruction Space is the location where it is kept * The Environmental Stack stores the addresses of partly called functions It indicates that an algorithm (function) might be invoked within another algorithm at times (function) For example, if a function A () calls function B () within it, all variables of function A () are temporarily saved on the system stack while function B () is called and performed within the function A () * Data Space: A location where the program's data, variables, and constants are stored and changed throughout execution When we wish to analyze an algorithm based on its Space complexity, we normally just examine Data Spaces and disregard Instruction Spaces and the Environment Stack That is, we just compute the amount of memory necessary to hold variables, constants, structures, and so on A list of several typical Spatial complexity words is provided below It is organized in the order of their execution time as the size of their input rises S.N O Big O Notation O (1) O (log n) O (n) O (n log n) O(n2) O(n3) O(ny) O(2n) O (n!) Name Constant Space Complexity Logarithmic Space Complexity Linear Space Complexity Linearithmic Space Complexity Quadratic Space Complexity Cubic Space Complexity Polynomial Space Complexity Exponential Space Complexity Factorial Space Complexity 37 To gain a deeper grasp Examine the chart below, which is a compilation of all charts for various Space Complexities The growth in the input data set clearly shows how space complexity increases Figure: Space Complexity To compute space complexity, we need to know the amount of memory consumed by various types of data type variables, which typically changes between operating systems, but the process of calculating space complexity remains the same For example, C Programming Language compilers often (but not always) require the following: Type Bool, char, unsigned char, signed char, int _int16, short, unsigned short Float, int32, int, unsigned int, long, unsigned long Double, _int64, long double, long Size byte byte byte byte Now I'll decode how space-time complexity is computed using an example: Because the variables a, b, c, and z in the above statement are all integer types, they will each take up bytes, resulting in a total memory usage of (4 (4) + 4) = 20 bytes This extra bytes are for the return value Because the spatial requirement in the above example is fixed, it is referred to as Constant Space Complexity 38 Constant Space Complexity or O (1) Space Complexity is defined as any method that requires a set amount of space for all input values 3) Evalutte I also utilized the stack and queue to tackle this problem based on the supplied circumstances • The stack is used to save process messages • A queue is used to hold the messages that have been transported Stack: For the stack implementation, I utilized dynamic arrays However, this method of expanding the array's size is too costly To push a message, for example, at n = 1, construct a new array of size and copy all the existing messages of the array to the new array before adding the new message To push a message at n = 2, establish a new array of size and transfer all of the array's existing messages to the new array before adding the new message Similarly, if we wish to push a message at n = n - 1, we construct a new array of size n, copy all the previous items of the array to the new array, and lastly add new elements The total time T (n) (number of copy operations) after n push operations is proportional to + + + n O (n2) As a result, we increase complexity by employing the array duplication approach If the array is full, duplicate the elements in a new array twice the size Pushing n items takes time proportionate to n using this method (not n2) To keep things simple, let's say we started with n = and worked our way up to n = 32 That is, we double at 1, 2, 4, 8, and 16 If we wish to add (push) a message at n = 1, we may duplicate the current size of the array and copy all messages from the old array to the new array, and so on When we get to n = 32, the total number of copy operations is + + + + 16 = 31, which is about equal to 2n (32) If we look closely, we can see that we are doing the doubling process long times In brief, we double the array size logn times for n push operations That is, the logn words will appear in the expression below The total time T (n) of a sequence of n push operations is O (n), while the depreciation time of a push operation is O (n) (1) 39 Figure: Stack Implementation Each method runs a certain number of statements including arithmetic operations, comparisons, and assignments, or calls to size and isEmpty, both of which run indefinitely As a result, in this implementation of the stack ADT, each method runs in constant time, that is, in O (1) time Assume there are n messages in the stack The following are the difficulties of stack operations using this representation: Space Complexity (for n push operations) Time Complexity of push () Time Complexity of pop () Time Complexity of size () 40 O (n) O (1) O (1) O (1) Time Complexity of isEmpty () Time Complexity of isFullStack () O (1) O (1) Queue: Each queue method in the array realization, like our array-based stack implementation, runs a certain amount of statements including arithmetic operations, comparisons, and assignments As a result, each method in this implementation executes in O (1) time 41 Figure: Queue Implementation 42 Figure: Queue Implementation 02 43 The table below illustrates the runtime of the techniques used to implement a queue using an array Space Complexity (for n enQueue operations) Time Complexity of enQueue Time Complexity of deQueue Time Complexity of size Time Complexity of isEmpty Time Complexity isFull Time Complexity of First O (n) O (1) O (1) O (1) O (1) O (1) O (1) Conclusion In summary, I worked on sophisticated algorithms and data structures in this project by creating a software chat system utilizing stack and queue ADTs Then I performed an efficiency evaluation of the system's data structures and algorithms Reference 1.Runestone.academy 2020 3.3 Big-O Notation — Problem Solving With Algorithms And Data Structures [online] Available at:https://runestone.academy/runestone/books/published/pythonds/AlgorithmAna lysis/BigONotation.html [Accessed March 2021] Jcsites.juniata.edu 2021 Algorithm Efficiency [online] Available at: http://jcsites.juniata.edu/faculty/rhodes/cs2/ch12a.htm [Accessed March 2021] 3.Study.com 2020 [online] Available at: https://study.com/academy/lesson/how-to-estimate-inmathdefinitionlesson-quiz.html [Accessed March 2021] 4.En.wikipedia.org 2020 Analysis Of Algorithms [online] Available at: https://en.wikipedia.org/wiki/Analysis_of_algorithms [Accessed March 2021] 5.ResearchGate 2020 How Can I Measure The Performance Of An Algorithm? [online] Available at: https://www.researchgate.net/post/How_can_I_measure_the_performance_of_a n_algorithm [Accessed March 2021] 6.Anon., n.d Time and Space Complexity [Online] Available at: https://www.hackerearth.com/practice/basicprogramming/complexityanalysis/time- and-space-complexity/tutorial/[Accessed 3 2020] 44 7.Kumer, B., 2020 Space Complexity [Online]Available at: https://dev.to/bks1242/spacecomplexity-529b [Accessed 3 2021] [8] W3schools.com 2021 Java Encapsulation and Getters and Setters [online] Available at: [Accessed 12 February 2021] [9] Programiz.com.2021.Bellman Ford's Algorithm.[online]Available at: https://www.programiz.com/dsa/bellman-ford-algorithm [Accessed 12 February 2021] [10] objects?, W., 2021 What is the difference between Abstract Data Types and objects? [online] Computer Science Stack Exchange Available at: [Accessed 12 February 2021] 45 ... in Computing Unit number and title Unit 19: Data Structures and Algorithms Submission date June 26th 2022 Date Received 1st submission June 26th 2022 Re-submission Date Date Received 2nd submission... Business Student Name/ID Number: Huỳnh Trần Anh Khoa – GCS2 0025 2 Unit Number and Title: Unit 19: Data Structures and Algorithms Academic Year: 2021 Unit Assessor: Assignment Title: Implement and assess... failed assignment Unit Learning Outcomes: LO3 Implement complex data structures and algorithms LO4 Assess the effectiveness of data structures and algorithms Assignment Brief and Guidance: Assignment

Ngày đăng: 03/12/2022, 15:03

Xem thêm:

w