Computer science traversal notes, Study notes of Computer science

Computer science traversal notes

Typology: Study notes

2023/2024

Uploaded on 12/17/2024

fahad-14
fahad-14 🇬🇧

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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
pf2

Partial preview of the text

Download Computer science traversal notes and more Study notes Computer science in PDF only on Docsity!

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

C

A

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