Ticket #13114: trac_13114.patch

File trac_13114.patch, 4.7 KB (added by ncohen, 9 years ago)
  • sage/graphs/generic_graph.py

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1339621523 -7200
    # Node ID e3046cda6b89585b4e0f3bfb3a56e6683e2f509f
    # Parent  b84e977a17a50653086898435a4e590a0252a712
    Bug in is_isomorphic for multigraphs
    
    diff --git a/sage/graphs/generic_graph.py b/sage/graphs/generic_graph.py
    a b  
    1645016450            True
    1645116451
    1645216452        Ensure that isomorphic looped graphs with non-range vertex labels report
    16453         correctly (trac #10814, fixed by #8395)::
    16454        
     16453        correctly (:trac:`10814`, fixed by :trac:`8395`)::
     16454
    1645516455            sage: G1 = Graph([(0,1), (1,1)])
    1645616456            sage: G2 = Graph([(0,2), (2,2)])
    1645716457            sage: G1.is_isomorphic(G2)
     
    1646816468            (True, {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5})
    1646916469            sage: D.is_isomorphic(D,edge_labels=True, certify = True)
    1647016470            (True, {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5})
    16471        
    16472         Ensure that trac #11620 is fixed::
    16473        
     16471
     16472        Ensure that trac :trac:`11620` is fixed::
     16473
    1647416474            sage: G1 = DiGraph([(0, 0, 'c'), (0, 4, 'b'), (0, 5, 'c'),
    1647516475            ...   (0, 5, 't'), (1, 1, 'c'), (1, 3,'c'), (1, 3, 't'), (1, 5, 'b'),
    1647616476            ...   (2, 2, 'c'), (2, 3, 'b'), (2, 4, 'c'),(2, 4, 't'), (3, 1, 't'),
     
    1648216482            True
    1648316483            sage: G1.is_isomorphic(G2,edge_labels=True)
    1648416484            True
    16485        
     16485
     16486        Ensure that :trac:`13114` is fixed ::
     16487
     16488            sage: g = Graph([(0, 0, 0), (0, 2, 0), (1, 1, 0), (1, 2, 0), (1, 2, 1), (2, 2, 0)])
     16489            sage: gg = Graph([(0, 0, 0), (0, 1, 0), (1, 1, 0), (1, 2, 0), (2, 2, 0), (2, 2, 1)])
     16490            sage: g.is_isomorphic(gg)
     16491            False
    1648616492        """
    1648716493        possible = True
    1648816494        if self._directed != other._directed:
     
    1688316889    edges = [ edge for edge in edge_iter ]
    1688416890    edge_labels = sorted([ label for v1,v2,label in edges if not label == standard_label])
    1688516891    i = 1
     16892
     16893    # edge_labels is sorted. We now remove values which are not unique
    1688616894    while i < len(edge_labels):
    1688716895        if edge_labels[i] == edge_labels[i-1]:
    1688816896            edge_labels.pop(i)
     
    1689516903
    1689616904    for u,v,l in edges:
    1689716905        if not l == standard_label:
    16898             index = edge_labels.index(l)
    1689916906            for el, part in edge_partition:
    1690016907                if el == l:
    1690116908                    part.append(i)
    1690216909                    break
     16910
    1690316911            G._backend.add_edge(u,i,None,True)
    1690416912            G._backend.add_edge(i,v,None,True)
    1690516913            G.delete_edge(u,v)
     
    1690716915        elif standard_label is not None:
    1690816916            G._backend.set_edge_label(u,v,None,True)
    1690916917
    16910     edge_partition = [el[1] for el in sorted(edge_partition)]
    16911 
     16918    # Should we pay attention to edge labels ?
    1691216919    if ignore_edge_labels:
    16913         edge_partition = [sum(edge_partition,[])]
     16920
     16921        # If there are no multiple edges, we can just say that all edges are
     16922        # equivalent to each other without any further consideration.
     16923        if not g_has_multiple_edges:
     16924            edge_partition = [el[1] for el in sorted(edge_partition)]
     16925            edge_partition = [sum(edge_partition,[])]
     16926
     16927        # An edge between u and v with label l and multiplicity k being encoded
     16928        # as an uv edge with label [l,k], we must not assume that an edge with
     16929        # multiplicity 2 is equivalent to a simple edge !
     16930        # Hence, we still distinguish edges with different multiplicity
     16931        if g_has_multiple_edges:
     16932
     16933            # Compute the multiplicity the label
     16934            multiplicity = lambda x : sum(map(lambda y:y[1],x))
     16935
     16936            # Sort the edge according to their multiplicity
     16937            edge_partition = sorted([[multiplicity(el),part] for el, part in sorted(edge_partition)])
     16938
     16939            # Gather together the edges with same multiplicity
     16940            i = 1
     16941            while i < len(edge_partition):
     16942                if edge_partition[i][0] == edge_partition[i-1][0]:
     16943                    edge_partition[i-1][1].extend(edge_partition[i][1])
     16944                    edge_partition.pop(i)
     16945                else:
     16946                    i += 1
     16947
     16948            # now edge_partition has shape [[multiplicity, list_of_edges],
     16949            # [multiplicity, liste of edges], ...], and we can flatted it to
     16950            # [list of edges, list of edges, ...]
     16951            edge_partition = [el[1] for el in sorted(edge_partition)]
     16952
     16953            # Now the edges are partitionned according to the multiplicity they
     16954            # represent, and edge labels are forgotten.
     16955
     16956    else:
     16957        edge_partition = [el[1] for el in sorted(edge_partition)]
     16958
    1691416959    new_partition = [ part for part in partition + edge_partition if not part == [] ]
    1691516960
    1691616961    return_data = []