Python
Since I couldn't golf dijkstra codes to compress to few hundreds bytes, here is ungolfed version of mine.
import sys, heapq, time
# dijkstra algorithm from
# http://code.activestate.com/recipes/119466-dijkstras-algorithm-for-shortest-paths/
def dijkstra(G, start, end):
def flatten(L):
while len(L) > 0:
yield L[0]
L = L[1]
q = [(0, start, ())]
visited = set()
while True:
(cost, v1, path) = heapq.heappop(q)
if v1 not in visited:
visited.add(v1)
if v1 == end:
return list(flatten(path))[::-1] + [v1]
path = (v1, path)
for (v2, cost2) in G[v1].iteritems():
if v2 not in visited:
heapq.heappush(q, (cost + cost2, v2, path))
nodes = tuple(sys.argv[1:])
print "Generating connections,",
current_time = time.time()
words = set(x for x in open("websters-dictionary", "rb").read().lower().split() if 3 <= len(x) <= max(5, *[len(l)+1 for l in nodes]))
print len(words), "nodes found"
def error():
sys.exit("Unreachable Route between '%s' and '%s'" % nodes)
if not all(node in words for node in nodes):
error()
# following codes are modified version of
# http://norvig.com/spell-correct.html
alphabet = 'abcdefghijklmnopqrstuvwxyz'
def edits(word):
splits = [(word[:i], word[i:]) for i in range(len(word) + 1)]
deletes = [a + b[1:] for a, b in splits if b]
replaces = [a + c + b[1:] for a, b in splits for c in alphabet if b]
prepends = [c+word for c in alphabet]
appends = [word+c for c in alphabet]
return words & set(deletes + replaces + prepends + appends)
# Generate connections between nodes to pass to dijkstra algorithm
G = dict((x, dict((y, 1) for y in edits(x))) for x in words)
print "All connections generated, %0.2fs taken" % (time.time() - current_time)
current_time = time.time()
try:
route = dijkstra(G, *nodes)
print '\n'.join(route)
print "%d hops, %0.2fs taken to search shortest path between '%s' & '%s'" % (len(route), time.time() - current_time, nodes[0], nodes[1])
except IndexError:
error()
Tests
$ python codegolf-693.py post shot
Generating connections, 15930 nodes found
All connections generated, 2.09s taken
post
host
hot
shot
4 hops, 0.04s taken to search shortest path between 'post' & 'shot'
$ python codegolf-693.py zink silicon
Generating connections, 86565 nodes found
All connections generated, 13.91s taken
zink
pink
pank
pani
panic
pinic
sinic
sinico
silico
silicon
10 hops, 0.75s taken to search shortest path between 'zink' & 'silicon'
Added user300's Tests
$ python codegolf-693.py vas arm
Generating connections, 15930 nodes found
All connections generated, 2.06s taken
vas
bas
bam
aam
arm
5 hops, 0.07s taken to search shortest path between 'vas' & 'arm'
$ python codegolf-693.py oxy pom
Generating connections, 15930 nodes found
All connections generated, 2.05s taken
oxy
poxy
pox
pom
4 hops, 0.01s taken to search shortest path between 'oxy' & 'pom'
$ python codegolf-693.py regatta gyrally
Generating connections, 86565 nodes found
All connections generated, 13.95s taken
Unreachable Route between 'regatta' and 'gyrally'
Some more
$ python codegolf-693.py gap shrend
Generating connections, 56783 nodes found
All connections generated, 8.16s taken
gap
rap
crap
craw
crew
screw
shrew
shrewd
shrend
9 hops, 0.67s taken to search shortest path between 'gap' & 'shrend'
$ python codegolf-693.py guester overturn
Generating connections, 118828 nodes found
All connections generated, 19.63s taken
guester
guesten
gesten
geste
gest
gast
east
ease
erse
verse
verset
overset
oversee
overseed
oversend
oversand
overhand
overhard
overcard
overcare
overtare
overture
overturn
23 hops, 0.82s taken to search shortest path between 'guester' & 'overturn'
may be "post->pot->hot->shot" is shorter. – YOU – 2011-02-08T09:21:08.660
@S.Mark: Then your algorithm beats mine and you win. The above is an example of a possible solution. A shorter solution beats a longer solution. – Martin York – 2011-02-08T09:24:38.010
intentionally? sorry, I just got it wrong. – YOU – 2011-02-08T09:25:45.783
2
Could I solve it in Whitespace for 0 program size?
– None – 2011-02-08T12:12:02.517@Tim Nordenfur: I would love to see a white space implementation. Note. there are two rules before program length to decide winner. But if you meet those requirements :-) – Martin York – 2011-02-08T15:56:17.810
What format is that dictionary in? I gunzip'd it but it is still gobbledygook. – Keith Randall – 2011-02-08T16:51:27.130
Ah, I see now. Some idiot decided that compressing it once helped, so why not compress it again? – Keith Randall – 2011-02-08T18:45:10.683
Should the program be able to exit when the end word is not reachable ? (i.e. not run forever) – Arnaud Le Blanc – 2011-02-08T19:43:21.100
@user300: That would be nice. – Martin York – 2011-02-08T20:51:50.620
@Martin, making the dictionary text file to some different format will break the rule? and if not, those prebuilding codes will count towards program code? – YOU – 2011-02-09T01:03:08.507
@ S.Mark: Interested to see where you are going with this. But feel free to change the format of the dictionary (no cost). The dictionary is just to provide a common set of words so that people were not failing because their system/language did not provide an extensive dictionary. – Martin York – 2011-02-09T20:36:38.503
@Martin, ok, I will post tomorrow, but I guess I already failed to golf dijkstra algorithm than 288 char solution :D Anyway, I will post just for reference, since I have no ability to create my own algorithm. – YOU – 2011-02-10T02:36:58.023