How to remove duplicate edges from graph in Python list

Published on Author Code Father
Remove duplicate edges from graph in Python list

If a tuple (n1, (n2, distance)) represents a bidirectional connection, I would introduce a normalization property which constraints the ordering of the two nodes in the tuple. This way, each possible edge has exactly one unique representation.

Consequently, a normalization function would map a given, potentially unnormalized, edge to the normalized variant. This function can then be used to normalize all given edges. Duplicates can now be eliminated in several ways. For instance, convert the list to a set.

def normalize(t):
    n1, (n2, dist) = t
    if n1 < n2: # use a custom compare function if desired
        return t
        return (n2, (n1, dist))

edges = [('i', ('e', 130)), ('e', ('i', 130)), ('g', ('a', 65)), ('g', ('d', 15)), ('a', ('g', 65))]

unique_edges = set(map(normalize, edges))

# set([('e', ('i', 130)), ('d', ('g', 15)), ('a', ('g', 65))])

The normalization function can also be formulated like this:

def normalize((n1, (n2, dist))):
    if n1 >= n2: 
        n1, n2 = n2, n1
    return n1, (n2, dist)