My program returns a list of tuples, which represent the edges of a graph, in the form of:
[(i, (e, 130)), (e, (i, 130)), (g, (a, 65)), (g, (d, 15)), (a, (g, 65))]
[(i, (e, 130)), (g, (a, 65)), (g, (d, 15))]
def edge_equal(edge_tuple1, edge_tuple2):
return edge_tuple1 == edge_tuple2 and edge_tuple2 == edge_tuple1
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 else: 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)