0% found this document useful (0 votes)
25 views4 pages

ALODS Assingment1

The document discusses algorithms for solving an exhaustive search problem involving filling a 2D grid with numbers from a set to satisfy constraints. It describes the exhaustive search algorithm, which calculates all possible combinations to find all solutions, and backtracking, which cuts down on combinations by only expanding states that satisfy constraints. A greedy approach is also described that filters the number set at each step based on constraints, but may miss solutions. The exhaustive search and backtracking algorithms are optimal as they find all solutions, while the greedy approach provided may only find one.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views4 pages

ALODS Assingment1

The document discusses algorithms for solving an exhaustive search problem involving filling a 2D grid with numbers from a set to satisfy constraints. It describes the exhaustive search algorithm, which calculates all possible combinations to find all solutions, and backtracking, which cuts down on combinations by only expanding states that satisfy constraints. A greedy approach is also described that filters the number set at each step based on constraints, but may miss solutions. The exhaustive search and backtracking algorithms are optimal as they find all solutions, while the greedy approach provided may only find one.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Algorithms and Data Structures - Assignment 1

Introduction
In the Exhaustive Search problem the program receives a 2D-grid of unknown size. All locations within this grid
have to be filled with nonzero numbers from a set of possible numbers N = {n1 , n2 , ..., nk }. Each grid has a various
number of groups with different constraints. A group consists of different locations (x, y ) inside the grid that have
to be filled according to the constraints. A (x, y ) location in the grid can be part of multiple groups.

Take an example grid of a 2x2 size. If N = {1, 2} all cells in the 2x2 grid are filled with the number 1 or 2,
because all positions in the grid have to be nonzero. The values that have to be filled in each of the locations is
dependent on the constraints of the groups. In the Exhaustive Search problem two different types of constraints
exists:

• Count constraint
If an integer ni of the set N is used more often than the value set for the count constraint, the current graph
is not a solution to the problem. An example for this would be if the count constraint is set to 1 for a group
within the grid and the set N contains the numbers N = {1, 2, 3}, each of the numbers within N can be used
exactly once.

• Sum constraint
If all locations of a group are filled with nonzero numbers, the sum of these numbers has to be equal to the
value set for the sum constraint. If the sum constraint is set to None, the constraint does not exist. This
means that there is no limit for the total sum of the values within this group.

When all empty locations in the grid have been filled, the algorithm checks the constraints for every group. If all
the constraints have been satisfied for all groups within the grid, then the filled grid is a solution to this problem.
?The algorithm will execute and returns the solution.?

Exhaustive search vs. backtracking


The exhaustive search technique, also known as brute force search, and the backtracking technique are both com-
mon problem-solving algorithms. This algorithm works by calculating all different combinations of a state and will
search within these combinations for the best solution to the problem. This makes it an optimal algorithm, as a
solution will be found if there is one. It creates these different combinations by recursively expanding the state
until it has reached a base case, which is a state that can not be further expanded. Once a base case has been
reached, the recursion will return back to the first state upon which can be further expanded. This way all possible
base cases and thus all solutions will be calculated. This recursion can be expressed as a tree in which each branch
of the tree are the states within the recursion, in here the state grows upon the previous state. The end of each
branch, known as a leaf, contains a base case.

The way in which the exhaustive search technique calculates all solutions works well for smaller problem instances,
but works inefficient for instances with larger state sizes. In the case of larger instances it results in a tree that
grows exponentially when the size of the state increases. Backtracking on the other hand is a refined form of the
exhaustive search technique that is able to drastically cut down the time complexity that occurs with exhaustive
search. It does this by setting up constraints that the states have to comply with in order for the recursion to
expand on this state. If a state is reached that does not comply with the constraints, none of the expansions have
to be calculated. This can greatly decrease the amount of states that have to be expanded on, improving the time
complexity. The algorithm will still be optimal as it finds all solutions that comply with the constraints.

Tree examples
In the following image there is an example of a tree that uses the exhaustive search algorithm. According to the
paragraph above, in this search method every state has to be expanded. In the example you see a 2x2 grid with

1
the possible numbers N = {1, 2}. In every depth level the next empty location in the grid is filled with one of the
two possible numbers. Once the base case has been reached, the algorithm will go up one level to fill the empty
location with the next number in N. If there is no number left from N that can be placed in the empty location,
the algorithm will continue upwards until it reaches an empty location that can be filled with another number from
N. If all possible combinations have been made, the algorithm will be finished. Figure 1 shows the fully expanded
tree in which the exhaustive search algorithm is used.

Figure 1: Tree example of exhaustive search

