From 2107f744a97d9c816e306ed2989bde1b817fb119 Mon Sep 17 00:00:00 2001 From: Henry Hickman Date: Tue, 7 Jan 2025 10:09:04 +1300 Subject: [PATCH] Added a new section to the Complexity and Tractability Chapter, detailing complexability (content sourced from Tim Bell's update document) --- .../sections/complexity.md | 29 +++++++++++++++++++ .../sections/sections.yaml | 15 ++++++---- 2 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 csfieldguide/chapters/content/en/complexity-and-tractability/sections/complexity.md diff --git a/csfieldguide/chapters/content/en/complexity-and-tractability/sections/complexity.md b/csfieldguide/chapters/content/en/complexity-and-tractability/sections/complexity.md new file mode 100644 index 0000000000..d4d88d691d --- /dev/null +++ b/csfieldguide/chapters/content/en/complexity-and-tractability/sections/complexity.md @@ -0,0 +1,29 @@ +# Complexity + +Complexity is a fancy word that usually means an indication of how much time an algorithm will take to complete. +It is usually measured in the number of steps an algorithm takes, rather than the amount of time, since the time depends on what kind of computer you run it on. +Complexity for an algorithm is often separated into best, average and worst case. + +For example, the Python “in” function applied to a list does a sequential search by checking each value in sequence until it either finds the value wanted, or gets to the end of the list. +The three complexity cases for this are: + +- Best case, when the value being searched for is first in the list e.g. 45 in [45, 32, 52] + +This only takes one step - the value 45 is compared with the first 45 in the list, and the algorithm stops. +You’re probably thinking that the best case is a bit optimistic, and it is! +If there were a million items in the list, there’s only one chance in a million that the best case will happen. +The best case is not very useful in this case, but for some algorithms their best case is terrible, in which as it’s not even worth worrying about the other cases. + +- Worst case, when the value being searched for is last in the list e.g. 52 in [45, 32, 52] + +This example will take 3 comparisons, and you can’t do worse than that. +If there were a million items in the list, it would take a million steps, and you need to be aware that there’s this possibility that it might occasionally take that long (which would really annoy the person waiting for it.) +It seems pessimistic, but it’s useful to know that it might happen sometimes, and if that’s not acceptable, you’d use a different algorithm. + +- Average case, which is what happens averaged over lots of searches. + +Sometimes the value will be near the start of the list, and sometimes near the end, but on average the algorithm will go about halfway through the list. +If the list has a million values in it, then on average it will take about half a million steps. +This is a realistic view of the algorithm’s performance. + + diff --git a/csfieldguide/chapters/content/structure/complexity-and-tractability/sections/sections.yaml b/csfieldguide/chapters/content/structure/complexity-and-tractability/sections/sections.yaml index d7ae1e6786..2dc7b378f0 100644 --- a/csfieldguide/chapters/content/structure/complexity-and-tractability/sections/sections.yaml +++ b/csfieldguide/chapters/content/structure/complexity-and-tractability/sections/sections.yaml @@ -4,20 +4,23 @@ whats-the-big-picture: algorithms-problems-and-speed-limits: section-number: 2 -tractability: +complexity: section-number: 3 -the-travelling-salesman-problem: +tractability: section-number: 4 -bin-packing-problem: +the-travelling-salesman-problem: section-number: 5 -other-intractable-problems: +bin-packing-problem: section-number: 6 -the-whole-story: +other-intractable-problems: section-number: 7 -further-reading: +the-whole-story: section-number: 8 + +further-reading: + section-number: 9