Skip to content

Commit aaaca94

Browse files
authored
add Python Greedy (#166)
1 parent 5e50126 commit aaaca94

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ A data structure is a data organization, management, and storage format that is
120120
<tr>
121121
<td>Greedy</td>
122122
<td>
123+
<a href="/concepts/python/greedy.md"><code>py🐍</code></a>
123124
</td>
124125
</tr>
125126
<tr>

concepts/python/greedy.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Greedy
2+
3+
From wikipedia
4+
5+
> A greedy algorithm is any algorithm that follows the problem-solving heuristic of making the locally optimal choice at each stage. In many problems, a greedy strategy does not produce an optimal solution, but a greedy heuristic can yield locally optimal solutions that approximate a globally optimal solution in a reasonable amount of time.
6+
7+
## 💻 Implementation
8+
9+
### Problems: Set-covering problem
10+
11+
Problem: Suppose you’re starting a radio show. You want to reach listeners in X number of cities. You have to decide what stations to play on to reach all those listeners. It costs money to be on each station, so you’re trying to minimize the number of stations you play on. You have a list of stations. Each station covers a region, and there’s overlap. How do you figure out the smallest set of stations you can play on to cover all the cities?
12+
13+
Brute Force Approach: List every possible subset of stations. From these, pick the set with the smallest number of stations that covers all the cities. => This takes O(2^n) to run.
14+
15+
Greedy Approach (Approximation Algorithm): Pick the station that covers the most cities that haven’t been covered yet. (It’s OK if the station covers some cities that have been covered already). Repeat until all the cities are covered. => This takes O(n^2) to run.
16+
17+
```python
18+
cities_needed = set(["mt", "wa", "or", "id", "nv", "ut", "ca", "az"])
19+
stations = {}
20+
stations["kone"] = set(["id", "nv", "ut"])
21+
stations["ktwo"] = set(["wa", "id", "mt"])
22+
stations["kthree"] = set(["or", "nv", "ca"])
23+
stations["kfour"] = set(["nv", "ut"])
24+
stations["kfive"] = set(["ca", "az"])
25+
26+
final_stations = set()
27+
while cities_needed:
28+
best_station = None
29+
cities_covered = set()
30+
for station, cities in stations.items():
31+
covered = cities_needed & cities
32+
if len(covered) > len(cities_covered):
33+
best_station = station
34+
cities_covered = covered
35+
cities_needed -= cities_covered
36+
final_stations.add(best_station)
37+
38+
print(final_stations) # {'kone', 'kfive', 'kthree', 'ktwo'}
39+
```
40+
41+
## 🔗 Further Reading
42+
43+
* [Greedy Algorithms](https://brilliant.org/wiki/greedy-algorithm/), brilliant.org
44+
* [Greedy Algorithms](https://www.geeksforgeeks.org/greedy-algorithms/), geeksforgeeks.org 2023
45+
* ▶️ [Greedy Algorithms Tutorial](https://www.youtube.com/watch?v=bC7o8P_Ste4), FreeCodeCamp, 2022

0 commit comments

Comments
 (0)