writing heuristic algorithm