

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Computer science traversal notes
Typology: Study notes
1 / 2
This page cannot be seen from the preview
Don't miss anything!


Depth first (uses stack - last in, first out) Start at root, follow one branch as far as it goes until the end of branch then backtrack Breath-first (uses queue) Start at root, scan every node connected and then scan left to right The path you take doesn't matter as long as you start at the root for both depth and breath 1)Example of Depth first using stack A---B stack stack stack | | -- -- -- | | -- (-->) -- (-->) E (-->) we reached the end of the node E which isnt connected to anything else C E -- B B so we back track and take pop out the top letters untill we find another route A A A Stack stack
-- (-->) -- (-->) we found a new route/node connected to A so we stack again B -- A A Stack -- -- (-->) C then A is popped and stack is empty, so we finished
Eg2) The algorithm goes from root 1 then goes to 2 then new route 3 then goes up to 5 then it backtracks from 5 to 4,2,1 to try find any new routes if it doesn't find new routes it is marked as completely discovered 1--2--4-- | 3 PROCEDURE DFS(v, endV) discovered[v] True IF v = endV THEN found True FOR each Neighbour u of v IF discovered[u] = False THEN DFS(u, endV) ENDFOR completelyExplored[v] True END PROCEDURE