620 Reputation

10 Badges

3 years, 89 days
changsha, China

MaplePrimes Activity

These are answers submitted by lcz

My advice is to look at the source codes of MathChem and write them yourself. I saw the introduction of package MathChem you mentioned.  MathChem is a python package for calculating topological Indices. This is actually a similar question I've asked before, but I talked about the igraph package.

Another similar discussion is as below.

I tried to install this package in python3, but unfortunately the run always failed. So I looked at the source codes, and it seemed to be based on Python 2.  I looked at  some functions in MathChem , and most of them could have been rewritten in Maple or made use of the Maple built-in functions.  

For example,  the spectral radius of adjacency matrix of a graph is what we talked about a while ago.  Carl Love,  dharr and acer  have an equally brilliant analysis of the problem.

SR := proc(G::Graph)
  local M := GraphTheory:-AdjacencyMatrix(G);
           ':-symmetric',NULL), M,
end proc:

  See more details in

Another example, we can calculate the first Zagreb index and  second Zagreb index of a graph by maple.

Zagrebindex_1:=g-> add((GraphTheory:-DegreeSequence(g))^~(2));
local d,i;
add([seq(Degree(g,d[i][1])*Degree(g,d[i][2]), i=1..NumberOfEdges(g))])
end proc:

90, 135

In MathChem, their codes are written in a similar way.

def zagreb_m1_index(self):
        """ Zagreb M1 Index """    
        return sum(map(lambda d: d**2, self.degrees()))
def zagreb_m2_index(self):
        """ Zagreb M2 Index 
        The molecular graph must contain at least one edge, otherwise the function Return False
        Zagreb M2 Index is a special case of Connectivity Index with power = 1"""
        return sum( map(lambda (e1, e2): self.degrees()[e1]*self.degrees()[e2] , self.edges()) )            

During my master's period, I once did research on chemical graph theory. Generally speaking, it is not too difficult to write codes for chemical index of graph. The difficulty is  calculating some parameters of graphs themselves.  For example, whether a graph is Hamiltonian, or to calculate  the crossing number  of a graph, and so on.  (They are NP hard. )  Even for linear time algorithms, such as testing whether a graph is planar, it is difficult to write code without specialized knowledge, but fortunately it is often readily available.

The unweighted  version was previously provided in Here, for the first question, we provide a  weighted graph version. Relevant theories can be referred to this paper.

  • Bapat R B. Resistance matrix of a weighted graph[J]. MATCH Commun. Math. Comput. Chem, 2004, 50(02).

As for whether the author discovered it for the first time, I haven't checked it carefully.

We just need to change the  laplacian matrix of  unweighted graph to the laplacian matrix of the weighted graph. So we improved the codes from last time as follows.

local M,M1,M2;
M1:=Matrix(upperbound(M), (i,j)-> `if`(M(i,j)<>0, -1/M(i,j), 0)):
end proc:
ResistanceMatrix:= (g::Graph)-> 
    (M-> Matrix(
            upperbound(M), (i,j)-> M[i,i]+M[j,j], 'shape' = 'symmetric'
         ) - 2*M
          , 'method'= 'pseudo'
S[1,8] #156/47


PS: Here I have not studied the corresponding relationship between graph vertex index and adjacency matrix index thoroughly.

However,  it seems that Rows and columns of the adjacency matrix follow the order given by Vertices:


["000", "003", "050", "053", "400", "403", "450", "453"]

In graph theory, the resistance distance between two vertices of a simple connected graph, G, is equal to the resistance between two equivalent points on an electrical network, constructed so as to correspond to G, with each edge being replaced by a 1 ohm resistance


PS: I remember that even though each edge is not set to a unit resistance, there is a corresponding theoretical calculation formula. 

So we can write the following function:

   local L,ML,n, i, j, R;
     ML:=LinearAlgebra:-MatrixInverse(L, method = pseudo):
         for i from 1 to n do
            for j from i to n do
               R:=Matrix(n,n,(i,j)->ML[i,i]+ML[j,j]-2*ML[i,j],shape = symmetric);
end proc:
S := SoccerBallGraph():


Note that finding the  pseudo inverse of the matrix is ​​not efficient, so this method seems to be limited to cases with fewer vertices or only has theoretical value. Is there a faster solution? I don't know yet.

See some talks in 

First you have to install this extra package, but even so, it looks like this function is not easy to use now.

Page 1 of 1