how do one efficiently implement a queue?

6 views (last 30 days)
Carlo Alessi
Carlo Alessi on 20 Feb 2017
Commented: Rik on 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 Comment
Rik
Rik on 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)

Sign in to comment.

Answers (0)

Categories

Find more on Language Fundamentals in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!