src/HOL/Data_Structures/Binomial_Heap.thy
changeset 72942 8b92a2ab5370
parent 72936 1dc01c11aa86
child 73053 2138a4a9031a
equal deleted inserted replaced
72937:686c7ee213e9 72942:8b92a2ab5370
   476   estimations of their complexity.
   476   estimations of their complexity.
   477 \<close>
   477 \<close>
   478 definition T_link :: "'a::linorder tree \<Rightarrow> 'a tree \<Rightarrow> nat" where
   478 definition T_link :: "'a::linorder tree \<Rightarrow> 'a tree \<Rightarrow> nat" where
   479 [simp]: "T_link _ _ = 1"
   479 [simp]: "T_link _ _ = 1"
   480 
   480 
       
   481 text \<open>This function is non-canonical: we omitted a \<open>+1\<close> in the \<open>else\<close>-part,
       
   482   to keep the following analysis simpler and more to the point.
       
   483 \<close>
   481 fun T_ins_tree :: "'a::linorder tree \<Rightarrow> 'a heap \<Rightarrow> nat" where
   484 fun T_ins_tree :: "'a::linorder tree \<Rightarrow> 'a heap \<Rightarrow> nat" where
   482   "T_ins_tree t [] = 1"
   485   "T_ins_tree t [] = 1"
   483 | "T_ins_tree t\<^sub>1 (t\<^sub>2 # rest) = (
   486 | "T_ins_tree t\<^sub>1 (t\<^sub>2 # rest) = (
   484     (if rank t\<^sub>1 < rank t\<^sub>2 then 1
   487     (if rank t\<^sub>1 < rank t\<^sub>2 then 1
   485      else T_link t\<^sub>1 t\<^sub>2 + T_ins_tree (link t\<^sub>1 t\<^sub>2) rest)
   488      else T_link t\<^sub>1 t\<^sub>2 + T_ins_tree (link t\<^sub>1 t\<^sub>2) rest)
   601 definition "T_rev xs = length xs + 1"
   604 definition "T_rev xs = length xs + 1"
   602 
   605 
   603 definition T_del_min :: "'a::linorder heap \<Rightarrow> nat" where
   606 definition T_del_min :: "'a::linorder heap \<Rightarrow> nat" where
   604   "T_del_min ts = T_get_min_rest ts + (case get_min_rest ts of (Node _ x ts\<^sub>1, ts\<^sub>2)
   607   "T_del_min ts = T_get_min_rest ts + (case get_min_rest ts of (Node _ x ts\<^sub>1, ts\<^sub>2)
   605                     \<Rightarrow> T_rev ts\<^sub>1 + T_merge (rev ts\<^sub>1) ts\<^sub>2
   608                     \<Rightarrow> T_rev ts\<^sub>1 + T_merge (rev ts\<^sub>1) ts\<^sub>2
   606   )"
   609   ) + 1"
   607 
   610 
   608 lemma T_del_min_bound:
   611 lemma T_del_min_bound:
   609   fixes ts
   612   fixes ts
   610   defines "n \<equiv> size (mset_heap ts)"
   613   defines "n \<equiv> size (mset_heap ts)"
   611   assumes "invar ts" and "ts\<noteq>[]"
   614   assumes "invar ts" and "ts\<noteq>[]"
   612   shows "T_del_min ts \<le> 6 * log 2 (n+1) + 2"
   615   shows "T_del_min ts \<le> 6 * log 2 (n+1) + 3"
   613 proof -
   616 proof -
   614   obtain r x ts\<^sub>1 ts\<^sub>2 where GM: "get_min_rest ts = (Node r x ts\<^sub>1, ts\<^sub>2)"
   617   obtain r x ts\<^sub>1 ts\<^sub>2 where GM: "get_min_rest ts = (Node r x ts\<^sub>1, ts\<^sub>2)"
   615     by (metis surj_pair tree.exhaust_sel)
   618     by (metis surj_pair tree.exhaust_sel)
   616 
   619 
   617   have I1: "invar (rev ts\<^sub>1)" and I2: "invar ts\<^sub>2"
   620   have I1: "invar (rev ts\<^sub>1)" and I2: "invar ts\<^sub>2"
   623 
   626 
   624   have "n\<^sub>1 \<le> n" "n\<^sub>1 + n\<^sub>2 \<le> n" unfolding n_def n\<^sub>1_def n\<^sub>2_def
   627   have "n\<^sub>1 \<le> n" "n\<^sub>1 + n\<^sub>2 \<le> n" unfolding n_def n\<^sub>1_def n\<^sub>2_def
   625     using mset_get_min_rest[OF GM \<open>ts\<noteq>[]\<close>]
   628     using mset_get_min_rest[OF GM \<open>ts\<noteq>[]\<close>]
   626     by (auto simp: mset_heap_def)
   629     by (auto simp: mset_heap_def)
   627 
   630 
   628   have "T_del_min ts = real (T_get_min_rest ts) + real (T_rev ts\<^sub>1) + real (T_merge (rev ts\<^sub>1) ts\<^sub>2)"
   631   have "T_del_min ts = real (T_get_min_rest ts) + real (T_rev ts\<^sub>1) + real (T_merge (rev ts\<^sub>1) ts\<^sub>2) + 1"
   629     unfolding T_del_min_def GM
   632     unfolding T_del_min_def GM
   630     by simp
   633     by simp
   631   also have "T_get_min_rest ts \<le> log 2 (n+1)" 
   634   also have "T_get_min_rest ts \<le> log 2 (n+1)" 
   632     using T_get_min_rest_bound[OF \<open>invar ts\<close> \<open>ts\<noteq>[]\<close>] unfolding n_def by simp
   635     using T_get_min_rest_bound[OF \<open>invar ts\<close> \<open>ts\<noteq>[]\<close>] unfolding n_def by simp
   633   also have "T_rev ts\<^sub>1 \<le> 1 + log 2 (n\<^sub>1 + 1)"
   636   also have "T_rev ts\<^sub>1 \<le> 1 + log 2 (n\<^sub>1 + 1)"
   634     unfolding T_rev_def n\<^sub>1_def using size_mset_heap[OF I1] by simp
   637     unfolding T_rev_def n\<^sub>1_def using size_mset_heap[OF I1] by simp
   635   also have "T_merge (rev ts\<^sub>1) ts\<^sub>2 \<le> 4*log 2 (n\<^sub>1 + n\<^sub>2 + 1) + 1"
   638   also have "T_merge (rev ts\<^sub>1) ts\<^sub>2 \<le> 4*log 2 (n\<^sub>1 + n\<^sub>2 + 1) + 1"
   636     unfolding n\<^sub>1_def n\<^sub>2_def using T_merge_bound[OF I1 I2] by (simp add: algebra_simps)
   639     unfolding n\<^sub>1_def n\<^sub>2_def using T_merge_bound[OF I1 I2] by (simp add: algebra_simps)
   637   finally have "T_del_min ts \<le> log 2 (n+1) + log 2 (n\<^sub>1 + 1) + 4*log 2 (real (n\<^sub>1 + n\<^sub>2) + 1) + 2"
   640   finally have "T_del_min ts \<le> log 2 (n+1) + log 2 (n\<^sub>1 + 1) + 4*log 2 (real (n\<^sub>1 + n\<^sub>2) + 1) + 3"
   638     by (simp add: algebra_simps)
   641     by (simp add: algebra_simps)
   639   also note \<open>n\<^sub>1 + n\<^sub>2 \<le> n\<close>
   642   also note \<open>n\<^sub>1 + n\<^sub>2 \<le> n\<close>
   640   also note \<open>n\<^sub>1 \<le> n\<close>
   643   also note \<open>n\<^sub>1 \<le> n\<close>
   641   finally show ?thesis by (simp add: algebra_simps)
   644   finally show ?thesis by (simp add: algebra_simps)
   642 qed
   645 qed