[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Levenshtein_distance and recreate the string

Raghu Go

4/30/2008 1:14:00 AM

Hi -

A general computer science question-

Given the levenshtein distance between two strings and one of the
strings S1, is it possible to re-create the second string.

For example -

S1 = "RUBY"

S2 = "BRUY"

lev_distance = 3

Given 3 and S2, is it possible to recreate S1??

Many thanks,
--
Posted via http://www.ruby-....

11 Answers

Phillip Gawlowski

4/30/2008 1:32:00 AM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ams Lo wrote:
| Hi -
|
| A general computer science question-
|
| Given the levenshtein distance between two strings and one of the
| strings S1, is it possible to re-create the second string.
|
| For example -
|
| S1 = "RUBY"
|
| S2 = "BRUY"
|
| lev_distance = 3
|
| Given 3 and S2, is it possible to recreate S1??

I see no reason why it shouldn't.

Looking at the Wikiality for the algorithm[0], the algorithm works,
essentially, on a matrix for the strings. Juxtaposing the axes should
solve that, in a naive, uneducated way, anyway.

After all, the Levenshtein distance is the same for RUBY -> BRUBY and
BRUBY -> RUBY.


[0] http://en.wikipedia.org/wiki/Levenshtei...

- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.bl...

~ "That's the whole problem with science. You've got a bunch of
~ empiricists trying to describe things of unimaginable wonder."
~ --- Calvin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail....

iEYEARECAAYFAkgXzAkACgkQbtAgaoJTgL+1SwCfeSqOFABxce96rWU0YRLl9zHN
maMAnAz7tdnSBMZGVKWTj6EQ6rutDoL0
=RsJI
-----END PGP SIGNATURE-----

Rick DeNatale

4/30/2008 1:38:00 AM

0

On Tue, Apr 29, 2008 at 9:13 PM, Ams Lo <rgowka1@gmail.com> wrote:
> Hi -
>
> A general computer science question-
>
> Given the levenshtein distance between two strings and one of the
> strings S1, is it possible to re-create the second string.
>
> For example -
>
> S1 = "RUBY"
>
> S2 = "BRUY"
>
> lev_distance = 3
>
> Given 3 and S2, is it possible to recreate S1??

Hmmm, smells like a homework assignment.

Hint, do you suppose that strings other than "RUBY" might have a
levenshtein distance of 3 from "BRUY"?

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

David A. Black

4/30/2008 1:39:00 AM

0

Hi --

On Wed, 30 Apr 2008, Phillip Gawlowski wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Ams Lo wrote:
> | Hi -
> |
> | A general computer science question-
> |
> | Given the levenshtein distance between two strings and one of the
> | strings S1, is it possible to re-create the second string.
> |
> | For example -
> |
> | S1 = "RUBY"
> |
> | S2 = "BRUY"
> |
> | lev_distance = 3
> |
> | Given 3 and S2, is it possible to recreate S1??
>
> I see no reason why it shouldn't.
>
> Looking at the Wikiality for the algorithm[0], the algorithm works,
> essentially, on a matrix for the strings. Juxtaposing the axes should
> solve that, in a naive, uneducated way, anyway.
>
> After all, the Levenshtein distance is the same for RUBY -> BRUBY and
> BRUBY -> RUBY.

But isn't it the same for RUBY -> RUBE and RUBA -> RUBE ? In which
case, if you had RUBE and the L. distance, you could not recreate
RUBY; you'd have a whole set of strings that were exactly that
distance from RUBE.


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.r... for details and updates!

Phillip Gawlowski

4/30/2008 1:57:00 AM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

David A. Black wrote:
|
| But isn't it the same for RUBY -> RUBE and RUBA -> RUBE ? In which
| case, if you had RUBE and the L. distance, you could not recreate
| RUBY; you'd have a whole set of strings that were exactly that
| distance from RUBE.

Good point. I don't know if the L. distance is unique for each
possibility or not.

Considering how the algorithm looks, the solutions probably aren't unique.

But then again, you can use weighted probabilities to achieve the
correct result, I guess.

N.B.: I'm a bit out of my depth here, having no comp.sci. background
(which I regret..).

- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.bl...

~ - You know you've been hacking too long when...
...you wake up and desperately try to start a compiler so you can use
the 15 minute waiting period to sleep some more.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail....

iEYEARECAAYFAkgX0csACgkQbtAgaoJTgL9Z2wCfcXY3WwdwtlgRjvFb0luHMTry
X6EAniZeT0iOJjL/kiWw5vnrFSpKNzmj
=Er07
-----END PGP SIGNATURE-----

markonlinux

4/30/2008 1:57:00 AM

0

On Apr 30, 11:13 am, Ams Lo <rgow...@gmail.com> wrote:
> Hi -
>
> A general computer science question-
>
> Given the levenshtein distance between two strings and one of the
> strings S1, is it possible to re-create the second string.
>
> For example -
>
> S1 = "RUBY"
>
> S2 = "BRUY"
>
> lev_distance = 3
>
> Given 3 and S2, is it possible to recreate S1??
>
> Many thanks,
> --
> Posted viahttp://www.ruby-....

would S1 be BOAT (BRUT, BRAT, BOAT), GRAN (BRAY, GRAY, GRAN), GOUT
(BOUY, BOUT, GOUT) or ....?
ie I think you need to know where you're going in order to know how to
get there.
What S1 are we recreating?


cheers,


--
Mark

David A. Black

4/30/2008 2:17:00 AM

0

Hi --

On Wed, 30 Apr 2008, Phillip Gawlowski wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> David A. Black wrote:
> |
> | But isn't it the same for RUBY -> RUBE and RUBA -> RUBE ? In which
> | case, if you had RUBE and the L. distance, you could not recreate
> | RUBY; you'd have a whole set of strings that were exactly that
> | distance from RUBE.
>
> Good point. I don't know if the L. distance is unique for each
> possibility or not.
>
> Considering how the algorithm looks, the solutions probably aren't unique.
>
> But then again, you can use weighted probabilities to achieve the
> correct result, I guess.
>
> N.B.: I'm a bit out of my depth here, having no comp.sci. background
> (which I regret..).

Me too. I leapt on this thread partly to give myself an excuse to get
a more than anecdotal acquaintance with the algorithm. I can't claim
that it's *much* more than that, but every little bit helps :-)


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.r... for details and updates!

Axel Etzold

4/30/2008 8:34:00 AM

0

Dear all,

maybe what the OP wants is not so much Levenstein distance but
the McIlroy-Hunt longest common subsequence (LCS) algorithm, an algorithm
which not only tells you how far apart two strings are (how
many changes you have to make to get from string A to string B),
but also where they have to be made.
There's a Ruby implementation by Austin Ziegler here:

http://raa.ruby-lang.org/project...

If you want to know how many strings have Levenstein distance n from
a given string A, you'll probably have to create all possible
combinations of letters from the alphabet of lengths length(A)-n through length(A)+n, and
throw away those that don't have the correct Levenstein distance.
That's going to be an enormous amount of words in any case ...
Even if only words from a dictionary are allowed, in most
cases, you'll still have many words that are "close" to each other ;-(

Best regards,

Axel




--
Psst! Geheimtipp: Online Games kostenlos spielen bei den GMX Free Games!
http://games.entertainment.gmx.net/de/entertainment/...

Eleanor McHugh

4/30/2008 11:02:00 AM

0

On 30 Apr 2008, at 02:13, Ams Lo wrote:
> Hi -
>
> A general computer science question-
>
> Given the levenshtein distance between two strings and one of the
> strings S1, is it possible to re-create the second string.
>
> For example -
>
> S1 = "RUBY"
>
> S2 = "BRUY"
>
> lev_distance = 3
>
> Given 3 and S2, is it possible to recreate S1??
>
> Many thanks,

Simple answer: no.

Each step that exists between S1 and S2 represents the choosing of one
change out of a set of changes the same size as your token space (so
for uppercase letters only that would be 26) and therefore to reverse
that change you would generate that many valid words. Selecting which
of those is the S1 word is impossible without additional constraints.

For the S1 -> S3 -> S1 case the actual search space (still assuming
capital letters only) is therefore 26 ** 3, or 17576 equally plausible
S1 candidates. Of course that assumes a naive search strategy, whereas
it's quite possible the space could be restricted based upon known
dictionary characteristics: certain tokens may be impossible at
certain locations; genetic algorithms may grow 'good' solutions faster
than exhaustive search; etc.

Ellie

Eleanor McHugh
Games With Brains
http://slides.games-with-...
----
raise ArgumentError unless @reality.responds_to? :reason



Vidar Hokstad

4/30/2008 12:21:00 PM

0

On Apr 30, 2:13 am, Ams Lo <rgow...@gmail.com> wrote:
>
> S1 = "RUBY"
>
> S2 = "BRUY"
>
> lev_distance = 3
>
> Given 3 and S2, is it possible to recreate S1??

Several people have given great answers as to why this isn't possible,
but let me give you another reason why, just
because I feel like it, and it's a useful thing to keep in mind.

Whenever you get a problem like this, a good way of approaching it if
you don't understand the algorithm is to think:

- If it _was_ possible, would that allow you to make impossibly
efficient compression algorithms?

In this case, the answer is that yes it would: You could pick a fixed
S2, for example an empty string, calculate the
Levensthein distance from S1 to the fixed S2. The distance would be
proportional to the length of S1. In fact, if
you choose S2 to be the empty string, then the distance would be equal
to the length of S1 (it'd take one deletion
per position in S1 to end up with an empty string)

In other words, you'd "compress" your test string "BRUY" down to the
number 4, but more importantly that "compression"
method would compress any string of any length, which is obviously
impossible (the reason this is impossible is that
if it was possible you could apply it to it's own output over and over
until you had compressed an arbitrary input
down to a bit).

In this case it's not a very hard problem to solve, but I find that
for a large number of questions about data
transformations, it's helpful to think about it in terms of
compression because the answers often become blatantly
obvious once you restate them that way.

Vidar

Rick DeNatale

4/30/2008 12:48:00 PM

0

On Wed, Apr 30, 2008 at 8:25 AM, Vidar Hokstad <vidar.hokstad@gmail.com> wrote:

> Several people have given great answers as to why this isn't possible,
> but let me give you another reason why, just
> because I feel like it, and it's a useful thing to keep in mind.
>
> Whenever you get a problem like this, a good way of approaching it if
> you don't understand the algorithm is to think:
>
> - If it _was_ possible, would that allow you to make impossibly
> efficient compression algorithms?
>
> In this case, the answer is that yes it would: You could pick a fixed
> S2, for example an empty string, calculate the
> Levensthein distance from S1 to the fixed S2. The distance would be
> proportional to the length of S1. In fact, if
> you choose S2 to be the empty string, then the distance would be equal
> to the length of S1 (it'd take one deletion
> per position in S1 to end up with an empty string)
>
> In other words, you'd "compress" your test string "BRUY" down to the
> number 4, but more importantly that "compression"
> method would compress any string of any length, which is obviously
> impossible (the reason this is impossible is that
> if it was possible you could apply it to it's own output over and over
> until you had compressed an arbitrary input
> down to a bit).
>
> In this case it's not a very hard problem to solve, but I find that
> for a large number of questions about data
> transformations, it's helpful to think about it in terms of
> compression because the answers often become blatantly
> obvious once you restate them that way.

Another thing to consider is why it's called a distance rather than,
say, a difference.

Consider this analogy.

Given that the highway distance between Raleigh, NC, and Washington,
DC is 250 miles. Given Washington, DC, and 250 miles, can I uniquely
identify Raleigh, NC.

No since the highway distance between Washington, DC and New York, NY
(as well as many other places) is also 250 miles.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...