This error usually occurs when one deployment project contains the project outputs of a second deployment project, and the second project contains the outputs of the first p
Build a dictionary with all the direct dependencies of each of the inputs. For each of those, add all unique indirect dependencies (e.g. go over each of the dependencies of the given item, and if it doesn't exist for the parent, add it). Repeat as long as you make at least one change to the dictionary. If there's an item that has itself in it's dependencies, it's a cyclical dependency :)
This is relatively inefficient, of course, but it's quite simple and easy to understand. If you were creating a compiler, you would probably just build a directed graph of all the dependencies, and search for paths in that - you can find a lot of ready algorithms for finding a path in a directed graph.