File: simplerw.c

package info (click to toggle)
rw 0.8%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,000 kB
  • ctags: 68
  • sloc: ansic: 322; makefile: 28
file content (197 lines) | stat: -rw-r--r-- 4,530 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// A simple program for calculating rank-width and rank-decompositions.
// Compile using gcc -O2 --std=c99 -pedantic rw.c simplerw.c -ligraph

// Philipp Klaus Krause, philipp@informatik.uni-frankfurt.de, pkk@spth.de, 2009 - 2011
// Copyright (c) 2009-2016 Philipp Klaus Krause
// Copyright (c) 2009-2015 Goethe-Universität Frankfurt

// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <igraph/igraph.h>

#include "rw.h"

static int num_vertices = 18;

static uint_fast32_t bitmask(int i)
{
	return(1ul << i);
}

static void set_am(int i, int j, int val)
{
	adjacency_matrix[i] &= ~bitmask(j);
	adjacency_matrix[j] &= ~bitmask(i);
	if(val)
	{
		adjacency_matrix[i] |= bitmask(j);
		adjacency_matrix[j] |= bitmask(i);
	}
}

static void tab(int l)
{
	while(l--)
		printf("\t");
}

void print_rank_dec(subset_t s, int l)
{
	tab(l);
	printf("cslot: %x\n", (unsigned)s);
	if(cslots[s] == 0)
		return;
	print_rank_dec(cslots[s], l + 1);
	print_rank_dec(s & ~cslots[s], l + 1);
}

/*int random_graph(void)
{
	int i, j;

	if(init_rw_dec(num_vertices))
		return(-1);

	srand(23);
	for(i = 0; i < num_vertices; i++)
		for(j = 0; j < num_vertices; j++)
			set_am(i, j, rand() % 2);

	for(i = 0; i < num_vertices; i++)
		printf("Adj. mat.: %x\n", (unsigned)(adjacency_matrix[i]));

	return(0);
}*/

int read_graph(const char *format, const char * filename)
{
	int ret, i, j;
	FILE *file;
	igraph_t igraph;
	igraph_matrix_t imatrix;
	igraph_bool_t isimple;

	// Open file
	if(!(file = fopen(filename, "r")))
	{
		printf("Failed to open file %s.\n", filename);
		return(-1);
	}
	
	// Read graph from file
	if(!strcmp("--edgelist", format))
		ret = igraph_read_graph_edgelist(&igraph, file, 0, 0);
	else if(!strcmp("--graphdb", format))
		ret = igraph_read_graph_graphdb(&igraph, file, 0);
	else if(!strcmp("--graphml", format))
		ret = igraph_read_graph_graphml(&igraph, file, 0);
	else if(!strcmp("--gml", format))
		ret = igraph_read_graph_gml(&igraph, file);
	else if(!strcmp("--pajek", format))
		ret = igraph_read_graph_pajek(&igraph, file);
	else
	{
		printf("Unknown file format %s.\n", format);
		fclose(file);
		return(-1);
	}

	fclose(file);

	if(ret)
	{
		printf("Failed to read a graph from file %s.\n", filename);
		return(-1);
	}

	// Check for undirectedness
	if(igraph_is_directed(&igraph) || igraph_is_simple(&igraph, &isimple) || !isimple)
	{
		printf("Input is not a simple, undirected graph from file %s.\n", filename);
		igraph_destroy(&igraph);
		return(-1);
	}

	// Get adjacency matrix
	if(igraph_matrix_init(&imatrix, 1, 1))
	{
		igraph_destroy(&igraph);
		return(-1);
	}
	igraph_get_adjacency(&igraph, &imatrix, IGRAPH_GET_ADJACENCY_BOTH, 0);
	igraph_destroy(&igraph);
	if(igraph_matrix_nrow(&imatrix) > MAX_VERTICES)
	{
		igraph_matrix_destroy(&imatrix);
		printf("Graph too large.\n");
		return(-1);
	}

	num_vertices = igraph_matrix_nrow(&imatrix);
	
	if(init_rw_dec(num_vertices))
	{
		destroy_rw();
		printf("Not enough memory.\n");
		return(-1);
	}

	for(i = 0; i < num_vertices; i++)
		for(j = 0; j < num_vertices; j++)
			set_am(i, j, MATRIX(imatrix, i, j));

	igraph_matrix_destroy(&imatrix);
	
	return(ret ? -1 : 0);
}

int main(int argc, char *argv[])
{
	int i;

	if(argc /*==*/ <= 2 || argc > 4)
	{
		printf("Usage: rw [--format filename]\n");
		printf("Supported formats: edgelist, graphdb, graphml, gml, pajek.\n");
		return(-1);
	}

	if(/*argc <= 1 ? random_graph() :*/ read_graph(argv[1], argv[2]))
	{
		printf("Failed to create input graph.\n");
		return(-1);
	}

	printf("%d vertices.\n", (int)num_vertices);

	for(i = 0; i <= num_vertices; i++)
	{
		printf("Calculating for subsets of size %d.\n", i);
		calculate_level(i);
	}

	printf("rank-width: %d\n", (int)get_rw());

	print_rank_dec(0x7ffffffful >> (31 - num_vertices), 0);

	destroy_rw();

	return(0);
}