I'm trying to build a directed graph implementation that I can add and delete nodes/edges from, and I can query whether any node participates in a cycle. By participate I mean that there exists a path from that node back to itself. Right now, whenever I query a single node, I have to run DFS on that node, and if I can reach the node again, then I return. However, I want to expand my query function by allowing for the user to query multiple nodes at once, so I want to use something more sophisticated than simply running DFS multiple times. Does anybody have any suggestions?
To anybody out there, I ended up using Tarjan's Strongly Connected Component algorithm. I kept track of which nodes were visited during each run through of the algorithm, and if I queried a node that was already visited, I would skip that node. Eventually, for a single query, I visited each node in the graph at most once.