Dynamic programming--optimal binary search tree

key_probability = [-1, 0.15, 0.1, 0.05, 0.1, 0.2]
virtual_key_probability = [0.05, 0.1, 0.05, 0.05, 0.05, 0.1]

expect_cost = [[0] *10 for i in range(10)]
probability_sum =  [[0] *10 for i in range(10)]
root = [[0] *10 for i in range(10)]

def optimal_bst():
 global expect_cost
 global probability_sum
 for i in range(1, 6):
  expect_cost[i][i-1] = virtual_key_probability[i-1]
  probability_sum[i][i-1] = virtual_key_probability[i-1]
 
 for step in range(0, 5):
  for i in range(1, 6):
   this_time_end = i + step
   if this_time_end > 5:
    break
   probability_sum[i][this_time_end] = probability_sum[i][this_time_end-1] + key_probability[this_time_end] + virtual_key_probability[this_time_end]
   # print i,this_time_end, probability_sum[i][this_time_end]
   print "item", expect_cost[i][i-1], expect_cost[i+1][this_time_end], probability_sum[i][this_time_end]
   print "value", expect_cost[i][i-1]+expect_cost[i+1][this_time_end]+probability_sum[i][this_time_end]
   expect_cost_min = expect_cost[i][i-1] + expect_cost[i+1][this_time_end] + probability_sum[i][this_time_end]
   print "expect", expect_cost_min
   root[i][this_time_end] = i
   # print i, this_time_end, expect_cost_min
   for j in range(i, this_time_end+1): 
    cur_cost = expect_cost[i][j-1] + expect_cost[j+1][this_time_end] +  probability_sum[i][this_time_end]
    if cur_cost < expect_cost_min:
     expect_cost_min = cur_cost
     root[i][this_time_end] =j

expect_cost[i][this_time_end] = expect_cost_min
   # print i, this_time_end, expect_cost[i][this_time_end]

def find_root(begin,end,r):
 if begin >= end:
  return
 k = root[begin][end]
 print begin, end, k
 find_root(begin,k-1,r)
 find_root(k+1,end,r)

optimal_bst()

# find_root(1,5)
for i in range(1,6):
 print root[i]
# print root
print "value",expect_cost[1][5]

时间: 2024-10-25 12:47:42

Dynamic programming--optimal binary search tree的相关文章

uva 10304 Optimal Binary Search Tree (区间DP)

uva 10304 Optimal Binary Search Tree 题目大意:给出N个结点(已知每个结点的权值)来建树,建树时要满足以下规则:左子树的节点的值要全小于父节点,右子树的节点的值要全大于父节点.要求最后建出的树总权值最小.总权值=各结点乘以层数(从0层开始)之后相加的和. 解题思路:dp[i][j]代表区间第i个结点到第j个结点组成的树最小的总权值.dp[j][i]=min(dp[j][i],dp[j][k?1]+dp[k+1][i]+sum[i]?sum[j?1]?num[k

[Coding Made Simple] Optimal Binary Search Tree

Given keys and frequency at which these keys are searched, how would you create a binary search tree from these keys such that the cost of searching is minimum. The cost of searching is defined as the sum of all node's search frequency * its depth; R

uva 10304 - Optimal Binary Search Tree 区间dp

题目链接 给n个数, 这n个数的值是从小到大的, 给出个n个数的出现次数. 然后用他们组成一个bst.访问每一个数的代价是这个点的深度*这个点访问的次数. 问你代价最小值是多少. 区间dp的时候, 如果l >= r, 那么返回0, l == r-1, 返回两个数中小的一个. 其他情况的话枚举分界点进行状态转移. #include <bits/stdc++.h> using namespace std; #define mem1(a) memset(a, -1, sizeof(a)) co

UVA 10304 Optimal Binary Search Tree

简单区间DP. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; const int maxn = 300; int n; int a[maxn], su

ITA 15.5 Optimal binary search trees

p400 页最后一段 When j >= i , we need to select a root kr from among ki ... kj and then make an optimal binary search tree with keys ki ... kr-1 as its left subtree and an optimal binary search tree with keys kr+1 ... kj as its right subtree.What happens

2018.3.5-6 knapsack problem, sequence alignment and optimal binary search trees

这周继续dynamic programming,这三个算法都是dynamic programming的. knapsack problem有一种greedy的解法,虽然简单但是不保证正确,这里光头哥讲的是dynamic的解法.其实和上次那个max weight independent set的算法差不多,同样是每个物件都判断一遍有这个物件和没这个物件两种情况,用bottom-up的方法来解,然后得到一个最大的value值,这时因为没有得到具体的选择方案,所以最后还需要一部重构的步骤得到具体方案.

[LeetCode] Find Mode in Binary Search Tree 找二分搜索数的众数

Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST. Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than or equal to the nod

235. Lowest Common Ancestor of a Binary Search Tree

1. 问题描述 Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T th

leetcode 109 Convert Sorted List to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ Convert Sorted List to Binary Search Tree Description Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. /** * De

Lowest Common Ancestor of a Binary Search Tree

1. Title 235. Lowest Common Ancestor of a Binary Search Tree 2. Http address https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ 3. The question Given a binary search tree (BST), find the lowest common ancestor (LCA) of two