how do one efficiently implement a queue?

5 Ansichten (letzte 30 Tage)
Carlo Alessi
Carlo Alessi am 20 Feb. 2017
Kommentiert: Rik am 20 Feb. 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 Kommentar
Rik
Rik am 20 Feb. 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)

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Language Fundamentals finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by