-2

What is a minimal-spanning subnet? How do you find it for a given set of IPv4 addresses?
I know the minimum spanning tree but what is the minimum spanning subnet? and how to find it by the given ipv4 addresses?

  • There is a specific section in Part 2 of [this two-part answer](https://networkengineering.stackexchange.com/a/53994/8499) that explains how to find the **Largest Common Network for Addresses**, often mistakenly called the smallest common network. The smallest network is `0.0.0.0/0` (`0` Network bits), and it is the smallest common network for all network and addresses. The confusion arises because people often look at the number of hosts in a network and confuse that number with the size of the Network. Remember that the larger the Network, the smaller the Host, and vice versa. – Ron Maupin Oct 29 '22 at 21:03

2 Answers2

1
  1. You have to understand that every IPv4 is represented by a 32-bit integer
  2. If this integer is split into 4 parts(subnets), then it is 4 x 8bit = 32 bits
  3. Minimal spanning subnet of a set of IPs means: the number of common bits(most significant, namely, from left->right as in representation of a 32-bit i.e. '128.0.0.0' => 0b10000000 00000000 00000000 00000000), or visually the prefix bits up to before the 1st different one. For example '136.0.0.0' => 0b10001000 00000000 00000000 00000000 has 4 common bits with '128.0.0.0'. The 5th most significant bit is different. Therefore, if we place as most significant bits the following '0b1000-', whatever IP can be derived from that prefix bits, belongs to that minimal spanning subnet.

One idea to find the minimal spanning subnet between a set of IPs, is to XNOR pair by pair all IPs, while at the same time saving with a mask of '1s' mask = 0XFFFFFFFF, the common bits. At the end, you receive the number of common bits, and then extract this number of one sample IP from the set and there you have it.

Below is a solution implemented in Python(assuming you receive a set of IPs in the form of a list of strings), relatively efficient. Time complexity O(n). Space complexity O(1).

# We need the IP in integer format for our operations
def convert_ipv4_string_to_integer(ipv4_address: str) -> int:
    list_temp = ipv4_address.split('.')
    return (int(list_temp[0]) << 24) | (int(list_temp[1]) << 16) | (int(list_temp[2]) << 8) | int(list_temp[3])

# With the use of 'XNOR' operation and the addition of 32-bit '1s' mask, we
# a) keep the intersected bits between the 1st pair of IPs
# b) in continuation we compare every pair, keep the intersection and at the same time find the common ground of the new
#    intersection and the remnants of our previous result, up until the end
# c) the resulted number, has at every intersected bit, '1', else '0'
def find_intersection_bits_of_ipv4s(list_test: list) -> int:
    mask_init = 0XFFFFFFFF
    temp_intersection_bits = mask_init & ~(convert_ipv4_string_to_integer(list_test[0]) ^ convert_ipv4_string_to_integer(list_test[1]))
    for i in range(2, len(list_test)):
        print(list_test[i])
        temp_intersection_bits = temp_intersection_bits & ~(convert_ipv4_string_to_integer(list_test[i]) ^ convert_ipv4_string_to_integer(list_test[i - 1]))
    return temp_intersection_bits

# We count the consecutive 1s from start, which is the number of bits that represent
# the minimal common spanning subnet(s) of an IPv4
def count_intersection_prefix_common_bits(intersections_bits: int) -> int:
    mask, counter = 0X80000000, 0
    for index in range(32):
        if (mask & intersections_bits) != 0:
            counter += 1
        else:
            break
        mask >>= 1
    return counter

# Exaple: test = ['128.1.11.3', '128.1.1.3', '136.1.55.200', '128.1.1.1', '128.1.3.0']
def solution(test: list) -> int:
    test = ['128.1.11.3', '128.1.1.3', '136.1.55.200', '128.1.1.1', '128.1.3.0'] # here only the first 4-bits are common

    intersection_bits = find_intersection_bits_of_ipv4s(test)
    result_number_of_bits = count_intersection_prefix_common_bits(intersection_bits)

    return result_number_of_bits
Confidenc3
  • 591
  • 4
  • 9
0

A minimal spanning subnet is the longest prefix that covers an entire set of addresses.

The way to find this is to calculate all of the addresses in binary. Count the equal bits in both the IP addresses and stop when they are unequal.

Bitwise AND all of the bits to the left, that would be the longest prefix that covers the set. Append 0's to the right to make it a valid IP address. (8 bits in each octet)