# Greedy

**It's a problem-solving strategy that involves making the locally optimal choice at each step, hoping that this will lead to the overall optimal solution.**

**Imagine it like this:** You're navigating a maze, and at each junction, you choose the path that seems most promising right now, without considering the entire maze layout.

**Key characteristics:**

* **Local optimization:** Focuses on making the best decision at each individual step, rather than considering the entire problem space at once.
* **Iterative approach:** Builds a solution incrementally, one step at a time.
* **Not always optimal:** While sometimes leading to the best solution, it doesn't guarantee it in every case.

**Common examples:**

* **Activity selection problem:** Choosing the maximum number of non-overlapping activities that can be completed within a given time frame.
* **Dijkstra's shortest path algorithm:** Finding the shortest path between nodes in a graph.
* **Kruskal's minimum spanning tree algorithm:** Constructing a minimum spanning tree for a weighted graph.
* **Huffman coding:** Efficient compression of data using variable-length codes.

**When to consider greedy approach:**

* Problems where optimal substructure property holds (optimal solution can be constructed from optimal solutions to subproblems).
* Problems where a greedy choice can be made at each step without affecting future choices.
* Problems where finding a global optimal solution is too complex or time-consuming.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs-57.gitbook.io/data-structure-and-algorithms/problems/greedy.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
