Course Activity: Knowledge Representation and Reasoning

Task 2: Greedy Search versus A Search

Imagine a situation where greedy best-first search will not find an optimal solution. Draw an example (such as a maze) and explain why A
search would not make the same mistakes.



Answer :

Sure, let's explore a situation where Greedy Best-First Search (GBFS) can fail to find an optimal solution, whereas A Search would succeed in this task.

### Example Scenario

Imagine we have a grid as a maze where the following characters are used:
- `S` represents the Start.
- `G` represents the Goal.
- `#` represents walls which are impassable.
- Numeric values represent the traversal cost for each cell.

Below is an example grid:
```
S 1 1 1 10
1 10 # 10 10
1 1 1 1 G
```

Grid Layout:
```
S 1 1 1 10
1 10 # 10 10
1 1 1 1 G
```

### Greedy Best-First Search Path

Greedy Best-First Search focuses solely on the heuristic (H), which in this scenario could be the straight-line distance to the goal (G). Here’s how GBFS might approach the grid:
- Start at `S`.
- Move directly towards the minimum heuristic value, choosing the cells that seem closest to the goal.
- Possible path: `S -> (0,1) -> (0,2) -> (0,3) -> (0,4) -> (2,4) -> G`

### Path and Total Cost with Greedy Best-First Search
Following the path: `S -> (0,1) -> (0,2) -> (0,3) -> (0,4) -> (2,4) -> G`:
- `S` to `(0,1)`: Cost = 1
- `(0,1)` to `(0,2)`: Cost = 1
- `(0,2)` to `(0,3)`: Cost = 1
- `(0,3)` to `(0,4)`: Cost = 10
- `(0,4)` to `(2,4)`: Cost = Assumed Heuristic Steps
- Total Cost (assuming 3 heuristic steps from `(0,4)` to G): 1 + 1 + 1 + 10 + 3 = 16

This path might be suboptimal due to the high cost at `(0,4)`.

### A
Search Path

A Search takes into account not just the heuristic (H), but also the cost from the start node to the current node, known as the g-cost (G). Therefore, it combines both aspects into its decision-making:

A
Search Path:
Following the optimal path: `S -> (0,1) -> (1,0) -> (2,0) -> (2,1) -> (2,2) -> (2,3) -> G`
- `S` to `(0,1)`: Cost = 1
- `(0,1)` to `(1,0)`: Cost = 1
- `(1,0)` to `(2,0)`: Cost = 1
- `(2,0)` to `(2,1)`: Cost = 1
- `(2,1)` to `(2,2)`: Cost = 1
- `(2,2)` to `(2,3)`: Cost = 1
- `(2,3)` to `G`: Cost = 1
- Total Cost: 1 + 1 + 1 + 1 + 1 + 1 + 1 = 7

### Explanation

- Greedy Best-First Search: This approach picks what seems to be the best immediate option based solely on the heuristic. As a result, it may choose a path with lower heuristic values but higher overall costs, leading to potentially suboptimal solutions. In this example, GBFS chose the path involving the high-cost cell `(0,4)`, resulting in a higher total cost.

- A Search: A balances both the cost to reach a node and the heuristic estimate to the goal, ensuring it assesses paths more holistically. This provides a more reliable and often optimal solution, as A avoids high-cost intermediate steps such as cell `(0,4)` in favor of lower cumulative cost paths.

### Conclusion

The failure of Greedy Best-First Search to find the optimal path in this example highlights its limitation of being shortsighted, focusing only on immediate heuristic gains. In contrast, A
Search effectively finds the optimal path by considering both the cost incurred and the potential cost to reach the goal, demonstrating its robustness in pathfinding tasks.