Monday, August 29, 2011
Fun With Boolean Statements
Thursday, August 25, 2011
Data Validation, Marcus, and the Cheese Minstrel: Part 1 of Marcus and the Cursed Cheese
Monday, August 22, 2011
The Importance of Good Comments: A Tale of the Baker's Apprentice
As part of his world renowned teaching program, the famous baker Breadtista insisted that each apprentice thoroughly document his or her work. Thus, apprentices created a reference that would live beyond their apprenticeship. All recipes, tips, and tricks would be clearly documented. Breadista strongly believed that such a resource provided his apprentices with a significant advantage.
As Breadtista was fond of pointing out, the key to a great recipe was 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 commenting his recipes. These comments added context and explanation.
Here is an excerpt from Breadtista's famous eight-chili strawberry muffins:
Further, Breadtista insisted that all comments provide meaningful information to the recipe itself. He hated recipe books by famous castle chefs that contained thirty page digressions of the author's lifelong quest for the perfect pancake. It was especially egregious if the recipe then produced chewy pancakes. Pancakes were not meant to be chewy, and comments were not meant to be life stories.
Despite his firm belief in the importance of good comments, Breadtista found it difficult to explain this philosophy to each new apprentice. Every year, he heard the same arguments:
- "Why should I comment? The only people reading this are bakers, and they should already know this."
- "I won't forget why I did that."
- "Isn't that step obvious?"
- “Just because it is obvious to you, does not mean that it will be obvious to others."
- "Just because it is obvious to you now, as you are writing the recipe, does not mean that you will remember it."
- "If it is so obvious, why did I have to draw you an example on the chalk board? Twice?"
It took ten years of trial and error, but eventually Breadtista found the perfect way to reinforce the importance of GOOD comments. The first three weeks of each apprenticeship, Breadtista would make each new apprentice work from the worst notes of previous students. Although it meant that he would now hear cries of "WHO WROTE THIS?" and "BUT WHAT DOES IT MEAN?" from the kitchen, this process saved him from having to repeat the same reasoning over and over.
---------------------------
If you are interested in learning more about writing good code, check out: The Importance of (Variable) Names or The Importance of Unit Testing Magic Spells.
Friday, August 19, 2011
The Game of Hamiltonian Paths: Part 9 of Ann's Visit to G'Raph
Read Ann's visit to G'Raph from the beginning with Part 1: The City of G'Raph. Or read about Ann's first (and highly unpleasant) visit to the city of Bool.
Tuesday, August 16, 2011
Minimum Spanning Trees, Prim's Algorithm, and Bridge Upgrades: Part 8 of Ann's Visit to G'Raph
"Then you keep adding the closest node that is not already in the set. Here we first add the inn..."
To be continued in Part 9: The Game of Hamiltonian Paths.
---------------------------
Follow Ann's visit to G'Raph from the beginning with Part 1: The City of G'Raph.
Also check out the new illustrations in Bullies, Bubble Sort, and Soccer Tickets and Linked Lists, Kindergarten, and Ocean Voyages.
Friday, August 12, 2011
Panicked Depth First Search: Part 7 of Ann's Visit to G'Raph
Depth first search is a search algorithm that fully explores a single path before backtracking to test other paths. The algorithm operates in a recursive way, first exploring all of the options down one subpath before considering other subpaths. For example, on a graph the algorithm will explore the neighbors of a neighbor before returning to original node to test other direct neighbors. Unlike breadth first search, which uses a queue based approach to track unexplored states, depth first search uses an approach based on a stack.
Ann realized that she had made a critical mistake almost as soon as she left the library. In her rush to get to the school, she had neglected to bring a map with her. In any other town, this would not have been a major problem -- she would just run in the general direction of the school. But finding your way around in G'Raph was not that simple.
Ann stopped when she reached Thomas's farm. There were two paths in front of her, and she was not sure which way led to the school. She looked around for the farmer, but Thomas was not in his radish patch.
Making a quick decision, Ann chose the path out of Thomas's farm that was on her right. She ran over the bridge, and found herself at the local pottery kiln. An assortment of bowls were lined up outside the kiln. Judging from the quality of craftsmanship at least a few of the bowls must have been made by third graders at the school. Ann hoped that she was getting close.
Now Ann had another decision to make -- two new paths led away from the kiln. Again Ann chose the path to her right, and she ran over the bridge. This time the result was less promising. Ann had come to a dead end. She was on an island that appeared to be a mud quarry. There were no new bridges that might take her to the school.
Ann backtracked quickly, returning to the pottery kiln island.
Ann backtracked to the pottery kiln again. This time there were no more paths to take, so she backtracked further. She returned to Thomas's farm. From the farm, she took the other bridge and arrived at another (smaller) radish farm. From the looks of it, this farm was decidedly less successful than Thomas's farm. Ann paused for the briefest moment to wonder what could cause the difference in radish growing performance between two almost indistinguishable islands of mud. Maybe it had something to do with crop rotation strategies.
Yet again, Ann was faced with a choice of bridges. She started with the path to her right. Much to her relief, this bridge led directly to the school.
Ann ran up to the school. Florence and Edgar were already there. Their scooters were parked nearby, and Edgar held a folded map of G'Raph in his hand. Ann cursed herself again for not having brought a map. She could have saved a lot of backtracking.
"You made it!" exclaimed Edgar. "I was afraid that you would get lost without a map."
"I did get lost." admitted Ann. "Or at least, I hit a few dead ends and had to backtrack."
"Ah… a depth first search." noted Florence. "Nice."
"Depth first search?" asked Ann. She was still a little out of breath from all the running.
"Depth first search is when you keep exploring along one path until you hit either a dead end or a node that you have seen before. Then you backtrack to the most recent decision point and try a different option. If there are no new options at that point, then you backtrack again." explained Edgar.
"Think about searching a binary tree." suggested Florence. "With depth first search, you keep exploring deeper and deeper until you come to a dead end."
"Or find what you are looking for." added Edgar.
"Yes. Good point." replied Florence. "You can also find what you are looking for. Edgar, remember that time we used depth first search on that choose your own adventure novel?"
"Fun times." answered Edgar.
Ann just nodded. She was too tired to appreciate the algorithmic beauty of her own frantic run. A map would have saved her from having to experience depth first search first hand.
The school's principal, having seen the group standing outside, came out to see what was going on.
"Have you seen a strange man in a dark cloak?" asked Ann without bothering to introduce herself.
"Umm… yes. Why do you ask?" asked the principal.
"Did he talk to anyone?" asked Ann, ignoring the principal's question.
"Yes. He talked with one of our star students, Elizabeth. He had some question about some sort of math problem." answered the principal. "I was there too, but honestly could not understand what he was talking about. Elizabeth seemed certain that she could figure it out though. She went right home to work on it. She is very bright, you know. Probably the best scholar that our school has ever taught. She will find the answer."
"Do you know what the question was?" asked Ann.
"It was something about islands and bridges. I think he called it 'vertex covering'. I had not heard of it before, but it sounded like fun."
Ann felt her stomach drop. She was familiar with the vertex covering problem from her own time in school. Like the traveling salesman problem, it was unknown whether the vertex covering problem had an efficient solution. The problem fell into a class of NP-hard problems.
Unless Ann missed her guess, Elizabeth had just fallen victim to the same spell as Geoffrey. G'Raph High School's star student was a risk of becoming unnaturally obsessed with a single NP-hard problem.
So far, the wizard had targeted two star scholars. Unfortunately, Ann had no idea why.
To be continued in Part 8: Minimum Spanning Trees, Prim's Algorithm, and Bridge Upgrades...
Follow Ann's visit to G'Raph from the beginning with Part 1: The City of G'Raph.
--------------------
Find out more about breadth first search in: Breadth First Search and the Search for the Pig Thief.
Or find out about stacks and queues in: Stacks, Queues, Priority Queues, and the Prince's Complaint Line.
Monday, August 8, 2011
The Traveling Salesman's Problem: Part 6 of Ann's Visit to G'Raph
To be continued in Part 7: Panicked Depth First Search…
Follow Ann's Visit to G'Raph from the beginning with Part 1: The City of G'Raph.
Friday, August 5, 2011
A Disagreement Over Data Structures: Part 5 of Ann's Visit to the City of G'Raph
Graphs can be represented by a variety of data structures. Two common representations are: adjacency matrices and adjacency lists. Both data structures can handle directed, undirected, weighted, or unweighted edges. An adjacency matrix represents a graph as a matrix -- with one row and one column for each node. The matrix value of row i, column j is the weight of the edge from node i to node j (or 0/1 for unweighted graphs). An adjacency list maintains a separate list of neighbors for each node.
Ann had obviously touched upon a controversial question. Florence and Edgar stood on opposite sides of the room glaring at each other. The question had seemed innocent enough. "Do you always represent graphs with these illustrations?" Ann had asked.
Florence had quickly answered. "No. Those are only for illustration. Adjacency matrices work better."
And Edgar had snapped. "No. The correct answer is that adjacency lists are the preferred tools."
Then the glaring had begun.
"What are adjacency matrices and adjacency lists?" asked Ann, starting with the most basic question. Both scholars immediately rushed to explain their data structures. The resulting sound reminded Ann of two chipmunks fighting.
"One at a time, please." Ann pleaded. "Florence, why don't you start with adjacency matrices?"
Florence smiled. "They are very simple. Every row represents a node and every column represents a node. If there is an edge between two nodes, you put their weight in the corresponding element. So M[i][j] would store the weight of the edge from node i to node j. Here is an example of some of the islands of G'Raph."
"Adjacency matrices are wonderful." continued Florence. "See how it puts all of the information into a single convenient form. They are very simple representations. And you can perform a bunch of computations by just multiplying the matrices."
Ann nodded. It seemed sensible enough.
"Why aren't there ones along the diagonal? Obviously you can get from the library to the library." asked Ann.
"Depending on what you are trying to represent, self edges (or loops) can certainly make sense. In this case, I am only representing the existence of bridges. And there is no bridge from the library to the library." explained Florence.
"Okay. Now Edgar, can you tell me about adjacency lists?" asked Ann, turning to the other scholar.
"Certainly." started Edgar. "You start with a list of each node in the graph. Imagine a giant array or linked list with the name of each node in it. Then for each of those nodes, you keep a list of all the nodes to which it connects. For graphs with a few edges, it can use a lot less memory. You only store the edges that are there Here is an example."
Ann studied the example. "Both forms can represent the same graph exactly, correct?" asked Ann. Both scholars nodded,
"And both forms can do directed graphs as well?" asked Ann. Both scholars nodded.
"And they can both represent weighted edges?" asked Ann. Both scholars nodded.
"Then why are you fighting? It seems like they both do the same thing, but have different advantages. A matrix is simpler to specify, and a list can take less space. Is that really worth fighting over?" asked Ann. Both scholars nodded.
"Space matters!" cried Edgar. "Have you ever tried using your precious matrices to represent the entire pigeon network, Florence? There are thousands and thousands of nodes!"
"Simplicity matters too!" answered Florence "I can traverse my matrices with two fixed size FOR loops. You need to iterate over lists of different size. I have seen your implementations… they have POINTERS!"
"Better to have pointers than a row with ten thousand useless zeros!" responded Edgar.
The argument was getting heated. Ann backed away from the two scholars. She was afraid that one of them would eventually throw a coffee mug.
Then Ann heard a low sigh from the corner of the room. Turning away from the argument, Ann peaked around a very large stack of books. There, at a tiny desk in the corner, was the most depressed looking person that she had ever seen. He hunched over his desk, staring at a single sheet of paper on his desk and shaking his head.
"Hello?" Ann asked.
The man looked up at her blankly. "Both data structures are completely reasonable in their own way." he explained without bothering to introduce himself.
"I can see that." Ann confirmed. "I think they both have advantages and disadvantages."
The man just nodded. "I wish they would see that. They make such a terrible racket when they argue. I have important work to do, you know."
"I see. Can I ask what you are working on?" asked Ann.
"It is not done." he answered. "I have not figured it out yet."
"Oh. What is the problem, then?" Ann probed.
The man did not answer for a while. Behind her, Ann could hear Florence shouting something about invertability. Ann tried to ignore the argument.
Finally, the man gave Ann a sad look. "It is a really simple problem. I do not know why I have not been able to come up with a good solution. It really is quite simple. I just want an algorithm that will give me the shortest path through a graph such that the path visits each node exactly once."
Ann thought about this for a second. "Sounds like a good problem." she offered.
The man nodded. "I have been working at it for a while without any success. It all started a long time ago, when a man in a dark cloak first told me about the problem. I think he might have been a traveling salesman. I never did see his face though. He just told me that he had a problem that he needed the best scholar in G'Raph to solve. I was young and naive. I told him that I would have an efficient algorithm by the end of the week… that was twenty-five years ago."
To be continued in Part 6: The Traveling Salesman's Problem...
Follow Ann's visit to G'Raph from the beginning with Part 1: The City of G'Raph.
-------
To learn more about matrices see Users, Peanut Vendors, and Matrix Indices.
To learn more about linked lists and arrays see Arrays, Linked Lists, and Zed's Coffee Shop or Linked Lists, Kindergarten and Ocean Voyages.