Path: blob/master/sage/graphs/graph_decompositions/rankwidth_c/simplerw.c
4079 views
// A simple program for calculating rank-width and rank-decompositions.1// Compile using gcc -O2 --std=c99 -pedantic rw.c simplerw.c -ligraph23// Philipp Klaus Krause, [email protected], [email protected], 2009 - 20114// Copyright (c) 2009-2011 Philipp Klaus Krause56// This program is free software; you can redistribute it and/or modify7// it under the terms of the GNU General Public License as published by8// the Free Software Foundation; either version 2 of the License, or9// (at your option) any later version.10//11// This program is distributed in the hope that it will be useful,12// but WITHOUT ANY WARRANTY; without even the implied warranty of13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14// GNU General Public License for more details.15//16// You should have received a copy of the GNU General Public License along17// with this program; if not, write to the Free Software Foundation, Inc.,18// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.1920#include <stdio.h>21#include <stdlib.h>22#include <string.h>2324#include <igraph/igraph.h>2526#include "rw.h"2728static int num_vertices = 18;2930static uint_fast32_t bitmask(int i)31{32return(1ul << i);33}3435static void set_am(int i, int j, int val)36{37adjacency_matrix[i] &= ~bitmask(j);38adjacency_matrix[j] &= ~bitmask(i);39if(val)40{41adjacency_matrix[i] |= bitmask(j);42adjacency_matrix[j] |= bitmask(i);43}44}4546static void tab(int l)47{48while(l--)49printf("\t");50}5152void print_rank_dec(subset_t s, int l)53{54tab(l);55printf("cslot: %x\n", (unsigned)s);56if(cslots[s] == 0)57return;58print_rank_dec(cslots[s], l + 1);59print_rank_dec(s & ~cslots[s], l + 1);60}6162/*int random_graph(void)63{64int i, j;6566if(init_rw_dec(num_vertices))67return(-1);6869srand(23);70for(i = 0; i < num_vertices; i++)71for(j = 0; j < num_vertices; j++)72set_am(i, j, rand() % 2);7374for(i = 0; i < num_vertices; i++)75printf("Adj. mat.: %x\n", (unsigned)(adjacency_matrix[i]));7677return(0);78}*/7980int read_graph(const char *format, const char * filename)81{82int ret, i, j;83FILE *file;84igraph_t igraph;85igraph_matrix_t imatrix;86igraph_bool_t isimple;8788// Open file89if(!(file = fopen(filename, "r")))90{91printf("Failed to open file %s.\n", filename);92return(-1);93}9495// Read graph from file96if(!strcmp("--edgelist", format))97ret = igraph_read_graph_edgelist(&igraph, file, 0, 0);98else if(!strcmp("--graphdb", format))99ret = igraph_read_graph_graphdb(&igraph, file, 0);100else if(!strcmp("--graphml", format))101ret = igraph_read_graph_graphml(&igraph, file, 0);102else if(!strcmp("--gml", format))103ret = igraph_read_graph_gml(&igraph, file);104else if(!strcmp("--pajek", format))105ret = igraph_read_graph_pajek(&igraph, file);106else107{108printf("Unknown file format %s.\n", format);109fclose(file);110return(-1);111}112113fclose(file);114115if(ret)116{117printf("Failed to read a graph from file %s.\n", filename);118return(-1);119}120121// Check for undirectedness122if(igraph_is_directed(&igraph) || igraph_is_simple(&igraph, &isimple) || !isimple)123{124printf("Input is not a simple, undirected graph from file %s.\n", filename);125igraph_destroy(&igraph);126return(-1);127}128129// Get adjacency matrix130if(igraph_matrix_init(&imatrix, 1, 1))131{132igraph_destroy(&igraph);133return(-1);134}135igraph_get_adjacency(&igraph, &imatrix, IGRAPH_GET_ADJACENCY_BOTH);136igraph_destroy(&igraph);137if(igraph_matrix_nrow(&imatrix) > MAX_VERTICES)138{139igraph_matrix_destroy(&imatrix);140printf("Graph too large.\n");141return(-1);142}143144num_vertices = igraph_matrix_nrow(&imatrix);145146if(init_rw_dec(num_vertices))147{148destroy_rw();149printf("Not enough memory.\n");150return(-1);151}152153for(i = 0; i < num_vertices; i++)154for(j = 0; j < num_vertices; j++)155set_am(i, j, MATRIX(imatrix, i, j));156157igraph_matrix_destroy(&imatrix);158159return(ret ? -1 : 0);160}161162int main(int argc, char *argv[])163{164int i;165166if(argc /*==*/ <= 2 || argc > 4)167{168printf("Usage: rw [--format filename]\n");169printf("Supported formats: edgelist, graphdb, graphml, gml, pajek.\n");170return(-1);171}172173if(/*argc <= 1 ? random_graph() :*/ read_graph(argv[1], argv[2]))174{175printf("Failed to create input graph.\n");176return(-1);177}178179printf("%d vertices.\n", (int)num_vertices);180181for(i = 0; i <= num_vertices; i++)182{183printf("Calculating for subsets of size %d.\n", i);184calculate_level(i);185}186187printf("rank-width: %d\n", (int)get_rw());188189print_rank_dec(0x7ffffffful >> (31 - num_vertices), 0);190191destroy_rw();192193return(0);194}195196197198