lcz

994 Reputation

11 Badges

6 years, 128 days
changsha, China

MaplePrimes Activity


These are questions asked by lcz

I see that one of the parameters of the  function Graph accepts a Array of neighbors.

So I gave it a try. 

G := GraphTheory:-Graph([1,2],[[2],[1]])

G := `Graph 10: an undirected unweighted graph with 2 vertices and 1 edge(s)`

It looks pretty good. But there's something wrong with the following codes.

G := GraphTheory:-Graph([0,1],[[1],[0]])

Error, (in GraphTheory:-Graph) invalid vertex 0 in list of neighbors

G := GraphTheory:-Graph(["0","1"],[["1"],["0"]])

Error, (in GraphTheory:-Graph) cannot determine if this expression is true or false: 1" < 1 or 2 < "1
 

G := GraphTheory:-Graph(["a","b"],[["b"],["a"]])

Error, (in GraphTheory:-Graph) cannot determine if this expression is true or false: b" < 1 or 2 < "b
 

How to use  Array of neighbors in  Graph  correctly?

 

PS: The source of the problem is that I got the following list of adjacencies of a graph  in Python and I want to convert it to  a graph in Maple. Its vertices are [0,1,2, ..., 9].

[[1, 2, 3, 4, 5, 6], [0, 2, 3, 4, 7, 8],
 [0, 1, 3, 5, 7, 9], [0, 1, 2, 6, 8, 9],
 [0, 1, 5, 6, 7, 8], [0, 2, 4, 6, 7, 9], 
[0, 3, 4, 5, 8, 9], [1, 2, 4, 5, 8, 9], 
[1, 3, 4, 6, 7, 9], [2, 3, 5, 6, 7, 8]]

 

G := GraphTheory:-Graph([$0..9],[[1, 2, 3, 4, 5, 6], 
[0, 2, 3, 4, 7, 8], [0, 1, 3, 5, 7, 9],
 [0, 1, 2, 6, 8, 9], [0, 1, 5, 6, 7, 8],
 [0, 2, 4, 6, 7, 9], [0, 3, 4, 5, 8, 9],
 [1, 2, 4, 5, 8, 9], [1, 3, 4, 6, 7, 9], 
[2, 3, 5, 6, 7, 8]])

Error, (in GraphTheory:-Graph) invalid vertex 0 in list of neighbors

I am trying to calculate the number of  complete subgraphs K4 of a graph. Based on some similar discussions, like following links,

I wrote the following codes:

NumberOfK4:=proc(g::Graph)
local  n,N, i, j, k,l, g1,G1;
uses GraphTheory;
n:=NumberOfVertices(g);
g1 := RelabelVertices(g, [$ i = 1..n]):
N:=0:
 for i from 1 to n-3 do
    for j from i+1 to n-2 do
         for k from j+1 to n-1 do
             for l from k+1 to n do
                G1:=InducedSubgraph(g1,[i,j,k,l]);
                   if NumberOfEdges(G1)=6 then N:=N+1; fi;
               od;
            od; 
         od;
  od;
N;
end proc:
with(GraphTheory): with(SpecialGraphs):
g:=CompleteGraph(5);
g1:=LineGraph(g);
NumberOfK4(g1)

5

I know the above codes are supposed to be inefficient in terms of speed. We want to improve this codes. One way I know  that is to take advantage of  the efficient graph theory programs such as igraph

Recently I learned how Maple calls external C programs. 

The define_external command links in an externally defined function (for example, from a DLL under Windows, or from a shared library under UNIX), and produces a Maple procedure that acts as an interface to this external function.

From the help documentation, we need to compile the DLL files from the  source of C language. If it is a C program that does not need additional libraries, we can compile successfully, such as help documents

    void mat_mult( double *A, double *B, double *C, int I, int J, int K )
    {
      int i, j, k;
      double t;
      for( i = 0; i < I; ++i )
        for( k = 0; k < K; ++k ) {
          t = 0.0;
          for( j = 0; j < J; ++j )
            t += A[i*J+j] * B[j*K+k];
          C[i*K+k] = t;
    }
    }

No offense, maple has relatively few functions on graph theory so far, while Sage has a strong advantage because of its ability to integrate open network resources. I think sage is considered better for graph theory researchers because of this.

