Skip to content

GH-161: add Python Brute Force #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ A data structure is a data organization, management, and storage format that is
<td>Brute force, Backtracking</td>
<td>
<a href="/concepts/cpp/brute_force.md"><code>cpp🐀</code></a>
<a href="/concepts/python/brute_force.md"><code>py🐍</code></a>
</td>
</tr>
<tr>
Expand Down
61 changes: 61 additions & 0 deletions concepts/python/brute_force.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Bruteforce

alias: `complete search`, `brute-force search`, `exhaustive search`

From usaco

> In many problems it suffices to check all possible cases in the solution space, whether it be all elements, all pairs of elements, or all subsets, or all permutations. Unsurprisingly, this is called **complete search** (or **brute force**), because it completely searches the entire solution space.

From wikipedia

> In computer science, brute-force search or exhaustive search, also known as generate and test, is a very general problem-solving technique and algorithmic paradigm that consists of systematically enumerating all possible candidates for the solution and checking whether each candidate satisfies the problem's statement.

## 💻 Implementation

### Problems: N Queens

N - Queens problem is to place n - queens in such a manner on an n x n chessboard that no queens attack each other by being in the same row, column or diagonal.

```python
def is_valid_state(state, n):
# check if it is a valid solution
return len(state) == n


def get_candidates(state, n):
# get candidate positions for the next queen in the state
if not state:
return range(n)

position = len(state)
candidates = set(range(n))
for row, col in enumerate(state):
candidates.discard(col)
dist = position - row
candidates.discard(col + dist)
candidates.discard(col - dist)
return candidates


def search(state, solutions, n):
if is_valid_state(state, n):
solutions.append(state.copy())

for candidate in get_candidates(state, n):
state.append(candidate)
search(state, solutions, n)
state.pop()

if __name__ == '__main__':
number_of_queens = 4
solutions = []
state = []
search(state, solutions, number_of_queens)
print(solutions) # [[1, 3, 0, 2], [2, 0, 3, 1]]
```

## 🔗 Further Reading

* [Python Program for N Queen Problem](https://www.geeksforgeeks.org/n-queen-problem-backtracking-3/), geeksforgeeks 2022
* ▶️ [What is backtracking and how to use it](https://www.youtube.com/watch?v=7TAmGm3aoqA), taptap 2022
* ▶️ [Solve Coding Interview Backtracking Problems](https://www.youtube.com/watch?v=A80YzvNwqXA), FreeCodeCamp, 2021