01背包问题回溯:
def bag(i, n, weight, value, total, result,currentweight,currentvalue):
global bestvalue, bestresult
if i >= n: # 当节点遍历结束判断此时是否为最佳
if bestvalue < currentvalue:
bestvalue = currentvalue
bestresult = result[:]
else: # 遍历未结束,则进行遍历
if currentweight + weight[i] <= total: # 若可以放入则放入
result[i] = 1
currentweight = currentweight + weight[i]
currentvalue = currentvalue + value[i]
bag(i + 1, n, weight, value, total, result,currentweight,currentvalue)
currentweight = currentweight - weight[i]
currentvalue = currentvalue - value[i]
result[i] = 0 # 不可以,进入下一个节点
bag(i + 1, n, weight, value, total, result,currentweight,currentvalue)
weight = [8,6,4,3]
value = [12,11,9,8]
total = 13
n = len(value)
result = [0 for i in range(n)]
i = 0
currentweight = 0
currentvalue = 0
bestvalue = 0
bag(i, n, weight, value, total, result, currentweight, currentvalue)
print("最大价值为:")
print(bestvalue)
print("放入物品为", end = "")
for i in range(len(bestresult)):
if bestresult[i] == 1:
print(i+1, "号", end = "")
01背包问题分支定界:
def order(list1, list2): # 排序过程中要保证两个列表对应, 按照value/weight进行排序
n = len(list1)
for i in range(n):
for j in range(0, n-i-1):
if list1[j]/list2[j] > list1[j + 1]/list2[j + 1]:
list1[j], list1[j + 1] = list1[j + 1], list1[j]
list2[j], list2[j + 1] = list2[j + 1], list2[j]
def bag(i, n, weight, value, total, result,currentweight,currentvalue):
global bestvalue, bestresult, boundary
if i >= n: # 当节点遍历结束判断此时是否为最佳
if bestvalue < currentvalue:
bestvalue = currentvalue
bestresult = result[:]
if bestvalue/currentweight:
boundary = bestvalue/currentweight # 如果此时value/weight变小则更改上界
#print(bestresult)
#print(bestvalue)
#print(currentweight)
else: # 遍历未结束,则进行遍历
if currentweight + weight[i] <= total: # 若可以放入则放入
if currentvalue != 0 and i < n-1:
temp = (total - currentweight + value[i] * (value[i + 1] / weight[i + 1])) / currentvalue # 计算此时代价
else:
temp = 0 #若此时价值为0,则界值定义为零
if temp < boundary: # 若代价未过界
result[i] = 1
currentweight = currentweight + weight[i]
currentvalue = currentvalue + value[i]
bag(i + 1, n, weight, value, total, result,currentweight,currentvalue)
currentweight = currentweight - weight[i]
currentvalue = currentvalue - value[i]
result[i] = 0 # 不可以,进入下一个节点
bag(i + 1, n, weight, value, total, result,currentweight,currentvalue)
"""
weight = [2, 3, 4, 7]
value = [1, 3, 5, 9]
total = 10
"""
inf = float("inf") # 定义无穷大
weight = [8,6,4,3]
value = [12,11,9,8]
total = 13
order(value, weight)
# print(weight)
# print(value)
n = len(value)
result = [0 for i in range(n)]
i = 0
currentweight = 0
currentvalue = 0
bestvalue = 0
boundary = inf #初始界为无限
bag(i, n, weight, value, total, result, currentweight, currentvalue)
print("最大价值为:")
print(bestvalue)
print("放入物品为", end = "")
for i in range(len(bestresult)):
if bestresult[i] == 1:
print(i+1, "号", end = "")
tsp(货郎问题)回溯:
def tsp(city, j, len, level, num):
global min,road
if level == num - 1: # 当节点遍历结束判断此时是否为最佳
if len + map[j][0] < min: # 若经过此最小则记录
min = len + map[j][0]
for i in range(num):
road[i] = city [i]
#print(road)
#print(min)
else: # 遍历未结束,则进行遍历
for i in range(num):
if city[i] == 0 and map[j][i] !=0 : # 若此城市未经过,且可以经过,则进行
city[i] = level + 2 # 由于初始level为0,故需加2
tsp(city, i, len+map[j][i],level+1, num) # 进入下一节点
city[i] = 0 #退回
inf = float("inf") # 定义无穷大
map = [[0, 5, 9, 4],
[5, 0, 13, 2],
[9, 13, 0, 7],
[4, 2, 7, 0]]
num = 4
min = inf
city = [1, 0, 0, 0] # 记录城市是否走过
road = [0, 0, 0, 0] # 记录最终路径
length = 0
start = 0
level = 0 # 第几个
tsp(city, start, length, level, num)
print("最短路径长", min)
print("依次经过:", end="")
for i in range(num):
print(road[i], ",", end="")
tsp(货郎问题)分支定界:
def findmin(list): # 寻找最小值函数
min = list[0]
n = len(list)
for i in range(n):
if i != 0 : # 由于邻接矩阵的特殊性,此时需要排除0,即自身
if min > list[i]:
min = list[i]
return min
def tsp(city,j,len,level,num):
global min, road, boundary
if level == num - 1: # 当节点遍历结束判断此时是否为最佳
if len + map[j][0] < min: # 若经过此最小则记录
min = len + map[j][0]
boundary = min # 出现可行解则改变上界
for i in range(num):
road[i] = city [i]
#print(road)
#print(min)
else: # 遍历未结束,则进行遍历
temp = len
for i in range(num):
if city[i] == 0:
temp = temp + findmin(map[i]) # 计算此时代价(已经过路径+未经过节点相连的最短边)
if temp < boundary:
for i in range(num):
if city[i] == 0 and map[j][i] !=0 : # 若此城市未经过,且可以经过,则进行
city[i] = level + 2 # 由于初始level为0,故需加2
tsp(city, i, len + map[j][i], level+1, num) # 进入下一节点
city[i] = 0 #退回
inf = float("inf") # 定义无穷大
map = [[0, 5, 9, 4],
[5, 0, 13, 2],
[9, 13, 0, 7],
[4, 2, 7, 0]]
num = 4
min = inf
boundary = inf # 初始上界为无线
city = [1,0,0,0] # 记录城市是否走过
road = [0,0,0,0] # 记录最终路径
length = 0
start = 0
level = 0 # 第几个
tsp(city, start, length, level, num)
print("最短路径长", min)
print("依次经过:", end="")
for i in range(num):
print("城市", road[i], ",", end="")