Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hello Holy Ones.
I hope you can help me out here. I've had a few brains scratching over this one, and while there are some solutions, none of them are very good.

I've got a file where each line represents one element, and the entries on the line are the matches to that element. The number of matches is arbitrary. I need to "network" those matches to find a non-redundant set of elements based on at least one element in common between lines. Here's an example:

Infile:
a b c d e
f b g
h i j k l
m f

I want to say that because lines 1 and 2 both contain "b", 1=2, and because lines 2 and 4 contain f, 2=4. Using the concept of if a=b and b=c then a=c, we can say that (1, 2, 4) is one complete set. Line 3 has no matches in common to any of the other lines, and so a second set is (3)

In the end I need a count of these sets.

I know to read the lines into a hash - the keys are the line numbers (for lack of something better) and the values are an array of the elements. I was thinking along the lines of needed to traverse the hash in an outer loop to grab an entry, then an inner loop traverse the hash once more looking for matches. When I find a match I can merge the values between the two hash entries and delete the inner-loop entry.

However, if I do this, the new value defined in the outer-loop hash entry will not be fully traversed, and I will run into problems there.

I have a solution that works in a 2-pass system where the first pass records pairs of line numbers with matches and the second pass networks those pairs. However, it's dead slow and while my little example is, well, little... I have large datasets to handle (think 200,000 lines with up to 10 matches per line!)

Any insights?

Thanks!


In reply to Building Networks of Matches by bowsie

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-03-29 12:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found