int bfs(void) { Graph gr; Slist network; struct BfsVertexdata_ bfstmp; BfsVertexdata netdata; int tmpid, retval; SlistNode listnode; my_clearscrn(); printf("--- NETWORK HOPS/BFS DEMO ---"); gr = GRAPHinit(bfs_match, bfs_destroy); /* Read net node(=vertex) data into graph.. */ if ((read_netnodes(gr, net_nodes, NR_OF_NETNODES)) != OK) { fprintf(stderr, "Fatal error when reading netnode data - bailing out.."); GRAPHdestroy(gr); exit(-1); } /* Read net connection(=edge) data into graph.. */ if ((read_netconnections(gr, net_connections, NR_OF_NETNODES)) != OK) { fprintf(stderr, "Fatal error when reading netconnections data - bailing out.."); GRAPHdestroy(gr); exit(-1); } prompt_and_pause("\n\nGraph initialized and graph data read.."); printf("\nGRAPH:"); /* Display graph.. */ GRAPHprint(gr, prt_bfs_vtx, prt_bfs_edge); printf("\nNr of vertices/edges: %d/%d", GRAPHvcount(gr), GRAPHecount(gr)); tmpid = read_int("\nEnter startnode id ", 1, 6); bfstmp.data = &tmpid; if ((retval = ALGObfs(gr, &bfstmp, &network, bfs_match)) != OK) { fprintf(stderr, "\nFatal error when calling 'ALGObfs()'(Return value: %d) - Bailing out..", retval); GRAPHdestroy(gr); exit(-1); } printf("\nNetwork Hops(BFS Analysis)\n--------------------------"); for (listnode = SLISThead(network); listnode != NULL; listnode = SLISTnext(listnode)) { netdata = (BfsVertexdata)SLISTdata(listnode); printf("\nNode%02d, color=%d, hops=%d", *(int *)netdata->data, netdata->color, netdata->hops); } prompt_and_pause("\n\nTime to tidy up.."); SLISTdestroy(network); GRAPHdestroy(gr); return OK; }
int dfs(void) { Graph gr; Slist tasks; DfsVertexdata taskdata; int retval; SlistNode listnode; my_clearscrn(); printf("--- TOPOLOGICAL SORTING/DFS DEMO ---"); gr = GRAPHinit(dfs_match, dfs_destroy); /* Read task node(=vertex) data into graph.. */ if ((read_tasknodes(gr, task_nodes, NR_OF_TASKNODES)) != OK) { fprintf(stderr, "Fatal error when reading task node data - bailing out.."); GRAPHdestroy(gr); exit(-1); } /* Read task connection(=edge) data into graph.. */ if ((read_taskconnections(gr, task_connections, NR_OF_TASKNODES)) != OK) { fprintf(stderr, "Fatal error when reading taskconnections data - bailing out.."); GRAPHdestroy(gr); exit(-1); } prompt_and_pause("\n\nGraph created and graph data read.."); printf("\nGRAPH:"); /* Display graph.. */ GRAPHprint(gr, prt_dfs_vtx, prt_dfs_edge); printf("\nNr of vertices/edges: %d/%d", GRAPHvcount(gr), GRAPHecount(gr)); if ((retval = ALGOdfs(gr, &tasks)) != OK) { fprintf(stderr, "\nFatal error when calling 'ALGOdfs()'(Return value: %d) - Bailing out..", retval); GRAPHdestroy(gr); exit(-1); } printf("\n\nTopological Sort(DFS Analysis):\n-------------------------------"); for (listnode = SLISThead(tasks); listnode != NULL; listnode = SLISTnext(listnode)) { taskdata = (DfsVertexdata)SLISTdata(listnode); printf("\nNode %c, color=%d", *(char *)taskdata->data, taskdata->color); } prompt_and_pause("\n\nTime to tidy up.."); SLISTdestroy(tasks); GRAPHdestroy(gr); return OK; }
int main(int argc, char * argv[]){ Graph g1 = graphScan("test1.txt"); printf("test1.txt\n"); GRAPHshow(g1); assert(eulerPath(g1) == 1); GRAPHdestroy(g1); Graph g2 = graphScan("test2.txt"); printf("test2.txt\n"); GRAPHshow(g2); assert(eulerPath(g2) == 2); GRAPHdestroy(g2); Graph g3 = graphScan("test3.txt"); printf("test3.txt\n"); GRAPHshow(g3); assert(eulerPath(g3) == 0); GRAPHdestroy(g3); return 0; }
void solver(){ int instance; Graph G; instance = 1; G = inputInstance(); while(G != NULL){ outInstance(instance, processInstance(G)); instance++; GRAPHdestroy(G); G = inputInstance(); } }
/* --- Function: void basics(void) --- */ int basics(void) { Graph gr; VertexNode vnode; EdgeNode enode; char data[STRSIZ], *pdata1, *pdata2; int retval; char mess[MESSIZ]; /* Initialize the graph. */ gr = GRAPHinit(bas_match, free); my_clearscrn(); printf("--- BASIC GRAPH USAGE ---\n\nGraph created.. - let's do some basic graph editing.."); /* --- Perform some graph operations --- */ /* --- Start with inserting some vertices.. --- */ /* Insert first vertex.. */ if ((pdata1 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(pdata1, "a"); sprintf(mess, "\n\nInsert first vertex: %s", pdata1); prompt_and_pause(mess); if (GRAPHinsvertex(gr, pdata1) != 0) /* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Insert next vertex.. */ if ((pdata1 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(pdata1, "b"); sprintf(mess, "\n\nInsert vertex: %s", pdata1); prompt_and_pause(mess); if (GRAPHinsvertex(gr, pdata1) != 0) /* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Insert next vertex.. */ if ((pdata1 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(pdata1, "c"); sprintf(mess, "\n\nInsert vertex: %s", pdata1); prompt_and_pause(mess); if (GRAPHinsvertex(gr, pdata1) != 0) /* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Insert next vertex.. */ if ((pdata1 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(pdata1, "d"); sprintf(mess, "\n\nInsert vertex: %s", pdata1); prompt_and_pause(mess); if (GRAPHinsvertex(gr, pdata1) != 0) /* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Insert next vertex.. */ if ((pdata1 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(pdata1, "e"); sprintf(mess, "\n\nInsert vertex: %s", pdata1); prompt_and_pause(mess); if (GRAPHinsvertex(gr, pdata1) != 0) /* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); prompt_and_pause("\n\nCheck out all vertex insertions above.."); /* --- Now, let's insert some edges.. --- */ /* Insert first edge.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "a"); strcpy(pdata2, "b"); sprintf(mess, "\nNow - let's insert edge: %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHinsedge(gr, data, pdata2) != 0) /* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Insert next edge.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "a"); strcpy(pdata2, "c"); sprintf(mess, "\n\nInsert edge: %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHinsedge(gr, data, pdata2) != 0) return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Insert next edge.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "b"); strcpy(pdata2, "c"); sprintf(mess, "\n\nInsert edge: %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHinsedge(gr, data, pdata2) != 0) /* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Insert next edge.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "b"); strcpy(pdata2, "d"); sprintf(mess, "\n\nInsert edge: %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHinsedge(gr, data, pdata2) != 0) /* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Insert next edge.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "c"); strcpy(pdata2, "b"); sprintf(mess, "\n\nInsert edge: %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHinsedge(gr, data, pdata2) != 0) /* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Insert next edge.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "c"); strcpy(pdata2, "c"); sprintf(mess, "\n\nInsert edge: %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHinsedge(gr, data, pdata2) != 0)/* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Insert next edge.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "c"); strcpy(pdata2, "d"); sprintf(mess, "\n\nInsert edge: %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHinsedge(gr, data, pdata2) != 0) /* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Insert next edge.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "d"); strcpy(pdata2, "a"); sprintf(mess, "\n\nInsert edge: %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHinsedge(gr, data, pdata2) != 0) /* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Insert next edge.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "e"); strcpy(pdata2, "c"); sprintf(mess, "\n\nInsert edge: %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHinsedge(gr, data, pdata2) != 0) /* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Insert next edge.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "e"); strcpy(pdata2, "d"); sprintf(mess, "\n\nInsert edge: %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHinsedge(gr, data, pdata2) != 0) /* Do the insertion.. */ return 1; /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Now, let's remove some edges.. */ /* Remove first edge.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "a"); strcpy(pdata2, "c"); pdata1 = pdata2; sprintf(mess, "\n\nRemove edge %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHremedge(gr, data, (void **)&pdata1) != 0) /* Do the removal.. */ { free(pdata2); return 1; } free(pdata1); /* Deallocate mem. held by removed edge.. */ /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Remove next edge.. */ strcpy(data, "c"); strcpy(pdata2, "c"); pdata1 = pdata2; sprintf(mess, "\n\nRemove edge %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHremedge(gr, data, (void **)&pdata1) != 0) /* Do the removal.. */ { free(pdata2); return 1; } free(pdata1); /* Deallocate mem. held by removed edge.. */ /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Remove next edge.. */ strcpy(data, "e"); strcpy(pdata2, "c"); pdata1 = pdata2; sprintf(mess, "\n\nRemove edge %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHremedge(gr, data, (void **)&pdata1) != 0) /* Do the removal.. */ { free(pdata2); return 1; } free(pdata1); /* Deallocate mem. held by removed edge.. */ /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Remove next edge.. */ strcpy(data, "a"); strcpy(pdata2, "b"); pdata1 = pdata2; sprintf(mess, "\n\nRemove edge %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHremedge(gr, data, (void **)&pdata1) != 0) /* Do the removal.. */ { free(pdata2); return 1; } free(pdata1); /* Deallocate mem. held by removed edge.. */ /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Remove next edge.. */ strcpy(data, "d"); strcpy(pdata2, "a"); pdata1 = pdata2; sprintf(mess, "\n\nRemove edge %s --> %s", data, pdata2); prompt_and_pause(mess); if (GRAPHremedge(gr, data, (void **)&pdata1) != 0) /* Do the removal.. */ { free(pdata2); return 1; } free(pdata1); /* Deallocate mem. held by removed edge.. */ free(pdata2); /* Deallocate mem. held by search key value(=a).. */ /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* Now, lets remove a vertex.. */ strcpy(data, "a"); pdata1 = data; sprintf(mess, "\n\nRemove vertex %s", data); prompt_and_pause(mess); if (GRAPHremvertex(gr, (void **)&pdata1) != 0) /* Do the removal.. */ return 1; free(pdata1); /* Deallocate mem. held by removed vertex.. */ /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); /* --- Do some invalid insertions/removals --- */ /* First invalid operation.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "f"); strcpy(pdata2, "a"); sprintf(mess,"\n\nNow - try to insert an invalid edge from %s --> %s", data, pdata2); prompt_and_pause(mess); retval = GRAPHinsedge(gr, data, pdata2); /* Do the insertion.. */ if (retval != 0) free(pdata2); /* Deallocte mem. held by search key value(=a).. */ /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); print_errmess(retval); /* Next invalid operation.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "c"); strcpy(pdata2, "b"); sprintf(mess,"\nInsert an existing edge from %s --> %s", data, pdata2); prompt_and_pause(mess); retval = GRAPHinsedge(gr, data, pdata2); /* Do the insertion.. */ if (retval != 0) free(pdata2); /* Deallocte mem. held by search key value(=b).. */ /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); print_errmess(retval); /* Next invalid operation.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "f"); strcpy(pdata2, "a"); pdata1 = pdata2; retval = GRAPHremedge(gr, data, (void **)&pdata2); /* Do the removal.. */ sprintf(mess, "\nRemove an invalid edge from %s --> %s", data, pdata2); prompt_and_pause(mess); if (retval == 0) { free(pdata2); /* Deallocte mem. held by removed edge.. */ free(pdata1); /* Deallocte mem. held by search key value(=b).. */ return 1; } free(pdata2); /* Deallocte mem. held by search key value(=b).. */ /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); print_errmess(retval); /* Next invalid operation.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "c"); strcpy(pdata2, "e"); pdata1 = pdata2; retval = GRAPHremedge(gr, data, (void **)&pdata2); /* Do the removal.. */ sprintf(mess, "\nRemoving an invalid edge from %s --> %s", data, pdata2); prompt_and_pause(mess); if (retval == 0) { free(pdata2); /* Deallocte mem. held by removed edge.. */ free(pdata1); /* Deallocte mem. held by search key value(=e).. */ return 1; } free(pdata2); /* Deallocte mem. held by search key value(=e).. */ /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); print_errmess(retval); /* Next invalid operation.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(pdata2, "c"); retval = GRAPHinsvertex(gr, pdata2); /* Do the insertion.. */ sprintf(mess, "\nInsert an existing vertex %s", data); prompt_and_pause(mess); if (retval != 0) /* Insertion failed - which is the case here.. */ free(pdata2); /* Deallocte mem. held by search key value(=c).. */ /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); print_errmess(retval); /* Next invalid operation.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(pdata2, "c"); pdata1 = pdata2; retval = GRAPHremvertex(gr, (void **)&pdata2); /* Do the insertion.. */ sprintf(mess, "\nRemove an existing vertex %s", pdata1); prompt_and_pause(mess); if (retval == 0) { free(pdata2); /* Deallocte mem. held by removed vertex.. */ free(pdata1); /* Deallocte mem. held by search key value(=c).. */ return 1; } free(pdata2); /* Deallocte mem. held by search key value(=c).. */ /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); print_errmess(retval); /* Next invalid operation.. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(pdata2, "d"); pdata1 = pdata2; retval = GRAPHremvertex(gr, (void **)&pdata2); /* Do the insertion.. */ sprintf(mess, "\nRemove an existing vertex %s", pdata1); prompt_and_pause(mess); if (retval == 0) { free(pdata2); /* Deallocte mem. held by removed vertex.. */ free(pdata1); /* Deallocte mem. held by search key value(=d).. */ return 1; } free(pdata2); /* Deallocte mem. held by search key value(=d).. */ /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); print_errmess(retval); /* --- Testing GRAPHis_adjacent() --- */ /* First test of GRAPHis_adjacent().. */ if ((pdata2 = (char *)malloc(STRSIZ)) == NULL) return 1; strcpy(data, "b"); strcpy(pdata2, "d"); retval = GRAPHis_adjacent(gr, data, pdata2); /* Do the test.. */ sprintf(mess, "\nTest if (%s, %s) are adjacent...", data, pdata2); prompt_and_pause(mess); /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); print_testmess(retval); /* Next test of GRAPHis_adjacent().. */ strcpy(data, "a"); strcpy(pdata2, "e"); retval = GRAPHis_adjacent(gr, data, pdata2); /* Do the test.. */ sprintf(mess, "\n\nTest if (%s, %s) are adjacent...", data, pdata2); prompt_and_pause(mess); /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); print_testmess(retval); /* Next test of GRAPHis_adjacent().. */ strcpy(data, "e"); strcpy(pdata2, "d"); retval = GRAPHis_adjacent(gr, data, pdata2); /* Do the test.. */ sprintf(mess, "\n\nTest if (%s, %s) are adjacent...", data, pdata2); prompt_and_pause(mess); /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); print_testmess(retval); /* Next test of GRAPHis_adjacent().. */ strcpy(data, "c"); strcpy(pdata2, "a"); retval = GRAPHis_adjacent(gr, data, pdata2); /* Do the test.. */ sprintf(mess, "\n\nTest if (%s, %s) are adjacent...", data, pdata2); prompt_and_pause(mess); /* Print the result.. */ print_info(gr, prt_bas_vtx, prt_bas_edge); print_testmess(retval); /* Free temporary dyn. memory - no longer used.. */ free(pdata2); /* Print all adjacent vertices - to a certain vertex.. */ strcpy(data, "c"); sprintf(mess, "\n\nFinally - print vertices adjacent to %s: ", data); prompt_and_pause(mess); vnode = GRAPHfindvertex(gr, data); printf("\nVertices adjacent(=incident) from %s (%d pcs): ", data, GRAPHgetedgecount(vnode)); for (enode = GRAPHgetedgehead(vnode); enode != NULL; enode = GRAPHgetedgenext(enode)) { printf("%s ", (char *)GRAPHgetedgedata(enode)); } /* Destroy the graph. */ sprintf(mess, "\n\nTime to tidy up.."); prompt_and_pause(mess); GRAPHdestroy(gr); return OK; }