assignment 5 debrief andy wang data structures, algorithms, and generic programming
TRANSCRIPT
![Page 1: Assignment 5 Debrief Andy Wang Data Structures, Algorithms, and Generic Programming](https://reader036.vdocument.in/reader036/viewer/2022082611/56649f005503460f94c1626c/html5/thumbnails/1.jpg)
Assignment 5 Debrief
Andy Wang
Data Structures, Algorithms, and Generic Programming
![Page 2: Assignment 5 Debrief Andy Wang Data Structures, Algorithms, and Generic Programming](https://reader036.vdocument.in/reader036/viewer/2022082611/56649f005503460f94c1626c/html5/thumbnails/2.jpg)
Word 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
![Page 3: Assignment 5 Debrief Andy Wang Data Structures, Algorithms, and Generic Programming](https://reader036.vdocument.in/reader036/viewer/2022082611/56649f005503460f94c1626c/html5/thumbnails/3.jpg)
Bunny to Tiger?
![Page 4: Assignment 5 Debrief Andy Wang Data Structures, Algorithms, and Generic Programming](https://reader036.vdocument.in/reader036/viewer/2022082611/56649f005503460f94c1626c/html5/thumbnails/4.jpg)
Brute Force Approachstart
atart..ztart, saart..smart..szart, start..stzrt, staat..stazt, stara..starz
amart..zmart, saart..szart, smart..smzrt, smaat..smazt, smara..smarz
…
n = number of words in a dictionary
Speed complexity: O(ad), a = 5*26
Space complexity: O(n)
d
![Page 5: Assignment 5 Debrief Andy Wang Data Structures, Algorithms, and Generic Programming](https://reader036.vdocument.in/reader036/viewer/2022082611/56649f005503460f94c1626c/html5/thumbnails/5.jpg)
Brute Force Approach
Speed complexity: (26*5)d
Suppose d = 4, we need 300,000,000 steps
Space complexity: 5 char/word * 5000 words = 25,000 characters
![Page 6: Assignment 5 Debrief Andy Wang Data Structures, Algorithms, and Generic Programming](https://reader036.vdocument.in/reader036/viewer/2022082611/56649f005503460f94c1626c/html5/thumbnails/6.jpg)
Adjacency Graph Approach
Idea: go through only words that are one character away
dear
bear, fear,.., year, dear, dead
dear, fear, …year, boar, beer, bead…beap
…
![Page 7: Assignment 5 Debrief Andy Wang Data Structures, Algorithms, and Generic Programming](https://reader036.vdocument.in/reader036/viewer/2022082611/56649f005503460f94c1626c/html5/thumbnails/7.jpg)
Adjacency Graph Approach
Need to build an adjacency graphbear: dear, fear, …year, boar, beer, bead…beap
dear: bear, fear,.., year, dear, dead
Need to avoid revisiting the same words
Do a BFS to find the shortest path
![Page 8: Assignment 5 Debrief Andy Wang Data Structures, Algorithms, and Generic Programming](https://reader036.vdocument.in/reader036/viewer/2022082611/56649f005503460f94c1626c/html5/thumbnails/8.jpg)
Building 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)
![Page 9: Assignment 5 Debrief Andy Wang Data Structures, Algorithms, and Generic Programming](https://reader036.vdocument.in/reader036/viewer/2022082611/56649f005503460f94c1626c/html5/thumbnails/9.jpg)
Building the Adjacency Graph
Speed complexity5 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
![Page 10: Assignment 5 Debrief Andy Wang Data Structures, Algorithms, and Generic Programming](https://reader036.vdocument.in/reader036/viewer/2022082611/56649f005503460f94c1626c/html5/thumbnails/10.jpg)
Building the Adjacency Graph
Space complexityIf 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)
![Page 11: Assignment 5 Debrief Andy Wang Data Structures, Algorithms, and Generic Programming](https://reader036.vdocument.in/reader036/viewer/2022082611/56649f005503460f94c1626c/html5/thumbnails/11.jpg)
Can We Do Better?
O(n) speed?
![Page 12: Assignment 5 Debrief Andy Wang Data Structures, Algorithms, and Generic Programming](https://reader036.vdocument.in/reader036/viewer/2022082611/56649f005503460f94c1626c/html5/thumbnails/12.jpg)
Visualizing the Solution Space
Try a simpler case
Three-letter words
Visualize ‘cat’
![Page 13: Assignment 5 Debrief Andy Wang Data Structures, Algorithms, and Generic Programming](https://reader036.vdocument.in/reader036/viewer/2022082611/56649f005503460f94c1626c/html5/thumbnails/13.jpg)
Visualizing (c, a, t)
c
a
t
(c, a, t)
![Page 14: Assignment 5 Debrief Andy Wang Data Structures, Algorithms, and Generic Programming](https://reader036.vdocument.in/reader036/viewer/2022082611/56649f005503460f94c1626c/html5/thumbnails/14.jpg)
Visualizing (c, a, t)
c
a
t
(c, a, t)
r
(r, a, t)
![Page 15: Assignment 5 Debrief Andy Wang Data Structures, Algorithms, and Generic Programming](https://reader036.vdocument.in/reader036/viewer/2022082611/56649f005503460f94c1626c/html5/thumbnails/15.jpg)
Visualizing (c, a, t)
a
t
(c, a, t)(r, a, t)
You can make words that are one character apart by collapsing one of the dimensions
(b, a, t)
![Page 16: Assignment 5 Debrief Andy Wang Data Structures, Algorithms, and Generic Programming](https://reader036.vdocument.in/reader036/viewer/2022082611/56649f005503460f94c1626c/html5/thumbnails/16.jpg)
Collapsing the Solution Space?
Idea: Use hashing
Create map<string, set<string>>
For cat, hash the following*at
c*t
ca*
After processing bat, cat, and ratmap[“*at”] will contain bat, cat, and rat
![Page 17: Assignment 5 Debrief Andy Wang Data Structures, Algorithms, and Generic Programming](https://reader036.vdocument.in/reader036/viewer/2022082611/56649f005503460f94c1626c/html5/thumbnails/17.jpg)
Hash-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
![Page 18: Assignment 5 Debrief Andy Wang Data Structures, Algorithms, and Generic Programming](https://reader036.vdocument.in/reader036/viewer/2022082611/56649f005503460f94c1626c/html5/thumbnails/18.jpg)
Modified BFS
Each word expands into several lists
dear
(bear, fear,.., year, dear, dead)
*ear, b*ar, be*r, bea*(dear, fear, …year, boar, beer, bead…beap)
…
*ear, d*ar, de*r, dea*
![Page 19: Assignment 5 Debrief Andy Wang Data Structures, Algorithms, and Generic Programming](https://reader036.vdocument.in/reader036/viewer/2022082611/56649f005503460f94c1626c/html5/thumbnails/19.jpg)
BFS Complexity
Speed complexity: O(n + edges)Since each word is visited at most once
Storage complexity: O(n)