Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/graphs/graph_decompositions/rankwidth_c/simplerw.c
4079 views
1
// A simple program for calculating rank-width and rank-decompositions.
2
// Compile using gcc -O2 --std=c99 -pedantic rw.c simplerw.c -ligraph
3
4
// Philipp Klaus Krause, [email protected], [email protected], 2009 - 2011
5
// Copyright (c) 2009-2011 Philipp Klaus Krause
6
7
// This program is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License as published by
9
// the Free Software Foundation; either version 2 of the License, or
10
// (at your option) any later version.
11
//
12
// This program is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License for more details.
16
//
17
// You should have received a copy of the GNU General Public License along
18
// with this program; if not, write to the Free Software Foundation, Inc.,
19
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21
#include <stdio.h>
22
#include <stdlib.h>
23
#include <string.h>
24
25
#include <igraph/igraph.h>
26
27
#include "rw.h"
28
29
static int num_vertices = 18;
30
31
static uint_fast32_t bitmask(int i)
32
{
33
return(1ul << i);
34
}
35
36
static void set_am(int i, int j, int val)
37
{
38
adjacency_matrix[i] &= ~bitmask(j);
39
adjacency_matrix[j] &= ~bitmask(i);
40
if(val)
41
{
42
adjacency_matrix[i] |= bitmask(j);
43
adjacency_matrix[j] |= bitmask(i);
44
}
45
}
46
47
static void tab(int l)
48
{
49
while(l--)
50
printf("\t");
51
}
52
53
void print_rank_dec(subset_t s, int l)
54
{
55
tab(l);
56
printf("cslot: %x\n", (unsigned)s);
57
if(cslots[s] == 0)
58
return;
59
print_rank_dec(cslots[s], l + 1);
60
print_rank_dec(s & ~cslots[s], l + 1);
61
}
62
63
/*int random_graph(void)
64
{
65
int i, j;
66
67
if(init_rw_dec(num_vertices))
68
return(-1);
69
70
srand(23);
71
for(i = 0; i < num_vertices; i++)
72
for(j = 0; j < num_vertices; j++)
73
set_am(i, j, rand() % 2);
74
75
for(i = 0; i < num_vertices; i++)
76
printf("Adj. mat.: %x\n", (unsigned)(adjacency_matrix[i]));
77
78
return(0);
79
}*/
80
81
int read_graph(const char *format, const char * filename)
82
{
83
int ret, i, j;
84
FILE *file;
85
igraph_t igraph;
86
igraph_matrix_t imatrix;
87
igraph_bool_t isimple;
88
89
// Open file
90
if(!(file = fopen(filename, "r")))
91
{
92
printf("Failed to open file %s.\n", filename);
93
return(-1);
94
}
95
96
// Read graph from file
97
if(!strcmp("--edgelist", format))
98
ret = igraph_read_graph_edgelist(&igraph, file, 0, 0);
99
else if(!strcmp("--graphdb", format))
100
ret = igraph_read_graph_graphdb(&igraph, file, 0);
101
else if(!strcmp("--graphml", format))
102
ret = igraph_read_graph_graphml(&igraph, file, 0);
103
else if(!strcmp("--gml", format))
104
ret = igraph_read_graph_gml(&igraph, file);
105
else if(!strcmp("--pajek", format))
106
ret = igraph_read_graph_pajek(&igraph, file);
107
else
108
{
109
printf("Unknown file format %s.\n", format);
110
fclose(file);
111
return(-1);
112
}
113
114
fclose(file);
115
116
if(ret)
117
{
118
printf("Failed to read a graph from file %s.\n", filename);
119
return(-1);
120
}
121
122
// Check for undirectedness
123
if(igraph_is_directed(&igraph) || igraph_is_simple(&igraph, &isimple) || !isimple)
124
{
125
printf("Input is not a simple, undirected graph from file %s.\n", filename);
126
igraph_destroy(&igraph);
127
return(-1);
128
}
129
130
// Get adjacency matrix
131
if(igraph_matrix_init(&imatrix, 1, 1))
132
{
133
igraph_destroy(&igraph);
134
return(-1);
135
}
136
igraph_get_adjacency(&igraph, &imatrix, IGRAPH_GET_ADJACENCY_BOTH);
137
igraph_destroy(&igraph);
138
if(igraph_matrix_nrow(&imatrix) > MAX_VERTICES)
139
{
140
igraph_matrix_destroy(&imatrix);
141
printf("Graph too large.\n");
142
return(-1);
143
}
144
145
num_vertices = igraph_matrix_nrow(&imatrix);
146
147
if(init_rw_dec(num_vertices))
148
{
149
destroy_rw();
150
printf("Not enough memory.\n");
151
return(-1);
152
}
153
154
for(i = 0; i < num_vertices; i++)
155
for(j = 0; j < num_vertices; j++)
156
set_am(i, j, MATRIX(imatrix, i, j));
157
158
igraph_matrix_destroy(&imatrix);
159
160
return(ret ? -1 : 0);
161
}
162
163
int main(int argc, char *argv[])
164
{
165
int i;
166
167
if(argc /*==*/ <= 2 || argc > 4)
168
{
169
printf("Usage: rw [--format filename]\n");
170
printf("Supported formats: edgelist, graphdb, graphml, gml, pajek.\n");
171
return(-1);
172
}
173
174
if(/*argc <= 1 ? random_graph() :*/ read_graph(argv[1], argv[2]))
175
{
176
printf("Failed to create input graph.\n");
177
return(-1);
178
}
179
180
printf("%d vertices.\n", (int)num_vertices);
181
182
for(i = 0; i <= num_vertices; i++)
183
{
184
printf("Calculating for subsets of size %d.\n", i);
185
calculate_level(i);
186
}
187
188
printf("rank-width: %d\n", (int)get_rw());
189
190
print_rank_dec(0x7ffffffful >> (31 - num_vertices), 0);
191
192
destroy_rw();
193
194
return(0);
195
}
196
197
198