0

I try to explain my problem better.

in input I have the number of nodes N and the number of edges P.

N represent the cities while P the water pipes.

So for example in input I have 4 and 2 {0,1,2,3,4} would be my graph and the 2 represents me the already existing water pipes

since in the following lines of the file I will have the already existing connections so since in this case I have the 2 for example in the file I'll have 3-1 and 2-1. so at the beginning I will have a graph with 5 vertices and an edge that goes from node 3 to node 1 and another arc that goes from 2 to 1.

now I have to determine how many and which connections to make to bring water to all the cities. so for example in this case a solution could be 0-4,0-3,0-2.

NOTE 0 represents the dam basin.

I was thinking that this problem seems to me very similar to MST but the graph I have is oriented and not weighted so I cannot apply the Prim or Kruskal algorithm. So since it doesn't make sense to apply MST maybe I could do this via a DFS or a BFS but I don't quite understand how.

I believe that if I do a dfs from the source then from node 0 and I see all the nodes that I have not been able to reach and I connect these nodes to the source I have the solution but I have not understood how to do it.

or would it be better to try to make l mst work on this type of graph then cloning it and adding weights = 1 to the arcs?

mimmolg99
  • 17
  • 7
  • What would it mean to find a spanning tree of a directed graph? – kaya3 Oct 23 '22 at 15:19
  • I have to find the minimum number of edges to make each node of the graph connected – mimmolg99 Oct 23 '22 at 16:07
  • To clarify what you're asking: is the idea to start with a directed graph and then *remove as many* edges as possible while keeping things connected, or is the idea to *add as few* edges as possible so that the result is connected? – templatetypedef Oct 23 '22 at 19:31
  • What kind of "connected"? Do you want every node to be reachable from every other, or do you want there to be *some* node which can reach every other, or some node which can be reached from every other, or do you want it so that an undirected graph with the same edges would be connected? If it's the last one, then you can ignore the fact that the graph is directed in the first place; if it's the first one, then the result will not be a tree. – kaya3 Oct 24 '22 at 00:31
  • @templatetypedef add as few edges as possible so that the result is connected – mimmolg99 Oct 24 '22 at 07:13
  • @kaya3 I need every node to be reachable from every other node – mimmolg99 Oct 24 '22 at 07:15
  • Then it won't be a tree. Trees are acyclic, and if you can get from node A to node B and then back again, your graph has cycles. – kaya3 Oct 24 '22 at 07:48
  • @kaya3 sorry maybe I didn't make myself clear. I have a source node and I have to make sure that from this source node I can reach all the nodes of the graph with the minimum number of edges, I am not interested in going back. – mimmolg99 Oct 24 '22 at 08:11
  • Then a single-source shortest-path algorithm will give you what you want, e.g. breadth-first search since your graph is unweighted. The number of edges in the result will be fixed since all trees on n nodes have n-1 edges anyway, so there is nothing to minimise in that regard. – kaya3 Oct 24 '22 at 08:14
  • @kaya3 yes but if the graph is disconnected with a simple visit it doesn't seem to work. Because if for example I have the following vertices 0,1,2,3,4 and at the beginning I have the connection between 3-> 1 and 2-> 1 now I definitely need 3 edges because in order to reach each node I have to have the connection between 0-> 4 0-> 3 0-> 2 or 0-> 4 0-> 3 3-> 2 or 0-> 4 0-> 2 2-> 3. I just need to find one of these configurations. – mimmolg99 Oct 24 '22 at 08:27
  • If the graph is disconnected then how can you possibly have a spanning tree? – kaya3 Oct 24 '22 at 08:28
  • @kaya3 because I had thought to connect all the nodes and then find the min spanning tree but I cannot find it because the graph I have to work on is not weighted and is direct. Do you have any other ideas? – mimmolg99 Oct 24 '22 at 08:33
  • Honestly I cannot tell what you are trying to do. You're giving contradictory information about the problem you're trying to solve. – kaya3 Oct 24 '22 at 08:37
  • @kaya3 then I try to explain myself better. I have a direct and unweighted graph, and as input I have the number of nodes and the number of arcs for example the number of nodes {0,1,2,3,4} so 4 and if I already have the following vertices connected {3-> 1} and {2-> 1} then I'll have 2. Now in the output I need to determine the minimum number and which edges to build in order to bring water to all vertices of the graph. so I should have the following connections {0-> 4} {0-> 3} {0-> 2} or the others I wrote above. – mimmolg99 Oct 24 '22 at 08:55
  • @mimmolg99 please edit the question to add additional information, user shouldn't have to read all comment to get all informations. – Alois Christen Oct 25 '22 at 08:36
  • @AloisChristen i've done – mimmolg99 Oct 25 '22 at 09:07

1 Answers1

0

" I know I can't apply it for the type of graph I have"

How do you know this?

  1. Unweighted. Make your graph weighted by applying a weight of 1 to every edge.

  2. Undirected. Make your graph directed by replacing every edge with two directed edges, one going each way.

ravenspoint
  • 19,093
  • 6
  • 57
  • 103