CS graph traversal algorithm