Actually I was turning an aciclic undirected graph into a spanning tree,
But you weren't.
Firstly, the data you started with was not acyclic (the edges 1-2, 1-4, and 2-4 composed a cycle) and you treated it as if it were directed by paying special attention to the nodes on the right side of your input edges. Secondly and more importantly, your algorithm won't guarantee that the resulting graph is connected or acyclic, the two requirements for a spanning tree.
so removing 2 4 was the correct approach.
If the nodes had been listed as 2-1, 1-4, 4-2 the cycle would not have been eliminated.
The code I posted works correctly for me, with that and some help I got on the CB, I went from 80+ minutes of run time, to 15.
I'm glad you got an answer that worked for you. Certainly the code you posted was not very efficient. But efficiency is meaningless without correctness and your statement of the problem leaves some question about the solutions.
Maybe you could summarize the help you got on the CB for those monks who may stumble upon this node while seeking enlightenment on their own graph related problems.
Good luck!
-sauoq
"My two cents aren't worth a dime.";
| [reply] |