Title: Assignment 5 Debrief
1Assignment 5 Debrief
- Andy Wang
- Data Structures, Algorithms, and Generic
Programming
2Word Ladder Game
- Idea Find a way to transform one word to
another through words that are one character away - Example bunny ? tiger
- bunny ? funny ? funky ? funks ? finks ? fines ?
tines ? tiles ? tiler ? tiger
3Bunny to Tiger?
4Brute Force Approach
start
- n number of words in a dictionary
- Speed complexity O(ad), a 526
- Space complexity O(n)
atart..ztart, saart..smart..szart, start..stzrt,
staat..stazt, stara..starz
d
amart..zmart, saart..szart, smart..smzrt,
smaat..smazt, smara..smarz
5Brute Force Approach
- Speed complexity (265)d
- Suppose d 4, we need 300,000,000 steps
- Space complexity
- 5 char/word 5000 words 25,000 characters
6Adjacency Graph Approach
- Idea go through only words that are one
character away
dear
bear, fear,.., year, dear, dead
dear, fear, year, boar, beer, beadbeap
7Adjacency Graph Approach
- Need to build an adjacency graph
- bear dear, fear, year, boar, beer, beadbeap
- dear bear, fear,.., year, dear, dead
- Need to avoid revisiting the same words
- Do a BFS to find the shortest path
8Building the Adjacency Graph
- For each word, do pair-wise comparisons against
all words - If a word is one character away, append to its
list - n number of words in a dictionary
- Speed complexity O(n2)
- Space complexity O(n2)
9Building the Adjacency Graph
- Speed complexity
- 5 comparisons to determine a word is one
character away - For each word, it needs to perform 5
comparisons/word 5,000 words 25,000
comparisons - For 5,000 words, we need 125,000,000 comparisons
- best case average case worst case
10Building the Adjacency Graph
- Space complexity
- If every word is one character away from every
word in a dictionary - We need 5 char/word 5,000 words 5,000 words
125,000,000 characters (worst case) - Average case 130,000 characters (from
empirical measurements)
11Can We Do Better?
12Visualizing the Solution Space
- Try a simpler case
- Three-letter words
- Visualize cat
13Visualizing (c, a, t)
a
c
(c, a, t)
t
14Visualizing (c, a, t)
a
c
r
(c, a, t)
(r, a, t)
t
15Visualizing (c, a, t)
- You can make words that are one character apart
by collapsing one of the dimensions
(b, a, t)
(r, a, t)
(c, a, t)
a
t
16Collapsing the Solution Space?
- Idea Use hashing
- Create mapltstring, setltstringgtgt
- For cat, hash the following
- at
- ct
- ca
- After processing bat, cat, and rat
- mapat will contain bat, cat, and rat
17Hash-Based Graph Construction
- Speed complexity O(n)
- 5 char/word 5000 words 25,000 hashing
operations - Space complexity O(n)
- 5 char/word 5000 words 25,000 characters
18Modified BFS
- Each word expands into several lists
dear
ear, dar, der, dea
(bear, fear,.., year, dear, dead)
ear, bar, ber, bea (dear, fear, year, boar,
beer, beadbeap)
19BFS Complexity
- Speed complexity O(n edges)
- Since each word is visited at most once
- Storage complexity O(n)