0% found this document useful (0 votes)
360 views1 page

DFS & Find A Cycle: Algorithm Depth - First - Search (G, V)

This document describes three graph algorithms: 1. Depth-first search (DFS) which marks vertices as they are visited and explores edges connecting to unmarked vertices recursively. 2. Find_a_Cycle which uses DFS to search a directed graph and returns true if a cycle is found, false otherwise. It tracks vertices "on the path" to detect cycles. 3. Connected_components which uses DFS to label connected components of an undirected graph with unique component numbers. It runs DFS on any unmarked vertex to fully explore each component.

Uploaded by

Moncef Mhasni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
360 views1 page

DFS & Find A Cycle: Algorithm Depth - First - Search (G, V)

This document describes three graph algorithms: 1. Depth-first search (DFS) which marks vertices as they are visited and explores edges connecting to unmarked vertices recursively. 2. Find_a_Cycle which uses DFS to search a directed graph and returns true if a cycle is found, false otherwise. It tracks vertices "on the path" to detect cycles. 3. Connected_components which uses DFS to label connected components of an undirected graph with unique component numbers. It runs DFS on any unmarked vertex to fully explore each component.

Uploaded by

Moncef Mhasni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 1

DFS & Find a cycle

Algorithm Depth_first_Search (G, v); Input: G=(V,E) (an undirected connected graph), and v (a vertex og G); Output: Begin Mark v; Per or! pre"ork on v; #pre"ork depend$ on the appication o %&'( &or a)) edge$ (v,w) do I w i$ un!arked then %epth* ir$t*'earch (G, w); Per or! post"ork or (v,w); #po$t"ork depend$ on the app)ication o %&'; it i$ $o!eti!e$ per or!ed on)+ on edge$ )eading to ne")+ !arked vertice$;( End Algorithm Find_a_Cycle (G,v); Input: G=(V,E) (an directed graph), Output: &ind*a*-+)c)e (tr e if G contains a cycle and a)$e other"i$e), Begin Mark v; v,on*the*path:=true; ..pre"ork #x,on*the*path i$ true i x i$ on the path ro! the root to current vertex( #initia))+ x,on*the*path:= a)$e or a)) vertice$, and &ind*a*-+c)e i$ a)$e( &or a)) edge$ (v,w) do I w i$ un!arked then &ind*a*-+c)e (G, w); I w,on*the*path then &ind*a*-+c)e:=tr e; ha)t; ..po$t"ork I w i$ the )a$t vertex on v$ )i$t then v,on*the*path:=false; ..po$t"ork End /)gorith! Connected_components (G); Input: G=(V,E) (an undirected graph), Output: v!component i$ $et the n m"er o the co!ponent containing v; or ever+ vertex v, Begin -o!ponent*0u!1er:=2; 3hi)e there i$ an un!arked vertex v do %epth*&ir$t*'earch (G,v); (u$ing the o))o"ing pre"ork: v!Component#$Component_% m"er;) -o!ponent*0u!1er:= -o!ponent*0u!1er42; end

You might also like