Greetings wise brothers.

I have been given a list of about 50_000 subnets, and asked to find which if any overlap. The largest is probably a /8, I don't know how small the smallest is, but I know that it must be at least a /24. I am looking for advice on an algorithm to use.

Given the large number of subnets to examine, a pairwise comparison between every pair in the list is clearly impractical.

The best algorithm I can think of, is to represent each subnet in a database as a string representing the known bits, (so 192.168.0.0/16 would be represented as the 16 known bits: 1100000010101000), and then sort by the mask and do substring searches in the database

Another idea I had was to construct a tree of up to 32 levels, and populate it with the networks according to their bit patterns. If when inserting a network I find the space taken, I would have found an overlap.

Any other ideas? Is there a standard way of doing this that google has somehow not found for me?


In reply to Algorithom to find overlaping subnets (Internet IPv4) by chrestomanci

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.