part of dfs: memory unefficient, needs correction altogether with the graph representation