Exemplo n.º 1
0
Arquivo: bfs.c Projeto: wqferr/P-A-T-H
void solver_bfs_destroy(solver *s) {
	bfs_data bfs = *((bfs_data *) solver_get_data(s));
	set_destroy(bfs.visited);
	list_destroy(bfs.queue);
	map_destroy(bfs.parent);
	free(solver_get_data(s));
	solver_destroy(s);
}
Exemplo n.º 2
0
Arquivo: driver.c Projeto: tuxfan/ska
int main(int argc, char *argv[])
{
	PetscErrorCode ierr;
	int i;
	grid *grd;

	MPI_Init(&argc,&argv);

	option options = parse_options(argc, argv);

	if (options.problem == JUMP) {
		grd = grid_create(-1, 1, options.nx,
		                  -1, 1, options.ny,
		                  -1, 1, options.nz);
	} else {
		grd = grid_create(0, 1, options.nx,
		                  0, 1, options.ny,
		                  0, 1, options.nz);
	}
	if (options.periodic > -1) grd->periodic[options.periodic] = 1;
	int rank;
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
	if (rank == 0) print_intro(&options);

	map *mp = map_create(options.map);
	grid_apply_map(grd, mp);

	problem *pb = problem_create(options.problem, grd->nd, mp->id);
	solver *sol = solver_create(grd, pb);

	ierr = solver_init(sol, grd);CHKERRQ(ierr);
	ierr = solver_run(sol);CHKERRQ(ierr);

	double *u = (double*) malloc(grd->num_pts*sizeof(double));
	double *diff = (double*) malloc(grd->num_pts*sizeof(double));
	grid_eval(grd, pb->sol, u);

	for (i = 0; i < grd->num_pts; i++)
		diff[i] = sol->state->phi[i] - u[i];

	print_norm(diff, grd->num_pts);

	free(u); free(diff);
	grid_destroy(grd); free(grd);
	map_destroy(mp); free(mp);
	problem_destroy(pb); free(pb);
	ierr = solver_destroy(sol);CHKERRQ(ierr); free(sol);

	if (options.eig && rank == 0) {
		system("./python/plot.py");
	}

	MPI_Finalize();

	return 0;
}
Exemplo n.º 3
0
int
main(int argc, char *argv[])
{ 
    if (argc != 2) {
        fprintf(stderr, "Expected 2 arguments\n");
        exit(1);
    }
    char *fname = argv[1];

    Solver solver;
    solver_init(&solver);

    printf("reading %s\n", fname);
    solver_read_file(&solver, fname);

    const bool solution_found = solver_naive(&solver);
    if (solution_found) {
        print_values(solver.values, solver.n_variables);
    }
    write_results(&solver, solution_found);

    solver_destroy(&solver);
}
Exemplo n.º 4
0
// This is a cheesy function that is not meant to go inside an inner loop.
// It is OK for one-off solving of 'plan b' puzzles,
// but for inner loops all of the structures should be created
// and initialized outside of the loop.
int solve_potential_bond_graph(
    const int *row_ptr, const int *col_ind, int nvertices, int k,
    int *binary_solution, int *pbest_score)
{
  int failflag = 0;

  int max_nvertices = nvertices;
  int max_ncomponents = nvertices;
  int max_target = nvertices;
  int max_nedges_undirected = nvertices + 1;
  int max_nedges_directed = max_nedges_undirected * 2;

  // Breadth first search, girth, and cycle allocation and initialization.
  BFS_WS bfs_ws;
  bfs_ws_init(&bfs_ws, nvertices);
  int *depth_ws = (int *) malloc(nvertices * sizeof(int));
  int *va_trace = (int *) malloc(nvertices * sizeof(int));
  int *vb_trace = (int *) malloc(nvertices * sizeof(int));

  int i, v;

  // Clear parent and depth arrays for girth and cycle related functions.
  for (v=0; v<nvertices; ++v) {
    bfs_ws.parent[v] = -1;
    depth_ws[v] = -1;
  }

  // Prepare to decompose the graph into connected components.
  int *component_labels = (int *) malloc(nvertices * sizeof(int));
  for (v=0; v<nvertices; ++v) {
    component_labels[v] = -1;
  }

  // Compute the connected components.
  CCGRAPH g;
  ccgraph_init(&g, max_nvertices, max_nedges_directed);
  ccgraph_compute(&g, row_ptr, col_ind, nvertices, &bfs_ws, component_labels);

  // Get the number of connected components that have cycles.
  // The purpose is to limit the size of the dynamic programming table
  // that is preallocated for solving the subset sum problem.
  int cyclic_component_count = 0;
  int c;
  for (c=0; c<g.ncomponents; ++c)
  {
    int component_nedges_undirected = ccgraph_get_component_nedges_undirected(
        &g, c);
    int component_nvertices = ccgraph_get_component_nvertices(
        &g, c);
    if (component_nedges_undirected >= component_nvertices) {
      cyclic_component_count++;
    }
  }

  // Initialize the subset sum solver workspaces.
  int *high = (int *) malloc(max_target * sizeof(int));
  int *low = (int *) malloc(max_target * sizeof(int));
  int *s3solution = (int *) malloc(cyclic_component_count * sizeof(int));
  S3TABLE s3table;
  s3table_init(&s3table, cyclic_component_count, max_target);
  s3table_clear(&s3table, cyclic_component_count, max_target);

  // Init the densest k-subgraph solver.
  SOLVER solver;
  solver_init(&solver, max_nvertices, max_ncomponents);

  // Get the densest k-subgraph.
  solver_toplevel(&solver,
      row_ptr, col_ind, &g, k,
      va_trace, vb_trace,
      &bfs_ws, depth_ws,
      &s3table, low, high, s3solution);

  // Require that the solution is complete.
  if (solver.nsolution != k) {
    print_csr_graph(row_ptr, col_ind, nvertices);
    assert(false);
  }

  // Fill the output values.
  memset(binary_solution, 0, nvertices * sizeof(int));
  for (i=0; i<solver.nsolution; ++i) {
    v = solver.solution[i];
    binary_solution[v] = 1;
  }
  *pbest_score = solver.score;

  // Free the memory.
  bfs_ws_destroy(&bfs_ws);
  ccgraph_destroy(&g);
  s3table_destroy(&s3table);
  solver_destroy(&solver);
  free(high);
  free(low);
  free(s3solution);
  free(component_labels);
  free(va_trace);
  free(vb_trace);
  free(depth_ws);

  return failflag;
}