// ************************************************************** // * * // * Spheres -- BellmanFord.cpp * // * * // * (C) Christian Scheideler, 2003 * // * * // ************************************************************** // BellmanFord computes shortest paths in some example graph // Special definitions and operations provided by Spheres.h: // // - Sphere: class that enables non-blocking method invocations (NMI). // To ensure the correct execution of NMIs, every method in a class // derived from Sphere that is to be called by NMI has to have the form // // void MethodName(usertype *plist) // // where plist is a pointer to any kind of usertype. // // - void Send(SpherePtr, SphereObj::Method, ParList): // This requests to call the method SphereObj::Method in the user object // referenced by SpherePtr using the parameter list referenced by ParList. // // - void Simulate(Time): // This runs a simulation for Time many rounds. // The Spheres.h class provides an environment for the simulation of // concurrent data structures, where concurrent executions are done at the // level of method invocations (i.e. each method invocation completes before // another method is invoked). To avoid inconsistencies in the data structure, // some general rules have to be obeyed: // // - A method may not have any side-effects other than modifying the // data inside the object. // // - A method must be total, meaning that it is well-defined for // EVERY legal state of the object. #include #include #include "Spheres.h" // some global values #define SIZE 6 // adjacency matrix of graph int A[SIZE][SIZE] = {{0,5,1,0,0,0}, {5,0,2,1,3,0}, {1,2,0,0,5,0}, {0,1,0,0,1,5}, {0,0,5,1,0,4}, {0,0,0,5,4,0}}; // parameter list for Node::Update method class UPList { public: int s, t, D; // contains min_x D_{s,x}(t) UPList(int u, int v, int d) { s = u; t = v; D = d; } }; class Node: public Sphere { private: int ID; // own ID int D[SIZE][SIZE]; // D_u,v(w) entries int minD[SIZE]; // min_v D_u,v(w) Node **neighbor; // array of pointers to neighbors int cost[SIZE]; // cost of edges public: Node(int u) { int v,w; // set node ID ID = u; // initialize D table and edge costs for (v=0; v0 && D[v][w]s; w = p->t; D[v][w] = cost[v] + p->D; // update D_{u,v}(w) cout << ID << ": update of D[" << v << "," << w << "] to " << D[v][w] << "\n"; if (D[v][w] < minD[w]) // min_v D_{u,v}(w) changes? { minD[w] = D[v][w]; for (v=0; v0) // v is a neighbor? { lp = new UPList(ID,w,minD[w]); Send(neighbor[v], Node::Update, lp); } } } delete p; } void Start(void *p) { int v,w; UPList *lp; for (w=0; w0) { // send min_v D_u,v(w) to v lp = new UPList(ID,w,minD[w]); Send(neighbor[v], Node::Update, lp); } } } } void Output() { int w; for (w=0; wConnect(List); // generate requests to call Node::Start in every node of the list for (i=0; iOutput(); cin >> i; }