text\<open>A binomial forest is often called a binomial heap, but this overloads the latter term.\<close>
text\<open>The children of a binomial heap node are a valid forest:\<close> lemma invar_children: "bheap (Node r v ts) \ invar (rev ts)" by (auto simp: bheap_def invar_def rev_map[symmetric])
subsection \<open>Operations and Their Functional Correctness\<close>
subsubsection \<open>\<open>link\<close>\<close>
context includes pattern_aliases begin
fun link :: "('a::linorder) tree \ 'a tree \ 'a tree" where "link (Node r x\<^sub>1 ts\<^sub>1 =: t\<^sub>1) (Node r' x\<^sub>2 ts\<^sub>2 =: t\<^sub>2) =
(if x\<^sub>1\<le>x\<^sub>2 then Node (r+1) x\<^sub>1 (t\<^sub>2#ts\<^sub>1) else Node (r+1) x\<^sub>2 (t\<^sub>1#ts\<^sub>2))"
end
lemma invar_link: assumes"bheap t\<^sub>1" assumes"bheap t\<^sub>2" assumes"rank t\<^sub>1 = rank t\<^sub>2" shows"bheap (link t\<^sub>1 t\<^sub>2)" using assms unfolding bheap_def by (cases "(t\<^sub>1, t\<^sub>2)" rule: link.cases) auto
text\<open>Longer, more explicit proof of @{thm [source] invar_merge}, to illustrate the application of the @{thm [source] merge_rank_bound} lemma.\<close> lemma assumes"invar ts\<^sub>1" assumes"invar ts\<^sub>2" shows"invar (merge ts\<^sub>1 ts\<^sub>2)" using assms proof (induction ts\<^sub>1 ts\<^sub>2 rule: merge.induct) case (3 t\<^sub>1 ts\<^sub>1 t\<^sub>2 ts\<^sub>2) \<comment> \<open>Invariants of the parts can be shown automatically\<close> from"3.prems"have [simp]: "bheap t\<^sub>1" "bheap t\<^sub>2" (*"invar (merge (t\<^sub>1#ts\<^sub>1) ts\<^sub>2)" "invar (merge ts\<^sub>1 (t\<^sub>2#ts\<^sub>2))"
"invar (merge ts\<^sub>1 ts\<^sub>2)"*) by auto
\<comment> \<open>These are the three cases of the @{const merge} function\<close>
consider (LT) "rank t\<^sub>1 < rank t\<^sub>2"
| (GT) "rank t\<^sub>1 > rank t\<^sub>2"
| (EQ) "rank t\<^sub>1 = rank t\<^sub>2" using antisym_conv3 by blast thenshow ?caseproof cases case LT \<comment> \<open>@{const merge} takes the first tree from the left heap\<close> thenhave"merge (t\<^sub>1 # ts\<^sub>1) (t\<^sub>2 # ts\<^sub>2) = t\<^sub>1 # merge ts\<^sub>1 (t\<^sub>2 # ts\<^sub>2)" by simp alsohave"invar \" proof (simp, intro conjI) \<comment> \<open>Invariant follows from induction hypothesis\<close> show"invar (merge ts\<^sub>1 (t\<^sub>2 # ts\<^sub>2))" using LT "3.IH""3.prems"by simp
\<comment> \<open>It remains to show that \<open>t\<^sub>1\<close> has smallest rank.\<close> show"\t'\set (merge ts\<^sub>1 (t\<^sub>2 # ts\<^sub>2)). rank t\<^sub>1 < rank t'" \<comment> \<open>Which is done by auxiliary lemma @{thm [source] merge_rank_bound}\<close> using LT "3.prems"by (force elim!: merge_rank_bound) qed finallyshow ?thesis . next \<comment> \<open>@{const merge} takes the first tree from the right heap\<close> case GT \<comment> \<open>The proof is anaologous to the \<open>LT\<close> case\<close> thenshow ?thesis using"3.prems""3.IH"by (force elim!: merge_rank_bound) next case [simp]: EQ \<comment> \<open>@{const merge} links both first forest, and inserts them into the merged remaining heaps\<close> have"merge (t\<^sub>1 # ts\<^sub>1) (t\<^sub>2 # ts\<^sub>2) = ins_tree (link t\<^sub>1 t\<^sub>2) (merge ts\<^sub>1 ts\<^sub>2)" by simp alsohave"invar \" proof (intro invar_ins_tree invar_link) \<comment> \<open>Invariant of merged remaining heaps follows by IH\<close> show"invar (merge ts\<^sub>1 ts\<^sub>2)" using EQ "3.prems""3.IH"by auto
\<comment> \<open>For insertion, we have to show that the rank of the linked tree is \<open>\<le>\<close> the
ranks in the merged remaining heaps\<close> show"\t'\set (merge ts\<^sub>1 ts\<^sub>2). rank (link t\<^sub>1 t\<^sub>2) \ rank t'" proof - \<comment> \<open>Which is, again, done with the help of @{thm [source] merge_rank_bound}\<close> have"rank (link t\<^sub>1 t\<^sub>2) = Suc (rank t\<^sub>2)" by simp thus ?thesis using"3.prems"by (auto simp: Suc_le_eq elim!: merge_rank_bound) qed qed simp_all finallyshow ?thesis . qed qed auto
lemma mset_forest_merge[simp]: "mset_forest (merge ts\<^sub>1 ts\<^sub>2) = mset_forest ts\<^sub>1 + mset_forest ts\<^sub>2" by (induction ts\<^sub>1 ts\<^sub>2 rule: merge.induct) auto
fun get_min_rest :: "'a::linorder forest \ 'a tree \ 'a forest" where "get_min_rest [t] = (t,[])"
| "get_min_rest (t#ts) = (let (t',ts') = get_min_rest ts inif root t \<le> root t' then (t,ts) else (t',t#ts'))"
subsubsection \<open>Instantiating the Priority Queue Locale\<close>
text\<open>Last step of functional correctness proof: combine all the above lemmas toshow that binomial heaps satisfy the specification of priority queues with merge.\<close>
interpretation bheaps: Priority_Queue_Merge where empty = "[]"and is_empty = "(=) []"and insert = insert and get_min = get_min and del_min = del_min and merge = merge and invar = invar and mset = mset_forest proof (unfold_locales, goal_cases) case 1 thus ?caseby simp next case 2 thus ?caseby auto next case 3 thus ?caseby auto next case (4 q) thus ?caseusing mset_forest_del_min[of q] get_min[OF _ \<open>invar q\<close>] by (auto simp: union_single_eq_diff) next case (5 q) thus ?caseusing get_min[of q] by auto next case 6 thus ?caseby (auto simp add: invar_def) next case 7 thus ?caseby simp next case 8 thus ?caseby simp next case 9 thus ?caseby simp next case 10 thus ?caseby simp qed
subsection \<open>Complexity\<close>
text\<open>The size of a binomial tree is determined by its rank\<close> lemma size_mset_btree: assumes"btree t" shows"size (mset_tree t) = 2^rank t" using assms proof (induction t) case (Node r v ts) hence IH: "size (mset_tree t) = 2^rank t"if"t \ set ts" for t using that by auto
from Node have COMPL: "map rank ts = rev [0..by auto
have"size (mset_forest ts) = (\t\ts. size (mset_tree t))" by (induction ts) auto alsohave"\ = (\t\ts. 2^rank t)" using IH by (auto cong: map_cong) alsohave"\ = (\r\map rank ts. 2^r)" by (induction ts) auto alsohave"\ = (\i\{0.. unfolding COMPL by (auto simp: rev_map[symmetric] interv_sum_list_conv_sum_set_nat) alsohave"\ = 2^r - 1" by (induction r) auto finallyshow ?case by (simp) qed
lemma size_mset_tree: assumes"bheap t" shows"size (mset_tree t) = 2^rank t" using assms unfolding bheap_def by (simp add: size_mset_btree)
text\<open>The length of a binomial heap is bounded by the number of its elements\<close> lemma size_mset_forest: assumes"invar ts" shows"length ts \ log 2 (size (mset_forest ts) + 1)" proof - from\<open>invar ts\<close> have
ASC: "sorted_wrt (<) (map rank ts)"and
TINV: "\t\set ts. bheap t" unfolding invar_def by auto
have"(2::nat)^length ts = (\i\{0.. by (simp add: sum_power2) alsohave"\ = (\i\[0.. by (simp add: interv_sum_list_conv_sum_set_nat) alsohave"?S \ (\t\ts. 2^rank t)" (is "_ \ ?T") using sorted_wrt_less_idx[OF ASC] by(simp add: sum_list_mono2) alsohave"?T + 1 \ (\t\ts. size (mset_tree t)) + 1" using TINV by (auto cong: map_cong simp: size_mset_tree) alsohave"\ = size (mset_forest ts) + 1" unfolding mset_forest_def by (induction ts) auto finallyhave"2^length ts \ size (mset_forest ts) + 1" by simp thenshow ?thesis using le_log2_of_power by blast qed
(* Warning: \<open>T_merge.induct\<close> is less convenient than the equivalent \<open>merge.induct\<close>, apparently because of the \<open>let\<close> clauses introduced by pattern_aliases; should be investigated.
*)
text\<open>A crucial idea is to estimate the time in correlation with the
result length, as each carry reduces the length of the result.\<close>
lemma T_ins_tree_length: "T_ins_tree t ts + length (ins_tree t ts) = 2 + length ts" by (induction t ts rule: ins_tree.induct) auto
lemma T_get_min_rest_estimate: "ts\[] \ T_get_min_rest ts = length ts" by (induction ts rule: T_get_min_rest.induct) auto
lemma T_get_min_rest_bound: assumes"invar ts" assumes"ts\[]" shows"T_get_min_rest ts \ log 2 (size (mset_forest ts) + 1)" proof - have 1: "T_get_min_rest ts = length ts"using assms T_get_min_rest_estimate by auto alsonote size_mset_forest[OF \<open>invar ts\<close>] finallyshow ?thesis . qed
time_fun del_min
lemma T_del_min_bound: fixes ts defines"n \ size (mset_forest ts)" assumes"invar ts"and"ts\[]" shows"T_del_min ts \ 6 * log 2 (n+1) + 2" proof - obtain r x ts\<^sub>1 ts\<^sub>2 where GM: "get_min_rest ts = (Node r x ts\<^sub>1, ts\<^sub>2)" by (metis surj_pair tree.exhaust_sel)
have I1: "invar (rev ts\<^sub>1)" and I2: "invar ts\<^sub>2" using invar_get_min_rest[OF GM \<open>ts\<noteq>[]\<close> \<open>invar ts\<close>] invar_children by auto
define n\<^sub>1 where "n\<^sub>1 = size (mset_forest ts\<^sub>1)"
define n\<^sub>2 where "n\<^sub>2 = size (mset_forest ts\<^sub>2)"
have"n\<^sub>1 \ n" "n\<^sub>1 + n\<^sub>2 \ n" unfolding n_def n\<^sub>1_def n\<^sub>2_def using mset_get_min_rest[OF GM \<open>ts\<noteq>[]\<close>] by (auto simp: mset_forest_def)
have"T_del_min ts = real (T_get_min_rest ts) + real (T_itrev ts\<^sub>1 []) + real (T_merge (rev ts\<^sub>1) ts\<^sub>2)" unfolding T_del_min.simps GM T_itrev itrev_Nil by simp alsohave"T_get_min_rest ts \ log 2 (n+1)" using T_get_min_rest_bound[OF \<open>invar ts\<close> \<open>ts\<noteq>[]\<close>] unfolding n_def by simp alsohave"T_itrev ts\<^sub>1 [] \ 1 + log 2 (n\<^sub>1 + 1)" unfolding T_itrev n\<^sub>1_def using size_mset_forest[OF I1] by simp alsohave"T_merge (rev ts\<^sub>1) ts\<^sub>2 \ 4*log 2 (n\<^sub>1 + n\<^sub>2 + 1) + 1" unfolding n\<^sub>1_def n\<^sub>2_def using T_merge_bound[OF I1 I2] by (simp add: algebra_simps) finallyhave"T_del_min ts \ log 2 (n+1) + log 2 (n\<^sub>1 + 1) + 4*log 2 (real (n\<^sub>1 + n\<^sub>2) + 1) + 2" by (simp add: algebra_simps) alsonote\<open>n\<^sub>1 + n\<^sub>2 \<le> n\<close> alsonote\<open>n\<^sub>1 \<le> n\<close> finallyshow ?thesis by (simp add: algebra_simps) qed
end
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.24Bemerkung:
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.