Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package il.ac.tau.cs.sw1.ex7;
- import java.util.*;
- public interface Greedy<T>{
- /**
- * A selection function, which chooses the best candidate to be added to the solution
- */
- Iterator<T> selection();
- /**
- * A feasibility function, that is used to determine if a candidate can be used to contribute to a solution
- */
- boolean feasibility(List<T> lst, T element);
- /**
- * An assign function, which assigns a value to a solution, or a partial solution
- */
- void assign(List<T> lst, T element);
- /**
- * A solution function, which will indicate when we have discovered a complete solution
- */
- boolean solution(List<T> lst);
- /**
- * The Greedy Algorithm
- */
- default List<T> greedyAlgorithm(){
- ArrayList<T> candidates_list = new ArrayList<T>(); // empty list
- Iterator<T> selection = selection();
- T element;
- while (selection.hasNext()) { // selection has more elements
- element = selection.next();
- if (feasibility(candidates_list, element)) {
- assign(candidates_list, element);
- }
- if (solution(candidates_list)) {
- return candidates_list;
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement