Assignment 5 Debrief - PowerPoint PPT Presentation

About This Presentation
Title:

Assignment 5 Debrief

Description:

bunny funny funky funks finks fines tines tiles tiler tiger. Bunny to Tiger? ... Visualize cat' Visualizing (c, a, t) c. a. t (c, a, t) Visualizing (c, a, t) c ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 20
Provided by: csF2
Learn more at: http://www.cs.fsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Assignment 5 Debrief


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

2
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

3
Bunny to Tiger?
4
Brute 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

5
Brute 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

6
Adjacency Graph Approach
  • Idea go through only words that are one
    character away

dear
bear, fear,.., year, dear, dead
dear, fear, year, boar, beer, beadbeap

7
Adjacency 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

8
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)

9
Building 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

10
Building 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)

11
Can We Do Better?
  • O(n) speed?

12
Visualizing the Solution Space
  • Try a simpler case
  • Three-letter words
  • Visualize cat

13
Visualizing (c, a, t)
a
c
(c, a, t)
t
14
Visualizing (c, a, t)
a
c
r
(c, a, t)
(r, a, t)
t
15
Visualizing (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
16
Collapsing 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

17
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

18
Modified 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)

19
BFS Complexity
  • Speed complexity O(n edges)
  • Since each word is visited at most once
  • Storage complexity O(n)
Write a Comment
User Comments (0)
About PowerShow.com