lecture 12 breadth first search - umass amherst 241 f19 lecture 12... · 2019. 10. 22. · depth...

40
Lecture 12 Breadth First Search ECE 241 – Advanced Programming I Fall 2019 Mike Zink

Upload: others

Post on 27-Sep-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

Lecture12BreadthFirstSearch

ECE241– AdvancedProgrammingIFall2019MikeZink

Page 2: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Overview

1

• BreadthFirstSearch• DepthFirstSearch

Page 3: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Objective

• Understandandbeabletoapplythebreadthfirstsearch(BFS)algorithm

• Understandandbeabletoapplythedepthfirstsearch(DFS)algorithm

Page 4: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Word Letter Puzzle

3

• Goal:Transformword“FOOL”into”SAGE”• Changeoneletteratatime• Ateachstep:transform onewordintoanother

FOOLPOOLPOLLPOLEPALESALESAGE

Page 5: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Word Letter Puzzle

4

• Cansolvethisproblemusingagraphalgorithm• Represent relationshipsbetweenwordsasgraph

• Usebreadthfirstsearchalgorithm• Findsefficientpathfromstartingworktoendingword

Page 6: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Word Letter Puzzle

5

• Firstproblem:howtoturnlargecollectionofwordsintoagraph

• Onlyconnectwordsthatdifferbysingleletter• Ifsuchgraphcanbecreated,anypathfromonewordtoanother isasolution

Page 7: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Word Letter Puzzle

6

Page 8: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Tackling the Problem

7

• Letsassume listofwords,allsamelength1. Startingpoint:Createavertexforeverywordinlist2. Compareallwordswitheachother3. Ifdifferentbyoneletter=>createedgebetween

them

Page 9: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Tackling the Problem

8

Analysis:• Assume listof5,110words• Comparingonewordtoeachotheris≈O(n2)• For5,110wordsthatismorethan26million

comparisons

Page 10: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Improved Approach

9

• Huge#ofbucket• Eachwith4-letterwordontop• Oneletteriswildcard“_”• Example:“POPE”and”POPS”match“POP_”

• Whenmatchingbucket isfound, addword• Onceallwords inrightbucket=>mustbeconnected ingraph

Page 11: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Building the Graph

10

from pythonds.graphs import Graph

def buildGraph(wordFile): d = {} g = Graph()

wfile = open(wordFile,'r')# create buckets of words that differ by one letterfor line in wfile:

word = line[:-1] for i in range(len(word)):

bucket = word[:i] + '_' + word[i+1:] if bucket in d: d[bucket].append(word) else: d[bucket] = [word]

# add vertices and edges for words in the same bucketfor bucket in d.keys():

for word1 in d[bucket]: for word2 in d[bucket]: if word1 != word2: g.addEdge(word1,word2)

return g

Page 12: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Sparsity of Matrix

11

Analysis:• 5,110four-letterwords• Adjacencymatrixwouldhave5,1102=26,112,100

cells• GraphcreatedbybuildGraph()has 53,286edges• =>Only.2%ofmatrixcellswouldbefilled!

Page 13: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Implementing Breadth First Search

12

• BreadthFirstSearch(BFS)isoneoftheeasiestalgorithmstosearchagraph

• GivenagraphGandstartingvertexsBFSexploresedges inthegraphtofindallverticesforwhichthereisapathfroms.

• Note:BFSfindsallverticesatdistancekfroms,beforeanyverticesatdistancek+1

Page 14: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Implementing Breadth First Search

13

• TovisualizeBFD,imaginethatitisbuildingonelevelatatime

• BFSaddsallchildrenofthestartingvertex• Thenitbeginstodiscoveranyofthegrandchildren• Tokeeptrackofprogressedgesarecoloredwhitegrayorblack:

• White:undiscoveredvertex• Gray:initiallydiscovered

• Black:vertexiscoloredblackwhencompletelyexpored

Page 15: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

BFS Example

14

Page 16: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

BFS Example

15

• Startingwithfool,addallnodesadjacenttoit

• Addedasnewnodestoexpand

Page 17: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

BFS Example

16

• Removes ”pool” fromfrontofthequeue

• Repeatsprocess for“pool”• When“cool”isexaminedalg.detectsthatitisalreadygrey=>shorterpathtocoolalreadyexists

• “poll”isonlynewnodeadded

Page 18: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

BFS Example

17

• Nextwordinqueue is”foil”• Onlynewnodethat“foil”canaddis“fail”

• Neitherofnexttwonodesaddanythingnewtoqueueortree

• Figureshowstreeafterexpandingallverticeson2ndlevel

Page 19: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

BFS Example

18

• FinalBFStreeshown• WithBFStree,canstartatanyvertexandfollowpredecessorarrowsbackto

• Findshortestwordladderfromanywordinthetreebacktothestartingvertex

Page 20: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Building the Graph

19

def traverse(y): x = y

while (x.getPred()): print(x.getId()) x = x.getPred()

print(x.getId())

traverse(g.getVertex('sage'))

• Function traverse()shows how to follow the predecessor links to print out the word ladder

Page 21: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

BFS - Analysis

20

• Whileloopexecutedatleastonetimepervertex=>O(V)

• LoopnestedwithinwhileloopisatleastexecutedonceforeachedgeingraphO(E)

• Incombination:O(E+V)• Followinglinkfromstartingnodetogoalnode =>O(V)• Plustimerequiredtobuildinitialgraph

