int main() { Graph *graph = getGraph("input.txt"); if (graph == NULL) { printf("Ошибка открытия файла\n"); return 0; } int connectedComponents = countConnectedComponents(graph); printf("Количество компонент связности: %d\n", connectedComponents); deleteGraph(graph); }
//****main**** int main(){ struct gnode *adj[MAX]; int num; printf("\nEnter no. of nodes in weighted graph: "); scanf("%d",&num); num-=1; createGraph(adj,num); inputGraph(adj,num); printGraph(adj,num); minimumSpanningTreePrim(adj,num); deleteGraph(adj,num); return 0; }
void submitCallback(Entry* entry) { //printf("%d: %s\n", entry->ID, swGetTextFromTextField(entry->textField)); char* text = swGetTextFromTextField(entry->textField); if (strlen(text) == 0) { if (entry->graph != NULL) { deleteGraph(entry->graph); } entry->graph = NULL; entry->active = 0; } else { if (entry->graph != NULL) { deleteGraph(entry->graph); } char* error = NULL; char* ffeq = ""; char* feq = ""; eqConvert(g_parseInfo, text, &feq, &ffeq, &error); if (error != NULL) { swPopup("Error in Parsing Function", error); return; } entry->graph = createGraph(feq, ffeq, GRAPH_PORT_SIZE, GRAPH_PORT_SIZE); free(error); free(feq); free(ffeq); entry->active = 1; } }
int main(int argc, char* argv[]) { Graph* g = init(); prim(g, 1); printf("Minimum spanning tree: \n"); int i; for (i=1; i <= g->nvertices; i++) { printf("%d->%d\n",parent[i],i); } deleteGraph(g); return 0; }
void deleteGraph(gctx_t * gctx, Agraph_t *g) { Agraph_t *sg; char *hndl; for (sg = agfstsubg (g); sg; sg = agnxtsubg (sg)) { deleteGraph(gctx, sg); } deleteGraphNodes(gctx, g); hndl = obj2cmd(g); if (g == agroot(g)) { agclose(g); } else { agdelsubg(agroot(g), g); } Tcl_DeleteCommand(gctx->ictx->interp, hndl); }
int main(int argc, char* argv[]) { Graph* g = newGraph(0); loadGraph(g, "graph.txt"); int* result = (int*) malloc(g->nvertices * sizeof(int)); bandwidth(g,result); printf("Bandwidth: "); int i; for(i=0; i<g->nvertices; i++) { printf("%d ", result[i]); } printf("\n"); deleteGraph(g); return 0; }
int main(int argc, char* argv[]) { //memory allocation could be better. could allocate as needed later in processing? could make loop to keep from //hand coding all the following steps. //initialize big matrix to store vectors if (!vorticities) { vorticities = (double *)malloc(3 * nx * ny * nz * sizeof(double)); } if (!vorticities) { fprintf(stderr, "Out of memory: malloc(vorticities) failed.\n"); exit(1); } if ((unsigned int)vorticities % 8) { fprintf(stderr, "Vorticities array is not qword aligned.\n"); exit(1); } numDist = (localLevel*2+1); //cube this for number to add. if (!distances) { distances = (double *)malloc(numDist*numDist*numDist * nx * ny * nz * sizeof(double)); } if (!distances) { fprintf(stderr, "Out of memory: malloc(distances) failed.\n"); exit(1); } if ((unsigned int)distances % 8) { fprintf(stderr, "Distances array is not qword aligned.\n"); exit(1); } if (!alphaSegs) { alphaSegs = (int *)malloc(3 * (numDist*numDist*numDist-1) * nx * ny * nz * sizeof(int)); } if (!alphaSegs) { fprintf(stderr, "Out of memory: malloc(alphaSegs) failed.\n"); exit(1); } if ((unsigned int)alphaSegs % 8) { fprintf(stderr, "alphaSegs array is not qword aligned.\n"); exit(1); } if (!alphaDists) { alphaDists = (double *)malloc( ((numDist*numDist*numDist-1) * nx * ny * nz * sizeof(double))/2); } if (!alphaDists) { fprintf(stderr, "Out of memory: malloc(alphaDists) failed.\n"); exit(1); } if ((unsigned int)alphaDists % 8) { fprintf(stderr, "alphaDists array is not qword aligned.\n"); exit(1); } if (!unionSets) { unionSets = (Parent *)malloc( nx * ny * nz * sizeof(Parent)); } if (!unionSets) { fprintf(stderr, "Out of memory: malloc(unionSets) failed.\n"); exit(1); } if ((unsigned int)unionSets % 8) { fprintf(stderr, "unionSets array is not qword aligned.\n"); exit(1); } if (!graphNodes) { graphNodes = (Node *)malloc( nx * ny * nz * sizeof(Node)); } if (!graphNodes) { fprintf(stderr, "Out of memory: malloc(graphNodes) failed.\n"); exit(1); } if ((unsigned int)graphNodes % 8) { fprintf(stderr, "graphNodes array is not qword aligned.\n"); exit(1); } if (argc >= 3) { maxAlpha = atof(argv[3]); numComponents = atoi(argv[4]); } //hardcoded file input FILE *inputFile = fopen(argv[1], "r"); FILE *outputFile = fopen(argv[2], "w"); readVorticitiesFile(vorticities, inputFile); fclose(inputFile); //calculate distances from nearby points to other nearby points calcAppAniDel(vorticities, distances); //average the distances calculated int sizeHeap = averageStoreDistances(distances, alphaSegs, alphaDists, vorticities); //free this memory now. free(vorticities); free(distances); //put the distances into a min-heap in-place buildMinHeap(alphaDists, alphaSegs, sizeHeap); MakeSets(unionSets); //initialize InitGraph(graphNodes); //now we call the big function that builds cycles--need vorticities to determine orientation buildCycles(alphaDists, alphaSegs, sizeHeap, unionSets, graphNodes, outputFile, numComponents); fclose(outputFile); free(alphaDists); free(alphaSegs); deleteGraph(graphNodes); free(unionSets); }
/* * Calcul du sous arbre recouvrant minimal * * @param g, graphe * @return un graphe avec les liaisons du sous arbre recouvrant minimal */ struct Graph* minSpanningTree(struct Graph g) { int l, c, i=0, nbEdges=0, cityA=-1, cityB=-1; int isConnexe; struct Edge edge; struct Graph* newGraph=NULL; struct Edge* matrixEdges=NULL; int* connexeComp=NULL; float** matrix; int aux, j, k; newGraph =(struct Graph*) malloc(sizeof(struct Graph)); newGraph->nbCities = g.nbCities; connexeComp = malloc(g.nbCities * sizeof(int)); matrixEdges = malloc((g.nbCities*g.nbCities) * sizeof(struct Edge)); newGraph->matrixCities = malloc(g.nbCities * sizeof(struct City)); for(j = 0; j < g.nbCities; j++) { newGraph->matrixCities[j] = g.matrixCities[j]; } matrix = initMatrix(g.nbCities); for(j = 0; j < g.nbCities; j++) { for(k = 0; k < g.nbCities; k++) { matrix[j][k] = -1.0; } } for (l = 0; l < g.nbCities-1; l++) { for (c = l+1; c < g.nbCities; c++) { if(g.matrix[l][c] != -1) { edge.cityA = l; edge.cityB = c; edge.distance = g.matrix[l][c]; matrixEdges[i] = edge; i++; } } } nbEdges = i; quickSortDistance(matrixEdges, nbEdges); for (i = 0; i < g.nbCities; i++) { connexeComp[i] = i; } k = 0; j = 0; while (k < (g.nbCities-1) && j<nbEdges) { cityA= matrixEdges[j].cityA; cityB= matrixEdges[j].cityB; if (connexeComp[cityA] != connexeComp[cityB]) { k++; matrix[cityA][cityB] = getDistance(g, cityA, cityB); aux = connexeComp[cityB]; for (i = 0; i < g.nbCities; i++) { if (connexeComp[i] == aux) { connexeComp[i] = connexeComp[cityA]; } } } j++; } i = 0; isConnexe = 1; while (i < g.nbCities-1 && isConnexe) { if (connexeComp[i] != connexeComp[i+1]) { isConnexe = 0; } i++; } newGraph->matrix = matrix; if (isConnexe == 0) { free(connexeComp); free(matrixEdges); deleteGraph(*newGraph); free(newGraph); printf("The graph is not connected : there is no minimum spanning tree between these cities.\n"); return NULL; } free(connexeComp); free(matrixEdges); return newGraph; }