Optimized ToGraph to avoid storing local hash of visited nodes