简体   繁体   中英

Graph directed and weighted

I have a weighted directed graph, with 10 nodes, which begins with the node 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 .

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.

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.

#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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