Exemple #1
0
void testFINDNEIGHBORS()
{
    graph_t *g = mkGraphABCDE();

    graph_add_edge(g, "A", "B", NULL);

    list_t *neighs = graph_find_neighbors(g, "A");

    CU_ASSERT(list_has(neighs, streq, "B"))
    CU_ASSERT(list_len(neighs) == 1);
    list_free(neighs);

    // adding an additional neighbor:
    graph_add_edge(g, "A", "C", NULL);
    neighs = graph_find_neighbors(g, "A");
    CU_ASSERT(list_has(neighs, streq, "B"));
    CU_ASSERT(list_has(neighs, streq, "C"));
    CU_ASSERT(list_len(neighs) == 2);
    list_free(neighs);

    // the graph is directed:
    graph_add_edge(g, "C", "A", NULL);
    neighs = graph_find_neighbors(g, "A");
    CU_ASSERT(list_has(neighs, streq, "B"));
    CU_ASSERT(list_has(neighs, streq, "C"));
    CU_ASSERT(list_len(neighs) == 2);
    list_free(neighs);

    // add an unrelated edge
    graph_add_edge(g, "D", "E", NULL);
    neighs = graph_find_neighbors(g, "A");
    CU_ASSERT(list_has(neighs, streq, "B"));
    CU_ASSERT(list_has(neighs, streq, "C"));
    CU_ASSERT(list_len(neighs) == 2);
    list_free(neighs);

    freeGraphABCDE(g);
}
Exemple #2
0
list_t *unvisited_neighbors(graph_t *g, void *current, list_t *visited)
{
    list_t *neighs = graph_find_neighbors(g, current);

    list_t *unvisited_neighs = list_new();
    iter_t *nit;
    for (nit = iter(neighs); !iter_done(nit); iter_next(nit))
        {
            void *neigh = iter_get(nit);
            if (!list_has(visited, g->comp, neigh))
                {
                    list_add(unvisited_neighs, neigh);
                }
        }
    iter_free(nit);
    list_free(neighs);
    return unvisited_neighs;
}