Ejemplo n.º 1
0
void main(int argc, char *argv[])
    {
    int V, E;

    if (argc < 2)
	fp = stdin;
    else
	if ((fp = fopen(argv[1], "rt")) == NULL)
	    {
	    printf("\nThat file does not exist!");
	    exit(1);
	    }

    input_adjmatrix(G, &V, &E);
    printf("\n\nAdjacency Matrix representaion for graph");
    print_adjmatrix(G, V);
    printf("\n\nDepth-First Search by recursion\n");
    DFS_adjmatrix(G, V);
    printf("\n\nDepth-First Search by stack\n");
    nrDFS_adjmatrix(G, V);
    printf("\n\nBreadth-First Search by queue\n");
    BFS_adjmatrix(G, V);
    printf("\n\nCount connected components by non-recursive DFS");
    count_components(G, V);

    fclose(fp);
    }
Ejemplo n.º 2
0
static void transform_symlink(struct subdir *d, const char *path,
			      char *buf, size_t size)
{
	const char *l = buf;
	size_t llen;
	char *s;
	int dotdots;
	int i;

	if (l[0] != '/' || d->base[0] != '/')
		return;

	strip_common(&l, &path);
	if (l - buf < (long) d->baselen)
		return;

	dotdots = count_components(path);
	if (!dotdots)
		return;
	dotdots--;

	llen = strlen(l);
	if (dotdots * 3 + llen + 2 > size)
		return;

	s = buf + dotdots * 3;
	if (llen)
		memmove(s, l, llen + 1);
	else if (!dotdots)
		strcpy(s, ".");
	else
		*s = '\0';

	for (s = buf, i = 0; i < dotdots; i++, s += 3)
		memcpy(s, "../", 3);
}
Ejemplo n.º 3
0
int main(int argc, char* argv[])
{
    size_t step_size = 1;

    while (1) {
        int opt = getopt(argc, argv, "hs:");
        if (opt == -1) break;

        switch (opt) {
            case 's':
                step_size = (size_t) strtoul(optarg, NULL, 10);
                break;

            case 'h':
                print_usage(stdout);
                return EXIT_SUCCESS;

            case '?':
                return EXIT_FAILURE;

            default:
                abort();
        }
    }

    if (optind >= argc) {
        print_usage(stderr);
        return EXIT_FAILURE;
    }

    const char* fn = argv[optind];
    FILE* f = fopen(fn, "r");
    if (f == NULL) {
        fprintf(stderr, "Can't open %s for reading.\n", fn);
        return EXIT_FAILURE;
    }

    fprintf(stderr, "Reading adjacency matrix ... ");

    /* some rather brittle parsing of matrix market files */
    char buffer[512];
    fgets(buffer, sizeof(buffer), f);
    if (strcmp(buffer, "%%MatrixMarket matrix coordinate integer general\n") != 0) {
        fprintf(stderr, "Error: Incorrectly formatted matrix market file.\n");
        return EXIT_FAILURE;
    }

    unsigned int n, n2, m;
    fscanf(f, "%u %u %u\n", &n, &n2, &m);

    edge_array_t E;
    E.size = m;
    E.m = 0;
    E.es = malloc_or_die(E.size * sizeof(edge_t));
    edge_t e;

    while (fgets(buffer, sizeof(buffer), f)) {
        sscanf(buffer, "%u %u %u\n", &e.u, &e.v, &e.w);
        e.u -= 1; e.v -= 1; /* make 0-based */
        push_edge(&E, &e);
    }
    fclose(f);
    rng_t* rng = rng_alloc();
    shuffle(rng, E.es, E.m);
    rng_free(rng);
    qsort(E.es, E.m, sizeof(edge_t), edge_cmp);
    fprintf(stderr, "done. (%zu edges)\n", E.m);

    unsigned int* ds = malloc_or_die(n * sizeof(unsigned int));
    size_t i;
    for (i = 0; i < E.m; i += step_size) {
        size_t c = count_components(E.es + i, n, E.m - i, ds);
        printf("%zu\n", c);
        fflush(stdout);
    }

    free(ds);
    free(E.es);

    return EXIT_SUCCESS;
}