the dfs class is based on the template method pattern (see section )which describes generic computation mechanism that can be specialized by redefining certain steps the way we identify vertices and edges that have already been visited during the traversal is in calls to methods isvisitedvisitand unvisit for us to do anything interestingwe must extend dfs and redefine some of its auxiliary methods this approach conforms to the template method pattern in code fragments through we illustrate some applications of dfs traversal class connectivitydfs (code fragment tests whether the graph is connected it counts the vertices reachable by dfs traversal starting at vertex and compares this number with the total number of vertices of the graph code fragment specialization of class dfs to test if graph is connected class componentsdfs (code fragment finds the connected components of graph it labels each vertex with its connected component numberusing the decorator patternand returns the number of connected components found code fragment specialization of dfs to compute connected components