Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sparse nauty #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ lib_LTLIBRARIES = NautyTracesInterface.la
NautyTracesInterface_la_SOURCES = src/NautyTracesInterface.c
NautyTracesInterface_la_CPPFLAGS = $(GAP_CPPFLAGS) $(NAUTY_CPPFLAGS) -DCONFIG_H
# Note that the latter is only for GAP 4.4.12
NautyTracesInterface_la_LDFLAGS = $(NAUTY_PATH)/nauty.o $(NAUTY_PATH)/nautil.o $(NAUTY_PATH)/naugraph.o $(NAUTY_PATH)/schreier.o $(NAUTY_PATH)/naurng.o $(NAUTY_PATH)/naugroup.o $(NAUTY_PATH)/nautinv.o -module -avoid-version
NautyTracesInterface_la_LDFLAGS = $(NAUTY_PATH)/nauty.o $(NAUTY_PATH)/nautil.o $(NAUTY_PATH)/naugraph.o $(NAUTY_PATH)/schreier.o $(NAUTY_PATH)/naurng.o $(NAUTY_PATH)/naugroup.o $(NAUTY_PATH)/nautinv.o -module -avoid-version $(NAUTY_PATH)/nauty.a
if SYS_IS_CYGWIN
NautyTracesInterface_la_LDFLAGS += -no-undefined -version-info 0:0:0 -Wl,$(GAPROOT)/bin/$(GAPARCH)/gap.dll
endif
Expand Down
186 changes: 186 additions & 0 deletions src/NautyTracesInterface.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include <nauty.h>
#include <naugroup.h>
#include <nautinv.h>
#include "nausparse.h"
#include <gtools.h>


static Obj automorphism_list;
Obj TheTypeNautyInternalGraphObject;
Expand Down Expand Up @@ -33,6 +36,7 @@ Obj NautyObjTypeFunc(Obj o)
#define IS_NAUTY_GRAPH_OBJ(o) (TNUM_OBJ(o) == T_NAUTY_OBJ)

#define NAUTY_GRAPH_PTR(o) (graph*)ADDR_OBJ(o)[0]
#define NAUTY_GRAPH_PTR_SPARSE(o) (sparsegraph*)ADDR_OBJ(o)[0]
#define NAUTY_GRAPH_SIZE(o) (size_t)ADDR_OBJ(o)[1]
#define NAUTY_GRAPH_ROWS(o) (size_t)ADDR_OBJ(o)[2]
#define NAUTY_GRAPH_COLS(o) (size_t)ADDR_OBJ(o)[3]
Expand All @@ -48,6 +52,17 @@ Obj NEW_NAUTY_GRAPH_OBJ(graph* graph_pointer, size_t size, size_t rows, size_t c
return o;
}

Obj NEW_NAUTY_SPARSEGRAPH_OBJ(sparsegraph* graph_pointer, size_t size, size_t rows, size_t cols )
{
Obj o;
o = NewBag(T_NAUTY_OBJ, 4 * sizeof(Obj));
ADDR_OBJ(o)[0] = (Obj)(graph_pointer);
ADDR_OBJ(o)[1] = (Obj)(size);
ADDR_OBJ(o)[2] = (Obj)(rows);
ADDR_OBJ(o)[3] = (Obj)(cols);
return o;
}

