added program to translate adjancency matrices into graphviz encoding. this is useful to display SOCs; adjusted Makefile accordingly

This commit is contained in:
Ämin on ThinkPad 2023-05-11 13:58:06 +02:00
parent e8cdd786a5
commit 5745c96829
2 changed files with 81 additions and 1 deletions

View File

@ -4,7 +4,7 @@
CFLAGS = -Wall -Ofast
all: SOCgen SOCadmissible
all: SOCgen SOCadmissible SOCgraphviz
%: src/%.c
gcc $(CFLAGS) -o $@ $^

80
src/SOCgraphviz.c Normal file
View File

@ -0,0 +1,80 @@
// SPDX-FileCopyrightText: 2023 Ämin Baumeler <amin@indyfac.ch> and Eleftherios-Ermis Tselentis <eleftheriosermis.tselentis@oeaw.ac.at>
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include <stdio.h>
int main(int argc, char *argv[]) {
// Parse command-line arguments
if(argc != 2) {
fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
fprintf(stderr, " <filename> File name with adjacency matrices of simple directed graphs\n");
fprintf(stderr, "\n");
fprintf(stderr, "[FILE FORMAT]\n");
fprintf(stderr, " Each line in `filename' must contain the adjacency matrix of a simple directed graph in the format\n");
fprintf(stderr, " {{a00,a01,...},{a10,a11,...},...} where aij=1 if and only if the graph has the edge i -> j\n");
fprintf(stderr, " The file `filename' may contain graphs with different order (number of vertices)\n");
fprintf(stderr, "\n");
fprintf(stderr, "This program translates to adjacency matrices into the Graphviz format, and prints them to stdout.\n");
return -1;
}
// Open file
FILE *fp;
fp = fopen(argv[1], "r");
if(fp == NULL){
fprintf(stderr, "ERROR: Could not read graphs from file `%s'.\n", argv[1]);
return -1;
}
// Parse
int headprinted = 1;
int line = 1;
int n = -1;
int row = 0;
int col = 0;
char ch;
printf("strict digraph \"File_%s_line_%d\" {", argv[1], line);
while(!feof(fp)) {
ch = fgetc(fp);
switch(ch) {
case '{':
if(row==0 && col==0 && headprinted==0) {
headprinted = 1;
printf("strict digraph \"File_%s_line_%d\" {", argv[1], line);
}
break;
case '0':
case '1':
if(ch=='1')
printf("%d->%d;", row,col);
col++;
break;
case '}':
if(n==-1)
n = col;
if(col==n) {
row++;
col=0;
}
break;
case '\n':
printf("}\n");
headprinted = 0;
line++;
row = 0;
col = 0;
n = -1;
break;
case ',':
case ' ':
case 0xffffffff:
break;
default:
fprintf(stderr, "File contains wrongly formatted graph\n");
fclose(fp);
return -1; // Format error
}
}
fclose(fp);
return 0;
}