Page 22: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Knights Tour Problem

21

• Puzzleplayedonchessboardwithsinglefigure,theknight

• Objective:findsequence ofmovesthatallowknighttovisiteverysquareonboard“exactly”once

• Suchsequence iscalled“tour”• Upperboundonpossible tours is1.35*1035

• Usegraphsearchtosolveproblem

Page 23: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Knights Tour Problem

22

Solveproblembyusingtwomainsteps:

• Represent legal moves of knight on chessboard as graph

• Use a graph algorithm to find path of length rows×columns−1 where every vertex on graph is visited exactly once

Page 24: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Knights Tour Problem

23

• Eachsquarerepresented asnodeingraph

• Eachlegalmoverepresented byedge

Page 25: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Building the Graph

24

from Graph import Graph

def knightGraph(bdSize):ktGraph = Graph()for row in range(bdSize):

for col in range(bdSize):nodeId = posToNodeId(row,col,bdSize)newPositions = genLegalMoves(row,col,bdSize)for e in newPositions:

nid = posToNodeId(e[0],e[1],bdSize) ktGraph.addEdge(nodeId,nid)

return ktGraph

def posToNodeId(row, column, board_size):return (row * board_size) + column

Page 26: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Building the Graph

25

def genLegalMoves(x,y,bdSize): newMoves = [] moveOffsets = [(-1,-2),(-1,2),(-2,-1),(-2,1), ( 1,-2),( 1,2),( 2,-1),( 2,1)]

for i in moveOffsets: newX = x + i[0] newY = y + i[1]

if legalCoord(newX,bdSize) and \ legalCoord(newY,bdSize):

newMoves.append((newX,newY))return newMoves

def legalCoord(x,bdSize):if x >= 0 and x < bdSize:

return True else:

return False

Page 27: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Complete Graph

26

• 336edges• Lessconnections forverticesonedgesofboard

• Sparsity:• Fullyconnectedgraph:4096egdes

• Matrixonly8.2%filled

Page 28: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Depth First Search (DFS)

27

• Solveproblemwidthdepthfirstsearch (DFS)algorithm• Createssearchtreebyexploringonebranchofthetreeasdeeplyaspossible

• Wewilllookattwoalgorithms:1. Directlysolvesproblembyexplicitlyforbiddinga

nodetobevisitedmorethanonce2. Moregeneral,butallowsnodes tobevisitedmore

thanonceasthetreeisconstructed

Page 29: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Implementing Knight’s Tour

28

• DFSexplorationofgraphfindspathwithexactly63edges

• Whendeadendisfound(moremovespossible)• Algorithmbacksuptreetonextdeepest vertexallowingalegalmove

Page 30: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

knighTour - Function

29

from Graph import Graph, Vertex

def knightTour(n,path,u,limit): u.setColor('gray') path.append(u) if n < limit:

nbrList = list(u.getConnections()) i = 0 done = False

while i < len(nbrList) and not done: if nbrList[i].getColor() == 'white': done = knightTour(n+1, path, nbrList[i], limit) i = i + 1

if not done: # prepare to backtrack path.pop() u.setColor('white') else: done = True return done

Page 31: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

DFS – Coloring

30

• DFSusescolorstokeeptrackwhichverticeshavebeenvisited

• White:unvisited• Gray:visited

• Ifneighbors ofparticularvertexhavebeenexplored&&lengthofvertices<64=>deadendreached

• Ifdeadendreached=>backtrack (ReturnfromknightTour wirh false)

Page 32: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

DFS – Coloring

31

• SinceDFSisrecursive,usestacktohelpwithbacktracking

• AfterreturnfromknightTour withstatusFalse:• Remaininsidewhileloop• Lookatnextvetex innbrlist

Page 33: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Simple Example

32

• Followingfiguresshowstepsofsearch• AssumegetConnections ordersnodesinalphabeticalorder

• StartwithcallingknightTour(0,path,A,6)

Page 34: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Simple Example

33

a)

b)

c)

• knightTour starts with node A (a))

• BandDareadjacenttoA• SinceBcomesnextinalphabet, itischosennext(b))

• Recursively callingknightTourexploresB

Page 35: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Simple Example

34

c)

d)

• B is adjacent to C and D• knightTour elects to explore C• C is dead end with no adjacent

white nodes (c))• Change color of C back to white

(d))• Backtracks search to vertex B

Page 36: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Simple Example

35

e)

f)

• Next vertex to explore is D (e))• knightTour makes recursive calls until

we get to node C again (f), g), h))

g)

h)

Page 37: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Simple Example

36

• whenwegettonodeCthetest n < limit fails• =>allnodes ingraphexhausted• return True toindicatethatwehavemadeasuccessfultourofthegraph

• returnthelist, path hasthevalues [A,B,D,E,F,C],whichisthetheorderweneedtotraverse thegraphtovisiteachnodeexactlyonce

Page 38: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink 37

Simple Example• Complete touraround8x8board

Page 39: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink

Next Steps

38

• NextlectureonThursday: BreadthFirstSearch• Nextdiscussion onToday:GraphsandBFS• Project1duenextMonday,10/28at11PM

Page 40: Lecture 12 Breadth First Search - UMass Amherst 241 F19 Lecture 12... · 2019. 10. 22. · Depth First Search (DFS) 27 • Solve problem width depth first search (DFS) algorithm •

ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink39