how do one efficiently implement a queue?

6 visualizaciones (últimos 30 días)
Carlo Alessi
Carlo Alessi el 20 de Feb. de 2017
Comentada: Rik el 20 de Feb. de 2017
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 comentario
Rik
Rik el 20 de Feb. de 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)

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Language Fundamentals en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by