int main(void)
{
#define N_NODES 6
node_t *nodes = (node_t*)calloc(sizeof(node_t), N_NODES);
for (int i = 0; i < N_NODES; i++)
	sprintf(nodes[i].name, "%c", 'a' + i);

add_edge(nodes,   nodes+1,  7);   //a-b
add_edge(nodes,   nodes+2,  9);   //a-c
add_edge(nodes,   nodes+5, 14);   //a-f
add_edge(nodes+1, nodes+2, 10);   //b-c
add_edge(nodes+1, nodes+3, 15);   //b-d
add_edge(nodes+2, nodes+3, 11);   //c-d
add_edge(nodes+2, nodes+5,  2);   //c-f
add_edge(nodes+3, nodes+4,  6);   //d-e
add_edge(nodes+4, nodes+5,  9);   //e-f
 
heap = (node_t**)calloc(sizeof(node_t), N_NODES + 1);
heap_len = 0;
 
calc_all(nodes);

for (int i = 0; i < N_NODES; i++) {
	show_path(nodes + i);
	putchar('\n');
}

//free memories 
free_edges();
free(heap);
free(nodes);

return 0;
}
// ==================================================================================
// main
// ==================================================================================
int main(int argc, char *argv[]){
  // Handle parameter
  if(argc != 2 && argc != 3){
    fprintf(stderr, USAGE_MESSAGE, argv[0]);
    return EXIT_FAILURE;
  }
  size_t N = atoi(argv[1]);

  // Create N nodes
  node_t *nodes = calloc(N, sizeof(node_t));
  int i, j, distance;
  int edge_count = 0;
  srand48(time(NULL));
  for(i=0; i<(N-1); ++i){
    for(j=i+1; j<N; ++j){
      // Add edges
      distance = lrand48() % RANGE_MAX + 1;
      add_edge(&(nodes[i].edge_head), &nodes[j], distance);
      add_edge(&(nodes[j].edge_head), &nodes[i], distance);
      edge_count++;
    }
  }

  // Print nodes with edges before dijkstra  
  if(argc == 3){
    printf("Nodes before dijkstra\n");
    for(i=0; i<N; ++i){
      print_node(&nodes[i]);
    }
    printf("\n");
  }

  // Measure time
  clock_t begin, end;
  double time_spent;
  printf("Starting dijkstra for problem size (node) %zu and %i edges\n", N, edge_count);
  begin = clock();
  dijsktra(nodes, N, &nodes[0]);
  end = clock();
  time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  printf("Time spent: %fs\n", time_spent);

  // Free memory
  for(i=0; i<N; ++i){
    // Print nodes after dijkstra
    if(argc == 3){
      printf("\nNodes after dijkstra:\n");
      print_node(&nodes[i]);
    }
    free_edges(nodes[i].edge_head);
  }  
  free(&nodes[0]);

  return EXIT_SUCCESS;
}
コード例 #3
0
ファイル: dijkstra.c プロジェクト: hungly/Pacman
int dijkstra(const int num_nodes, int ** nodes_cost, int ** routing_table) {
    int i;

#	define N_NODES num_nodes

    // For each node
    for (int start = 0; start < N_NODES; start++) {
        // create the nodes
        node_t *nodes = calloc(sizeof(node_t), N_NODES);

        // give the node name (ID)
        for (i = 0; i < N_NODES; i++) {
            nodes[i].name = i;
        }

#	define E(a, b, c) add_edge(nodes + a, nodes + b, c)
        // load the data from the cost table to the memory
        for (int i = 0; i < N_NODES; i++) {
            for (int j = 0; j < N_NODES; j++) {
                if (nodes_cost[i][j] != 0) {
                    E(i, j, nodes_cost[i][j]);
                }
            }
        }
#	undef E

        // Create HEAP
        heap = calloc(sizeof(heap_t), N_NODES + 1);
        heap_len = 0;

        // Calculate all path fro current node to al other nodes
        calc_all(nodes + start);
        for (i = 0; i < N_NODES; i++) {
            routing_table[(nodes + start)->name][(nodes + i)->name] = show_path(nodes + start, nodes + i);
        }

        // Free memory
        free_edges();
        free(heap);
        free(nodes);
    }
    return 0;
}
コード例 #4
0
ファイル: utilities.c プロジェクト: mvanderkolff/navi-misc
GHashTable *
query_edges (PyObject *edge_list)
{
	PyObject   *data = NULL;
	PyObject   *edge = NULL;
	GHashTable *ret  = NULL;

	data = PyObject_GetAttrString (edge_list, "data");
	if (data == NULL)
		return NULL;

	ret = g_hash_table_new (g_direct_hash, g_direct_equal);

	int pos = 0;
	while (PyDict_Next (data, &pos, NULL, &edge)) {
		PyObject *u, *v, *weight;
		u      = PyObject_GetAttrString (edge, "u");
		v      = PyObject_GetAttrString (edge, "v");
		weight = PyObject_GetAttrString (edge, "weight");
		float  weightf = (float) PyFloat_AsDouble (weight);
		float *weightp = g_new (float, 1);
		*weightp = weightf;

		/* FIXME: this is a really stupid way to hash (u,v) */
		g_hash_table_insert (ret, GINT_TO_POINTER (GPOINTER_TO_INT (u) + GPOINTER_TO_INT (v)), weightp);

		Py_DECREF (u);
		Py_DECREF (v);
		Py_DECREF (weight);
	}
	if (PyErr_Occurred ())
		goto error;

	Py_DECREF (data);

	return ret;

error:
	Py_XDECREF (data);
	if (ret)
		free_edges (ret);
}
int main(int argc, char *argv[]) {
	unsigned int T, N, t, a, b, l;
	scanf("%u%u", &T, &N);
	node_t *nodes = calloc(sizeof(node_t), N);
	for (t = 0; t < T; ++t) {
		scanf("%u%u%u", &a, &b, &l);
		--a;
		--b;
		add_edge(nodes + a, nodes + b, l);
		add_edge(nodes + b, nodes + a, l);
	}
	heap = calloc(sizeof(heap_t), N);
	heap_len = 0;
 
	calc_all(nodes);
	for (i = 0; i < N; i++) {
		show_path(nodes + i);
		putchar('\n');
	}
	free_edges();
	free(heap);
	free(nodes);
	return 0;
}