// IMPLEMENT THIS FOR TASK 3 Edge getNextFlight(Graph g, int *dfsOrdered, int *nDiffCountries, Vertex currCountry){ int next = dfsOrdered[(*nDiffCountries)]; if (!isAdjacent(g, currCountry, next)) { // backtrack if not adjacent return mkEdge(currCountry, st[currCountry]); } // otherwise, go (*nDiffCountries)++; return mkEdge(currCountry, next); }
Graph mkTestGraph(int graphNum) { Graph g = NULL; if (graphNum == 0) { g = newGraph(0); assert(numV(g) == 0); assert(numE(g) == 0); } else if (graphNum == 1) { g = newGraph(1); assert(numV(g) == 1); assert(numE(g) == 0); } else if (graphNum == 2) { g = newGraph(2); assert(numV(g) == 2); assert(numE(g) == 0); } else if (graphNum == 3) { g = newGraph(5); Edge e301 = mkEdge(0, 1, 7); Edge e302 = mkEdge(0, 2, 6); Edge e303 = mkEdge(0, 3, 5); Edge e304 = mkEdge(0, 4, 4); Edge e312 = mkEdge(1, 2, 3); Edge e323 = mkEdge(2, 3, 2); Edge e334 = mkEdge(3, 4, 1); insertE(g, e301); insertE(g, e302); insertE(g, e303); insertE(g, e304); insertE(g, e312); insertE(g, e323); insertE(g, e334); assert(numV(g) == 5); assert(numE(g) == 7); } return g; }
void addNode(double array[], int ID, Heap h){ int i; Node thisNode = h->array[ID]; for (i=0; i < h->size; i++){ if (array[i] > 0.0){ Edge e = mkEdge(thisNode, h->array[i], array[i]); addEdge(thisNode,e); } } }
Graph readGraph(FILE *in) { Graph g; char line[100]; // get #vertices and create graph int nV = 0; if (fgets(line,100,in) == NULL) readError(); if (sscanf(line,"%d",&nV) != 1) readError(); if (nV < 2) readError(); g = newGraph(nV); // read edge data and add edges Vertex v, w; while (fgets(line,100,in) != NULL) { sscanf(line,"%d-%d",&v,&w); insertE(g, mkEdge(g,v,w)); } return g; }
// THIS NEEDS TO BE COMPLETED FOR TASK 1 // Reads in data from the file with the given filename and creates a newGraph // The file must be of the format // numVertices // v0 v1 v2 v3 // v1 v2 v4 Graph readGraph(char * filename) { FILE *fp; fp = fopen (filename, "r"); // open data file assert (fp != NULL); int city = 0; int dest = 0; char c = 0; // First line of file has the number of vertices int numV; fscanf(fp, "%d", &numV); Graph g = newGraph(numV); // scan through file and insert edges into graph int counter = 0; while (counter < numV) { fscanf(fp, "%d", &city); counter++; while (c != '\n') { fscanf (fp, "%d", &dest); insertE(g, mkEdge(city, dest)); c = getc(fp); } c = 0; } int v; char name[100]; char capital[100]; long population; while(fscanf(fp, "%d %s %s %ld", &v, name, capital, &population) == 4) { setVertexData(g, v, name, capital, population); } fclose(fp); return g; }
// Reads in data from the file with the given filename and creates a newGraph // The file must be of the format // numVertices // v1 v2 weight v1 v2 weight informant cityname // v1 v2 weight v1 v2 weight informant cityname Graph readGraph(char * filename) { FILE *fp; fp = fopen (filename, "r"); // open data file assert (fp != NULL); int city = 0; int dest = 0; int weight = 0; //First line of file has the number of vertices int numV; fscanf(fp, "%d", &numV); Graph g = newGraph(numV); // scan through file and insert edges into graph int counter=0; char informant; char name[MAXLINE]; while (counter < numV) { fscanf(fp,"%d",&city); counter++; while(fscanf(fp, "%d %d", &dest,&weight) == 2){ insertE(g,mkEdge(city, dest, weight)); } //TASK - YOU MUST STORE THIS INFO IN THE GRAPH fscanf(fp,"%c %[^\n]",&informant,name); setCityName(g, city, name); if (informant == 'i') { insertInformant(g, city); } } fclose(fp); return g; }
void SensorPointXY::sense() { _robotPoseObject=0; RobotType* r= dynamic_cast<RobotType*>(robot()); std::list<PoseObject*>::reverse_iterator it=r->trajectory().rbegin(); int count = 0; while (it!=r->trajectory().rend() && count < 1){ if (!_robotPoseObject) _robotPoseObject = *it; it++; count++; } for (std::set<BaseWorldObject*>::iterator it=world()->objects().begin(); it!=world()->objects().end(); it++){ WorldObjectType* o=dynamic_cast<WorldObjectType*>(*it); if (o && isVisible(o)){ EdgeType* e=mkEdge(o); if (e && graph()) { e->setMeasurementFromState(); addNoise(e); graph()->addEdge(e); } } } }
int main (int argc, char * argv[]) { printf("Blackbox tests...\n"); printf("Test 1: newGraph..."); //empty graph Graph g1a = mkTestGraph(0); destroyGraph(g1a); //single graph Graph g1b = mkTestGraph(1); destroyGraph(g1b); printf("Passed!\n"); printf("Test 1a: mkEdge..."); //zero cost edge Edge e1a = mkEdge(0, 1, 0); assert(e1a.v == 0); assert(e1a.w == 1); assert(e1a.weight == 0); Edge e1ar = mkEdge(1, 0, 0); assert(e1ar.v == 1); assert(e1ar.w == 0); assert(e1ar.weight == 0); //edge Edge e1b = mkEdge(1, 5, 10); assert(e1b.v == 1); assert(e1b.w == 5); assert(e1b.weight == 10); Edge e1br = mkEdge(5, 1, 10); assert(e1br.v == 5); assert(e1br.w == 1); assert(e1br.weight == 10); printf("Passed!\n"); printf("Test 2: insertE..."); //double graph Graph g2 = mkTestGraph(2); Edge e2 = mkEdge(0, 1, 12); insertE(g2, e2); assert(numE(g2) == 1); destroyGraph(g2); printf("Passed!\n"); printf("Test 3: isAdjacent..."); //double graph Graph g3a = mkTestGraph(2); Edge e3a = {0, 1, 12}; insertE(g3a, e3a); assert(numV(g3a) == 2); assert(numE(g3a) == 1); assert(isAdjacent(g3a, 0, 1) == 1); assert(isAdjacent(g3a, 1, 0) == 1); destroyGraph(g3a); //graph Graph g3b = mkTestGraph(3); assert(isAdjacent(g3b, 0, 1) == 1); assert(isAdjacent(g3b, 0, 2) == 1); assert(isAdjacent(g3b, 0, 3) == 1); assert(isAdjacent(g3b, 0, 4) == 1); assert(isAdjacent(g3b, 1, 2) == 1); assert(isAdjacent(g3b, 2, 3) == 1); assert(isAdjacent(g3b, 3, 4) == 1); destroyGraph(g3b); printf("Passed!\n"); printf("Test 4: adjacentVertices..."); Graph g4a = mkTestGraph(2); Edge e4a = {0, 1, 12}; insertE(g4a, e4a); Vertex adj4a[2]; //allocate space for max number of vertices assert(adjacentVertices(g4a, 0, adj4a) == 1); assert(adj4a[0] >= 0); assert(adjacentVertices(g4a, 1, adj4a) == 1); assert(adj4a[0] >= 0); destroyGraph(g4a); printf("Passed!\n"); printf("Test 5: incidentEdges..."); Graph g5 = mkTestGraph(2); Edge e5 = {0, 1, 12}; insertE(g5, e5); Edge edges5[1]; //allocate space for max num of edges assert(incidentEdges(g5, 0, edges5) == 1); int v5 = edges5[0].v; int w5 = edges5[0].w; assert( (v5 == 0 && w5 == 1) || (v5 == 1 && w5 == 0) ); assert(edges5[0].weight == 12); assert(incidentEdges(g5, 1, edges5) == 1); v5 = edges5[0].v; w5 = edges5[0].w; assert( (v5 == 0 && w5 == 1) || (v5 == 1 && w5 == 0) ); assert(edges5[0].weight == 12); destroyGraph(g5); printf("Passed!\n"); printf("Test 6: edges..."); Graph g6 = mkTestGraph(2); Edge e6 = {0, 1, 12}; insertE(g6, e6); Edge es6[1]; //allocate space for max num of edges assert(edges(es6, 1, g6) == 1); int v6 = es6[0].v; int w6 = es6[0].w; assert( (v6 == 0 && w6 == 1) || (v6 == 1 && w6 == 0) ); assert(es6[0].weight == 12); destroyGraph(g6); printf("Passed!\n"); printf("All Test Passed! You are a Beast!\n"); return EXIT_SUCCESS; }