15.08.2013 Views

General Computer Science 320201 GenCS I & II Lecture ... - Kwarc

General Computer Science 320201 GenCS I & II Lecture ... - Kwarc

General Computer Science 320201 GenCS I & II Lecture ... - Kwarc

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Example 10 (Kruskal’s algorithm, a graph algorithm for spanning trees) <br />

Randomly add a pair to the tree if it won’t create a cycle. (i.e. tear down a wall)<br />

Repeat until a spanning tree has been created.<br />

c○: Michael Kohlhase 21<br />

Definition 11 An algorithm is a collection of formalized rules that can be understood and executed,<br />

and that lead to a particular endpoint or result.<br />

Example 12 An example for an algorithm is a recipe for a cake, another one is a rosary — a<br />

kind of chain of beads used by many cultures to remember the sequence of prayers. Both the<br />

recipe and rosary represent instructions that specify what has to be done step by step. The<br />

instructions in a recipe are usually given in natural language text and are based on elementary<br />

forms of manipulations like “scramble an egg” or “heat the oven to 250 degrees Celsius”. In<br />

a rosary, the instructions are represented by beads of different forms, which represent different<br />

prayers. The physical (circular) form of the chain allows to represent a possibly infinite sequence<br />

of prayers.<br />

The name algorithm is derived from the word al-Khwarizmi, the last name of a famous Persian<br />

mathematician. Abu Ja’far Mohammed ibn Musa al-Khwarizmi was born around 780 and died<br />

around 845. One of his most influential books is “Kitab al-jabr w’al-muqabala” or “Rules of<br />

Restoration and Reduction”. It introduced algebra, with the very word being derived from a part<br />

of the original title, namely “al-jabr”. His works were translated into Latin in the 12th century,<br />

introducing this new science also in the West.<br />

The algorithm in our example sounds rather simple and easy to understand, but the high-level<br />

formulation hides the problems, so let us look at the instructions in more detail. The crucial one<br />

is the task to check, whether we would be creating cycles.<br />

Of course, we could just add the edge and then check whether the graph is still a tree, but this<br />

would be very expensive, since the tree could be very large. A better way is to maintain some<br />

information during the execution of the algorithm that we can exploit to predict cyclicity before<br />

altering the graph.<br />

Creating a spanning tree<br />

When adding a wall to the tree, how do we detect that it won’t create a cycle?<br />

When adding wall 〈x, y〉, we want to know if there is already a path from x to y in the tree.<br />

In fact, there is a fast algorithm for doing exactly this, called “Union-Find”.<br />

Definition 13 (Union Find Algorithm) <br />

The Union Find Algorithm successively puts<br />

nodes into an equivalence class if there is a<br />

path connecting them.<br />

Before adding an edge 〈x, y〉 to the tree, it<br />

makes sure that x and y are not in the same<br />

equivalence class.<br />

14<br />

Example 14 A partially constructed<br />

maze

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!