Changeset 8614:6e54905ea249


Ignore:
Timestamp:
02/05/08 23:03:02 (5 years ago)
Author:
Robert L. Miller <rlm@…>
Branch:
default
Message:

small optimization to graph isomorphism code
enumeration is now abstract, instead of explicit

Location:
sage/graphs
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sage/graphs/graph_isom.pxd

    r8098 r8614  
    7373    cdef int _first_smallest_nontrivial(self, int *W, int k, int n) 
    7474    cdef void _get_permutation_from(self, PartitionStack zeta, int *gamma) 
    75     cdef _enumerate_graph_from_discrete(self, int **G, int n) 
     75    cdef int _compare_with(self, int **G, int n, PartitionStack other) 
     76 
     77cdef int _is_automorphism(int **G, int n, int *gamma) 
  • sage/graphs/graph_isom.pyx

    r8250 r8614  
    577577                    s = m 
    578578                    while alpha[s] != -1: 
    579                         if alpha[s] == j: alpha[s] = t # TODO this will only happen once, so should break 
     579                        if alpha[s] == j: 
     580                            alpha[s] = t 
     581                            break 
    580582                        s += 1 
     583                    while alpha[s] != -1: s += 1 
    581584                    r = j 
    582585                    while True: 
     
    619622                    s = m 
    620623                    while alpha[s] != -1: 
    621                         if alpha[s] == j: alpha[s] = t # this will only happen once, so should break 
     624                        if alpha[s] == j: 
     625                            alpha[s] = t 
     626                            break 
    622627                        s += 1 
     628                    while alpha[s] != -1: s += 1 
    623629                    r = j 
    624630                    while True: 
     
    713719            i += 1 
    714720            if self.levels[i-1] == -1: break 
    715  
    716 # (TODO) 
    717 # Important note: the enumeration should be kept abstract, and only comparison 
    718 # functions should be written. This takes up too much memory and time. Simply 
    719 # iterate starting with the most significant digit in the matrix, and return 
    720 # as soon as a contradiction is encountered. 
    721  
    722     cdef _enumerate_graph_from_discrete(self, int **G, int n): 
     721     
     722    cdef int _compare_with(self, int **G, int n, PartitionStack other): 
    723723        cdef int i, j 
    724         enumeration = Integer(0) 
    725724        for i from 0 <= i < n: 
    726725            for j from 0 <= j < n: 
    727                 if G[i][j]: 
    728                     enumeration += Integer(2)**((n-(self.entries[i]+1))*n + n-(self.entries[j]+1)) 
    729         return enumeration         
    730  
    731 cdef _enumerate_graph_with_permutation(int **G, int n, int *gamma): 
     726                if G[self.entries[i]][self.entries[j]]: 
     727                    if not G[other.entries[i]][other.entries[j]]: 
     728                        return 1 
     729                elif G[other.entries[i]][other.entries[j]]: 
     730                    return -1 
     731        return 0 
     732 
     733cdef int _is_automorphism(int **G, int n, int *gamma): 
    732734    cdef int i, j 
    733     enumeration = Integer(0) 
    734735    for i from 0 <= i < n: 
    735736        for j from 0 <= j < n: 
    736737            if G[i][j]: 
    737                 enumeration += Integer(2)**((n-(gamma[i]+1))*n + n-(gamma[j]+1)) 
    738     return enumeration 
    739  
    740 cdef _enumerate_graph(int **G, int n): 
    741     cdef int i, j # enumeration = 0 
    742     enumeration = Integer(0) 
    743     for i from 0 <= i < n: 
    744         for j from 0 <= j < n: 
    745             if G[i][j]: 
    746                 enumeration += Integer(2)**((n-(i+1))*n + n-(j+1)) 
    747     return enumeration 
     738                if not G[gamma[i]][gamma[j]]: 
     739                    return 0 
     740    return 1 
    748741 
    749742def _term_pnest_graph(G, PartitionStack nu): 
     
    11621155        46080 
    11631156    """ 
    1164     cdef int i, j, # local variables 
     1157    cdef int i, j, m # local variables 
    11651158     
    11661159    cdef OrbitPartition Theta, OP 
     
    13291322    nu = PartitionStack(Pi) 
    13301323    Theta = OrbitPartition(n) 
    1331     G_enum = _enumerate_graph(M, n) 
    13321324    output = []     
    13331325    if dig: _dig = 1 
     
    15261518 
    15271519            if verbosity > 3: 
    1528                 print 'automorphism discovered:' 
     1520                print 'checking for automorphism:' 
    15291521                print [gamma[iii] for iii in range(n)] 
    15301522             
    1531             # if G^gamma == G, the permutation is an automorphism, goto 10 
    1532             if G_enum == _enumerate_graph_with_permutation(M, n, gamma): 
     1523            # if G^gamma == G, goto 10 
     1524            if _is_automorphism(M, n, gamma): 
    15331525                state = 10 
    15341526            else: 
     
    15481540                state = 9; continue 
    15491541 
    1550             # if G(nu) > G(rho), goto 9 
    1551             # if G(nu) < G(rho), goto 6 
    1552             # if G(nu) == G(rho), get the automorphism and goto 10 
    1553             m1 = nu._enumerate_graph_from_discrete(M, n) 
    1554             m2 = rho._enumerate_graph_from_discrete(M, n) 
    1555              
    1556             if m1 > m2: 
     1542            # if G(nu) > G(rho) (returns 1), goto 9 
     1543            # if G(nu) < G(rho) (returns -1), goto 6 
     1544            # if G(nu) == G(rho) (returns 0), get the automorphism and goto 10 
     1545            m = nu._compare_with(M, n, rho) 
     1546             
     1547            if m > 0: 
    15571548                state = 9; continue 
    1558             if m1 < m2: 
     1549            if m < 0: 
    15591550                state = 6; continue 
    15601551 
Note: See TracChangeset for help on using the changeset viewer.