+ Reply to Thread
Results 1 to 7 of 7

Thread: Priority Queues I

  1. #1
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Priority Queues I

    Priority Queues

    A priority queue is similar to a Queue. Items are removed from the queue in a certain order. In a queue the order is FIFO (meaning first-in, first-out). The first item added to the queue is the first item to be removed from the queue.

    A priority queue removes items in an order defined by a comparator. A comparator is an object that defines how objects should be ordered. You might have seen this is if you have looked at sorting objects.

    Let us look at this compare to method:

    Code:
    public int compareTo(Golfer g) {
            if (g.nScore > nScore) {
                return -1;
            }
    
            if (g.nScore < nScore) {
                return 1;
            }
    
            return -g.sName.compareTo(sName);
        }
    If we had objects in a priority queue that had this compareTo method, the next item removed would be the one with the lowest score. However, if the scores are equal, then the player in alphabetical order will be returned.

    This is a nice way to do some sorting without actually calling Arrays.sort or Collections.sort.

    Creating a Priority Queue

    A priority queue is a generic class that can only hold objects. It is created like this:

    Code:
    PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
    Adding to a Priority Queue

    Adding an item is as simple as calling the .add method of the PriorityQueue class.

    Code:
    pq.add(5);
    pq.add(4);
    pq.add(8);
    pq.add(12);
    pq.add(1);
    pq.add(-1);
    Removing Items

    We simply call the remove method and the next item is removed. The next item is determined by the comparator.

    Try this code:

    Code:
     while (!pq.isEmpty()) {
          System.out.println(pq.remove());
     }
    The output is:

    -1
    1
    4
    5
    8
    12
    Noticed, how it is sorted? This makes for a really great way to do some sorting.

    Other uses?

    Another use for this class is in implementing Dijkstra's Single Pair Shortest Path Algorithm. This is something I will discribe later. Basically Dijkstra's Algorithm allows us to find the shortest path between any two nodes. We would use a priority queue to choose the next node that is a shortest distance from the starting node.

    It is called the Single Pair Shortest Path Algorithm because it finds all the shortest paths from a start node to all other nodes.

    Dijkstra's Algorithm is just one use of this algorithm.
    Last edited by chili5; 11-12-2009 at 12:36 PM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Priority Queues I

    Well written, +rep!

  4. #3
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: Priority Queues I

    Wonderful + Rep!

  5. #4
    ThemePark is offline Programmer
    Join Date
    Oct 2008
    Location
    Århus, Denmark
    Posts
    105
    Rep Power
    0

    Re: Priority Queues I

    Cool, didn't know about priority queues or that Java had them. +rep

    So the Djikstra algorithm you mention, can be used for approximating a solution to the TSP problem, right? If so, I'd love to see an article on that.

    Edit:
    Okay, so I have to spread some rep before I can give you any. Once I can +rep you again, I will.

  6. #5
    Join Date
    Aug 2009
    Location
    ~/
    Posts
    918
    Rep Power
    19

    Re: Priority Queues I

    Good Tutorial +rep

  7. #6
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: Priority Queues I

    Quote Originally Posted by ThemePark View Post
    Cool, didn't know about priority queues or that Java had them. +rep

    So the Djikstra algorithm you mention, can be used for approximating a solution to the TSP problem, right? If so, I'd love to see an article on that.

    Edit:
    Okay, so I have to spread some rep before I can give you any. Once I can +rep you again, I will.
    Thanks. I believe it can, I haven't looked at the TSP problem very much but doesn't it just come down to finding the shortest path from a city to some other city? http://nayuki.eigenstate.org/files/d...te200601p5.pdf

    So yes, you can use Dijkstra's algorithm to get a solution for this problem. I'll do a write up later.

  8. #7
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Priority Queues I

    Nice job. +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Beginner C# Queues
    By chili5 in forum CSharp Tutorials
    Replies: 0
    Last Post: 08-20-2011, 09:33 AM
  2. priority scheduling
    By aszaszin in forum C# Programming
    Replies: 2
    Last Post: 07-30-2010, 11:09 AM
  3. A simple priority list.
    By AloneInTheDark in forum Community Projects
    Replies: 6
    Last Post: 03-23-2009, 03:02 PM
  4. priority queue heap.
    By gammaman in forum Programming Theory
    Replies: 2
    Last Post: 03-23-2009, 10:23 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts