int searchController(Graph graph, int start, int stop, int option) { Dllist close; Dllist node; close = new_dllist(); switch(option) { case BFS_SEARCH: breathFirstSearch(graph, start, stop, close); break; case BFS_TRAVERSE: traverseBFS(graph, start, close); break; case DFS_SEARCH: deepFirstSearch(graph, start, stop, close); break; case DFS_TRAVERSE: traverseDFS(graph, start, close); break; default: printf("This is not an option\n"); free_dllist(close); return; } node = dll_first(close); printf("Visit %d\n", jval_i(node->val)); free_dllist(close); }
main() { IS is; Queue q; Stack s; Dllist l; int i; Jval j; is = new_inputstruct(NULL); while (get_line(is) > 0) { q = new_queue(); s = new_stack(); l = new_dllist(); for (i = 0; i < strlen(is->fields[0]); i++) { queue_enqueue(q, new_jval_c(is->fields[0][i])); stack_push(s, new_jval_c(is->fields[0][i])); dll_append(l, new_jval_c(is->fields[0][i])); dll_prepend(l, new_jval_c(is->fields[0][i])); } while (!queue_empty(q)) { j = queue_dequeue(q); printf("%c", j.c); j = stack_pop(s); printf("%c", j.c); printf("%c", l->flink->val.c); dll_delete_node(l->flink); printf("%c", l->flink->val.c); dll_delete_node(l->flink); printf(" "); } printf("\n"); free_queue(q); free_stack(s); free_dllist(l); } }
int deepFirstSearch(Graph graph, int start, int stop, Dllist close) { Dllist node, stack; JRB visited; int output[100]; int temp; int i, n; visited = make_jrb(); stack = new_dllist(); dll_prepend(stack, new_jval_i(start)); while(!dll_empty(stack)) { node = dll_first(stack); temp = jval_i(node->val); dll_delete_node(node); if(jrb_find_int(visited, temp) == NULL) { // reportFunc(temp); dll_append(close, new_jval_i(temp)); jrb_insert_int(visited, temp, new_jval_i(temp)); if(compare(temp, stop) == 0) { jrb_free_tree(visited); free_dllist(stack); return 1; } n = outdegree(graph, temp, output); for(i = 0; i < n; i++) { if(jrb_find_int(visited, output[i]) == NULL) { dll_prepend(stack, new_jval_i(output[i])); } } } } jrb_free_tree(visited); free_dllist(stack); return 0; }
/* parseResult * Parses a message containing the graph adjacency matrix * Message format "[graphsize]:[graph adjacency matrix]" */ static void parseResult(char *pch) { /* Get gsize */ pch = strtok(NULL, ":"); int gsize = atoi(pch); /* Get Clique Count */ pch = strtok(NULL, ":"); int clCount = atoi(pch); /* Get matrix */ pch = strtok(NULL, ":"); int *g = ChartoGraph(pch, gsize); /* Verify integrity of g */ int realCount = CliqueCount(g, gsize); /* Message is invalid */ if (realCount != clCount) { fprintf(stderr, "Message could not be validated!\n"); fprintf(stderr, "Clique count from message: %d, actual clique count: %d!\n", clCount, realCount); return; } /* Update scheduler */ if(clCount == 0) { fprintf(stderr, "Counterexample successfully received!\n"); if(gsize > _Scheduler->currCEsize) { /* Found a counterexample */ /* Update Scheduler */ _Scheduler->currCEsize = gsize; /* clear list and add new counterexample */ free_dllist(_Scheduler->counterExamples); _Scheduler->counterExamples = new_dllist(); _Scheduler->listSize = 0; addCounterExample(g); /* Update current pointer */ _Scheduler->currPtr = dll_first(_Scheduler->counterExamples); /*print only when save a counterexample*/ fprintf(stderr, "get a counterexample with bigger size, size: %d\n, currCEsize: %d\n", gsize, _Scheduler->currCEsize); /* Save counterexample into a file */ SaveGraph(g,gsize, "../../../counterexamples"); } /* Just add new counterexample */ else if(gsize == _Scheduler->currCEsize) { fprintf(stderr, "Saving a counterexample with same size\n"); addCounterExample(g); SaveGraph(g,gsize, "../../../counterexamples"); } } }
int solution(Graph graph, Jval start, Jval stop, Dllist stackVisit, Jval (*cloneFunc)(Jval), int (*compare)(Jval, Jval), void (*reportFunc)(Jval)) { Dllist stackRes; Dllist node; Jval nowNode, temp; int counter = 0; stackRes = new_dllist(); node = dll_first(stackVisit); nowNode = cloneFunc(node->val); dll_delete_node(node); dll_prepend(stackRes, nowNode); while(!dll_empty(stackVisit)) { if(isAdjacent(graph, nowNode, start, compare)) { dll_prepend(stackRes, start); counter++; break; } do { node = dll_first(stackVisit); temp = cloneFunc(node->val); dll_delete_node(node); if(isAdjacent(graph, nowNode, temp, compare)) { dll_prepend(stackRes, temp); nowNode = temp; counter++; break; } } while(!dll_empty(stackVisit)); } printf("Solution: The shortest path between two node: \n"); while(!dll_empty(stackRes)) { node = dll_first(stackRes); reportFunc(node->val); dll_delete_node(node); } free_dllist(stackVisit); return counter; }
void BFS(Graph graph, Jval start, Jval stop, Jval (*cloneFunc)(Jval), int (*compare)(Jval, Jval), void (*reportFunc)(Jval)) { Dllist node, queue; JRB visited; Jval *output; Jval temp, tmp; int i, n; visited = make_jrb(); queue = new_dllist(); dll_append(queue, start); if((output = myMalloc(sizeof(Jval), 100)) == NULL) { return; } while(!dll_empty(queue)) { node = dll_first(queue); temp = cloneFunc(node->val); dll_delete_node(node); if(jrb_find_gen(visited, temp, compare) == NULL) { reportFunc(temp); jrb_insert_gen(visited, temp, temp, compare); if(compare(temp, stop) == 0) { jrb_free_tree(visited); free_dllist(queue); free(output); return; } n = getAdjacentVertices(graph, temp, output, compare); for(i = 0; i < n; i++) { if(jrb_find_gen(visited, output[i], compare) == NULL) { dll_append(queue, output[i]); } } } } }