The backtracking algorithm works similar to the exhaustive search algorithm, but expands upon this algorithm with
constraints on every state. Similar to the exhaustive search algorithm it will fill the next empty location in the grid
with a possible number from N. Once this new state has been reached, it will check whether the state satisfies
the constraints that it has been given. Were the state to satisfy these constraints, then the algorithm will set a
checkpoint on this state. It will then continue to the next empty location. If the following state does not satisfy
the constraints that is has been given, then any of the states expanded on this one will also not satisfy these
constraints. The algorithm will then go back to the last checkpoint state to expand upon this states with new
numbers from N. Figure 2 shown an illustration of a tree in which the backtracking technique is used.

Figure 2: Tree example of backtracking search

As using the backtracking algorithm will not expand upon states that do not satisfy the constraints, it might
significantly decrease the amount of states it has to process. This might greatly improve the time complexity of
the algorithm. In the worst case scenario this algorithm will visit the same amount of states as the exhaustive
search algorithm would. Even though there might be a decrease in the amount of states that will be visited, this
algorithm will reach the same solutions as those that would be reached using the exhaustive search algorithm. This
makes the backtracking algorithm a complete algorithm, as it will find all solutions that were to exist.

Greedy approach
Definition: Greedy is an algorithmic paradigm that builds up a solution piece by piece, always choosing the next
piece that offers the most obvious and immediate benefit. So the problems where choosing locally optimal also
leads to global solution are the best fit for Greedy. [1]

In the use case for this assignment, a greedy approach exists where the set N is filtered before expanding the
tree. Filtering the set according to the constraints given before, we offer the most obvious and immediate benefit.
An example is given in Figure 3. The first group consists of the top two cells. Were one of the two cells to contain
a 1, it would be possible to calculate what numbers could fit in the second cell. With the count constraint of 1

2
and the sum constraint of 3 in this case, the remaining possible number is N = {2}. The greedy approach will
choose the lowest and only number from this set to expand the tree. Continuing the expansion of the following
states results in the only possible solution.

Figure 3: Greedy approach to CSP Figure 4: Greedy approach with N=1,2,3,4

This specific greedy approach does not return all valid solutions as shown in Figure 4. Given a set N = {1, 2, 3, 4}
with a sum constraint of 4, multiple solutions exist. As the greedy approach only expands on the state using the
lowest number left from N once it has been filtered, only one solution is given. This does not make the greedy
approach method an optimal algorithm in this specific use case. A greedy approach would have to find a solution
in the only branch that it expands upon, as it does not make use of recursion. By using this approach on different
use cases, a conclusion could be made that this specific greedy approach would work best on grids of 2x2 and grids
where the groups don’t overlap, as it this approach does not keep track of the global combinations.

Summary and Discussion


The exhaustive search problem uses the exhaustive search technique or the expanded backtracking method. In this
specific report the exhaustive search technique has been used to fill a 2D-grid of unknown size with numbers from
a set of numbers to satisfy two different constraints that have been given to the location in the grid. By using
the exhaustive search technique, a fully expanded tree containing all different combinations of the states will be
calculated in order to find the solutions to the problem. This makes the exhaustive search technique an optimal
algorithm by definition, as all solutions that were to exist will be found. The use of the exhaustive search method
limits the size of the grid, as in increase in the grid size would exponentially grow the amount of states that had to
be calculated, causing a steep increase in the time complexity. To find improvement over the way that the exhaus-
tive search technique calculates the possible states, the backtracking method has been compared. This method
can greatly decrease the amount of states that has to be calculated, as in each step within the recursion the state
is examined to see if it satisfies the given constraints. Were the state to not satisfy these constraints, then this
state will not be expanded on. Using this technique, all states that satisfy the constraints will be expanded on and
all valid solutions will still be found, making this an optimal algorithm. With the possibility to greatly decrease the
amount of states that have to be calculated, the time complexity of the algorithm may also greatly decrease. The
time complexity of the backtracking method will equal that of the exhaustive search technique only in the worst
case scenario, making the backtracking technique a preferred algorithm in solving a problem via brute force.

Both the exhaustive search technique as the backtracking method have been compared to the greedy approach
method. This method expands on states by always choosing the next step that offers the most obvious and imme-
diate benefit to this state. Using this method will not guarantee a solution to be found, as the approach does not
keep track of the global combinations. As the greedy approach is not able to find a solution for all problems and
can only be used in specific problems, it can not be called an optimal algorithm. In the exhaustive search problem
used in this assignment, a greedy approach has been discovered which would filter out the numbers from N that
could not be used following the constraints. Of the numbers left in N, the smallest number will be expanded upon
as the risk of exceeding the sum constraint will be minimized. As shown in the previous paragraph, this greedy
algorithm works well for the use cases given, but will not find all solutions if it were to find any at all.

3
Contributions
Chris Bergsma (S3738639) : Exhaustive vs. backtracking Report
Irene de Koning (S3636623) : Introduction Report, Tree examples Report
Together : Greedy Approach Report, Summary and Discussion
The code of the assignment has been written together.

References
[1] GeeksForGeeks. Greedy Algorithms. https://www.geeksforgeeks.org/greedy-algorithms/. Feb. 2023.

You might also like