944,185 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 940
  • C RSS
Nov 10th, 2009
0

concerns of priority queue with unsorted array

Expand Post »
hi

im trying to write some code for a priority queue using an unsorted array..it should be easy but i have a few concerns about it..

say if i have a graph of something like this:

node 0, priority 100
node 1, priority 50
node 2, priority 200
node 3, priority 250

then the array would look something like

pq[0] = 100
pq[1] = 50
pq[2] = 200
pq[3] = 250

so far so good..the index of the array corresponds to the node number and the value corresponds to the priority...
but what if i was to do a delete min operation? the array would look something like

pq[0] = 100
pq[1] = 200
pq [2] = 250

i deleted the element with lowest priority and shifted everything down to fit the array..but the problem is now pq[1] = 200, which would mean that the node number 1 has a priority of 200 however this is not the case? node 1 just got deleted, the node with priotiy 200 is node 2...

i was thinking maybe implement it using 2 arrays, 1 for storing the node numbers and the other for storing the priorities..but then again that might be doing too much

any help would be appreciated. thanks.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ssDimensionss is offline Offline
21 posts
since May 2008
Nov 10th, 2009
0
Re: concerns of priority queue with unsorted array
Your program is using the array subscript to serve as a node which is the problem. Even though you removed the data, the second subscript, pq[1], isn't going to change.

Have you tried doing it with a linked list?
Reputation Points: 34
Solved Threads: 16
Junior Poster
Chilton is offline Offline
106 posts
since Oct 2009
Nov 10th, 2009
0
Re: concerns of priority queue with unsorted array
no i have not, i was simply wondering if it was possible to do it with an array. I have sort of got a work around by setting the value of the priority to -1 when you try to do delete it. this way the index still corresponds to the correct node and as there are no negative priorities, the next delete min would just check for the node with lowest priority that is >0. is this ok?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ssDimensionss is offline Offline
21 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Advanced Programming
Next Thread in C Forum Timeline: help me





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC