how do one efficiently implement a queue?
6 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Rik
2017 年 2 月 20 日
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)
回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!