16 Jop April 08
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

16 Jop April 08

on

  • 458 views

 

Statistics

Views

Total Views
458
Views on SlideShare
458
Embed Views
0

Actions

Likes
0
Downloads
2
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

16 Jop April 08 Document Transcript

  • 1. The Joy of Programming The Beautiful Markov String S.G. GaneSh Replacement Algorithm algorithms need not always be a dry and esoteric subject. In this column, we will look at how to convert binary values to unary values by repeatedly applying simple, automated rules using Markov’s string replacement algorithm. W hen we hear of some strange algorithms • Step 5: “###” (apply rule 3) with names like ‘Dijkstra’s shortest path’, • Step 6: “1##” (apply rule 4) ‘Markov’s string replacement’ or ‘Sieve of • Step 7: “11#” (apply rule 4) Eratosthenes’, we usually dislike the arcane and dry • Step 8: “111” (apply rule 4) solutions they describe. However, such algorithms • Terminate. can have interesting applications or can provide some unusual solutions that are not possible with the Looks interesting, but does it work in all cases? Let us straightforward solutions we often arrive at based on try another one. ‘100’ is decimal 4 and unary 1111. Here are common sense. the strings during each step of applying the rules: We’ll explore ‘Markov’s string replacement’ algorithm in this article. What this algorithm does is “100” => “0#00” => “00##0” => “00#0##” => “000####” simple string rewriting based on a set of rules. Let’s => “00####” => “0####” => “####” => “1###” => “11##” => look at an interesting application of this algorithm: “111#” => “1111”. converting a binary to unary by string replacement, by applying a set of rules. The idea described here is It does indeed work! Now, what is the logic behind these from en.wikipedia.org/wiki/Markov_algorithm rules? The first two steps do the actual work. The third rule just removes leading zeros, and the last rule converts “#”s Let us look at the rules first: to “1”s. • Rule 1: “#0” => “0##” The first and second rule moves 1 from left to right, • Rule 2: “1” => “0#” repeatedly. Each time it shifts, it increases the number of • Rule 3: “0” => “” #’s by two. Remember that the value of 1 in a binary number • Rule 4: “#” => “1” is its power of two, and the rules implicitly calculate that. By the time you are unable to apply the first two rules The symbol ‘=>’ stands for ‘replace with’. Take any any more, all the entries in the string would be “#”s, with binary number – preferably, a small one. Treat that leading “0”s that were created because of this process. binary number as a string. Start repeatedly applying Simple, isn’t it? the rules from the top to the bottom. Apply the rules There is another very interesting application of this from left to right on the string. Only if you cannot algorithm. It is possible to write programs that create apply the current rule or the previous rules anymore grammatically correct, non-sense text! If you’re interested on the string, go to the next rule. Once you finish to know how, read the chapter on ‘Generating Text’ in applying the last rule, stop the process. Programming Pearls by Jon Bentley, Addison-Wesley, 2000. Consider the binary number ‘11’. Its equivalent The algorithm described here is simple, but powerful: decimal value is 3. So, its unary value is 1 repeated 3 This algorithm is shown to be ‘Turing complete’ (to describe times, that is, 111. By applying these rules, we should it informally, Turing complete algorithms can be used to get ‘111’. Do the rules work? Let us check: implement any computation that any powerful computer in the world is capable of performing!). • Input string: “11” • Step 1: “0#1” (apply rule 2) S.G. Ganesh is a research engineer at Siemens (Corporate • Step 2: “0#0#” (apply rule 2) Technology). His latest book is “60 Tips on Object Oriented • Step 3: “00###” (apply rule 1) Programming”, published by Tata McGraw-Hill in December • Step 4: “0###” (apply rule 3) last year. You can reach him at sgganesh@gmail.com. www.openITis.com | LINUX For YoU | AprIL 2008 85 cmyk