生成有向无环图的快速算法

时间:2015-06-02 10:27:23

标签: c++ optimization directed-acyclic-graphs

我遇到了问题,非常感谢您的帮助。 我必须生成一个具有2 ^ N个节点的DAG,其值为0到2 ^(N-1),具有以下属性: 如果x

#include <iostream>
#include <vector>
#include <math.h>
typedef unsigned int unint;
using namespace std;
class Node
{
    friend class DAG;
private:
    unint value;
    vector<Node* > neighbourTo;
    vector<Node* > neighbors;
public:
    Node(unint );
};
Node::Node(unint _value)
    : value(_value) {}
class DAG
{
private:
    int noNodes;
    vector<Node* > nodes;
public:
    DAG(int );
    void initializeNodes(int ,int );
    int isPowerOf2(unsigned int );
    int getMaxNaighbourTo(int );
    int getMinNeighbor(int );
    int numberOfPathsLengthK(int );
    int recursion(Node& , int );
    void print();
};
DAG::DAG(int size)
{
    noNodes = size;

    nodes.resize(noNodes);
    int i, j;

    initializeNodes(0, noNodes-1);
    for(i = 0; i < noNodes-1; i++)
    {
        for(j = i+1; j < noNodes; j++)
        {
            if(isPowerOf2(i ^ j))
            {
                nodes[i]->neighbors.push_back(nodes[j]);
                nodes[j]->neighbourTo.push_back(nodes[i]);
            }
        }
    }
}
void DAG::initializeNodes(int min, int max)
{
    if(max == min)
        nodes[max] = new Node(max);
    else
    {
        int s = (max + min)/2;
        initializeNodes(min, s);
        initializeNodes(s+1, max);
    }
}
int DAG::isPowerOf2(unsigned int value)
{
    return ((value != 0) && !(value & (value - 1)));
}
int DAG::getMaxNaighbourTo(int index)
{
    if(index > 0 && index <= (noNodes-1))
    {
        int size = nodes[index]->neighbourTo.size();
        return nodes[index]->neighbourTo[size-1]->value;
    }
    return -1;
}
int DAG::getMinNeighbor(int index)
{
    if(index >= 0 && index < (noNodes-1))
        return nodes[index]->neighbors[0]->value;
    return -1;
}
int DAG::numberOfPathsLengthK(int K)
{
    if(K <= 0)
        return 0;
    long int paths = 0;
    for(int i = 0; i < nodes.size(); i++)
    {
        paths += recursion(*nodes[i], K - 1);
    }
    return (paths % 100003);
}
int DAG::recursion(Node& node, int K)
{
    if( K <= 0 )
        return node.neighbors.size();
    else
    {
        long int paths = 0;
        for(int i = 0; i < node.neighbors.size(); i++)
        {
            paths += recursion(*node.neighbors[i], K - 1);
        }
        return paths;
    }
}
void DAG::print()
{
    for(int i = 0; i < nodes.size(); i++)
    {
        cout << "Node: " << nodes[i]->value << "\tNeighbors: ";
        for(int j = 0; j < nodes[i]->neighbors.size(); j++)
        {
            cout << nodes[i]->neighbors[j]->value << " ";
        }
        cout << endl;
    }
}
int main()
{
    int
    N, M, K,
    i, j;
    cin >> N >> M >> K;
    DAG graf(pow(2, N));
    graf.print();
    cout << "==1==" << endl;
    cout << graf.getMaxNaighbourTo(M) << endl;
    cout << "==2==" << endl;
    cout << graf.getMinNeighbor(M) << endl;
    cout << "==3==" << endl;
    cout << graf.numberOfPathsLengthK(K) << endl;
    return 0;
}

这是一个简单的输出:

4 3 2
Node: 0     Neighbors: 1 2 4 8
Node: 1     Neighbors: 3 5 9
Node: 2     Neighbors: 3 6 10
Node: 3     Neighbors: 7 11
Node: 4     Neighbors: 5 6 12
Node: 5     Neighbors: 7 13
Node: 6     Neighbors: 7 14
Node: 7     Neighbors: 15
Node: 8     Neighbors: 9 10 12
Node: 9     Neighbors: 11 13
Node: 10    Neighbors: 11 14
Node: 11    Neighbors: 15
Node: 12    Neighbors: 13 14
Node: 13    Neighbors: 15
Node: 14    Neighbors: 15
Node: 15    Neighbors:
2
7
48

节点是节点指针的向量,节点a是保存节点值和两个向量的类,一个节点指向当前节点的邻居,另一个节点指向当前节点的节点是邻居。上面的代码是用C ++编写的。 我为任何语法错误道歉。英语不是我的母语。

2 个答案:

答案 0 :(得分:2)

第一个明显的非算法性能增益是来构建图形,如果您只需要打印邻居就可以这样做,而无需创建数据结构。这里的第二个改进是避免用每个输出线冲洗流......

对于算法改进,给定一个数字N=0011010(例如,任何数字有效),您需要确定哪个数字满足这两个要求,N xor M是2的幂,并且N > M。第一个要求意味着两个数字在一位中完全不同,第二个要求意味着该位必须在M中点亮而在N中不亮,所以答案只看上面的位是:M = { 1011010, 0111010, 0011110, 0011011 }。现在,您可以通过扫描N中的每个位来获取所有这些内容,如果它是0,则设置它并打印该值。

// assert that 'bits < CHAR_BITS * sizeof(unsigned)'
const unsigned int max = 1u << bits;
for (unsigned int n = 1; n < max; ++n) {
   std::cout << "Node: " << n << " Neighbors: ";
   for (unsigned int bit = 0; i < bits; ++i) {
      unsigned int mask = 1 << bit;
      if (!(n & mask)) {
         std::cout << (n | mask);
      }
   }
   std::cout << '\n';
}

对于给定节点的最小和最大邻居,您可以应用相同的推理,给定数量N的最大可达邻居将是M,使得N中的最高0位被点亮。对于最小可达邻居,您需要M使得最低0位设置为1.

答案 1 :(得分:0)

我有空闲时间写草图,看看:

<form action="" method="post" class="horizontal-form" role="form" >
<input type="hidden" name="submit_form" value="true" />
<input type="text" name="field_name" class="form-control" value="" >
<button type="submit" class="btn"><i class="icon-ok"></i> Send</button>
</form>

DEMO

对于struct node { std::vector<std::shared_ptr<node> > link; }; int main() { int N = 4; int M = 1<<N; std::vector<std::shared_ptr<node> > tree(M, std::make_shared<node>()); for(int i=0;i<M;++i) { std::cout<<"node: "<<i<<" is connected to:\n"; for(int p=0;p<N;++p) { int j= (1<<p) ^ i; //this is the evaluation you asked for //it's the inverse of i ^ j = 2^p if(j<=i) continue; tree[i]->link.push_back(tree[j]); std::cout<<j<<" "; } std::cout<<std::endl; } } ,即N=4个节点,程序打印

2^4=16

我希望这就是你要找的东西。玩得开心。