Skip to content
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

[ feature ]: Added Tilling problem ( domino tiling ) in backtracking algorithm folder #1897

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Changes from 1 commit
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
103 changes: 103 additions & 0 deletions docs/backtracking algorithms/Tiling-problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
---

id: Tiling-Problem-Domino-Tiling
Jivan052 marked this conversation as resolved.
Show resolved Hide resolved
sidebar_position: 6
title: Tiling-problem
sidebar_label: Tiling Problem

---
## Definition 📖

The **Tiling Problem** involves determining the number of ways to tile a given grid using dominoes (or similar tiles) of a fixed size. Specifically, in the **Domino Tiling** problem, the goal is to tile a `2 x n` grid using `1 x 2` dominoes. The dominoes can be placed either horizontally or vertically.

## Characteristics ✨

- **Grid Tiling**:
- The problem asks how to completely cover a grid without gaps or overlaps, using tiles of a fixed size.

- **Recursive Nature**:
- The problem has a recursive structure, where solving the problem for smaller grids can help solve the larger grid.

- **Dynamic Programming Solution**:
- The problem can be solved using dynamic programming to optimize the computation and avoid recalculating subproblems multiple times.

## Time Complexity ⏱️

- **Best Case: `O(n)`** 🌟

The problem can be solved efficiently using dynamic programming in linear time for a grid of size `2 x n`.

- **Average Case: `O(n)`** 🔄

The dynamic programming approach ensures that the time complexity remains linear regardless of the specific tiling configuration.

- **Worst Case: `O(n)`** 💥

Even in the worst case, the time complexity remains linear since dynamic programming avoids redundant calculations.

## Space Complexity 💾

- **Space Complexity: `O(n)`**
Requires linear space to store the solutions for subproblems in the dynamic programming table.

## C++ Implementation 💻

Here’s a simple implementation of the Domino Tiling problem using dynamic programming in C++:

```cpp
#include <iostream>
#include <vector>
using namespace std;

int tilingWays(int n) {
if (n <= 1) return 1;

vector<int> dp(n + 1, 0);
dp[0] = 1; // Base case: 1 way to tile a 2x0 grid
dp[1] = 1; // Base case: 1 way to tile a 2x1 grid

for (int i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2]; // Recursive relation
}

return dp[n];
}

int main() {
int n = 5; // Grid size 2 x n
cout << "Number of ways to tile the grid: " << tilingWays(n) << endl;
return 0;
}
```
## Applications of Tiling Problem 🌐

- **Computer Graphics:**
- Tiling problems are often encountered in computer graphics and image processing, where grids need to be covered with tiles or tiles need to be aligned.
- **Floor Planning:**
- Used in architectural design to efficiently cover floor spaces with tiles or other materials.
- **Puzzles and Games:**
- Many puzzle games or challenges involve tiling problems, like placing pieces in a grid to fit without gaps.

## Advantages and Disadvantages

**Advantages:** ✅

- **Optimal Substructure:**
- The problem can be broken down into smaller subproblems, making it suitable for dynamic programming.
- **Efficient Solution:**
- The dynamic programming approach ensures that the problem can be solved efficiently in linear time.

**Disadvantages:** ⚠️

- **Memory Usage:**

- The dynamic programming approach requires linear space, which can be inefficient for very large values of `n`.

- **Limited Grid Sizes:**

- The solution applies specifically to `2 x n` grids; it may need adjustments for other grid sizes.

## Summary 📚
The Domino Tiling Problem is a classic example of dynamic programming, where the goal is to determine how many ways we can tile a `2 x n `grid using `1 x 2` dominoes.
The problem can be efficiently solved in linear time using dynamic programming, making it a great example of breaking down larger problems into smaller subproblems.
It's widely applicable in fields like computer graphics, floor planning, and puzzle design.
Loading