lecture 12 breadth first search - umass amherst 241 f19 lecture 12... · 2019. 10. 22. · depth...
TRANSCRIPT
Lecture12BreadthFirstSearch
ECE241– AdvancedProgrammingIFall2019MikeZink
ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink
Overview
1
• BreadthFirstSearch• DepthFirstSearch
ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink
Objective
• Understandandbeabletoapplythebreadthfirstsearch(BFS)algorithm
• Understandandbeabletoapplythedepthfirstsearch(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
ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink
Word Letter Puzzle
4
• Cansolvethisproblemusingagraphalgorithm• Represent relationshipsbetweenwordsasgraph
• Usebreadthfirstsearchalgorithm• Findsefficientpathfromstartingworktoendingword
ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink
Word Letter Puzzle
5
• Firstproblem:howtoturnlargecollectionofwordsintoagraph
• Onlyconnectwordsthatdifferbysingleletter• Ifsuchgraphcanbecreated,anypathfromonewordtoanother isasolution
ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink
Word Letter Puzzle
6
ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink
Tackling the Problem
7
• Letsassume listofwords,allsamelength1. Startingpoint:Createavertexforeverywordinlist2. Compareallwordswitheachother3. Ifdifferentbyoneletter=>createedgebetween
them
ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink
Tackling the Problem
8
Analysis:• Assume listof5,110words• Comparingonewordtoeachotheris≈O(n2)• For5,110wordsthatismorethan26million
comparisons
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
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
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!
ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink
Implementing Breadth First Search
12
• BreadthFirstSearch(BFS)isoneoftheeasiestalgorithmstosearchagraph
• GivenagraphGandstartingvertexsBFSexploresedges inthegraphtofindallverticesforwhichthereisapathfroms.
• Note:BFSfindsallverticesatdistancekfroms,beforeanyverticesatdistancek+1
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
ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink
BFS Example
14
ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink
BFS Example
15
• Startingwithfool,addallnodesadjacenttoit
• Addedasnewnodestoexpand
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
ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink
BFS Example
17
• Nextwordinqueue is”foil”• Onlynewnodethat“foil”canaddis“fail”
• Neitherofnexttwonodesaddanythingnewtoqueueortree
• Figureshowstreeafterexpandingallverticeson2ndlevel
ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink
BFS Example
18
• FinalBFStreeshown• WithBFStree,canstartatanyvertexandfollowpredecessorarrowsbackto
• Findshortestwordladderfromanywordinthetreebacktothestartingvertex
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
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
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
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
ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink
Knights Tour Problem
23
• Eachsquarerepresented asnodeingraph
• Eachlegalmoverepresented byedge
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
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
ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink
Complete Graph
26
• 336edges• Lessconnections forverticesonedgesofboard
• Sparsity:• Fullyconnectedgraph:4096egdes
• Matrixonly8.2%filled
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
ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink
Implementing Knight’s Tour
28
• DFSexplorationofgraphfindspathwithexactly63edges
• Whendeadendisfound(moremovespossible)• Algorithmbacksuptreetonextdeepest vertexallowingalegalmove
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
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)
ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink
DFS – Coloring
31
• SinceDFSisrecursive,usestacktohelpwithbacktracking
• AfterreturnfromknightTour withstatusFalse:• Remaininsidewhileloop• Lookatnextvetex innbrlist
ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink
Simple Example
32
• Followingfiguresshowstepsofsearch• AssumegetConnections ordersnodesinalphabeticalorder
• StartwithcallingknightTour(0,path,A,6)
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
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
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)
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
ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink 37
Simple Example• Complete touraround8x8board
ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink
Next Steps
38
• NextlectureonThursday: BreadthFirstSearch• Nextdiscussion onToday:GraphsandBFS• Project1duenextMonday,10/28at11PM
ECE 241 – Data Structures Fall 2018 © 2018 Mike Zink39