Algorithm for build order of some projects

根据教科书中的问题,给定项目列表及其依赖关系,需要找到一个允许项目按顺序构建的有效顺序。输入包含项目和依赖对,如(a, d)表示项目d依赖于项目a。示例中给出了一个不高效的拓扑排序直接算法,其时间复杂度为O(N+N*D),其中N是项目数量,D是依赖数量。现在的目标是通过改进图结构和拓扑排序算法来提高效率。" 108798431,9842837,面试经验分享:阿里Java P7岗位三轮技术面与HR面详述,"['Java', '面试', '分布式系统', '数据库优化', 'JVM调优']
摘要由CSDN通过智能技术生成

Problem from the textbook.

You are given a list of projects and a list of dependencies(which is a list of pairs of projects, where the second project is dependent on the first project). All of a project's dependencies must be built before the project is. Find a build order that will allow the projects to be built. If there is no valid build order, return a error.

Example:

Input

Projects: a, b, c, d, e, f

dependencies:(a, d), (f, b), (b, d), (f, a), (d, c)

output: f, e, a, b, d, c

A straight forward algorthm by topological sort. But it is not very efficient.

#include <algorithm>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <vector>
#include <list>

using namespace std;

vector<char> buildOrder(const vector<char> &projects,
                        const vector<pair<char, char>> &dependencies)
{
    unordered_map<char, int> table;
    queue<char> processNext, order;
    vector<char> result;

    // build the dependenciesTree
    list<pair<char, char>> tree;
    
    // create the table.
    for(auto &x : projects)
        table[x] = 0;

    // count inbound for each node:
    for (auto &item : dependencies)
    {
        ++table[item.second];
        tree.push_back(item);
    }

    for (auto &x : table)
    {
        if (x.second == 0)
            processNext.push(x.first);
    }

    while (!processNext.empty())
    {
        auto &x = processNext.front();
        // iterator all about x to modify the inbound.
        for (auto iter = tree.begin(); iter != tree.end();)
        {
            // if it is the edge of the node x.
            if (iter->first == x)
            {
                table[iter->second]--;
                // add the 0 inbound node to processNext
                if(table[iter->second] == 0)
                    processNext.push(iter->second);
                iter = tree.erase(iter);
            }
            else
            {
                ++iter;
            }
        }

        // update to inbound and add to ProcessNext();
        order.push(x);
        processNext.pop();
    }

    // we build the order correctly.
    if(order.size() == projects.size()) {
        while(!order.empty()) {
            result.push_back(order.front());
            order.pop();
        }
    }
    return result;
}

int main()
{
    vector<char> projects = {'a', 'b', 'c', 'd', 'e', 'f'};
    vector<pair<char, char>> dependencies = {
        make_pair('a', 'd'), make_pair('f', 'b'), make_pair('b', 'd'), make_pair('f', 'a'),
        make_pair('d', 'c')};
    auto x = buildOrder(projects, dependencies);
    for(auto & a : x)
        cout << a << " ";
    cout << endl;
    return 0;
}

output:

the time complexity is O(N+N*D) where N is the number of the projects, and the D is the number of the dependencies.

Now  try to improve this algorithm by graph structure and topological sort algorithm.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值