From 5745c968290ac4dca87ca2ec95315818c17ff059 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=84min=20on=20ThinkPad?= Date: Thu, 11 May 2023 13:58:06 +0200 Subject: [PATCH] added program to translate adjancency matrices into graphviz encoding. this is useful to display SOCs; adjusted Makefile accordingly --- Makefile | 2 +- src/SOCgraphviz.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 src/SOCgraphviz.c diff --git a/Makefile b/Makefile index 35009e0..1b805ae 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ CFLAGS = -Wall -Ofast -all: SOCgen SOCadmissible +all: SOCgen SOCadmissible SOCgraphviz %: src/%.c gcc $(CFLAGS) -o $@ $^ diff --git a/src/SOCgraphviz.c b/src/SOCgraphviz.c new file mode 100644 index 0000000..2fb7e85 --- /dev/null +++ b/src/SOCgraphviz.c @@ -0,0 +1,80 @@ +// SPDX-FileCopyrightText: 2023 Ă„min Baumeler and Eleftherios-Ermis Tselentis +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#include + +int main(int argc, char *argv[]) { + // Parse command-line arguments + if(argc != 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + fprintf(stderr, " 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; +}