Answers

 

Sarah F

Currently unemployed. Seeking 100% telecommuting opportunities

see all my questions

Can someone provide an algorithm similar to TinyURL?

posted March 30, 2008 in Software Development, Web Development | Closed

Share This Question

Share This

Good Answers (2)

 

Adrian K

Independent iPhone, iPad and Android Software Developer, Entrepreneur and Architect - Invite me: invite@akosma.com

see all my answers

Best Answers in: Web Development (5), Software Development (3), Business Development (2), Computers and Software (2), Job Search (1), Economics (1), International Law (1), Internationalization and Localization (1), Wireless (1)

This was selected as Best Answer

I think that the page I have referenced below will provide you with an excellent answer to your question:

"As we have established, there are 62,193,780 possible values for TinyURL's. TinyURL's are generated by a Base 36 hash (36 indicating the number of characters a-z and 0-9, the array of possible values out of which a TinyURL can be constructed), autoincremented by MySQL with an initial value count of zero. What is a hash function? Imagine five buckets in a row. Now, starting with bucket one, we'll drop one of 36 characters into the bucket, and check to make sure that for all other bucket sets out there, there isn't a bucket that also has that particular character in the first bucket. If there is, we'd insert a character in the second bucket and run that comparison again, and so on and so forth . A hash is extremely efficient computationally, because instead of doing a tree comparison against all 62 million values, we're incrementally comparing a set of 5 characters that has only 36 possible values."

Basically, TinyURL and similar services use a hashing function. See the Wikipedia link below for a list of similar functions.

I hope this helps!

Best regards,

Adrian Kosmaczewski

Links:

posted March 30, 2008

 

Francesco P

ICT Consultant

see all my answers

Best Answers in: Web Development (1)

lilURL is a simple PHP/MySQL script for generating little URLs. It's similar to TinyURL, Shorl, MakeAShorterLink, etc, but you can run it on your own server..

Miny Urlis a very nice tiny url script with admin section and option to add ads in the redirected pages

Links:

posted March 30, 2008

More Answers (4)

 

Daniel D

IT Manager, Software Architect, Consultant

see all my answers

My take on TinyURL is that it is a combination of a backend database and a hashing algorithm such as MD5 or SHA-1. The runtime produces a hash of the desired "long" URL, stores the "long" URL in the database using the hash as the key to accessing it, and produces a short or "tiny" url with the hash value in the query string of the URL. When the "tiny" URL is used, the runtime looks up the 'long' URL from the database, and redirects the request to the original 'long' URL.

posted March 30, 2008

 

Fabrizio C

Senior Frontend Engineer / Developer

see all my answers

Best Answers in: Web Development (3)

Hi Sarah,

The simpler implementation of this service is to store, in a database table, a data pair (id, url) where your id has the autoincrement option, e.g.

1 -> www.linkedin.com
2 -> www.anotherurl.com/path/complete/to/the/page.php
3 -> ...

So when a user ask for http://yourtinyurlservice.com/1 you simply do a select into your table (select url from urlstable where id = '1') and then you do a redirect to that url.

You could refine this, looking before if a certain url was already 'tinyurled' (so you prevent multiple insertions of the same url), you could add a date field in your table and delete the older entries with a stored procedure or with a cronjob or with an admin panel... and so on.

Hope this will be useful for you.

-Fab-

Clarification added March 30, 2008:

Note: you could also use an hashing algorithm like SHA1, MD5 and others, but be careful because in this way you are not completely sure to prevent duplicated hash, especially if you use only 5 characters of the hash (like tinyurl does)

posted March 30, 2008

 

Ernest S

Linux/MySQL System Architect (RHCE, CMDBA)

see all my answers

Best Answers in: Mentoring (2), Web Development (2), Travel Tools (1), Certification and Licenses (1), Business Development (1), Product Design (1), Career Management (1), Databases (1), Software Development (1)

Here's a simple one. Let's say that you want your URLs to be no more than 8 characters, so that your resulting URL would look like this:

http://www.example.com/ABCDEFGH

You have a total of 63 characters to work with for each space: 0-9, A-Z, and a-z. That gives you a total of 63^8 =248,155,780,267,521
possible URLS - which is probably more than you'll ever need.

Anyway, start at 00000000, and when the request for a long URL comes in, look it up in a table, and if it's there, return whatever short URL you assigned to it. If it isn't, simply map it to the next available short URL in the sequence, and store the key-value pair for future use. When you hit zzzzzzzz, odds are that it's safe to start reusing, so you can wrap around back to 00000000.

This is, of course, potentially a very disk-space intensive operation (a fully-populated database of URL maps could end up taking up a terabyte or two on disk), so you might want to scale down to 5 or 6 characters, depending on what you think your needs are going to be and how much disk you've got available.

posted March 30, 2008

 

Eve D

Founder of Crowdfunding.co.za

see all my answers

Eve D suggests this expert on this topic:

Justin has developed his own version of tinyurl, for the South African audience. www.tinylink.co.za

posted March 31, 2008