BacktrackingAssignmentMissedNotes

.pdf
School
Debre Berhan University**We aren't endorsed by this school
Course
COMPUTER S COSC2043
Subject
Computer Science
Date
Jan 11, 2025
Pages
7
Uploaded by ProfCloverDuck38
Assignment: Understanding Backtracking AlgorithmPrinciplesCourse: Advanced AlgorithmsDue Date: [During Final Exam]Group Assignment: FOR BOTH SECTIONObjectiveThisassignmentaimstoprovideacomprehensiveunderstandingofbacktracking algorithms, their principles, and their real-world applications.By completing this assignment, each group will:1.Understand the concept and principles of backtracking.2.Solve a problem using a backtracking approach.3.Analyze the efficiency and limitations of backtracking algorithms.4.Demonstrateteamworkandcollaborationinsolvingcomputationalproblems.Assignment Components1. Problem Solving and ImplementationEach group will be assigned a unique problem from the provided list. Yourtask is to:Understand the Problem:Break down the problem requirements andconstraints.Design the Algorithm:Develop a backtracking algorithm tailored to theproblem.Implement the Solution:Write a program in a language of your choice(preferably Python, Java, or C++).Analyze Performance:Evaluate the algorithm’s time complexity, spacecomplexity, and potential optimizations.2. Report SubmissionEach group must prepare a detailed report (3-5 pages) that includes:Introduction:Abriefdescriptionoftheassignedproblemanditssignificance.AlgorithmDesign:Explanationofthebacktrackingapproach,constraints, and steps.Code Explanation:Pseudocode or code snippets with clear comments.Analysis:Discussionoftimecomplexity,spacecomplexity,andchallenges faced.Conclusion:Insights and lessons learned from solving the problem.3. Peer Collaboration & EffortEach group member’s contribution will be assessed based on:
Background image
- Self-reported Contribution: Description of individual tasks completed.- Peer Feedback: Evaluation of teamwork, communication, and effort.Assigned ProblemsGroupAssigned ProblemGroup 1-sec 1N-Queens ProblemGroup 2-sec 1Sudoku SolverGroup 3-sec 1Word Search in a GridGroup 4-sec 1Subset Sum ProblemGroup 5-sec 1Knight’s Tour ProblemGroup 6-sec 2String PermutationsGroup 7-sec 2Combination Sum ProblemGroup 8-sec 2Hamiltonian Path ProblemGroup 9-sec 2Maze SolverGroup 10-sec 2Crossword Puzzle SolverGroup 11-sec 2Partition ProblemEvaluation CriteriaComponentWeightageCode Implementation40%Algorithm Design and Analysis30%Report Quality20%Peer Collaboration & Effort10%Resources5.Course module.6.Recommended Textbook: "Introduction to Algorithms" by Cormen et al.7.Online Platforms: GeeksforGeeks, LeetCode, HackerRank.Contact InformationIf you have any questions or need further clarification, feel free to contactvia phone or email(2gethas@gmail.com).Good luck, and enjoy exploring the fascinating world of backtracking algorithms!"Direct copying of information from websites is strictly prohibited. Pleaseensure that the information you provide is verified by yourself."What Is a Selection Sort Algorithm?Selection sort is a simple comparison-based sorting algorithm that divides the input list intoa sorted part at the beginning and an unsorted part at the end. The algorithm repeatedly
Background image
selects the smallest (or largest, depending on the order) element from the unsorted partand swaps it with the first unsorted element, gradually growing the sorted portion until theentire list is sorted.Selection sort is a straightforward and efficient comparison-based sortingalgorithm ideal for small datasets.Each iteration incrementally builds a sorted section by adding one element at atime.Thealgorithmworks by identifying the smallest element in the unsorted portionof the array and swapping it with the first unsorted element, effectively movingit to the front.Alternatively, depending on the sorting order desired, the largest element canbe selected and placed at the end of the array.During each iteration, the selection sort identifies the correct element andpositions it appropriately within the sorted section of the array.How Does the Selection Sort Algorithm Work?Selection sort takes the smallest element in an unsorted array and brings it to the front.You’ll review each item (left to right) until you find the smallest one. The first item in thearray is now sorted, while the rest of the array is unsorted. Here’s the step-by-stepprocess:1. InitializationStart with an unsortedarrayor list.The array is conceptually divided into two parts: the sorted part, which isinitially empty, and the unsorted part, which contains all the elements.2. Iteration Over the ArrayThe algorithm iterates over the array, one element at a time, looking for thesmallest element in the unsorted portion during each iteration.3. Finding the Minimum ElementIn each iteration, assume that the first element of the unsorted portion is thesmallest.Compare this element with the rest of the elements in the unsorted portion.If a smaller element is found, update the smallest element to this new value.4. Swapping ElementsAfter the smallest element in the unsorted portion is found, swap it with thefirstelementoftheunsortedportion.Thiseffectively moves the smallestelement to its correct position in the sorted portion.5. Reduce the Unsorted PortionAfter each swap, the boundary between the sorted and unsorted portions movesone element to the right. The sorted portion grows by one element, and theunsorted portion decreases by one.6. Repeat the ProcessThe process is repeated for the remaining unsorted elements. Each time, thenext smallest element is selected and moved to the end of the sorted portion.7. Completion
Background image
The algorithm continues until the entire array is sorted. The array is fully sortedwhen the unsorted portion is reduced to a single element.Let's take a simple example to illustrate how selection sort works. Consider the array: [29,10, 14, 37, 14].1. First IterationThe unsorted array is [29, 10, 14, 37,14].Findthesmallestelementintheunsorted portion: 10.Swap 10 with the first element (29).The array now looks like this: [10, 29,14, 37, 14].2. Second IterationThe unsorted array is [29, 14, 37, 14].Findthesmallestelementintheunsorted portion: 14.Swap 14 with the first element of theunsorted portion (29).The array now looks like this: [10, 14,29, 37, 14].3. Third IterationThe unsorted array is [29, 37, 14].Findthesmallestelementintheunsorted portion: 14.Swap 14 with the first element of theunsorted portion (29).The array now looks like this: [10, 14,14, 37, 29].4. Fourth IterationThe unsorted array is [37, 29].Findthesmallestelementintheunsorted portion: 29.Swap 29 with the first element of theunsorted portion (37).The array now looks like this: [10, 14,14, 29, 37].5. Final ArrayThe array is now fully sorted: [10, 14,14, 29, 37].Time ComplexitySelection sort has a time complexity of O(n²), where n is the number of elements in thearray. For each n element, the algorithm makes n-1 comparisons to find the minimumelement.Thisquadratictimecomplexity makes selection sort less efficient for largedatasets than more advanced algorithms like quick sort or merge sort.Space ComplexitySelection sort is an in-place sorting algorithm, meaning it doesn't require additional storagespace apart from the input array. Its space complexity is O(1), as it only uses constantextra memory.What Is Quicksort?Quicksort is a fast sorting algorithm that works by splitting a largearray of dataintosmallersub-arrays.Thisimpliesthateachiterationsplitstheinputintotwocomponents, sorts them and then recombines them. The technique is highly efficientfor bigdata setsbecause its average and best-casecomplexityisO(n*logn).
Background image
There might be a few drawbacks to Quicksort, but it performs well in practice and canoutperformothersorting algorithms in speed and space efficiency. Quicksort wascreatedbyTonyHoarein1961andremainsoneofthefastest,mosteffectivegeneral-purpose sorting algorithms available today.How Does Quicksort Work?Quicksort works by recursively sorting the sub-lists to either side of a given pivot anddynamically shifting elements inside the list around that pivot.As a result, the Quicksort method can be summarized in three steps:1.Pick: Select an element.2.Divide: Split the problem set, move smaller parts to the left of the pivot andlarger items to the right.3.Repeat and combine: Repeat the steps and combine the arrays that havepreviously been sorted.Let’stakealookatanexampleto get a better understanding of the Quicksortalgorithm. In this example, the array below contains unsorted values, which we willsort using Quicksort.1. Select a PivotThe process starts by selecting one element known as the pivot from the list. This canbe any element. A pivot can be:Any element at random.The first or last element.Middle element.For this example, we’ll use the last element,4, as our pivot.2. Rearrange the ArrayNow, the goal here is to rearrange the list such that all the elements less than the pivotare to its left, and all the elements greater than the pivot are to the right of it.Remember:The pivot element is compared to all of the items starting with the first index.If the element is greater than the pivot element, a second pointer is appended.When compared to other elements, if a smaller element than the pivot elementis found, the smaller element is swapped with the larger element identifiedbefore.
Background image
Let’s simplify the above example:Every element, starting with7, will be compared to the pivot(4). A secondpointer will be placed at7because7is bigger than4.The next element, element2will now be compared to the pivot. As2is lessthan4, it will be replaced by the bigger figure7which was found earlier.The numbers7and2are swapped. Now, pivot will be compared to the nextelement,1which is smaller than4.So once again,7will be swapped with1.The procedure continues until the next-to-last element is reached, and at theend, the pivot element is then replaced with the second pointer. Here, number4(pivot) will be replaced with number6.As elements2,1, and3are less than4, they are on the pivot’s left side. Elements canbe in any order:‘1’,’2’,’3’, or ‘3’,’1’,’2’, or‘2’,’3’,’1’. The only requirement is thatall of the elements must be less than the pivot. Similarly, on the right side, regardlessof their sequence, all components should be greater than the pivot.
Background image
In other words, the algorithm searches for every value that is smaller than the pivot.Values smaller than pivot will be placed on the left, while values larger than pivot willbe placed on the right. Once the values are rearranged, it will set the pivot in itssorted position.3. Divide the SubarraysOnce we have partitioned the array, we can break this problem into two sub-problems.First, sort the segment of the array to the left of the pivot, and then sort the segmentof the array to the right of the pivot.In the same way that we rearranged elements in step two, we’ll pick a pivotelement for each of the left and right sub-parts individually.Now, we’ll rearrange the sub-list such that all the elements are less than the pivotpoint, which is toward the left. For example, element3is the largest among thethree elements, which satisfies the condition. Hence, the element,3, is in its sortedposition.In a similar manner, we will again work on the sub-list and sort the elements2and1. We will stop the process when we get a single element at the end.Repeat the same process for the right-side sub-list. The subarrays are subdivideduntil each subarray consists of only one element.Now, the array is sorted.
Background image