how do one efficiently implement a queue?
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi, I'm implementing a wavefront algorithm and I need a queue for the open list. I know that shrinking and enlarging an array, to simulate a queue harms performance. Is there another way to do that?
this is my pseudo-code for the wavefront algorithm:
-----------------
costs = zeros(height, width);
adiacients = [ 0 1; 1 0; 1 1; 1 -1; 0 -1; -1 0; -1 -1; -1 1];
open_list = target_point; % goal
j = 2; % costs
while not(isempty(open))
x = pop(open_list);
for i=1:length(adiacients)
current = x + adiacients(i);
if inside_map(current) and costs(current(1), current(2)) == 0
costs(current(1) current(2)) = j;
insert(open, current);
end
end
j++;
end
1 commentaire
Rik
le 20 Fév 2017
Can you predict the maximum size of your queue? Because if you have rough estimate, you could pre-allocate a large empty list and use a counter variable. As long as that list isn't insanely large, that should keep your code reasonably fast. (of course this is a terrible idea if you want a first-in-first-out stack)
Réponses (0)
Voir également
Catégories
En savoir plus sur Language Fundamentals dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!