Short version for problem in link -> https://codereview.stackexchange.com/q/227081/207512
Here is a short code snippet for the problem
While coding for 0/1 Knapsack using Branch and Bound in python, I used a queue from the queue module, to store all the nodes ....
The code snippet where the queue is checked to not be empty, and then values are fetched and pushed into the queue as per code.
Here, q = queue.Queue()
initialised with an empty node u(-1,0,0, 0) . u2 is an empty node in beginning to store node in every iteration of loop.
while not q.empty():
u = q.get()
............some code ..........
q.put(u2)
display_queue(q)
..........some code..............
The display_queue
is defined as:
def display_queue(que):
print("\nThe Nodes in queue currently: ")
for item in list(que.queue):
print("level:{}, profit:{}, weight:{}, bound:{}".format(item.level, item.profit, item.weight, item.bound))
print()
The problem:
If q was containing [(0,0,35)] initially. After adding (1,3,70), the queue becomes [(1,3,70),(1,3,70)]. Note: these are random values. Please don't confuse with the data on the output.
Output sample: Assume for some reason the queue is increasing, after suitable computations.
I have read the answers of related questions in stack overflow like: Why does adding a new value to list<> overwrite previous values in the list<> but am not able to implement the suggested correction.
When I tried to implement a miniature version of the code, I got everything correct. That is, the same thing(almost) but different outputs!