Skip to content

Instantly share code, notes, and snippets.

@lucarinelli
Created January 24, 2018 16:20
Show Gist options
  • Save lucarinelli/8328eef4da8e1c5568dbe240efbf8979 to your computer and use it in GitHub Desktop.
Save lucarinelli/8328eef4da8e1c5568dbe240efbf8979 to your computer and use it in GitHub Desktop.
20160909 my solution
#include <stdio.h>
#include <sdtlib.h>
#define lastWords(a,b) if(a){printf(b);exit(EXIT_FAILURE);}
typedef struct v_item_t v_item_t;
typedef struct v_set_t v_set_t;
typedef struct v_t v_t;
typedef struct simbolTable_t st_t;
struct v_item_t{
v_t *this;
v_item_t *next,*previous;
int st_index;//?needed?
}
struct v_set_t{
st_t *taken;
v_item_t *list;
}
struct v_t{
char *name;
v_item_t *edges;
}
struct symbolTable_t{
int size;
char **names;
v_item_t **pointers;
}
v_set_t *loadSet(FILE *input);
v_set_t *selectVerticesFromFile(FILE *input, v_set_t *set);
int verifyIS(v_set_t *set);
v_set_t *maxIS(v_set_t *set);
v_set_t *maxIS_r(v_set_t *set, v_item_t *from);
v_item_t stPointerByName(st_t *st, char *n);
void stRemove(st_t *st, char *n);
void fprintSet(FILE *output, v_set_t *set);
void freeSet(v_set_t *set);
void freeSetNoV(v_set_t *set);
void stInsert(st_t *st, char *n, v_item_t *pointer);
int main(int argc, char **argv){
FILE *input1, *input2, *output;
v_set_t *set, *reduced, *max;
lastWords(argc!=4,"Wrong parameters, expected 3 file names.\n");
input1=fopen(argv[1],"r");
lastWords(input1==NULL,"Can NOT open first input file.\n");
set=loadSet(input1);
fclose(input1);
input2=fopen(argv[2],"r");
lastWords(input2==NULL,"Can NOT open second input file.\n");
reduced=selectVerticesFromFile(input2,set);
fclose(input2);
if(verifyIS(reduced))
printf("It's an independent set!\n");
else
printf("Not an independent set!\n");
freeSetNoV(reduced);
max=maxIS(set);
output=fopen(argv[3],"w");
lastWords(output==NULL,"Error output file.\n");
fprintSet(output,max);
fclose(output);
freeSet(set);
freeSet(max);
return 0;
}
int verifySet(v_set_t *set){
v_item_t *tmpv,*tmpe;
for(tmpv=set->list;tmp!=NULL;tmpv=tmpv->next)
for(tmpe=tmpv->this->edges;tmpe!=NULL;tmpe=tmpe->next)
if(stPointerByName(set->taken,tmpe->this->name)!=NULL)
return 0;
return 1;
}
v_set_t *maxIS_r(v_set_t *set, v_item_t *from){
v_item_t *tmp;
if(verifyIS(set))return set;
for(tmp=from;tmp!=NULL;tmp=tmp->next){
stRemove(set->taken,tmp->this->name);
set->list=listRemove(set->list,tmp);
maxIS(set,tmp->next);
set->list=listInsertBefore(set->list,tmp->next,tmp);
stInsert(set->taken,tmp->this->name,tmp);
maxIS(set,tmp->next);
}
return NULL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment