src/HOL/Data_Structures/Binomial_Heap.thy
changeset 66547 188c7853f84a
parent 66546 14a7d2a39336
child 66548 253880668a43
equal deleted inserted replaced
66546:14a7d2a39336 66547:188c7853f84a
   441   finally show ?case 
   441   finally show ?case 
   442     by (simp)
   442     by (simp)
   443 qed
   443 qed
   444    
   444    
   445 text \<open>The length of a binomial heap is bounded by the number of its elements\<close>  
   445 text \<open>The length of a binomial heap is bounded by the number of its elements\<close>  
   446 lemma size_mset_heap:      
   446 lemma size_mset_bheap:      
   447   assumes "invar_bheap ts"
   447   assumes "invar_bheap ts"
   448   shows "2^length ts \<le> size (mset_heap ts) + 1"
   448   shows "2^length ts \<le> size (mset_heap ts) + 1"
   449 proof -
   449 proof -
   450   from \<open>invar_bheap ts\<close> have 
   450   from \<open>invar_bheap ts\<close> have 
   451     ASC: "sorted_wrt (op <) (map rank ts)" and
   451     ASC: "sorted_wrt (op <) (map rank ts)" and
   496 
   496 
   497   have 1: "t_insert x ts \<le> length ts + 1" 
   497   have 1: "t_insert x ts \<le> length ts + 1" 
   498     unfolding t_insert_def by (rule t_ins_tree_simple_bound)
   498     unfolding t_insert_def by (rule t_ins_tree_simple_bound)
   499   also have "\<dots> \<le> log 2 (2 * (size (mset_heap ts) + 1))" 
   499   also have "\<dots> \<le> log 2 (2 * (size (mset_heap ts) + 1))" 
   500   proof -
   500   proof -
   501     from size_mset_heap[of ts] assms 
   501     from size_mset_bheap[of ts] assms 
   502     have "2 ^ length ts \<le> size (mset_heap ts) + 1"
   502     have "2 ^ length ts \<le> size (mset_heap ts) + 1"
   503       unfolding invar_def by auto
   503       unfolding invar_def by auto
   504     hence "2 ^ (length ts + 1) \<le> 2 * (size (mset_heap ts) + 1)" by auto
   504     hence "2 ^ (length ts + 1) \<le> 2 * (size (mset_heap ts) + 1)" by auto
   505     thus ?thesis using le_log2_of_power by blast
   505     thus ?thesis using le_log2_of_power by blast
   506   qed
   506   qed
   545   have "t_merge ts\<^sub>1 ts\<^sub>2 \<le> 2 * (length ts\<^sub>1 + length ts\<^sub>2) + 1" by auto
   545   have "t_merge ts\<^sub>1 ts\<^sub>2 \<le> 2 * (length ts\<^sub>1 + length ts\<^sub>2) + 1" by auto
   546   hence "(2::nat)^t_merge ts\<^sub>1 ts\<^sub>2 \<le> 2^(2 * (length ts\<^sub>1 + length ts\<^sub>2) + 1)" 
   546   hence "(2::nat)^t_merge ts\<^sub>1 ts\<^sub>2 \<le> 2^(2 * (length ts\<^sub>1 + length ts\<^sub>2) + 1)" 
   547     by (rule power_increasing) auto
   547     by (rule power_increasing) auto
   548   also have "\<dots> = 2*(2^length ts\<^sub>1)\<^sup>2*(2^length ts\<^sub>2)\<^sup>2"    
   548   also have "\<dots> = 2*(2^length ts\<^sub>1)\<^sup>2*(2^length ts\<^sub>2)\<^sup>2"    
   549     by (auto simp: algebra_simps power_add power_mult)
   549     by (auto simp: algebra_simps power_add power_mult)
   550   also note BINVARS(1)[THEN size_mset_heap]
   550   also note BINVARS(1)[THEN size_mset_bheap]
   551   also note BINVARS(2)[THEN size_mset_heap]
   551   also note BINVARS(2)[THEN size_mset_bheap]
   552   finally have "2 ^ t_merge ts\<^sub>1 ts\<^sub>2 \<le> 2 * (n\<^sub>1 + 1)\<^sup>2 * (n\<^sub>2 + 1)\<^sup>2" 
   552   finally have "2 ^ t_merge ts\<^sub>1 ts\<^sub>2 \<le> 2 * (n\<^sub>1 + 1)\<^sup>2 * (n\<^sub>2 + 1)\<^sup>2" 
   553     by (auto simp: power2_nat_le_eq_le n\<^sub>1_def n\<^sub>2_def)
   553     by (auto simp: power2_nat_le_eq_le n\<^sub>1_def n\<^sub>2_def)
   554   from le_log2_of_power[OF this] have "t_merge ts\<^sub>1 ts\<^sub>2 \<le> log 2 \<dots>"    
   554   from le_log2_of_power[OF this] have "t_merge ts\<^sub>1 ts\<^sub>2 \<le> log 2 \<dots>"    
   555     by simp
   555     by simp
   556   also have "\<dots> = log 2 2 + 2*log 2 (n\<^sub>1 + 1) + 2*log 2 (n\<^sub>2 + 1)"
   556   also have "\<dots> = log 2 2 + 2*log 2 (n\<^sub>1 + 1) + 2*log 2 (n\<^sub>2 + 1)"
   589   shows "t_get_min ts \<le> log 2 (size (mset_heap ts) + 1)"
   589   shows "t_get_min ts \<le> log 2 (size (mset_heap ts) + 1)"
   590 proof -
   590 proof -
   591   have 1: "t_get_min ts = length ts" using assms t_get_min_estimate by auto
   591   have 1: "t_get_min ts = length ts" using assms t_get_min_estimate by auto
   592   also have "\<dots> \<le> log 2 (size (mset_heap ts) + 1)"
   592   also have "\<dots> \<le> log 2 (size (mset_heap ts) + 1)"
   593   proof -
   593   proof -
   594     from size_mset_heap[of ts] assms have "2 ^ length ts \<le> size (mset_heap ts) + 1"
   594     from size_mset_bheap[of ts] assms have "2 ^ length ts \<le> size (mset_heap ts) + 1"
   595       unfolding invar_def by auto
   595       unfolding invar_def by auto
   596     thus ?thesis using le_log2_of_power by blast
   596     thus ?thesis using le_log2_of_power by blast
   597   qed
   597   qed
   598   finally show ?thesis by auto 
   598   finally show ?thesis by auto 
   599 qed  
   599 qed  
   613   shows "t_get_min_rest ts \<le> log 2 (size (mset_heap ts) + 1)"
   613   shows "t_get_min_rest ts \<le> log 2 (size (mset_heap ts) + 1)"
   614 proof -
   614 proof -
   615   have 1: "t_get_min_rest ts = length ts" using assms t_get_min_rest_estimate by auto
   615   have 1: "t_get_min_rest ts = length ts" using assms t_get_min_rest_estimate by auto
   616   also have "\<dots> \<le> log 2 (size (mset_heap ts) + 1)"
   616   also have "\<dots> \<le> log 2 (size (mset_heap ts) + 1)"
   617   proof -
   617   proof -
   618     from size_mset_heap[of ts] assms have "2 ^ length ts \<le> size (mset_heap ts) + 1"
   618     from size_mset_bheap[of ts] assms have "2 ^ length ts \<le> size (mset_heap ts) + 1"
   619       by auto
   619       by auto
   620     thus ?thesis using le_log2_of_power by blast
   620     thus ?thesis using le_log2_of_power by blast
   621   qed
   621   qed
   622   finally show ?thesis by auto 
   622   finally show ?thesis by auto 
   623 qed  
   623 qed  
   645   assumes BINVAR: "invar_bheap (rev ts)"
   645   assumes BINVAR: "invar_bheap (rev ts)"
   646   shows "t_rev ts \<le> 1 + log 2 (n+1)"
   646   shows "t_rev ts \<le> 1 + log 2 (n+1)"
   647 proof -
   647 proof -
   648   have "t_rev ts = length ts + 1" by (auto simp: t_rev_def)
   648   have "t_rev ts = length ts + 1" by (auto simp: t_rev_def)
   649   hence "2^t_rev ts = 2*2^length ts" by auto
   649   hence "2^t_rev ts = 2*2^length ts" by auto
   650   also have "\<dots> \<le> 2*n+2" using size_mset_heap[OF BINVAR] by (auto simp: n_def)
   650   also have "\<dots> \<le> 2*n+2" using size_mset_bheap[OF BINVAR] by (auto simp: n_def)
   651   finally have "2 ^ t_rev ts \<le> 2 * n + 2" .
   651   finally have "2 ^ t_rev ts \<le> 2 * n + 2" .
   652   from le_log2_of_power[OF this] have "t_rev ts \<le> log 2 (2 * (n + 1))"
   652   from le_log2_of_power[OF this] have "t_rev ts \<le> log 2 (2 * (n + 1))"
   653     by auto
   653     by auto
   654   also have "\<dots> = 1 + log 2 (n+1)"
   654   also have "\<dots> = 1 + log 2 (n+1)"
   655     by (simp only: of_nat_mult log_mult) auto  
   655     by (simp only: of_nat_mult log_mult) auto