Exemplo n.º 1
0
void Graph::build_dfs(int index_node){
    dfs_visit(index_node);
    for(auto n:list_graph){
        if(n.second.color==white){
            dfs_visit(n.first);
        }
    }
    print_dfs();
}
Exemplo n.º 2
0
Arquivo: main.c Projeto: LinusMelb/C
int main(int argc, char** argv) {
	
	// parse command line options
	Options options = get_options(argc, argv);

	// build graph from data file
	Graph* graph = read_graph(options.filename);

	// validate vertex ids now that we know graph size
	validate_vertex_ids(options, graph->n);

	// branch to relevant function depending on execution mode
	switch (options.part) {
		case PRINT_DFS:
			print_dfs(graph, options.source);
			break;
		case PRINT_BFS:
			print_bfs(graph, options.source);
			break;
		case DETAILED_PATH:
			detailed_path(graph, options.source, options.dest);
			break;
		case ALL_PATHS:
			all_paths(graph, options.source, options.dest);
			break;
		case SHORTEST_PATH:
			shortest_path(graph, options.source, options.dest);
			break;
	}

	// clean up
	free_graph(graph);

	// done!
	exit(EXIT_SUCCESS);
}
Exemplo n.º 3
0
static int judge_backwards(struct gspan *gs, GList *right_most_path, GList *projection,
						GHashTable *pm_backwards)
{
	GList *l1, *l2, *l3, *values = NULL;
	int i;
	int rmp0;

	rmp0 = 	GPOINTER_TO_INT(g_list_nth_data(right_most_path, 0));

	for (i=g_list_length(right_most_path)-2,l1=g_list_last(right_most_path);
					i >= 0; i--, l1 = g_list_previous(l1)) {
		int rmp = GPOINTER_TO_INT(l1->data);
		int rmpi = GPOINTER_TO_INT(g_list_nth_data(right_most_path, i));


		for(l2 = g_list_first(projection); l2; l2 = g_list_next(l2)) {
			struct pre_dfs *p = (struct pre_dfs *)l2->data;
			struct history *h;
			struct edge *last_edge;
			struct node *last_node, *to_node, *from_node;
			struct edge *edge;
			
			h = alloc_history();
			build_history(h, p);

			last_edge = (struct edge *)
					g_list_nth_data(h->edges, rmp0);
			last_node = graph_get_node(gs->min_graph, 
								last_edge->to);
			edge = (struct edge *)
					g_list_nth_data(h->edges, rmpi);
			to_node = graph_get_node(gs->min_graph, edge->to);
			from_node = graph_get_node(gs->min_graph, edge->from);

			for (l3 = g_list_first(last_node->edges); l3; 
							l3 = g_list_next(l3)) {
				struct edge *e = (struct edge *)l3->data;

				if (g_list_find(h->has_edges, 
							GINT_TO_POINTER(e->id)))
					continue;

				if (!g_list_find(h->has_nodes, 
							GINT_TO_POINTER(e->to)))
					continue;

				if (e->to == edge->from && 
						(e->label > edge->label || 
						(e->label == edge->label &&
							last_node->label > 
							to_node->label))) {
					struct dfs_code *dfsc;
					struct pre_dfs *npdfs;
					int from_id;
					int to_id;

					from_id = ((struct dfs_code *)
							g_list_nth_data(
							gs->min_dfs_codes, 
							rmp0))->to;

					to_id = ((struct dfs_code *)
							g_list_nth_data(
							gs->min_dfs_codes,
							rmpi))->from;
#ifdef DEBUG
					printf("!! %d %d %d ", i, rmp, rmpi);
					print_dfs(g_list_nth_data(
							gs->min_dfs_codes,
							rmpi));
					printf("\n");
					print_id_list(right_most_path);
#endif
					dfsc = malloc(sizeof(struct dfs_code));
					if (!dfsc) {
						perror("malloc dfsc in jb()");
						exit(1);
					}
					dfsc->from = from_id;
					dfsc->to = to_id;
					dfsc->from_label = last_node->label;
					dfsc->edge_label = e->label;
					dfsc->to_label = from_node->label;

					npdfs = malloc(sizeof(struct pre_dfs));
					if (!npdfs) {
						perror("malloc npdfs in jb()");
						exit(1);
					}
					npdfs->id = 0;
					npdfs->edge = e;
					npdfs->prev = p;

					if (g_hash_table_contains(pm_backwards, 
									dfsc))
						values = g_hash_table_lookup(
							pm_backwards,dfsc);
					values = g_list_append(values, npdfs);
					g_hash_table_insert(pm_backwards, dfsc,
									values);
					values = NULL;

				}
			}
			free_history(h);
		}

		if (g_hash_table_size(pm_backwards) > 0)
			return 1;
	}

	return 0;
}