Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prove heapify_btu to be in linear time #16

Open
maxhaslbeck opened this issue Oct 21, 2020 · 0 comments
Open

Prove heapify_btu to be in linear time #16

maxhaslbeck opened this issue Oct 21, 2020 · 0 comments

Comments

@maxhaslbeck
Copy link
Collaborator

maxhaslbeck commented Oct 21, 2020

For our verification of correctness and running time of heapsort we use a sub program heapify_btu.
We prove that it runs in O(n log n) because this suffices to prove heapsort in O(n log n), but it actually is O(n)

definition heapify_btu_cost :: "_ \<Rightarrow> ecost"
where "heapify_btu_cost xs\<^sub>0 = cost ''call'' (enat (h - Suc l) + 1) + cost ''icmp_slt'' (enat (h - Suc l) + 1)
+ cost ''if'' (enat (h - Suc l) + 1) + cost ''sub'' (enat (h - Suc l) +1)
+ cost ''sift_down'' (enat (h - Suc l))"
\<comment> \<open>TODO: heapify_btu actually is O(n), not O(n * log n) ! but we don't need it for heapsort in O(n*log n)\<close>

Prove that heapify_btu is a linear time algorithm.

A proof sketch can be found here: https://en.wikipedia.org/wiki/Binary_heap#Building_a_heap
An Intuitive explanation can be found following the Pseudocode in https://en.wikipedia.org/wiki/Heapsort#Pseudocode .

At the moment the cost of each sift_down operation is linear in the length of the slice h-l, but actually, that needs to be refined to be dependent on the position l' for which siftdown is called.

One would need to relax:

definition Rsd where "Rsd i = TId(''sift_down'':=sift_down3_cost (Discrete.log i))"
lemma sift_down3_refines_heapify_btu_step:
shows "sift_down3 l' xs \<le> \<Down>Id( timerefine (Rsd (h-l)) (heapify_btu_step l' xs\<^sub>0 xs))"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant