C代写-COMP2521-Assignment 1
时间:2022-06-29
29/06/2022, 18:40 COMP2521 22T2 - Assignment 1https://cgi.cse.unsw.edu.au/~cs2521/22T2/assignments/ass1 1/8Assignment 1Efficient Set ADTChangelogAll important changes to the assignment specification and files will be listed here.[21/06 13:00] Assignment released[25/06 17:20] Clarified the array constraint and added more clarifications from the forum threadAimsTo implement a set ADT using a balanced binary search tree and analyse the time complexity of its operationsTo give you practice with binary search trees and complexity analysisTo appreciate the importance of using efficient data structures and algorithmsAdminMarks contributes 15% towards your final mark (see Assessment section for more details)Submit see the Submission sectionDeadline 8pm on Monday of Week 7Late penalty 0.2% per hour or part thereof deducted from the attained mark, submissions later than 5 days not acceptedPrerequisite KnowledgeRecursionAnalysis of AlgorithmsAbstract Data TypesBinary Search TreesBalanced BSTs (including AVL Trees)BackgroundSetsA set is a collection, or ensemble, of distinct items. We call these items elements, or members of the set. We can also say that theybelong to that set. In this assignment, we are concerned with sets of integers.Usually, we write a set by displaying its elements between two curly braces. For example, is a set whose elements are 1,4, 6, 9. Note that the order in which the elements are written does not make the set different, so for example, andand all represent the same set.NotationConventionally, sets are denoted by capital Roman letters and their elements denoted by small Roman letters.Now let and be sets and be an item.{1, 4, 6, 9}{1, 4, 6, 9}{6, 9, 1, 4} {1, 6, 9, 4}A B v29/06/2022, 18:40 COMP2521 22T2 - Assignment 1https://cgi.cse.unsw.edu.au/~cs2521/22T2/assignments/ass1 2/8is said to be in , written as , if is an element of .Set is said to be equal to set , written as , if both sets have the same elements.Set is said to be a subset of set , written as , if all elements of are also elements of . For example, we have thatand .Operations on SetsIn Lecture 2.2 - Abstract Data Types (ADTs), we introduced some fundamental set operations:insert an item into a setcheck if an item is a member of a setcheck the size, or cardinality, of a setdisplay a set using standard set notationget the union of two setsget the intersection of two setsFor those unfamiliar with set theory, we define union and intersection as follows:UnionLet and be two sets. The union of and , denoted by , is the set of all elements that are contained in , or , orboth and . Formally,For example:IntersectionLet and be two sets. The intersection of and , denoted by , is the set of all elements that are contained in bothand . Formally,For example:Here, we define a few more set operations: difference, floor and ceiling.DifferenceLet and be two sets. The difference of and , denoted by , is the set of elements that are in , but not in .Formally,For example:FloorLet be a set and be an item (integer). The floor of in is the largest element in that is less than or equal to . Formally,For example:v A v ∈ A v AA B A = BA B A ⊆ B A B{1, 2, 3} ⊆ {1, 2, 3, 5} {1, 5, 8} ⊈ {1, 5, 3, 9}A B A B A ∪B A BA BA ∪B = {x : x ∈ A or x ∈ B}{1, 2, 3} ∪ {2, 4} = {1, 2, 3, 4}{1, 5} ∪ {2, 6, 8} = {1, 2, 5, 6, 8}A B A B A ∩B ABA ∩B = {x : x ∈ A and x ∈ B}{1, 2, 3} ∩ {2, 4} = {2}{1, 5} ∩ {2, 6, 8} = ∅A B A B A−B A BA−B = {x : x ∈ A and x ∉ B}{1, 2, 3} − {2, 4} = {1, 3}{1, 5} − {2, 6, 8} = {1, 5}A v v A A vfloor(A, v) = max{x : x ∈ A and x ≤ v}floor({1, 2, 3}, 5) = 3floor({1 5 8} 5) = 529/06/2022, 18:40 COMP2521 22T2 - Assignment 1https://cgi.cse.unsw.edu.au/~cs2521/22T2/assignments/ass1 3/8Note that the floor of an item may not be defined if there are no elements in the set which are less than or equal to it. We discusshow to handle such cases below.CeilingLet be a set and be an item (integer). The ceiling of in is the smallest element in that is greater than or equal to .Formally,For example:Note that the ceiling of an item may not be defined if there are no elements in the set which are greater than or equal to it. Wediscuss how to handle such cases below.Set ADTA set is an example of an abstract data type: there is a collection of operations that can be performed on them, but the exact detailsof the implementation are unimportant, as long as they produce the desired behaviour from the user's perspective.In lectures, we considered several different implementations of the Set ADT: unordered/ordered arrays and unordered/ordered linkedlists. Although these were relatively simple to implement, each of them were flawed/inefficient in some way:Unordered OrderedArray Insertion requires membership querySearch requires linear scanInsertion requires array shift to maintain orderLinked List Insertion/search requires traversal of the list Insertion/search requires traversal of the listWe then introduced binary search trees as a data structure that could have improved search and insertion costs. But theseoperations are only guaranteed to be efficient if the tree is relatively balanced. Thus, your task in this assignment will be toimplement a Set ADT using a balanced binary search tree.CursorsAs you have learned, an ADT does not provide users access to the internal representation of the data type. But what if a user wantedto know what elements are contained in a set? With only the basic operations listed above, the only way the user could do this is tocheck every possible item for membership in the set, like so:i = smallest possible integerwhile i <= largest possible integer:check if i is in the seti = i + 1However, this is not feasible given how many possible integers there are. Therefore, many collection data types provide access toitems via cursors (or iterators). Cursors provide a relatively simple way for users to iterate over the items in a collection:let s be a setc = create a cursor for swhile thereIsStillANextItem(c):i = getNextItem(c)Setting UpChange into the directory you created for the assignment and run the following command:$ unzip /web/cs2521/22T2/ass/ass1/downloads/files.zipIf you're working at home, download files.zip by clicking on the above link and then run the unzip command on the downloadedfile.You should now have the following files:Makefile a set of dependencies used to control compilationSet.h interface to the Set ADT - cannot be modifiedfloor({1, 5, 8}, 5) = 5A v v A A vceiling(A, v) = min{x : x ∈ A and x ≥ v}ceiling({1, 5, 8}, 6) = 8ceiling({1, 5, 8}, 5) = 5O(n)O(n)O(n)O(n) O(n)29/06/2022, 18:40 COMP2521 22T2 - Assignment 1https://cgi.cse.unsw.edu.au/~cs2521/22T2/assignments/ass1 4/8Set.c implementation of the Set ADT (incomplete)SetStructs.h definition of structs used in the Set ADT (incomplete)testSet.c a main program containing some basic tests for the Set ADTanalysis.txt a template for you to enter your time complexity analysis for selected functionsUsually, the structs used by an ADT implementation are defined in the .c file along with the functions. However, in this assignment,they are defined in a separate file, SetStructs.h, because our tests will need access to these structs in order to make it easier toindependently test each function, as well as to check whether your trees are balanced. You may add extra fields to these structs anddefine additional structs if needed, but you must use the given structs/fields in your implementation as follows:You must use struct node for binary search tree nodesThe elements of the set must be stored in the item fields of the struct nodeThe left and right pointers should be used to connect a tree node to its left and right subtrees respectivelyThe tree field must be used to point to the binary search tree that stores all the elements of the setNote that the only files that you are allowed to submit are Set.c, SetStructs.h and analysis.txt. This means all your codefor implementing the Set ADT must be placed in Set.c, except the struct definitions which should be in SetStructs.h.Task 1: ImplementationYour task is to use a balanced binary search tree to implement a Set ADT with the aforementioned operations. We have broken theoperations up into groups - you must fully implement all the operations in each group before moving on to the next group.Group 1: Basic OperationsOperation/Function DescriptionSetNew Creates a new empty setSetFree Frees all memory associated with the given setSetInsert Inserts an item into the given set. Any integer may be inserted (including negative integers) except for thevalue UNDEFINED.Note: The worst-case time complexity of this function must be . Inefficient solutions will be heavilypenalised.SetSize Returns the number of elements in the given setSetContains Checks if an item is in the given set. Returns true if it is, and false otherwise.SetShow Prints the elements in the given set in ascending order between curly braces, with items separated by acomma and space. Do not print a newline. For example, {2, 4, 7, 8}.Group 2: Further OperationsOperation/Function DescriptionSetUnion Given two sets and , returns a new set (defined above)SetIntersection Given two sets and , returns a new set (defined above)SetDifference Given two sets and , returns a new set (defined above)NoteWhen we say you must use a balanced binary search tree, we mean a height-balanced binary search tree. This means forevery node in the tree, the absolute difference in height between its left and right subtrees must be no greater than 1.Important ConstraintThe method of converting the given tree(s) into an array or linked list, solving the main problem using the array/linked list andthen returning the result or converting the result back into a tree is strictly forbidden, as such solutions go against the spirit ofusing binary search trees.O(logn)A B A ∪BA B A ∩BA B A−B29/06/2022, 18:40 COMP2521 22T2 - Assignment 1https://cgi.cse.unsw.edu.au/~cs2521/22T2/assignments/ass1 5/8SetEquals Returns true if the two given sets contain the same elements, and false otherwise.SetSubset Given two sets, returns true if the first set is a subset of the second set, and false otherwise.SetFloor Given a set and an item , returns (defined above) or UNDEFINED if the result is undefined.UNDEFINED is #defined in Set.h, and you may assume that this value is never inserted into the set viaSetInsert. You may also assume that this function is never given UNDEFINED as the item.SetCeiling Given a set and an item , returns (defined above) or UNDEFINED if the result is undefined.You may assume that this function is never given UNDEFINED as the item.Group 3: Cursor OperationsAs described above, cursors provide a way for users to iterate over elements of the set without accessing the internal representationof the set. There are three cursor operations:Operation/Function DescriptionSetCursorNew Creates a cursor for the given set, initially positioned at the smallest element of the setSetCursorFree Frees all memory associated with the given cursorSetCursorNext Returns the element at the cursor's current position, and then moves the cursor to the next greatest element.If there is no next greatest element, then all subsequent calls to SetCursorNext on this cursor shouldreturn UNDEFINED.Here is an example of how a cursor would be used:int main(void) {Set s = SetNew();SetInsert(s, 7);SetInsert(s, 4);SetInsert(s, 8);SetInsert(s, 1);SetCursor cur = SetCursorNew(s);int item;while ((item = SetCursorNext(cur)) != UNDEFINED) {printf("%d ", item);}SetCursorFree(cur);SetFree(s);}This would produce the output:1 4 7 8The following are some clarifications about the expected behaviour of the cursor:A user should be able to create multiple cursors for a given set and iterate over the set independently with each cursor.SetCursorNext should continue to work as described above if elements are inserted after the cursor has been created.For example, suppose a set contains the elements 2, 5 and 8, and a cursor is currently positioned at element 5. If 7 is nowinserted into the set, then calling SetCursorNext should return 5, then 7, then 8, then UNDEFINED. But if 3 was insertedinstead, then SetCursorNext will not return 3, as cursors only move forwards, not backwards.In order to obtain full marks for this part, all cursor operations should have a worst-case time complexity of . A solution wherethe cursor operations have a worst-case time complexity of is worth half the marks. Solutions that are slower than this willnot receive marks for this part.You must also explain the design and implementation of your solution and how it met the time complexity requirement of orin analysis.txt.Task 2: Complexity AnalysisA v floor(A, v)A v ceiling(A, v)This is a challenge! You should not expect to receive hints for this part.Note: You may need to modify some of your existing code to get this working.O(1)O(logn)O(1)O(logn)29/06/2022, 18:40 COMP2521 22T2 - Assignment 1https://cgi.cse.unsw.edu.au/~cs2521/22T2/assignments/ass1 6/8Task 2: Complexity AnalysisYou are required to determine the worst-case time complexity of your implementation of the following operations, and write youranswers in analysis.txt along with an explanation of each answer.SetUnionSetIntersectionSetDifferenceSetEqualsSetSubsetSetFloorSetCeilingYour answers should be in big-O notation. If an operation involves two sets, then the time complexity should be in terms of and ,the sizes of the two sets respectively, otherwise it should just be in terms of , the size of the one set.In your explanations, you may use time complexities established in lectures without proof, as long as you indicate that it wasestablished in lectures. For example, you may use the fact that the worst-case time complexity of searching in an AVL tree is, as this was established in lectures.TestingWe have provided a main program testSet.c containing basic test cases. You should examine testSet.c to see what the testsdo and how the tests call your functions.To run these tests, first run make, which compiles your code and creates an executable called testSet, and then run ./testSet.All of the given tests (except for SetShow) are assertion-based, which means that the program will exit as soon as a test fails. If youwant to ignore a test for the time being, then you can comment out the corresponding function which runs that test.We strongly recommend you to add your own tests, as the given tests are very simple. You can easily add your own tests by creatinga new function in testSet.c and then calling it from the main function. You are also free to completely restructure the testingprogram if you want.DebuggingDebugging is an important and inevitable aspect of programming. We expect everyone to have an understanding of basic debuggingmethods, including using print statements, knowing basic GDB commands and running Valgrind. In the forum and in help sessions,tutors will expect you to have made a reasonable attempt to debug your program yourself using these methods before asking forhelp.You can learn about GDB and Valgrind in the Debugging with GDB and Valgrind lab exercise.Frequently Asked QuestionsAre we allowed to create our own functions? You are always allowed to create your own functions. All additional functions youcreate should be made static.Are we allowed to create our own #defines and structs? Yes.Are we allowed to modify Set.h in any way? No.Are we allowed to change the signatures of the given functions? No. If you change these, your code won't compile and wewon't be able to test it.What errors do we need to handle? You should handle common errors such as NULL returned from malloc by printing an errormessage to stderr and terminating the program. You are not required to handle other errors.What invalid inputs do we need to handle? You are not required to handle invalid inputs, such as NULL being passed in as a set.It is the user's responsibility to provide valid inputs.Will we be assessed on our tests? No. You will not be submitting any test files, and therefore you will not be assessed on yourtests.Are we allowed to share tests? No. Testing is an important part of software development. Students that test their code more willlikely have more correct code, so to ensure fairness, each student should independently develop their own tests.n mnO(logn)29/06/2022, 18:40 COMP2521 22T2 - Assignment 1https://cgi.cse.unsw.edu.au/~cs2521/22T2/assignments/ass1 7/8SubmissionYou must submit the files Set.c, SetStructs.h and analysis.txt. You can submit via the command line using the givecommand:$ give cs2521 ass1 Set.c SetStructs.h analysis.txtYou can also submit via give's web interface. You can submit multiple times. Only your last submission will be marked. You can checkthe files you have submitted here.WARNING:After you submit, you must check that your submission was successful by going to your submissions page. Check that thetimestamp is correct. If your submission does not appear under Last Submission or the timestamp is not correct, then resubmit.CompilationYou must ensure that your final submission compiles on CSE machines. Submitting non-compiling code leads to extra administrativeoverhead and will result in a 10% penalty.Every time you make a submission, a dryrun test will be run on your code to check that it compiles. Please ensure that your finalsubmission successfully compiles, even for parts that you have not completed.Assessment CriteriaThis assignment will contribute 15% to your final mark.Correctness76% of the marks for this assignment will be based on the correctness of your code, and will be based on autotesting. Marks forcorrectness will be distributed as follows:Group 1 SetFree See memory errors/leaks section belowSetInsert 18%SetSize 2%SetContains 2%SetShow 4%Group 2 SetUnion 6%SetIntersection 6%SetDifference 6%SetEquals 6%SetSubset 6%SetFloor 6%SetCeiling 6%Group 3 SetCursorNew, SetCursorFree, SetCursorNext 8%Please note that we expect you to prioritise completing functions correctly over implementing as many functions as possible(potentially incorrectly), so partial marks will not be awarded. You are expected to exercise your debugging skills to fix problems withthe function you are currently working on, before moving on to the next function.Memory errors/leaksYou must ensure that your code does not contain memory errors or leaks, as code that contains memory errors is unsafe and it isbad practice to leave memory leaks in your program.Our tests will include a memory error/leak test for each operation. If a memory error/leak arises from your code, you will receive apenalty which is 10% of the marks for that operation. For example, the penalty for causing a memory error/leak in the SetEqualsoperation will be 0.6%. Note that our tests will always call SetFree at the end of the test (and SetCursorFree if appropriate) to29/06/2022, 18:40 COMP2521 22T2 - Assignment 1https://cgi.cse.unsw.edu.au/~cs2521/22T2/assignments/ass1 8/8free all memory associated with the set.Complexity analysis14% of the marks for this assignment will be based on the correctness of your complexity analysis in analysis.txt and thequality of your explanations.Style10% of the marks for this assignment will come from hand marking of the readability of the code you have written. These marks willbe awarded on the basis of clarity, commenting, elegance and style. The following is an indicative list of characteristics that will beassessed, though your program will be assessed wholistically so other characteristics may be assessed too (see the style guide formore details):Consistent and sensible indentation and spacingUsing blank lines and whitespaceUsing functions to avoid repeating codeDecomposing code into functions and not having overly long functionsUsing comments effectively and not leaving planning or debugging commentsThe course staff may vary the assessment scheme after inspecting the assignment submissions but it will remain broadly similar tothe description above.Originality of WorkThis is an individual assignment. The work you submit must be your own work and only your work apart from the exceptions below.Joint work is not permitted. At no point should a student read or have a copy of another student's assignment code.You may use any amount of code from the lectures and labs of the current iteration of this course. You must clearly attribute thesource of this code in a comment.You may use small amounts (< 10 lines) of general purpose code (not specific to the assignment) obtained from sites such as StackOverflow or other publicly available resources. You must clearly attribute the source of this code in a comment.You are not permitted to request help with the assignment apart from in the course forum, help sessions or from course lecturers ortutors.Do not provide or show your assignment work to any other person (including by posting it publicly on the forum) apart from theteaching staff of COMP2521. When posting on the course forum, teaching staff will be able to view the assignment code you haverecently submitted with give.The work you submit must otherwise be entirely your own work. Submission of work partially or completely derived from any otherperson or jointly written with any other person is not permitted. The penalties for such an offence may include negative marks,automatic failure of the course and possibly other academic discipline. Assignment submissions will be examined both automaticallyand manually for such issues.Relevant scholarship authorities will be informed if students holding scholarships are involved in an incident of plagiarism or othermisconduct. If you knowingly provide or show your assignment work to another person for any reason, and work derived from it issubmitted, then you may be penalised, even if the work was submitted without your knowledge or consent. This may apply even ifyour work is submitted by a third party unknown to you.COMP2521 22T2: Data Structures and Algorithms is brought to you bythe School of Computer Science and Engineeringat the University of New South Wales, Sydney.For all enquiries, please email the class account at cs2521@cse.unsw.edu.auCRICOS Provider 00098G