ALODS Assingment1
ALODS Assingment1
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.?
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.
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.
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.
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.
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.