feedback-arc-set/generator.c

187 lines
3.3 KiB
C

/**
* @file generator.c
* @date 2018-05-21
*
* @brief Client program which generates solutions and submits them.
*/
#include "common.h"
#include <limits.h>
static const char *pname;
struct sigaction sa;
struct AdjListNode {
int dest;
struct AdjListNode *next;
};
struct AdjList {
struct AdjListNode *head;
};
struct Graph {
int V;
struct AdjList *array;
};
static volatile sig_atomic_t quit = 0;
static void sig_handler(int signum)
{
quit = 1;
}
void usage(void)
{
fprintf(stderr, "SYNOPSIS\n"
"\tgenerator EDGE1...\n"
"EXAMPLE\n"
"\tgenerator 0-1 1-2 1-3 1-4 2-4 3-6 4-3 4-5 6-0\n");
exit(EXIT_FAILURE);
}
struct AdjListNode *newAdjListNode(int dest)
{
struct AdjListNode *newNode = malloc(sizeof(struct AdjListNode));
newNode->dest = dest;
newNode->next = NULL;
return newNode;
}
struct Graph *createGraph(int V)
{
struct Graph *graph = malloc(sizeof(struct Graph));
graph->V = V;
graph->array = malloc(V * sizeof(struct AdjList));
for (int i = 0; i < V; i++)
graph->array[i].head = NULL;
return graph;
}
void addEdge(struct Graph *graph, int src, int dest)
{
struct AdjListNode *newNode = newAdjListNode(dest);
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
}
int strToInt(char *str)
{
char *endptr;
long val;
val = strtol(str, &endptr, 10);
if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) ||
(errno != 0)) {
perror(pname);
return -2;
}
if (val < INT_MIN || val > INT_MAX) {
fprintf(stderr, "[%s] Please choose a smaller node: ", pname);
return -1;
}
return (int) val;
}
struct Graph *parse(int argc, char *argv[])
{
char *edge;
char *token;
char *str;
int V = 0;
int src;
int dest;
int edges_counter = 0;
int edges[(argc - 1) * 2];
for (int i = 1; i < argc; i++) {
edge = argv[i];
str = strdup(edge);
int count = 0;
while ((token = strsep(&str, "-"))) {
if (count == 0) {
src = strToInt(token);
edges[edges_counter] = src;
edges_counter++;
if (src > V)
V = src;
}
if (count == 1) {
dest = strToInt(token);
edges[edges_counter] = dest;
edges_counter++;
if (dest > V)
V = dest;
}
if (count > 1) {
fprintf(stderr, "[%s] An edge consists of two "
"nodes separated by a dash\n", pname);
free(str);
exit(EXIT_FAILURE);
}
switch (src) {
case -2:
free(str);
exit(EXIT_FAILURE);
break;
case -1:
fprintf(stderr, "%s\n", edge);
free(str);
exit(EXIT_FAILURE);
break;
default:
break;
}
count++;
}
}
struct Graph *graph = createGraph(V+1);
for (int i = 0; i < (sizeof(edges) / sizeof(int)); i+=2) {
addEdge(graph, edges[i], edges[i+1]);
}
return graph;
}
void printGraph(struct Graph *graph)
{
for (int v = 0; v < graph->V; v++) {
struct AdjListNode* pCrawl = graph->array[v].head;
printf("\n Adjacency list of vertex %d\n head ", v);
while (pCrawl) {
printf("-> %d", pCrawl->dest);
pCrawl = pCrawl->next;
}
printf("\n");
}
}
int main(int argc, char *argv[])
{
if (argc == 1)
usage();
pname = argv[0];
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = &sig_handler;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
while (quit == 0) {
struct Graph *graph = parse(argc, argv);
printGraph(graph);
break;
}
return 0;
}