else who met the wizard?”
The mayor thought for a moment. “Not here in G’Raph. But
I had an intern who told me a story that sounded similar to
what you’re claiming. He had a good friend in the town of
Bool who became obsessed with one of these hard problems.
What did you call them? NT? ND?”
“NP,” offered Ann.
“Sure, sure. Anyway, my intern’s friend had a fun little
problem called 3-SAT or something like that. Sounded like
the type of thing you would find in the Sunday paper.”
“The town of Bool? Are you sure?” asked Ann.
The mayor nodded. “You always know a Boolean when
you meet one.”
Ann couldn’t agree more. At the thought of Bool, her mind
flashed back to Dr. Conjunctione and his insistence on
solving 3-SAT. In that instant, the scope of the problem
became frighteningly clear.
After a few more minutes of discussion, Ann decided that
she wasn’t going to find any more answers here. She knew
that there was a wizard who was casting spells on
computational scholars; now she had to figure out why.
For the first time in her quest, Ann knew where to go next.
She needed to pay a visit to the one place where she might
find more information about NP-hard problems: the Library
of Alexandria.
COMPUTATIONAL THINKING
Reflections on Algorithms
Complex algorithms build on a core set of fundamental concepts. Mastering these
basic concepts and learning to combine them is the key to solving new problems.
***
For the millionth time, Ann wished that she had a better
algorithm for solving quests. She wanted something that
required less guessing and that guaranteed a solution. Most of
all, she wanted something fast. After nearly a year of
searching, Ann was tired.
As she looked down at the rugged dirt road, her mind
wandered back to her time at G’Raph and the algorithms she
learned there. Those algorithms, such as Dijkstra’s shortest-
path algorithm and Prim’s algorithm for minimum spanning
trees, went beyond anything that Ann could have imagined a
year ago. Yet these new algorithms used concepts Ann had
learned in first grade.
Dijkstra’s shortest-path algorithm was based on a few
simple loops:
WHILE there are unvisited nodes …
FOR each of the neighbors of the current node …
Similarly, Prim’s algorithm was just a loop that added new
nodes to a set until there was nothing left to add. It also
required updating and finding “close” nodes.
Everything she learned in G’Raph built on the core set of
computer science concepts she had learned in school. As this
realization struck Ann, a burst of hope flowed through her.
She had the tools to solve this quest. Now she needed to
figure out how to apply them.
Computational Graffiti
Recursion can allow a complex algorithm to be specified in a short, simple, and
often beautiful form. A function calls itself on a subset of the data, using the
results from those subproblems to form the final solution. However, recursion can
also add computational overhead due to the new recursive function calls.
***
While traveling to the Library of Alexandria, Ann could see
the signs of chaos engulfing the kingdom. The traditionally
smooth operation of the kingdom’s services barely squeaked
along under the burden of additional complexities. Overhead,
the pigeons of the kingdom’s vast carrier network flew about
aimlessly. The postal carriers had stopped sorting the mail
before embarking on their delivery routes. Instead, they stood
in front of each mailbox and flipped through their full bags to
find that house’s letters. Everything was a mess.
Ann first noticed the computational graffiti in the outskirts
of Alexandria. Initially, it looked harmless—the rebellious
proclamations of teenagers: “DFS rulez” or “Dynamic
Programming FTW.” However, the signs soon became more
troubling: “Say ‘No’ to Big-O” and “BRUTE FORCE
ALGORITHMS FOREVER.”
At first, Ann thought these signs were jokes. Who would
argue against an efficient algorithm? The absurdity made
them almost amusing.
Then, Ann saw a message that stopped her cold:
int RecursiveAdd(int n, int m) {
if (m == 0) return n;
return RecursiveAdd(n, m-1) + 1;
}
The recursive algorithm for adding two numbers covered the
entire north wall outside a florist’s shop. It was written in the
classical language of C, which Ann had learned in school.
While it was technically valid, the sheer inefficiency of the
approach shocked Ann.
For example, given n = 10 and m = 5, the function would
be called a total of six times!
User calls: RecursiveAdd(10, 5)
Recursive call: RecursiveAdd(10, 4)
Recursive call: RecursiveAdd(10, 3)
Recursive call: RecursiveAdd(10, 2)
Recursive call: RecursiveAdd(10, 1)
Recursive call: RecursiveAdd(10, 0)
Mercifully, the recursive chain would stop after the sixth
call. The last call would return 10. The next call would add
1 to that result and return 11. This process would continue to
work its way back up until the top level function returned
(10 + 4) + 1 = 15.
Why take a simple mathematical operation, m + n, and
specify it as a chain of m + 1 function calls? The overhead
was staggering. The graffiti clearly pointed to the complete
collapse of computational thinking.
Tearing herself away from the disturbing image, she broke
into a full run. There was no time to lose.
The NP-Hard Curse
NP-hard is a class of computational problems for which there are no known
efficient and exact algorithmic solutions.
***
By the time Ann arrived at the Library of Alexandria, things
had deteriorated further. She heard reports from throughout
the kingdom of people falling victim to an obsession with
specific NP-hard problems. It appeared that over eighty
percent of the kingdom’s top scholars had been cursed. Most
recently, Ann had learned that the Bureau of Farm Animal
Accounting: Large Mammal Division—the greatest
collection of complexity theorists in the kingdom—had
fallen. The nine theorists, including Clare O’Connell, had
vanished.
“I need everything that you have on NP-hard problems,
computational complexity … and curses,” ordered Ann as
she approached the main desk.
Peter, the librarian’s apprentice, looked worried.
“Everything?”
“Everything,” confirmed Ann.
A look of panic replaced the worry on Peter’s face. “Are
you here to curse me?” he asked. Then, with more resolve, he
puffed out his chest and declared, “I will protect the wisdom
in the Library of Alexandria with my life!”
Ann froze in surprise. It never occurred to her that she
might be mistaken for the very wizard that she needed to
stop. However, judging from the young librarian’s reaction,
he seemed certain that she was here to destroy the scrolls,
curse him, or, at the very least, add to the already plentiful
graffiti.
“Umm, no. I’m not here to curse you or do anything bad to
the library. I’m Princess Ann. I was sent by my father, King
Fredrick, to stop the coming darkness. I need your help.”
The panic on Peter’s face melted into relief. The relief
gave way to gleeful excitement, which produced its own
scary sort of expression. Peter’s entire face was contorted
into a massive smile, and he seemed to have stopped
blinking. Ann wished he could have stayed at relief.
“I can help!” Peter cried as he turned and ran into the
stacks.
He returned fifteen minutes later carrying ten scrolls, two
books, and an ancient clay tablet. “Here’s the first batch. I
brought these up now so you can get started. Unfortunately,
this is the type of request where our caching system doesn’t
work too well.”
“Caching system?” asked Ann, but Peter had already
disappeared back into the stacks.
Ann took the scrolls over to a table in the corner. As she
worked, Peter returned with new batches of scrolls. Ann
noticed that over half the scrolls on computational
complexity were from the Bureau of Farm Animal
Accounting.
All together, about one hundred scrolls covered the table
before Peter finally paused for a moment. He breathed
heavily from the constant running, and sweat dripped from
his nose.
“What are you looking for?” he asked over the mountain of
material.
“I’m not sure,” Ann answered, wondering why he hadn’t
asked this question sooner. “Anything that might help me
understand this curse or its purpose.”
“I heard that the curse targets scholars and forces them to
become obsessed with NP-hard problems to the exclusion of
all else. One thinker was helping the navy rewrite their
manuals to use functions, but he became obsessed with
packing cargo into the ships’ holds. They say he now lives in
the cargo hold of a cow-transport ship in hopes of being
inspired,” Peter babbled, his energy returning.
Ann winced at the story. It had been her request that had
sent that scholar to help the navy. She wondered if he would
have otherwise avoided the curse. She tried to push that
doubt from her mind and refocused on the problem.
“What’s all the excitement over NP-hard problems
anyway?” asked Ann.
Peter gasped. “NP-hard problems are some of the hardest
problems in the world,” he explained. “Everyone wants to
solve them! You would be rich and famous!”
“Why?” asked Ann.
“Because no one has figured out an efficient algorithm for
them yet,” answered Peter. “You can always check whether
you have a valid solution, but generating an exact solution
from scratch is much harder.”
Ann continued to look unimpressed.
“Take the problem of Hamiltonian paths,” Peter continued.
“You need to find a path through a graph that touches each
node one time. It’s really easy to check if any given path is
valid—you simply test the path. Does it step on the same
node twice? Yet it’s incredibly hard to even determine
whether any such path exists.”
“Is it that important?” asked Ann. “I guess the Hamiltonian
path problem is interesting, but solving it won’t cure the
common cold.”
“It’s very important!” exclaimed Peter. “Well, maybe not
Hamiltonian paths themselves, but solving these types of NP-
hard problems. They have real, practical applications.
Further, some NP-hard problems can be reduced to different
NP-hard problems. That means if you solve one problem,
you can solve another. If you found an efficient, exact
solution to the Hamiltonian paths problem, you would find a
solution to the 3-SAT problem. It would revolutionize
everything.”
“Oh,” Ann said. She had never really appreciated the
scope of these problems. “And it’s hard to solve these
problems?” she asked.
“Over three-quarters of the kingdom’s best scholars are
spending every waking moment on them now with no luck.
So …” Peter trailed off.
“We need to figure out how to break the curse,” finished
Ann.
Everyday Algorithms
Algorithms are essential for thousands of everyday tasks. We use algorithms for
everything from adding two numbers together to navigating a store.
***
Peter watched Ann for ten minutes before he had the courage
to disturb her. Even then, it took him five false starts to get
the words out. If Ann hadn’t been so absorbed in her work,
she would have found the entire incident creepy.
“Excuse me,” Peter ventured.
Ann looked up, dazed. “What?”
Even though her tone was pleasant, the words sent a wave
of guilt through Peter. He had been trained not to disturb
patrons unless there was a fire or the library was about to
close. Stopping Ann’s research for a question seemed
terribly rude.
“Do you know what ‘the darkness’ is? Is it the curse?” he
asked. “I only ask because it could help me locate other
resources for you. The master librarian has said that I should
make helping you my top priority.”
“I’m not sure, but I think ‘the darkness’ refers to a new
dark age. A computational dark age, to be exact.”
“Computational dark age?”
“The wizard appears to be targeting the computational
thinkers, and clearly the kingdom is already suffering.”
Something didn’t sound right to Peter. “Does it matter that
much?” he asked.
“Excuse me?” asked Ann. “Have you seen the graffiti? The
cursed scholars? The wandering tribes of bureaucrats?”
“I mean, it seems like a lot of problems to result from a
curse on scholars.”
“Algorithms,” Ann said. Her entire face came to life, and
Peter could hear the passion in her voice. “Algorithms are
everywhere. The entire kingdom runs on algorithms.”
Ann set a scroll down on the table and continued,
“Consider something as small as adding two numbers; it’s an
algorithm. You add the two rightmost digits. Then the next
two. Then the next two. It’s a loop over the digits—right to
left. And there are specific steps for each digit. You add the
numbers, determine whether you need to carry one to the next
column, and so forth. It’s an algorithm.”
“I meant new algorithms,” Peter managed to interrupt.
“The impact of new algorithms.”
Ann switched topics seamlessly. “There’s always the need
for new algorithms. Sometimes you need to solve a new
problem, or solve an old problem more efficiently. The
scholars I met in G’Raph worked on these types of new
algorithms daily. Or sometimes there’s a real-world twist to
a known problem that requires you to adapt the solution. The
importance of continued algorithmic development should not
be underestimated.”
At the mention of adapting the algorithm for the real
world, Peter flashed back to his own experience with
insertion sort. He shook that thought out of his head and tried
again.
“That isn’t what I meant,” he said. He waited a moment,
not providing Ann a new topic on which to rant.
Ann looked back at him. “Then what did you mean?” she
asked.
“The wizard has cursed all of the scholars, so they aren’t
developing any new algorithms. I get that, and that’s bad. But
why is it impacting everything else so quickly? It should take
a while for the kingdom to get this bad, right? Not everyone
needs a new algorithm every day. It’s not like a loaf of bread
in a damp closest. Algorithms don’t get all green and fuzzy
with age.”
“Oh, right … that,” said Ann, the energy draining out of
her voice.
Ann didn’t speak for a moment. She looked at Peter,
carefully studying him. Peter got the distinct feeling that he
wouldn’t like what came next.
“It’s much worse than that,” Ann said finally.
“Worse?” asked Peter.
“Worse,” confirmed Ann.
“Uh, how?” asked Peter nervously.
“I think the spells are targeting more than the scholars,”
explained Ann. “The curse is seeping into all aspects of life.
It’s using the scholars—using them to target the very
algorithms themselves. You curse an expert in sorting, and
you curse the sorting algorithm itself. The wizard is
unraveling the very basis of computation in the kingdom.”
“Oh,” was all that Peter could manage. His legs suddenly
seemed a bit wobbly. He felt around for a chair, pulled it
behind him, and fell into it.
“Oh,” he repeated.
“Yeah,” Ann agreed.
“So?” asked Peter.
“I have to stop it … fast.”
The Quicksort Message
Quicksort is a recursive sorting algorithm that is similar to merge sort. Like merge
sort, quicksort partitions the items into two groups, recursively sorts those groups,
and merges the results into a single sorted list. Unlike merge sort, quicksort uses
the items’ values in the partitioning. Quicksort chooses a random element, called a
pivot, and divides the current list into two sublists: items less than or equal to the
pivot, and items greater than the pivot. Since the lists are partitioned by value, the
merge operation consists of simply appending one sorted list to the other.
The worst-case performance of quicksort is O(N²). However, quicksort takes
N log N time on average. In fact, quicksort can be faster than other O(N log N)
sorting algorithms such as merge sort.
***
“What are you doing?” asked Ann.
“Putting away books,” answered Peter. “We ask the
patrons to leave the books out for us, because they always
mess things up when they try to help. They take the book back
to the general area where they got it, see a gap, and shove it
there. It messes up the whole order. And you should see what
they do with the scrolls.”
“Okay,” agreed Ann. “But what are you doing with the
those books now?”
“Sorting them,” answered Peter. “It’s easier if I sort them
here. That way I can go through the stacks in order and put
away the books. It’s like the merge step of a gigantic merge
sort. It saves a lot of time.”
“But how are you sorting them? It almost looks like you’re
picking things at random.”
“Oh,” Peter said. “This is something new that an
accountant showed me. She called it quicksort. It has a
terrible worst-case big-O complexity, but it actually works
pretty well.”
“An accountant?” asked Ann.
“I think she works for a farm animal bureau, but she
seemed to understand sorting. She obviously isn’t a
computational expert though, or she would have known this
quicksort has a worst-case time of O(N²). Imagine trying to
sort more than a few shelves of books—”
“Clare O’Connell?” interrupted Ann. She stood up, staring
intensely at the book cart.
Peter paused. In the five days Ann had been working at the
library, she had barely looked up from her books. Her sudden
interest in the reshelving of books unnerved him. The fact that
she was paying any attention to him now was unnerving.
“That sounds familiar,” Peter answered hesitantly.
“Tell me everything,” commanded Ann. She stood next to
him now, still staring at the bookshelves. “What did she
say?”
“Uh, okay … yeah, she was in here a few weeks ago to get
a scroll on cows—or turkeys—or some type of animal. I’m
pretty sure it was cows, though.”
“What about the sorting?” prompted Ann.
“All she said was that there was a better way to sort,”
Peter said. “She showed me this algorithm that she called
quicksort. You start with a stack of books to sort. Pick one
book at random. You call that the pivot book.
“Then you use the pivot to split the stack into two smaller
stacks. Books before the pivot go on the left and books after
the pivot go on the right.
“You keep recursively splitting piles until you have one
book in each pile.
“Then you merge them back together by putting the left pile
in front of the right pile.”
Peter shrugged. “That’s all there is. As I said, it’s worst
case O(N²), but it usually goes faster than that. More
importantly, it’s a lot easier to merge library books by putting
one pile on top of another.”
Ann didn’t respond. She continued to stare at the shelf of
library books. Finally, she whispered, “It has an N log N
expected running time.”
“Expected?” asked Peter.
“It means that on average it will take N log N,” answered
Ann. “I heard rumors that Clare was working with expected
running times.”
“The worst case is still N²,” objected Peter. He firmly
believed that worst-case running time spoke for itself.
That broke Ann out of her trance. “That’s the beauty of it,”
answered Ann. “You would need to always make a bad pick
to have a that bad a running time. For example, if you always
chose the smallest value for the pivot, it would be bad. You
would need to split the books N times, with each split
requiring up to N comparisons.”
Ann turned back to the bookshelf. Her voice dropped to
almost a whisper. “The chances of that must be very small.”
She spun toward Peter and grabbed his shoulders. Her
eyes lit up with a wild energy. “When did you say Clare was
here?” she asked.
Peter tried to take a step back, but Ann held him tight.
“When was she here?” asked Ann.
“Two weeks ago, I think.”
The Bureau of Farm Animal Accounting had fallen three
weeks ago.
“It’s a message!” shouted Ann. “Quicksort is a message!”
Comments and the Baker’s Apprentice
Comments are additional text within code that can greatly improve the code’s
readability by providing additional insight, explanation, or summarization. For
example, a comment before a large block of numeric statements might simply
explain “Solve a · x² + b · x + c = 0 for x,” allowing the user to understand exactly
what the following block of code is meant to do.
***
“Now this is how you write an algorithm,” proclaimed Ann.
After days of reading through some of the worst-written
scrolls in the history of the kingdom, Clare’s scroll on “The
Application of QuickSort to Filing Expense Reports”
provided a welcome change.
“How so?” asked Peter, leaning in to look at the scroll. He
stared for a moment. “I guess it’s nice handwriting,” he said.
“It’s so much more than that,” said Ann. “The algorithm
itself is readable. The variable names make sense, the
functions are well defined, and there are comments. Good
comments!”
“Comments?” asked Peter. “That seems unnecessary. I
thought a good algorithm was supposed to make sense on its
own.”
“Not always,” said Ann. “In complex algorithms, good
comments can help make everything more understandable.
Other times, a good comment can provide useful insights into
why an approach was used.”
Ann paused as she thought back. “Our castle’s baker,
Breadtista, was the first person to show me the importance of
a good comment. I spent a summer as his apprentice. In fact, I
spent a lot of summers apprenticing; my father is a fan of a
broad education. Anyway, as part of his world-renowned
teaching program, Breadtista insisted that each apprentice
thoroughly document his or her work. All recipes, tips, and
tricks must be clearly documented to create a reference that
lives beyond the apprenticeship.”
“Why make every apprentice repeat the work?” asked
Peter. “Breadtista could simply copy his best recipes, right?”
“Learning to write a good recipe is part of the education.
As Breadtista often pointed out, the key to a great recipe is
capturing the perfect amount of information. Where other
bakers wrote recipe books filled with lists of bland
instructions, such as ‘Add two eggs and blend well,’
Breadtista insisted on adding comments to his recipes. He
used wonderful comments that added context and the perfect
amount of explanation.”
Peter looked unconvinced. In his time as the librarian’s
apprentice, he couldn’t remember a single instance when he
had needed additional context. When the master librarian
gave instructions, Peter wrote them down verbatim.
“Here,” said Ann as she pulled an old notebook out of her
bag. “Here’s Breadtista’s recipe for his famous eight-chili
strawberry muffins.”
Step 5: Cut the strawberries into cubes of 1 cm per side. [This size
allows the flavors to blend while preserving the fruit’s texture.]
Step 6: Add the strawberries to the dough.
Step 7: Gently stir until the strawberries are mixed into the dough.
“The comments have the brackets around them,” explained
Ann, pointing at the symbols “[” and “].”
“I guess it’s useful to know why the strawberries should
be that size,” said Peter. “How else do you use comments?
Can you provide information about how you thought up a
recipe?”
Ann shook her head adamantly. “Comments should always
provide information meaningful to the recipe itself.
Breadtista would rant for hours about recipe books by
famous castle chefs that contained thirty-page digressions on
the author’s lifelong quest for the perfect pancake. He
considered it especially egregious if the recipe then
produced chewy pancakes. As he would say: ‘Pancakes
should not be chewy, and comments should not be life
stories.’ He felt strongly about it.”
“Did it take you long to learn to write good comments?”
asked Peter.
Ann started laughing. “It took me a month to write any
comments at all. I didn’t see the use of them. I’d complain
and say things like ‘I won’t forget what I did’ and ‘Isn’t that
step obvious?’ I was such a pain.
“But Breadtista was patient. He would always give the
same answer: ‘Just because it’s obvious to you at this
moment doesn’t mean you won’t forget it, or that someone
else won’t need an explanation when they read the recipe.’
He gave a very logical argument, but I still ignored it.
“Then Breadtista found the perfect way to reinforce the
importance of good comments. He started making me work
from the worst notes of previous students. It was agonizing.
Sometimes I had no idea what the instructions meant. Other
times, they went on forever.”
Ann flipped a few pages back in her notebook. “Look at
this mess!”
Step 1: Measure out 1/4 of an ounce of yeast into a small bowl. [We
put it into a bowl so that we can add water in step 2 to activate the
yeast.]
Step 2: Add two tablespoons of warm water to the bowl with the yeast.
[We add the water to the bowl in order to activate the yeast.]
Step 3: Wait 5 minutes. [We wait 5 minutes in order to allow the
yeast to activate in the water.]
“A third-year apprentice wrote that recipe,” Ann explained.
“It was horrible. I never made it past step 85 in that recipe.
In fact, I ‘accidentally’ spilled a full pitcher of water over the
parchment so as to spare anyone else from reading it.”
Ann sighed. “After that, I learned the value of good
comments. It turns out that nothing reinforces the concept of
understandable, well-documented recipes more than having
to read other people’s recipes.”
“And the quicksort scroll?” asked Peter, returning to the
topic at hand.
“Well-commented,” answered Ann with a smile.
“Sure. But does it have the information you need?”
Ann looked worried. “Is has some of it—a direction at
least. We still have so much work to do. But I fear we don’t
have much time left.”
The Curse of Excessive Commenting
Good comments can improve the readability of code. However, overcommenting
can do the opposite. Unnecessary comments waste both space and the reader’s
attention without adding value.
***
The New Athens blacksmith, Drex, had been cursed. Drex
freely admitted that he had provoked the wizard Marcus, but
the curse seemed like an extreme reaction. The wizard could
have simply insulted him back or walked away. There were
plenty of reasonable options. He didn’t have to curse Drex.
As much as the curse annoyed Drex, his apprentice Rachel
suffered more. The Curse of Excessive Commenting was
designed to annoy both the teacher and the student. Whenever
Drex explained something, he now did it in excruciating
detail.
“Watch how I form this hinge,” Drex instructed. “First I
pick up the metal with these large tongs. They are made of a
heavier metal, so they won’t burn or melt in the fire. Then I
use the tongs to put metal in the fire, where it will heat up.”
As he spoke, Drex grabbed a small blob of metal with his
tongs and shoved it into the fire. After a moment, he felt
obligated to add, “I’m still heating it up in the fire.” He
reiterated this observation five more times before the metal
was hot enough to work.
“Now, I use the hammer to flatten the metal,” Drex
explained. “I’m hitting the metal with the hammer. I’m hitting
it again. I’m hitting it again.”
Rachel stood off to the side, watching. After the tenth
repetition of “I’m hitting it again,” she rolled her eyes. Not
only were these descriptions annoying, but it also made it
hard for her to follow what was going on. Drex narrated at
such a low level that it was difficult to pay attention to the
high-level flow. The constant stream of tiny details overran
the concept of forming the hinge.
“I’m hitting it again,” narrated Drex.
The first time Drex had described the process of making a
hinge, it had been simple. He had described the entire first
ten minutes of work as: “First, flatten out a small piece of
metal.” That was all. He had left unspoken the low-level
details that any blacksmith should easily pick up: to flatten
metal you heat it and hit it with a hammer.
“I’m hitting it again,” narrated Drex.
The commentary was driving Rachel crazy. She thought
back bitterly to the encounter with the wizard three days ago.
Drex had confronted Marcus to complain about his magic
candle. The candle had burned out, which magic candles
should never do. When Drex had discovered that the
wizard’s apprentice had created the candle, he had made the
fateful mistake of insulting Marcus’s teaching skills. “At least
I tell my apprentices what they need to know,” Drex had
bragged. It turned out to be a mistake to taunt a sleep-
deprived wizard at two in the morning.
“I’m hitting it again,” narrated Drex.
At least Marcus wasn’t evil. He had placed only a
temporary curse on Drex, forcing him to overcomment on all
of his actions for the next week.
“I’m hitting it again,” narrated Drex.
Unfortunately, Rachel didn’t know if she could last another
four days.
Data Structures for Research
Data structures can often be used as core elements of complex algorithms. In
these cases, data structures do more than just store data. They organize the data
and help make operations, such as finding a specific value, efficient.
***
“You want them right here?” asked Peter. He motioned
toward a tall stack of books. He had serious doubts about the
pile’s stability.
“On top,” said Ann without looking up. “I should also
have some more requests in a minute.”
“Are you sure you want them on top?”
Ann looked up in surprise. “Of course I’m sure. I have a
system.”
“A system?” asked Peter. “It looks more like a stack of
books. I think it would make more sense if I put the new
books on the bottom. I keep putting new books on top, and the
early books keep getting buried deeper.”
Ann considered the pile for a moment. It now contained at
least fifty books and was beginning to lean dangerously to
one side.
“It’s a stack,” Ann said.
“I can see that,” said Peter. “I helped stack them. Wouldn’t
it help to organize them? It might be more efficient.”
“You don’t understand. It’s a computational stack—a last-
in, first-out data structure. It’s organized.”
“How is that pile organized?” asked Peter in disbelief.
“The most recently requested books are on top,” answered
Ann. Then, after a moment, she set her pen down and turned
her full attention to Peter. “Everyone has their own way of
doing research. Some people like to try a bunch of different
ideas at the same time. Some people jump around. I use
depth-first search.”
“What does depth-first search have to do with research?”
“I keep going deeper and deeper into a subject until I hit a
dead end. Then I backtrack and try something different. A
stack is perfect for that type of research.
“Consider the books that you just brought up: On the Use
of Dice for Selecting Random Numbers in the Range of 1 to
6 and Advanced Probability for the Study of Garden
Gnomes. Both books are related to the topic that I read about
a few minutes ago—the use of randomness in certain
algorithms. So I decided to dig deeper into randomness.
Next, I’ll take one of those two books off the top of the stack,
read it, and possibly ask for other related books.
“As I finish with books, I take them off the stack. This
means that when I hit a dead end in one of my investigations,
I find older books that are lower in the stack. My research
automatically backtracks to earlier ideas.”
Peter nodded intently. “What a great data structure!” he
exclaimed. “It keeps your information organized exactly as
you need it. It’s efficient, too! Inserting and removing books
are both O(1) operations; you put new books on top and take
books off the top. No need to sort anything. It’s amazing.”
“I guess so,” Ann agreed. “For the record, the insertion
operation is called ‘push’ and the removal operation is
called ‘pop.’ You push data onto a stack and pop it off.”
“Did you develop this data structure yourself?”
Ann looked confused. “A stack of books? I doubt it. I’m
quite confident that people have been putting books into piles
for a long time.”
“Oh, right.” Peter paused. “Why are you researching
randomness, anyway?”
“I’m not sure,” Ann admitted. “I got the idea from this
magic purse that I was given. It magically tracks how much
money is in it.”
Ann showed Peter the purse from Marcus. She took a dime
out, and the counter decreased by $0.10.
“What does that have to do with anything?” asked Peter.
Ann shrugged. “Since I left on my journey, the value has
slowly trended downward. During any given week it bounces
up and down, but the long-term trend is the same. Something
about that reminded me of randomized algorithms. They
randomly explore different solutions, always trying to find
something better.”
Peter looked back and forth between Ann and the purse. “I
still don’t get it.”
“It’s a long shot,” admitted Ann. “But something about
Clare’s message made me think of the purse. As I said, I keep
following one idea deeper and deeper until I hit a dead end.”
Peter looked down at the stack of books and shuffled
uncomfortably. “But do you have time for that? For dead
ends, I mean.”
“I don’t know,” answered Ann honestly.
“And what if you’re right?” asked Peter, sounding more
worried.
“We win,” answered Ann. “That would be a good thing.”
“But the wizard will still be there. He’ll probably be a
few feet away by the time you try your idea. Couldn’t he just
cast a giant fireball at us?”
“This wizard doesn’t really seem to be the fireball type,”
offered Ann.
“How about a different curse then? What about boils?”
“I think I have an idea about that too,” Ann said. “If the
NP-hard curse is as powerful as I think it is, he needs to use
a lot of magic. As long as we can avoid being cursed by the
first spell, we should be safe from any followup spells for a
while.”
“Are you sure?” asked Peter.
“No. But all evidence seems to point that way.”
Peter nodded to himself a few times. Then, reaching some
internal conclusion, he turned and walked purposefully
toward the library’s main doors.
“Where are you going?” Ann asked. She wondered if she
had managed to frighten him into running. Admittedly, fleeing
in terror would actually be the safest course of action for
him.
“To make a new friend,” said Peter. “If your research
works out and we avoid the curse and the wizard doesn’t
have enough magic left to smite us with a fireball, then we
could use some more help. And if your research doesn’t
work out, I guess it doesn’t matter anyway.”
After he left, Ann looked back at the stack of books. She
tried to push all of those ‘ands’ out of her mind and focus on
the research at hand. She still needed to figure out how to
avoid being cursed in the first place. She carefully reached
over and popped the next book of the stack.
Expected Running Time
The expected running time of an algorithm indicates how long an algorithm will
take on average. This gives an indication of how the algorithm will perform on
typical problems. However, unlike worst-case analysis, the expected running time
can be optimistic for difficult instances.
***
A wizard in a dark black cloak stood at the foot of the library
stairs. In his right hand, he held a solid wooden staff carved
with mathematical symbols. A large hood obscured his face.
“Good morning. I am looking for Peter,” announced the
wizard. He stopped at the foot of the steps and looked up at
the two kids standing there.
Peter didn’t answer. He gripped Ann’s arm tightly and
took a half step backward. “Are you sure about your plan?”
he asked Ann in a whisper.
Ann looked at the wizard standing at the foot of the library
steps. “No,” she whispered back truthfully. “But we’re out of
time. This is our only option.”
“We could run,” offered Peter.
Ann didn’t respond. She stared at the wizard, waiting for
the spell to come.
The wizard cleared his throat loudly. He looked mildly
annoyed that he didn’t seem to have their full attention.
“Excuse me. Are you Peter?” the wizard asked again as he
started to approach. “I have a problem for which I need your
help. I have been told that you are a great computational
mind.”
Peter’s grip tightened.
“Ask me,” Ann said.
The wizard paused and studied Ann for a moment. Then,
he pulled back his hood. His face reminded Ann of any of the
many tenured professors at her school. His gray hair barely
covered his head and deep wrinkles adorned his face, but his
eyes blazed intensely. He gave her a wicked smile. “Princess
Ann? What a truly pleasant surprise. I had not expected to run
into you so soon. Of course, I had hoped our paths would
indeed cross.”
“Are you going to ask me a question or not?” asked Ann.
The wizard’s smile widened to consume his entire face.
“Let’s talk about a little problem that I call the traveling
salesman problem. I need to find a fast, exact algorithm to
solve that very problem on large graphs.” His voice was
calm and almost soothing; it reminded Ann of countless
lectures that had put her to sleep. As he spoke, his right hand
began to wave his staff back and forth. A faint blue glow
emanated from the top.
“I was always partial to an algorithm that I learned in the
sixth grade: randomized hill-climbing,” stated Ann.
The wizard paused, looking confused. “Randomized hill-
climbing?” he asked.
“It’s quite simple,” explained Ann. “Start with a guess at
the solution. It will probably be wrong, but you can at least
find out how good it is. In this case, guess a path and
compute its total distance.
“Then, you start the optimization loop. At each step in the
loop you:
1. Propose a small random change to your solution.
2. Check the new solution.
3. If the new solution is better, take it. If it isn’t better, then
take it with some small probability. I like to roll two
six-sided dice and take a bad move if I roll a double
six.
“You always take a move that will improve things. And
sometimes you take a move that makes things worse, so that
you can ultimately find better solutions.”
The wizard stared at Ann with his mouth open. Ann
noticed a small bead of sweat on his forehead. “That’s a
heuristic algorithm—a series of guesses! It doesn’t guarantee
that you’ll find the solution. It searches almost blindly.”
“That’s true,” Ann agreed. “But it can do reasonably well
most of the time.”
“Most of the time?” asked the wizard.
“Surely you know that there are no efficient, exact
algorithms for NP-hard problems … yet. In fact, there might
not be any efficient solutions. No one knows. But some
algorithms perform quite reasonably on most cases.”
The wizard took a step back and raised his staff. “What
trickery is this?” he asked.
“No trickery,” answered Ann. “It’s the same thinking as
expected-time algorithm analysis. Worst-case performance of
algorithms only tells you part of the story. My kindergarten
teacher once learned that the hard way when she—”
“Enough!” the wizard bellowed.
Ann smiled back condescendingly. “It seems that you
underestimated the full range of computational analysis at my
disposal. There’s a new world of analysis where algorithms
and probability meet. Your curse has no power there.”
“You have to be a world expert in heuristic algorithms and
expected running times for this spell to have no effect,”
objected the wizard. “How did you know?”
“I found a few clues along the way,” Ann said.
“This is not over!” the wizard shouted.
He turned to leave—but found Terrible Todd blocking his
retreat. The candlemaker’s apprentice grinned as he grabbed
the wizard by the arms and lifted him. Ann had no doubt that
he could detain the wizard a day or two until Marcus and
Sir Galwin arrived. And there was clearly no need to worry
about Todd falling victim to the NP-hard curse.
“Yeah, it is over,” shouted Peter, as he peeked out from
behind Ann. Then more quietly, he mumbled, “Totally worth
giving up my season tickets.” He sounded as though he was
still trying to convince himself.
The wizard spun back to Ann. “I am just the beginning,” he
proclaimed.
“The beginning of what?” asked Ann.
The old wizard laughed. “Chaos will engulf the entire
kingdom. The computational foundations will crumble. It
will be a new dark age.”
“Why?” asked Ann.
“The first step in taking over, of course,” said the wizard
simply. The bluntness caught Ann off-guard. He continued,
“Do you think your computational rule will last forever? I am
just the beginning. Even if you break the kingdom’s
computational thinkers from my spell, do you think you’re
prepared for the challenges ahead?”
To everyone’s surprise, Ann shrugged. “Who knows? I
expect that I’ll do well most of the time.”
Returning Home
One year after she ventured forth to stop the darkness, Ann
returned home to the castle. Trumpets announced her arrival,
banners flew from every post, and a full honor guard lined
her path. The castle had prepared a hero’s welcome. A year
ago, Ann would have been thrilled at this celebration. Now,
she just wanted to sleep.
Ann proceeded directly to the throne room. Her father
waited there, alone. He smiled proudly as she entered the
room.
“I have stopped the darkness,” Ann declared. “Or, at least,
I’ve stopped the wizard who was trying to plunge the
kingdom into a computational dark age. Marcus is still
traveling around and undoing the damage.”
“You did well,” the king declared. “I am very proud of
you. Welcome home, Ann.”
“Thank you” was all that Ann could manage. A small
doubt continued to nag at her. She tried to push it down and
bask in her success, but it refused to be quiet.
King Fredrick noticed the look on her face. “What is
wrong? You succeeded.”
“I know,” answered Ann. “It’s just …” She trailed off.
Her father waited for her to finish.
“What if I hadn’t succeeded?” Ann asked, letting a flood
of doubts rush out. “I came so close to failing. I’m not sure
why it had to be me on this quest. I wasn’t prepared at all.”
Her father sat silently, watching her. After a moment, he
spoke. “That is why it had to be you; you had to learn. To be
precise, the seer said that it needed to be the person of my
choice who would venture forth alone. I believed that you
could use the experience. And, anyway, you were not paying
attention during the prophecy.”
Ann was shocked at this revelation. “What if I had
failed?”
“I had confidence in you,” he said calmly. “Some things
have to be learned through practice and application. It is not
enough to read about the concept of quests; you must go out
and apply them. You must experiment with techniques through
trial and error.”
Ann didn’t look convinced.
The king grew serious. “Listen carefully, Ann. When you
take the throne, there will always be new challenges. Some
will be easier and some will be more difficult, but you must
face them all. The fate of the kingdom will depend on you
again and again.”
She nodded, but the doubt continued to eat at her. “What if
I can’t handle them? What if I’m not smart enough?”
“Some of the building blocks you have already; others, you
will learn. It is up to you to continue to learn how to apply
them.”
Ann left the throne room with a sense of unease. She was
safe at home. She had succeeded in her quest; she had saved
the kingdom from plunging into a computational dark age.
She knew that she should be happy.
Yet the encounter with the wizard still haunted her. Despite
having stopped him, she had no clue as to his full
motivations. What did he mean that he was “just the
beginning”?
As Ann walked down the hall, her mind drifted back to the
words of both the wizard and her father. There would always
be more challenges to face.
Acknowledgements
A tremendous thanks goes out to all of the people who read
earlier versions of this book and provided valuable
feedback: Tim Bell, Abbi Bull, John Bull, Meg Bull, Edith
Kubica, Regan Lee, Pat Stephenson, Kristen Stubbs, and Phil
Wagner. Thanks to everyone who supported the earlier online
version of these stories, and in particular to Eleanor Rieffel
and Kristen Stubbs for their support in publicizing the
stories. Thank you to my editor, Marjorie Carlson, whose
help was critical. Thank you to Meagan O’Brien for her
wonderful cover design. A deep thank you to my family for
their support.
About the Author
Jeremy Kubica began his career in computer science by
learning to program a Commodore in the second grade. There
he soon mastered the secret arts of variables and loops—
skills that helped propel him toward fame and ever greater
challenges.
He has a B.S. in Computer Science from Cornell University
and a Ph.D. in Robotics from Carnegie Mellon University.
He spent his graduate school years creating algorithms to
detect killer asteroids (actually stopping them was, of
course, left as ‘Future work’).
He is the author of the Computational Fairy Tales blog.
Table of Contents
Copyright
Dedication
A Note to Readers
Major Characters
THE START OF A QUEST
The Darkness is Coming
An Algorithm for Quests
Variables and Magic Gifts
The If-Else Life of the King’s Turtle
Loops and Making Horseshoes
The Town of Bool
Unhappy Magic Flowers and Binary
The Importance of (Variable) Names
Pseudocode for the Quest Algorithm
DATA STRUCTURES
Arrays, Linked Lists, and Zed’s Coffee
Strings and Pigeon Messages
The Swimmy Friends Pet Store
Pointers and Walk-In Closets
Linked Lists and Ocean Voyages
The Prince’s Complaint Line
Binary Search Trees and the Spider
Pointers, Linked Lists, and Trees
Caching and the Library of Alexandria
ALGORITHMS
Functions and Sailing
Big-O Notation and the Wizards’ War
Detecting Curses with Recursion
Hunting Dragons with Binary Search
Why Tailors Use Insertion Sort
Bullies and Bubble Sort
Merge Sort and Lines of Kindergarteners
Sorting During the Flu Outbreak
The Oracle’s Array
Big-O and Hitting Things with Hammers
GRAPHS
The City of G’Raph
Directed Graphs and Bridges
Bridge Weights
Dijkstra’s Algorithm on Scooters
A Disagreement over Data Structures
The Traveling Salesman’s Problem
Panicked Depth-First Search
Bridge Upgrades
The Game of Hamiltonian Paths
COMPUTATIONAL THINKING
Reflections on Algorithms
Computational Graffiti
The NP-Hard Curse
Everyday Algorithms
The Quicksort Message
Comments and the Baker’s Apprentice
The Curse of Excessive Commenting
Data Structures for Research
Expected Running Time
Returning Home
Acknowledgements
About the Author