def form_groups(links):
    """
    >>> form_groups([(1, 2), (3, 4), (2, 5), (6, 8), (4, 7)])==[{1,2,5},{3,4,7},{6,8}]
    True
    """
    groups = []
    groupMap = {}
    for a, b in links:
        assert a is not None and b is not None, links
        assert b not in groups, 'Links not sorted left-to-right: ' + repr((
            a, b))
        if a not in groupMap:
            groups.append({a})
            groupMap[a] = groups[-1]
        assert b not in groupMap[a], 'Redunant link?: ' + repr((a, b))
        groupMap[a].add(b)
        groupMap[b] = groupMap[a]
    return groups
