简体   繁体   English

有向图和加权图

[英]Graph directed and weighted

I have a weighted directed graph, with 10 nodes, which begins with the node a . 我有一个加权有向图,有10个节点,从节点a开始。 And the nodes are connected to each other (the specific connection is shown in the code) and the edge shows how likely (probability) it is to go to the next node. 节点相互连接(代码中显示了特定的连接),边缘显示了到达下一个节点的可能性(概率)。 If for example the node a is connected to node b and c , and both have the same probability to got to (0.5) the first node in the list should be taken, so here b . 例如,如果节点a连接到节点bc ,并且两者都有相同的概率达到(0.5),则应采用列表中的第一个节点,因此这里b

And the user enters the number of times the traversal function should go trough the graph. 然后用户输入遍历函数应通过图形的次数。 The return of the traversal function should be the number of times the node e has been accessed. 遍历函数的返回应该是节点e被访问的次数。

I already did some of it, if you could help me and tell me how I should access each node and check it if it's the one with the " e " or not, would be great. 如果您可以帮助我并告诉我如何访问每个节点并检查是否为带有“ e ”的节点,那么我已经做了一些工作,那就太好了。

#include <iosrteam>
#include <cstdlib>
#include <vector>
using namespace std;

struct node {
        const char* label;
        vector<float> prob;
        vector<node*> succ;
};

// TODO: implement function traversal

void node_init(node* a, const char label[]) {
    a->label = label;
}

void edge_init(node* a, node* b, float probability) {
    a->succ.push_back(b);
    a->prob.push_back(probability);
}

void init_graph(node* nodes) {
    node_init(nodes, "a");
    node_init(nodes+1, "b ");
    node_init(nodes+2, "c ");
    node_init(nodes+3, "d ");
    node_init(nodes+4, "e ");
    node_init(nodes+5, "f ");
    node_init(nodes+6, "g ");
    node_init(nodes+7, "h ");
    node_init(nodes+8, "i ");
    node_init(nodes+9, "j ");

    edge_init(nodes, nodes+1, 0.5);
    edge_init(nodes, nodes+2, 0.5);
    edge_init(nodes+1, nodes+3, 0.3);
    edge_init(nodes+1, nodes+4, 0.3);
    edge_init(nodes+1, nodes+5, 0.4);
    edge_init(nodes+2, nodes+4, 0.5);
    edge_init(nodes+2, nodes+5, 0.5);
    edge_init(nodes+3, nodes, 1.0);
    edge_init(nodes+4, nodes+6, 0.5);
    edge_init(nodes+4, nodes+7, 0.5);
    edge_init(nodes+5, nodes+6, 0.25);
    edge_init(nodes+5, nodes+7, 0.25);
    edge_init(nodes+5, nodes+8, 0.25);
    edge_init(nodes+5, nodes+9, 0.25);
    edge_init(nodes+6, nodes, 1.0);
    edge_init(nodes+7, nodes, 1.0);
    edge_init(nodes+8, nodes, 1.0);
    edge_init(nodes+9, nodes, 1.0);
}

int main(int argc, char *argv[]) {
    if (argc > 1) {
        srand(time(0));
        node *nodes= new node[12];
        init_graph(nodes);
        int fische = traversal(nodes,atoi(argv[1]));
        cout << endl << "Number of e-access: " << e << endl;
        delete [] nodes;
    } else {
        cout << "Call with number of steps for traversal.\n";
    }
    return 0;
}

Here is what I figured out, it seems to be working (our tutor told us to sum op the probabilities and to compare it to the randomly generated number, don't ask me why): 这是我发现的,似乎很有效(我们的老师告诉我们对概率求和并将其与随机生成的数字进行比较,不要问我为什么):

#include <iostream>
#include <cstdlib>
#include <vector>
#include <time.h>

using namespace std;

struct node {
    const char* label;
    vector<float> prob;
    vector<node*> succ;
};

// TODO: implement function traversal

int traversal(node* start, int argv ){
    int e=0;
    int i=0;
    cout<<start->label<<endl;
    if(string(start->label).compare("e ") == 0){
        e++;
    }
    if(argv>0) {
        float summe =0;
        double ran = (double)rand() / RAND_MAX;
        for(int i=0; i<start->prob.size();i++){
            summe = summe + start->prob[i];
            if(summe>ran){
                e =e + traversal(start->succ[i], argv-1);
                break;
            }

        }
    }
    return e;
}


void node_init(node* a, const char label[]) {
    a->label = label;
}

void edge_init(node* a, node* b, float probability) {
    a->succ.push_back(b);
    a->prob.push_back(probability);
}

void init_graph(node* nodes) {
    node_init(nodes, "a ");
    node_init(nodes+1, "b ");
    node_init(nodes+2, "c ");
    node_init(nodes+3, "d ");
    node_init(nodes+4, "e ");
    node_init(nodes+5, "f ");
    node_init(nodes+6, "g ");
    node_init(nodes+7, "h ");
    node_init(nodes+8, "i ");
    node_init(nodes+9, "j ");

    edge_init(nodes, nodes+1, 0.5);
    edge_init(nodes, nodes+2, 0.5);
    edge_init(nodes+1, nodes+3, 0.3);
    edge_init(nodes+1, nodes+4, 0.3);
    edge_init(nodes+1, nodes+5, 0.4);
    edge_init(nodes+2, nodes+4, 0.5);
    edge_init(nodes+2, nodes+5, 0.5);
    edge_init(nodes+3, nodes, 1.0);
    edge_init(nodes+4, nodes+6, 0.5);
    edge_init(nodes+4, nodes+7, 0.5);
    edge_init(nodes+5, nodes+6, 0.25);
    edge_init(nodes+5, nodes+7, 0.25);
    edge_init(nodes+5, nodes+8, 0.25);
    edge_init(nodes+5, nodes+9, 0.25);
    edge_init(nodes+6, nodes, 1.0);
    edge_init(nodes+7, nodes, 1.0);
    edge_init(nodes+8, nodes, 1.0);
    edge_init(nodes+9, nodes, 1.0);
}

int main(int argc, char *argv[]) {
    if (argc > 1) {
        srand(time(0));
        node *nodes= new node[12];
        init_graph(nodes);
        int e = traversal(nodes,atoi(argv[1]));
        cout << endl << "Number of e-access: " << e << endl;
        delete [] nodes;
    } else {
        cout << "Call with number of steps for traversal.\n";
    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM