### A graph should be traversed

Hello, 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 :)

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778`` `````` #include #include #include using namespace std; // TODO: implement struct node struct node { const char* label; vector prob; vector succ; }; // TODO: implement function traversal void node_init(node* a, const char label[] /*TODO: arguments*/) { a->label = label; } void edge_init(node* a, node* b, float probability/*TODO: arguments*/) { a->succ.push_back(b); a->prob.push_back(probability); } void init_graph(node* nodes /*TODO: arguments*/) { 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 "); // TODO: add missing nodes 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); // TODO: add missing edges } 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; }``````
Last edited on
Topic archived. No new replies allowed.