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.";

In reply to Re: Re: Re: H of A optimization by sauoq
in thread H of A optimization by abitkin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.