Algorithms

[알고리즘] 도달 가능 모든 경로의 종류 찾아내는 알고리즘

Roien 2015. 7. 4.
반응형


주어진 directed graph에서 모든 도달 가능한 경로(path)의 종류 counting 하는 알고리즘 구현물


uint32 count_all_possible_paths(

    item_t *items,

    uint32 n,

    uint32 start_idx,

    sint32 dest_idx,

    uint32 l

    )

{

    uint32 j, path_count = 0;

    stack<u32>  visit_history;

    queue<long> que;

    uint32 pos, distance;

    adj_node_t *adj_node;

    list_node_t *node;

 

    for (j = 0; j < n; j++) {

        items[j].visited  = false;

        items[j].distance = 0;

    }

 

    que.push(start_idx);

    items[start_idx].distance = 0;

 

    while (!que.empty()) {

        j = que.front();

        que.pop();

 

        distance = items[j].distance;

        visit_history.push(j);

 

        if (RET_TYPE_FAILURE == items[j].adj_list->get_head_position(&pos))

            continue;

 

        while (1) {

            node = items[j].adj_list->get_next(&pos);

            if (NULL == node)

                break;

 

            adj_node = (adj_node_t *)node->data;

            int k = item_find_slot(items, adj_node->vertex->name, n);

 

            items[k].distance = distance + 1;

            items[k].visited  = true;

 

            if (items[k].distance <= l) {

                que.push(k);

            }

        }

    }

 

    while (!visit_history.empty()) {

        j = visit_history.top();

        visit_history.pop();

        if (j == dest_idx) {

            path_count++;

        }

    }

 

return path_count;

}

 

반응형

댓글