Depth-first search: Difference between revisions

From Algowiki
Jump to navigation Jump to search
Line 7: Line 7:


==== DFS(''G'') ====
==== DFS(''G'') ====
:#'''for''' each vertex ''u'' &isin; ''V'' [''G''] <br/>
:'''for''' each vertex ''u'' &isin; ''V'' [''G'']  
::#'''do''' color['''u'''] &larr; WHITE <br/>
::'''do''' color['''u'''] &larr; WHITE
3<br/>
::: ''&pi;''[''u''] &larr; NIL
4<br/>
:''time'' &larr; 0
5<br/>
:: '''do if''' ''color''[''u''] == WHITE
6<br/>
::: '''then''' DFS-VISiT(''u'')
 
====DFS-VISIT(''u'')====
: ''color''[''u''] &larr; GRAY 
: ''time'' &larr; ''time'' + 1
: ''d''[''u''] &larr; ''time''
: '''for''' each ''v'' &isin; ''Adj''[''u'']

Revision as of 17:03, 14 August 2014

Definition

Pseudocode

DFS(G)

for each vertex uV [G]
do color[u] ← WHITE
π[u] ← NIL
time ← 0
do if color[u] == WHITE
then DFS-VISiT(u)

DFS-VISIT(u)

color[u] ← GRAY
timetime + 1
d[u] ← time
for each vAdj[u]