add DFS search code for pattern-matching