Example #1
0
void topological_sort(int** graph, int num_vertices, int* result)
{
    int i;
    int* colors = (int*)BJAM_CALLOC(num_vertices, sizeof(int));
    for (i = 0; i < num_vertices; ++i)
        colors[i] = white;

    for(i = 0; i < num_vertices; ++i)
        if (colors[i] == white)
            do_ts(graph, i, colors, &result);

    BJAM_FREE(colors);
}
Example #2
0
void topological_sort(int** graph, int num_vertices, int* result)
{
    int i;
    int* colors = (int*)calloc(num_vertices, sizeof(int));
    if ( DEBUG_PROFILE )
        profile_memory( num_vertices*sizeof(int) );
    for (i = 0; i < num_vertices; ++i)
        colors[i] = white;

    for(i = 0; i < num_vertices; ++i)
        if (colors[i] == white)
            do_ts(graph, i, colors, &result);

    free(colors);
}
Example #3
0
/* Main routite of topological sort. Calls itself recursively on all
   adjacent vertices which were not yet visited. After that, 'current_vertex'
   is added to '*result_ptr'.
*/
void do_ts(int** graph, int current_vertex, int* colors, int** result_ptr)
{
    int i;

    colors[current_vertex] = gray;
    for(i = 0; graph[current_vertex][i] != -1; ++i) {
        int adjacent_vertex = graph[current_vertex][i];

        if (colors[adjacent_vertex] == white)
            do_ts(graph, adjacent_vertex, colors, result_ptr);
        else if (colors[adjacent_vertex] == gray)
            ; /* This is loop. Not sure what to do... */
    }
    colors[current_vertex] = black;
    **result_ptr = current_vertex;
    (*result_ptr)++;
}
Example #4
0
/* Main routite of topological sort. Calls itself recursively on all
   adjacent vertices which were not yet visited. After that, 'current_vertex'
   is added to '*result_ptr'.
*/
void do_ts(int** graph, int current_vertex, int* colors, int** result_ptr)
{
    int i;

    colors[current_vertex] = gray;
    for(i = 0; graph[current_vertex][i] != -1; ++i) {
        int adjacent_vertex = graph[current_vertex][i];

        if (colors[adjacent_vertex] == white)
            do_ts(graph, adjacent_vertex, colors, result_ptr);
        /* The vertex is either black, in which case we don't have to do
           anything, a gray, in which case we have a loop. If we have a loop,
           it's not clear what useful diagnostic we can emit, so we emit
           nothing.  */
    }
    colors[current_vertex] = black;
    **result_ptr = current_vertex;    
    (*result_ptr)++;
}