I don't know if Maple has any guidance on integrating a publicly available  graph theory library ,such as igraph . 

Where is the difficulty of using the written source code? And my computer system is Windows 10 , are there any special requirements. If Maple does this better, it might be a wonderful thing for graph theorists.

Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible along each branch before backtracking. see  https://en.wikipedia.org/wiki/Depth-first_search 

Here I write a  DFS function in maple  language according to the principle of  BFS algorithm.  

Pseudo codes:

procedure DFS_iterative(G, v) is
    let S be a stack
    S.push(v)
    while S is not empty do
        v = S.pop()
        if v is not labeled as discovered then
            label v as discovered
            for all edges from v to w in G.adjacentEdges(v) do 
                S.push(w)

Maple DFS codes:

DFS:=proc(g::Graph,x)
local  s,seen,vertex,nodes,w,vertexlist;
s := stack[new]():
stack[push](x, s):
seen :=[x]:
vertexlist:= []:
while not stack[empty](s) do
      vertex:=stack[pop](s):
      vertexlist:= [op(vertexlist),vertex]:
      nodes:= Neighborhood(g, vertex):
      for w in nodes do
            if not evalb(w in seen) then:
                stack[push](w, s):
                seen :=[op(seen),w]:
            end if:
      end do:  
end do:
return  vertexlist
end proc:
with(GraphTheory):with(SpecialGraphs):
P := PetersenGraph():
DrawGraph(P);
DFS(P,1)

[1, 6, 10, 9, 8, 4, 3, 7, 5, 2]

The result of a depth-first search of a graph can be conveniently described in terms of a spanning tree of the vertices reached during the search.

But I haven't figured out how to improve the  above codes to get this DFS spanning tree.

 

PS: The stack seems to be deprecated in maple2021.

As another way to search BFS algorithm, Carl Love  provided an example of the application of BFS algorithm. 

I also wrote a BFS code implementation based on the queue as follows.

BFS:=proc(g::Graph,x)
local  s,seen,vertex,nodes,w,vertexlist;
s:= SimpleQueue():
s:-enqueue(x):
seen :=[x]:
vertexlist:= []:
while not s:-empty() do
      vertex:=s:-dequeue():
      vertexlist:= [op(vertexlist),vertex]:
      nodes:= Neighborhood(g, vertex):
      for w in nodes do
            if not evalb(w in seen) then:
                s:-enqueue(w):
                seen :=[op(seen),w]:
            end if:
      end do:  
end do:
return  vertexlist
end proc:
BFS(P,1)

[1, 2, 5, 6, 3, 9, 4, 8, 7, 10]

 

 

VertexConnectivity returns the vertex connectivity of a graph, that is the minimum number of vertices whose removal disconnects the graph.  When I was filtering some graphs with a certain  vertex connectivity, maple was much slower than mathematica. I'm going to store all graphs in file  named op21new.g6.  When mathematica selects all graphs with a connectivity of 6, it only takes 15 seconds to complete.

mma codes are the following 

L = Import[
  "C:/Users/asus/Desktop/op21new.g6"]; # Change this line
t = AbsoluteTiming[L1 = Select[L, VertexConnectivity[#] == 6 &];]

{15.6484, Null}

But my maple has been running for half an hour and it is not over yet.

with(GraphTheory):
L:=ImportGraph("C:/Users/asus/Desktop/op21new.g6", graph6, output=list): # Change this line
L2:=CodeTools:-Usage(select(g->VertexConnectivity(g)=6,L)):

Because Mapleprimes does not support graph6 format file uploading, I changed it to TXT format. If you want to use it, you only need to change the suffix name.

op21new.txt

 The vertex connectivity of a graph  is the minimum number of vertices whose removal disconnects the  
  graph. With Maple, it's easy to get the connectivity of a graph, but  I can not  find a smallest vertex cut of 
the graph g by maple. 

 If a graph has some cut vertices, I see that the function ArticulationPoints seems to be able to solve.
 But for general smallest vertex cut, I don’t see a suitable function.  For example

with(GraphTheory):
G:=CompleteGraph(2,2,2,2);
DrawGraph(G);
VertexConnectivity(G)

6

This seems easy to do in Mathematica. See https://reference.wolfram.com/language/ref/FindVertexCut.html.

First 8 9 10 11 12 13 14 Last Page 10 of 25