assignment 5 debrief andy wang data structures, algorithms, and generic programming

19
Assignment 5 Debrief Andy Wang Data Structures, Algorithms, and Generic Programming

Upload: melina-sullivan

Post on 03-Jan-2016

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Assignment 5 Debrief Andy Wang Data Structures, Algorithms, and Generic Programming

Assignment 5 Debrief

Andy Wang

Data Structures, Algorithms, and Generic Programming

Page 2: Assignment 5 Debrief Andy Wang Data Structures, Algorithms, and Generic Programming

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

Bunny to Tiger?

Page 4: Assignment 5 Debrief Andy Wang Data Structures, Algorithms, and Generic Programming

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

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

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

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

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

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

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

Can We Do Better?

O(n) speed?

Page 12: Assignment 5 Debrief Andy Wang Data Structures, Algorithms, and Generic Programming

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

Visualizing (c, a, t)

c

a

t

(c, a, t)

Page 14: Assignment 5 Debrief Andy Wang Data Structures, Algorithms, and Generic Programming

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

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

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

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

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

BFS Complexity

Speed complexity: O(n + edges)Since each word is visited at most once

Storage complexity: O(n)