/* * graph_dfs() * DFS que encontra as articulacoes do grafo */ void graph_dfs(Graph g, vertex me, vertex art[], int *n) { link p; vertex other; int c = 0; pre[me] = low[me] = cnt++; for(p=g->adj[me]; p; p=p->next) { other = p->v; if(pre[other] == -1) { parnt[other] = me; graph_dfs(g, other, art, n); if(low[other] < low[me]) low[me] = low[other]; if(low[other] >= pre[me]) c++; } else if(parnt[me] != other && pre[other] < low[me]) low[me] = pre[other]; } if((parnt[me] != me && c > 0) || (parnt[me] == me && c > 1)) art[ (*n)++ ] = me; }
int graph_dfs(graph_t *graph, gnode* start_gnode, int vpath[], int vpathIndex, int cost, int cvMNum, int vmustNum) { gnode_t * gnode = NULL; if (graph->visit[start_gnode->nodeIndex]) return 0; //printf(" V%d,", start); if(start_gnode->nodeIndex == t){/ if(cvMNum == vmustNum){ for(int i = 0; i < vpathIndex; i++){ printf("%d->",vpath[i]); } printf("\b\bcost:%d!\n", cost); } return 0; } graph->visit[start_gnode->nodeIndex] = 1; if(graph->relation[start_gnode->nodeIndex].vmust){ cvMNum++; } cost += start_gnode->weight; gnode = graph_first_adj(graph, start); while (gnode != NULL) { graph_dfs(graph, gnode, vpath, vpathIndex, cost, cvMNum, vmustNum); gnode = graph_next_adj(graph, start, gnode->nodeIndex); } graph->visit[start_gnode->nodeIndex] = 0; return 0; }
void graph_dfs(fsnode **graph, int vert) { fsnode *w; visited[vert] = TRUE; printf("%5d", vert); for (w = graph[vert]; w; w = w->link) if (!visited[w->vert]) graph_dfs(graph, w->vert); }
void graph_dfs(struct graph *g, int v) { int i; g->visited[v - 1] = 1; printf("Vertex %d\n", v); for (i = 0; i < g->nvertices; i++) { if (g->m[v - 1][i] > 0 && g->visited[i] == 0) { graph_dfs(g, i + 1); } } }
int main(void) { int d = 0; int i = 0; int x = 0; graph my_graph = NULL; if (1 == scanf("%d", &d)) { my_graph = graph_new(d); } while (2 == scanf("%d%d", &i, &x)) { graph_add_edge(my_graph, i, x); } /* graph my_graph = graph_new(8); graph_bi_add_edge(my_graph, 0, 1); graph_bi_add_edge(my_graph, 0, 4); graph_bi_add_edge(my_graph, 1, 0); graph_bi_add_edge(my_graph, 1, 5); graph_bi_add_edge(my_graph, 2, 3); graph_bi_add_edge(my_graph, 2, 5); graph_bi_add_edge(my_graph, 2, 6); graph_bi_add_edge(my_graph, 3, 2); graph_bi_add_edge(my_graph, 3, 6); graph_bi_add_edge(my_graph, 3, 7); graph_bi_add_edge(my_graph, 4, 0); graph_bi_add_edge(my_graph, 5, 1); graph_bi_add_edge(my_graph, 5, 2); graph_bi_add_edge(my_graph, 5, 6); graph_bi_add_edge(my_graph, 6, 2); graph_bi_add_edge(my_graph, 6, 3); graph_bi_add_edge(my_graph, 6, 5); graph_bi_add_edge(my_graph, 6, 7); graph_bi_add_edge(my_graph, 7, 3); graph_bi_add_edge(my_graph, 7, 6); */ graph_dfs(my_graph); graph_print(my_graph); graph_free(my_graph); return EXIT_SUCCESS; }
TEST(SEARCH, depth_first_search) { int i; fsnode **list = newFSNodes(); for (i = 0; i < FSNODE_N; i++) { printf("%d : ", i); graph_showList(list[i]); } graph_fsoper_init(); graph_dfs(list, 0); printf("\n"); }
/* * graph_findarts() * Encontra e devolve todas as articulacoes do grafo */ void graph_findarts(Graph g, vertex art[], int *n) { vertex v; *n = cnt = 0; for(v=0; v<g->v; v++) pre[v] = low[v] = parnt[v] = -1; for(v=0; v<g->v; v++) { if(pre[v] == -1) { parnt[v] = v; graph_dfs(g, v, art, n); } } }
int is_connected(vertex *root) { // First check if all vertices can be visited doing a DFS or A BFS // Since BFS was already done along with DFS for checking edge, for practice sake lets do a DFS graph_dfs(root); // Now check if all vertices have been marked as visited, for those with an edgecount > 0 vertex *node = root; while (node != NULL) { if (!node->visited && get_edge_count(node)) return 0; node = node->nextvertex; } return 1; }
void test_six_vertex_complete_graph() { Graph g = { 6, { {0, 1, 1, 1, 1, 1, }, {1, 0, 1, 1, 1, 1, }, {1, 1, 0, 1, 1, 1, }, {1, 1, 1, 0, 1, 1, }, {1, 1, 1, 1, 0, 1, }, {1, 1, 1, 1, 1, 0, } }, {"a", "b", "c", "d", "e", "f"}, }; graph_dfs(g, printer); }
// DFS from a given node void graph_dfs(vertex *root) { if (root == NULL) { return; } // Mark as being visited root->visited = 1; printf("visited %c\n", root->element); edge *node = root->nextedge; while (node != NULL) { // If node->link is being visited, do not visit again if (!node->link->visited) graph_dfs(node->link); node = node->nextedge; } }
void test_generic_graph() { Graph g = { 8, { {0, 1, 0, 0, 1, 1, 0, 0}, {1, 0, 1, 0, 0, 0, 1, 0}, {0, 1, 0, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 1, 0, 0, 1}, {1, 0, 0, 1, 0, 0, 1, 1}, {1, 0, 0, 0, 0, 0, 1, 0}, {0, 1, 1, 0, 1, 1, 0, 0}, {0, 0, 0, 1, 1, 0, 0, 0} }, {"a", "b", "c", "d", "e", "f", "g", "h"}, }; graph_dfs(g, printer); }
int graph_toposort(struct graph *graph, struct stack *topost) { /* Reset color */ int i, cycle = 0; for (i = 0; i < graph->nr; ++i) { vertex_dfs_reset(graph->vertices + i); } for (i = 0; i < graph->nr; ++i) { if (graph->vertices[i].color == WHITE) { cycle = graph_dfs(graph, i, topost); if (cycle) { break; } } } return cycle; }
int main(int argc, char *argv[]) { int v1; int v2; graph g; char option; int size = 0; graph_t type = DIRECTED; const char *optstring = "s:"; while((option = getopt(argc, argv, optstring)) != EOF) { switch(option) { case 's': size = atoi(optarg); break; default: printf("input the number of vertices\n"); return EXIT_FAILURE; } } g = graph_new(size); while(2==scanf("%d%d", &v1, &v2)) { g = graph_add_edge(g, v1, v2, type); } /** g = graph_add_edge(g, 0, 1, type); g = graph_add_edge(g, 0, 4, type); g = graph_add_edge(g, 5, 1, type); g = graph_add_edge(g, 5, 2, type); g = graph_add_edge(g, 5, 6, type); g = graph_add_edge(g, 6, 2, type); g = graph_add_edge(g, 6, 3, type); g = graph_add_edge(g, 6, 7, type); g = graph_add_edge(g, 3, 2, type); g = graph_add_edge(g, 3, 7, type); */ graph_print(g); graph_dfs(g); g = graph_free(g); return EXIT_SUCCESS; }
void test_three_vertex_graph() { Graph g = {3, {{0, 1, 0}, {1, 0, 1}, {0, 1, 0} }, {"a", "b", "c"} }; graph_dfs(g, printer); }