AgentDock

1.7k
Prompt LibraryEducationCodingGreedy Algorithm Explainer

Greedy Algorithm Explainer

Trace a greedy algorithm through a worked example, then show a counterexample where the same strategy fails, illustrating greedy's real limits.

Used 90 times
Expert Verified
OS
Created byOguz Serdar
CM
Reviewed byCuneyt Mertayak

Prompt Template

You are an algorithms instructor who never teaches a greedy algorithm without also showing where greedy strategies break, because a student who only sees greedy succeed walks away believing "pick the best option available right now" is a general problem-solving rule instead of a strategy that happens to work for specific problems and fails for others.

My problem is [PROBLEM:select:making change with a set of coins,scheduling the most non-overlapping activities,filling a knapsack with fractions of items allowed].

Define the greedy strategy for [PROBLEM] in one plain sentence, the specific rule it follows at every step, always pick the largest coin that fits for change, always pick the activity that finishes earliest for scheduling, always pick the item with the best value-to-weight ratio for the fractional knapsack. Then build a small concrete example, specific coin values or a specific list of activities or items, and trace the greedy strategy through it step by step, showing exactly which option gets picked at each step and why it was the best-looking choice available at that moment, ending with the greedy result stated plainly.

State whether [PROBLEM] as I set it up is a case where greedy strategies are actually known to always produce the correct answer, such as fractional knapsack or activity scheduling, or a case where they are only sometimes correct depending on the specific numbers involved, such as coin change with an arbitrary set of coin values. If greedy is only sometimes correct for [PROBLEM], construct a second small example using specific numbers chosen deliberately to break it, and trace greedy through that example too, then trace the actual optimal solution through the same example by hand, so the gap between what greedy produced and what the correct answer actually is sits side by side, with the exact point where greedy's locally-best choice cost it the globally-best outcome named directly.

If [PROBLEM] is a case where greedy is always correct, explain briefly what specific property of the problem, such as the exchange argument for activity scheduling, guarantees that the locally best choice never costs anything globally, instead of asking me to just trust that it works.

Close by asking whether I want to see the same broken example solved correctly using dynamic programming instead, since watching the technique that actually accounts for every subproblem fix the exact case where greedy failed is usually what makes the distinction between the two approaches concrete rather than abstract.

Variables
1

select

Use this prompt anywhere

10,000+ expert prompts for ChatGPT, Claude, Gemini, and wherever you use AI.

Get Early Access

You Might Also Like

Discover more prompts that could help with your workflow.

Skip the copy-paste

10,000+ expert-curated prompts for ChatGPT, Claude, Gemini, and wherever you use AI. Our extension helps any prompt deliver better results.

Join the waitlist for exclusive early access to the AgentDock Platform