I have an unweighted directed graph that may or may not have cycles in it. Now given a set of nodes, I need to return a graph that has the given nodes and the minimum amount of nodes such that they are connected. I cannot create new edges, so I need to use the existing ones.
Hopefully these pictures makes it clear. Starting with a graph,

Lets say we want the graph that has nodes c,f, and g, the function would return this graph

However, there is one more condition. Each of the edges has a boolean variable called required. If this is set to true, this edge and the corresponding nodes also need to be included in the graph.
Here is another picture to illustrate
The black edges are not required, but the red one is required
Let’s say we are given a and c as the nodes to include.

So instead of returning a graph that is A->C,
it will return this

To make the point even clearer, if we wanted a graph with b and c, it would also return that graph, because that edge is required.
How does one go about returning this graph? I would prefer not to have a graph with cycles returned, but I realize that isn’t always possible because the edges of a cycle might all be marked as required.
My initial thought was to make a copy of the graph keeping on the required edges and then attempt to piece the disjoint graphs back together. But during all my attempts to piece the graphs back together, I was able to find a counter example showing that wouldn’t be the minimal graph.
Does your intended output in the first example include the node e, from which all others are reachable? Or do you mean weak connectedness (meaning that the direction of the edges does not matter)? I guess the second is the case (judging from your A, B, C example), and then you can
Is that what you wanted?