0

Title says it all. Why is the choice of data structure for Dijkstra's algorithm a priority queue, rather than a simple sorted list?

flag
2 
This is about undergraduate level algorithms rather than research level mathematics. And it reads like a homework question. – David Eppstein Mar 10 at 2:02
You're right about undergraduate level algorithms, but it's not homework. – munch Mar 10 at 2:07
1 
Did you try StackOverflow? – Harry Gindi Mar 10 at 2:15
@fpqc it's not really programming. i'd call this math.. – munch Mar 10 at 2:17

closed as off topic by David Eppstein, Harry Gindi, Noah Snyder, Reid Barton, Scott Carnahan Mar 10 at 2:44

3 Answers

3

Consider the running time for adding a new element to a sorted list, keeping the list sorted. If the list is an array, you can find the insertion point in $O(\log n)$ steps, where $n$ is the current size of the list. But then you have to make room for the new element by shifting all the elements behind it one step back, and that takes $n/2$ steps on the average. Or you could use a linked list, but then binary search is not available, and it takes $n/2$ steps (on the average) to find the insertion point (and $O(1)$ to do the actual insertion). For a properly implemented priority queue, insertion is $O(\log n)$, and so is fixing up the queue after removing the smallest member.

link|flag
1

See http://en.wikipedia.org/wiki/Dijkstra's_algorithm#Running_time.

link|flag
I read that, but it doesn't seem totally legit. In the end, it's performing heap sort on the edges, which is O(nlogn). Sorting a list and extracting the min element is O(nlogn). Seems to be equivalent runtimes.. – munch Mar 10 at 1:53
But in Dijkstra's algorithm, you don't just want the minimum member once. Instead, you have a collection of edges, and repeatedly remove one (the minimum) and add more, looking for the minimum all over again, and so forth. Estimating the running time of all these operations seems a bit more involved than just waving your arms and saying $O(n\log n)$. – Harald Hanche-Olsen Mar 10 at 2:07
-1

You have to understand a priority queue is an abstract data structure. There isn't really "a priority queue", there's implementations of the idea of a priority queue.

You could very well keep a list, sort it, and then that would be your priority queue. The thing is, that will (likely) have a worse running time than other ways of implementing a priority queue. Keep in mind complexity deals with asymptotic running time. In practice, there are still constant in there and they make a difference, so we want to use a better implementation of a priority queue than constantly sorting an entire list.

So don't let "priority queue" versus "sorted list" confuse you; they are conceptually one in the same.

link|flag
@GMan: you're right, but I feel it is clear from context that he is comparing a sorted list and a priority list in their "natural" forms, which are a linked list and a heap, respectively. – yanzhang Mar 10 at 5:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.