-
greedy algorithm problem
I really need help to solve a problem using greedy algorithm, I try to search in the net for a code in c, but I find nothing. The kind of problem that I need to solve is fractional or knapsack problem or problem escalation intervals or tree problem Huffman but I need to solve using the greedy algorithm. (Keywords: greedy algorithms, greedy algorithm).
Thank's.
-
Remember that in general greedy algorithms have this structurte
Code:
Greedy (E : set of elements)
1. F = Ø
2. while E = Ø 6 do
3. Greedy choice x ∈ E
4. E = E − {x}
5. if F ∪ {x} feasible then
6. F = F ∪ {x}
7. end if
8. end while
9. return F
Hope this helps
-
Here also pseudocode for discrete problem of knapsack
Code:
Discrete-Knapsack (X : set of objects , a, c : respective weights and
values, b : weight of knapsack)
Descending-Sort(ci/ai)
S = Ø
for i = 1 to n
if b ≥ ai
S = S ∪ {xi}
b = b − ai
end if
end for