LESSON PLANS
Complexity – It’s Simple
By submitting this form, you are giving IEEE permission to contact you and send you email updates about free and paid IEEE educational content.
This lesson allows students to learn about complexity through illustrative games, teamwork activities and design tasks. Students will gain an intuitive understanding of different growth rates and how they determine the performance of algorithms such as sorting. Advanced students can also develop skills in analyzing the complexity of algorithms.
Age Levels: 14-18
Required Materials
Preparation
Make cards from cardboard or paper and number them sequentially starting from 1. Three cards per student are required. Alternatively two or three decks of playing cards can be used but the order of the cards has to be explained to the students.
Design Challenge
You are an engineer given the challenge of analyzing, developing and executing algorithms using games.
Water Lilies 9 weeks. Since the number of lilies doubles every week, if at 9 weeks the pond is halfway covered, it is completely covered after 10 weeks. An example for six weeks is given in the teacher resource “Water Lilies.”
The growth of the sequence describing the area covered is exponential.
How much can they carry?
2D surface areas grow quadratically with the length of the sides, while 3D items can grow cubically (i.e. to the power of three). Therefore, the weight that a skeleton can carry is proportional assuming that all sides are growing linearly a cubic sequence grows faster than a quadratic one.
An increase in an animal’s height can only be achieved by making that animal’s skeleton of a material which is harder and stronger than for smaller animals, or by enlarging the size of the animal’s bones. Because of the small weight of an ant, its skeleton (and muscles) are strong enough to carry ten to fifty times its own weight, while the same is not true for humans. Think about a building’s weight supported by a pillar. If the upper floors are too heavy for the stone pillar, it begins to crack and crumble. For a uniform material, the weight it can carry is proportional to the cross-sectional area. So if you double all the dimensions of the building its weight increases by 8 times, the pillars capacity will only go up fourfold.
This can also be illustrated by considering a mouse falling down an elevator shaft. Due to its small weight it can survive, while larger animals would not. (Example adapted from T.W. Körner, The Pleasures of Counting, Cambridge University Press, 1996)
Complete the Sequences
The sequences are continued as follows, with the respective growth functions given in parentheses:
The first sequence grows slowest (its growth function is just a constant) while the third sequence grows fastest (its growth function is quadratic). The fourth sequence also has a quadratic growth function and thus grows at the same rate, but its individual items will always be lower than those of the third sequence.
So after 7 questions it is clear that the number must be 54. (There are cases in which only 6 questions are necessary. In fact, the average number of questions is log 100 = 6.64.)
This is an optimal method because with every question the set of possible numbers is split in two approximately equal parts, with one containing the required number. In fact, for this problem, every method that halves the possible numbers per question is optimal. Another solution would be to ask questions such as “Is the number divisible by 2?”
Optional: Find the Complexity
The complexity of the bisection method is logarithmic, i.e. of order O(log N). This is a consequence of halving the possible numbers at every question. If all integers are allowed, then no method can solve the problem in finite time.
Bubble Sort If the deck is not sorted, there will be at least two consecutive cards that are not in the correct order. The algorithm will eventually swap these two cards. Therefore, if there are no more cards to be swapped, the algorithm terminates and the deck is sorted.Bubble Sort has the advantage of being very simple to implement, but it is not as efficient as more advanced sorting algorithms such as merge sort (see below). The run time complexity of bubble sort for n cards is quadratic, or O(n2 ).Faster Sorting Algorithms
While the run time complexity of bubble sort is quadratic, better methods exist. For example merge sort has a run time complexity of O(n log n) for n cards. An interesting feature of merge sort is that its structure allows it to be parallelized. Another algorithm with run time complexity O(n log n) that is often used in practicing quick sort. It generally performs well even though its run time complexity is quadratic and thus worse than that of merge sort. It is also common to use hybrid versions of different sorting algorithms to adapt to specific problems. For a derivation of these results see T. H. Cormen et al., Introduction to Algorithms.
For sorting based on comparing individual items, there is no method with a worst case run time complexity better than O(n log n) that does not exploit parallel operations such as merging different stacks of cards at the same time. This is because n cards can be ordered in nn different ways, and each comparison can half the number of possible orderings, leading to a complexity of O(log (nn )) = O(n log n).
Advanced students can write a comparative essay on various sorting methods and the explain criteria for preferring one method over the other. Advanced students can be asked to give arguments of the computational complexity of the considered algorithms and why the performance of comparison sort algorithms is fundamentally limited.
The lesson can be done in as little as 1 class period for older students. However, to help students from feeling rushed and to ensure student success (especially for younger students), split the lesson into two periods giving students more time to brainstorm, test ideas and finalize their design. Conduct the testing and debrief in the next class period.
What is an Algorithm?
A computer can be used to perform a plethora of tasks that can help us with our work. Sometimes these tasks are easy to describe in words, such as “sort this deck of cards”, or “find the fastest way from Paris to New York”. However, for a computer these tasks might be very hard to solve. Internally a computer can only perform rather primitive operations and only their correct arrangement solves a problem. Such an arrangement is called an algorithm. Since each primitive operation takes some time (a few nanoseconds in a modern computer), an algorithm requires a certain run time in order to finish its task.
Why are Some Algorithms Better than Others?
Each task requires a specially designed algorithm that can solve it. Sorting a set of cards cannot be done with an algorithm that is designed to find the shortest path between two cities. Also, quite intuitively, sorting a deck of 200 cards takes longer than sorting just 52 cards (or in the trivial case, 2 cards). Therefore the run time depends on the problem and its size (computer scientists speak of a problem instance).
Algorithms should be compared independently of the problem instance (otherwise the best algorithm would always be to just immediately give the answer calculated before.) This is where the concept of the growth of a sequence comes in useful: The size of the problem can be regarded as the parameter n in a sequence that is specific to the algorithm. There are therefore algorithms of various complexity, corresponding to the sequence describing them. Algorithms are then compared by comparing the growth of the sequences. Therefore an algorithm of quadratic complexity is better than an algorithm of exponential complexity.
Complexity – it’s Useful
The efficiency of algorithms is crucial for a plethora of current computing applications. Some problems are very hard to solve because the only known algorithms solving them have a very high complexity.
A widely known example of high complexity is the traveling salesman problem (TSP). In this problem, a trader wants to do business in a set of cities. He wants to find the shortest possible route between these cities without visiting any city more than once, and he wants to come back to his starting city at the end of his itinerary. Solving this efficiently would be very useful for the assignment of routes for airplanes, scheduling of tasks on machines and even the manufacturing of microprocessors.
While the complexity of the TSP prevents an efficient solution of these problems, for some applications, high complexity can even be desirable. When an encrypted message is sent, it should be very hard to get at the transmitted information without knowing some shared secret. In this case, the encryption is designed in such a way that the decryption has a very high complexity and thus eavesdropping is prevented. Secure communication would not be possible without the high complexity of decryption. These examples and numerous others show that knowing the complexity of a problem is very useful for computing and beyond.
Internet Connections
Recommended Reading
Writing Activity
Compare different sorting methods and explain why you would prefer one over the other. Write a short text on the importance of computational complexity in cryptography.
Note: Lesson plans in this series are aligned to one or more of the following sets of standards:
Principles and Standards for School Mathematics
Algebra Standard
As a result of activities, all students should develop
Problem Solving Standard
As a result of activities, all students should develop
Common Core State Standards for Mathematics Grades 9-12 (ages 14 – 18)
Algebra Standard
Functions Standard
Standards for Technological Literacy – All Ages
The Nature of Technology
The Designed World
CSTA K-12 Computer Science Standards Grades 6-9 (ages 11-14)
CSTA K-12 Computer Science Standards Grades 9-12 (ages 14-18)
5.3 Level 3: Applying Concepts and Creating Real-World Solutions (L3)
5.3.A Computer Science in the Modern World (MW)
5.3.B Computer Science Concepts and Practices (CP)
9. Analyze data and identify patterns through modeling and simulation.
The Growth of Sequences
Water Lilies
On a pond there is a single water lily. Every week the number of water lilies on the pond doubles. The pond is completely covered by water lilies after 10 weeks. After how many weeks is only half the pond covered by water lilies? Explain your reasoning.
How much can they carry?
The weight that a skeleton can carry is proportional to its cross-sectional area. The weight of an animal is proportional to its volume. Why do you think an ant can carry ten to fifty times its own weight while a human can hardly carry its own weight? (Hint: Compare the growth of an area and a volume.)
Complete the Sequences
Find the next two elements in each of the following sequences.
For the first three sequences, also find their respective growth function (in terms of n).
Which sequence grows slowest?
Which sequences grows fastest?
A Number Guessing Game
Work together with your neighbor. One of you has to think of a number between 1 and 100. Don’t tell it to your partner yet. The aim of your partner is to guess that number in as few tries as possible. You are only allowed to answer questions with “yes” or “no”. Once your partner has found the correct number, switch roles.
The Bisection Method
You are only allowed to ask questions such as “Is the number greater than 20?” or “Is the number greater than 54?” What is the best strategy you can find in this case? How many tries do you need in the worst case?
Explain in a few words why your method is optimal.
Optional: Find the Complexity
Can you find a sequence describing how many questions are required in the worst case if numbers from 1 to N are allowed? The answer is best expressed in big-O notation. (Hint: Try the logarithm.) What happens if all integers are allowed?
If you want to know more about finding the complexity, you can look at the book by T. H. Cormen et al., Introduction to Algorithms or at the website http://www.londoninternational.ac.uk/current_students/programme_resources/cis/pdfs/subject_guides/level_2/cis226_vol2/cis226_chap1.pdf
Sorting Algorithms
You will now look at a few sorting methods. Your teacher will give you cards and you will learn two different sorting methods.
To mimic the behavior of a computer, you can only to perform certain simple operations: You are allowed to look at two cards at a time, compare them and move them wherever you want, depending only on the values of the two cards. Such operations could include swapping the cards, inserting one card before the other, etc. Of course, you may also open new auxiliary stacks of cards.
However, you are not allowed to just look at all the cards, remember their value and immediately place them in the right position.
Bubble Sort
Get in groups of three. Each group will get eight cards. Shuffle the cards and place the cards face down on the table.
A simple method for sorting is bubble sort. Apply the following steps to your 8 cards:
The first few swaps are illustrated in the figure to the right. The first two cards need to be swapped, in the center the cards are already in the
correct order and the “8” and the “3” need to be swapped again.
After completion, turn over all the cards and check whether they are sorted. Explain in a few words why this method works.
Optional: Get in groups of five to ten and find your own sorting method using only the allowed operations as explained above. Can you get any better than bubble sort? Why / why not?
Lesson Plan Translation