void NautyObjFreeFunc( Obj o )
{
DYNFREE(ADDR_OBJ(o)[0],ADDR_OBJ(o)[1]);
Expand Down Expand Up @@ -102,6 +117,29 @@ Obj NAUTY_GRAPH(Obj self, Obj source_list, Obj range_list, Obj nr_vertices_gap,
return NEW_NAUTY_GRAPH_OBJ( g, g_sz, n, m );
}


Obj NAUTY_DENSE_TO_SPARSE(Obj self, Obj nauty_graph )
{

int n,m,v;

graph* g = NAUTY_GRAPH_PTR( nauty_graph );
size_t g_sz = NAUTY_GRAPH_SIZE( nauty_graph );
n = NAUTY_GRAPH_ROWS( nauty_graph );
m = NAUTY_GRAPH_COLS( nauty_graph );


DYNALLSTAT(sparsegraph,sg,sg_sz);
DYNALLOC2(sparsegraph,sg,sg_sz,m,n,"malloc");
EMPTYGRAPH(sg,m,n);

nauty_to_sg(g,sg,m,n);

return NEW_NAUTY_SPARSEGRAPH_OBJ( sg, sg_sz, n, m );
}



Obj NAUTY_DENSE(Obj self, Obj nauty_graph, Obj is_directed, Obj color_data )
{
DYNALLSTAT(graph,cg,cg_sz);
Expand Down Expand Up @@ -201,6 +239,136 @@ Obj NAUTY_DENSE(Obj self, Obj nauty_graph, Obj is_directed, Obj color_data )
return return_list;
}





Obj NAUTY_SPARSE(Obj self, Obj nauty_graph, Obj is_directed, Obj color_data )
{
DYNALLSTAT(sparsegraph,csg,csg_sz);
DYNALLSTAT(int,lab,lab_sz);
DYNALLSTAT(int,ptn,ptn_sz);
DYNALLSTAT(int,orbits,orbits_sz);
static optionblk options;
if( is_directed == True ){
static DEFAULTOPTIONS_DIGRAPH(temp_options);
options=temp_options;
}else{
static DEFAULTOPTIONS_SPARSEGRAPH(temp_options2);
options=temp_options2;
}

statsblk stats;

int n,m,v;
set *gv;

int nr_edges;

int len_source;
int len_range;

int current_source;
int current_range;

Obj p;
UInt4 *ptr;

UInt global_list;

sparsegraph* sg = NAUTY_GRAPH_PTR_SPARSE( nauty_graph );
size_t sg_sz = NAUTY_GRAPH_SIZE( nauty_graph );
n = NAUTY_GRAPH_ROWS( nauty_graph );
m = NAUTY_GRAPH_COLS( nauty_graph );

// Write automorphisms
global_list = GVarName( "__NAUTYTRACESINTERFACE_GLOBAL_AUTOMORPHISM_GROUP_LIST" );
automorphism_list = NEW_PLIST(T_PLIST, 0);
SET_LEN_PLIST( automorphism_list, 0 );
AssGVar( global_list, automorphism_list );
options.userautomproc = userautomproc;

options.getcanon = TRUE;

nauty_check(WORDSIZE,m,n,NAUTYVERSIONID);

// Allocate graph
DYNALLOC2(sparsegraph,csg,csg_sz,m,n,"malloc");
DYNALLOC1(int,lab,lab_sz,n,"malloc");
DYNALLOC1(int,ptn,ptn_sz,n,"malloc");
DYNALLOC1(int,orbits,orbits_sz,n,"malloc");

EMPTYGRAPH(csg,m,n);

if( color_data != False ){

options.defaultptn = FALSE;

Obj obj_lab = ELM_PLIST( color_data, 1 );
Obj obj_ptn = ELM_PLIST( color_data, 2 );

for(int i=0;i<n;i++){
lab[ i ] = INT_INTOBJ( ELM_PLIST( obj_lab, i + 1 ) ) - 1;
ptn[ i ] = INT_INTOBJ( ELM_PLIST( obj_ptn, i + 1 ) );
}
}

// Call nauty
sparsenauty(sg,lab,ptn,orbits,&options,&stats,csg);

//Convert labeling permutation
p = NEW_PERM4(n);
ptr = ADDR_PERM4(p);

for(int v= 0; v < n; v++){
ptr[v] = lab[v];
}

Obj return_list = NEW_PLIST( T_PLIST, 2 );
SET_LEN_PLIST( return_list, 2 );

SET_ELM_PLIST( return_list, 1, automorphism_list );
SET_ELM_PLIST( return_list, 2, p );

automorphism_list = NEW_PLIST(T_PLIST, 0);
SET_LEN_PLIST( automorphism_list, 0 );
AssGVar( global_list, automorphism_list );

DYNFREE(csg,csg_sz);
DYNFREE(lab,lab_sz);
DYNFREE(ptn,ptn_sz);
DYNFREE(orbits,orbits_sz);

return return_list;
}


Obj NAUTY_SPARSE_GRAPH_PRINT_TEST(Obj self, Obj nauty_graph)
{

int n,m;

sparsegraph* g = NAUTY_GRAPH_PTR_SPARSE( nauty_graph );

putgraph_sg(stderr, g, 0);
return True;
}


Obj NAUTY_DENSE_GRAPH_PRINT_TEST(Obj self, Obj nauty_graph)
{

int n,m;

graph* g = NAUTY_GRAPH_PTR( nauty_graph );
n = NAUTY_GRAPH_ROWS( nauty_graph );
m = NAUTY_GRAPH_COLS( nauty_graph );

putgraph(stderr, g, 0, m, n);
return True;
}


Obj NautyDense(Obj self, Obj source_list, Obj range_list, Obj nr_vertices_gap, Obj is_directed, Obj color_data )
{
Obj graph, return_list;
Expand All @@ -210,6 +378,19 @@ Obj NautyDense(Obj self, Obj source_list, Obj range_list, Obj nr_vertices_gap, O
return return_list;
}


Obj NautySparse(Obj self, Obj source_list, Obj range_list, Obj nr_vertices_gap, Obj is_directed, Obj color_data )
{
Obj graph, graph2, return_list;
graph = NAUTY_GRAPH(self, source_list,range_list,nr_vertices_gap,is_directed);
NautyObjFreeFunc( graph );
graph2 = NAUTY_DENSE_TO_SPARSE(self, graph);
return_list = NAUTY_SPARSE( self, graph2, is_directed, color_data );
NautyObjFreeFunc( graph2 );
return return_list;
}


typedef Obj (* GVarFunc)(/*arguments*/);

#define GVAR_FUNC_TABLE_ENTRY(srcfile, name, nparam, params) \
Expand All @@ -221,8 +402,13 @@ typedef Obj (* GVarFunc)(/*arguments*/);
// Table of functions to export
static StructGVarFunc GVarFuncs [] = {
GVAR_FUNC_TABLE_ENTRY("NautyTracesInterface.c", NautyDense, 5, "source_list,range_list,n,is_directed,color_data"),
GVAR_FUNC_TABLE_ENTRY("NautyTracesInterface.c", NautySparse, 5, "source_list,range_list,n,is_directed,color_data"),
GVAR_FUNC_TABLE_ENTRY("NautyTracesInterface.c", NAUTY_GRAPH, 4, "source_list,range_list,n,is_directed" ),
GVAR_FUNC_TABLE_ENTRY("NautyTracesInterface.c", NAUTY_DENSE_TO_SPARSE, 1, "source_list" ),
GVAR_FUNC_TABLE_ENTRY("NautyTracesInterface.c", NAUTY_DENSE, 3, "graph,is_directed,color_data" ),
GVAR_FUNC_TABLE_ENTRY("NautyTracesInterface.c", NAUTY_SPARSE, 3, "graph,is_directed,color_data" ),
GVAR_FUNC_TABLE_ENTRY("NautyTracesInterface.c", NAUTY_DENSE_GRAPH_PRINT_TEST, 1, "graph" ),
GVAR_FUNC_TABLE_ENTRY("NautyTracesInterface.c", NAUTY_SPARSE_GRAPH_PRINT_TEST, 1, "graph" ),

{ 0 } /* Finish with an empty entry */

Expand Down