void addEdge(struct Graph* graph, int src, int dest) { struct AdjListNode* newNode = newAdjListNode(dest); newNode->next = graph->array[src].head; graph->array[src].head = newNode; //Assuming source exists newNode = newAdjListNode(src); newNode->next = graph->array[dest].head; graph->array[dest].head = newNode; }
void addEdge(struct Graph *graph, int src, int dest) { struct adjListNode *noded = newAdjListNode(dest); noded->next = graph->array[src].head; graph->array[src].head = noded; struct adjListNode *nodes = newAdjListNode(src); nodes->next = graph->array[dest].head; graph->array[dest].head = nodes; }
void addEdge(Graph *graph, int src, int dest) { AdjListNode *newNode = newAdjListNode(dest); newNode->next = graph->array[src].head; graph->array[src].head = newNode; newNode = newAdjListNode(src); newNode->next = graph->array[dest].head; graph->array[dest].head = newNode; }
void addEdge(struct Graph* graph,int src,int dest, int weight) { struct AdjListNode* newNode =newAdjListNode(dest,weight); newNode->next=graph->array[src].head; graph->array[src].head= newNode; newNode =newAdjListNode(src,weight); newNode->next =graph->array[dest].head; graph->array[dest].head=newNode; }
struct Graph* createBoard() { struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph)); graph->array = (struct AdjList*) malloc((NUM_NODES + 2) * sizeof(struct AdjList)); int i, j; for (i = 0; i < NUM_NODES + 2; ++i) { graph->array[i].head = NULL; } int dim = sqrt(NUM_NODES); for (i = 0; i < NUM_NODES + 2; ++i) { for (j = 0; j < NUM_NODES + 2; ++j) { //For one of the extra source nodes if (i == 0) { if (j > 0 && j <= dim) { struct AdjListNode* newNode = newAdjListNode(j); newNode->next = graph->array[i].head; graph->array[i].head = newNode; } //For the other extra source node } else if (i == NUM_NODES + 1) { if (j < NUM_NODES + 1 && j > NUM_NODES - dim) { struct AdjListNode* newNode = newAdjListNode(j); newNode->next = graph->array[i].head; graph->array[i].head = newNode; } //For all other nodes } else { //For the first row, add edges to source node if (i <= dim && j == 0) { struct AdjListNode* newNode = newAdjListNode(j); newNode->next = graph->array[i].head; graph->array[i].head = newNode; //For the last row, add edges to source node } else if (i > NUM_NODES - dim && j == NUM_NODES + 1) { struct AdjListNode* newNode = newAdjListNode(j); newNode->next = graph->array[i].head; graph->array[i].head = newNode; } if ((j != 0 && j!= NUM_NODES + 1) && ((j == i-1 && (i-1) % dim != 0) || (j == i+1 && i % dim != 0) || j == i + dim || j == i - dim)) { struct AdjListNode* newNode = newAdjListNode(j); newNode->next = graph->array[i].head; graph->array[i].head = newNode; } } } } return graph; }
void addEdge(struct Graph* graph, int src, int dest){ struct AdjListNode* newNode = newAdjListNode(dest); // pointer_a = pointer_b 让 a 指向 (b 指向 的) object newNode->next = graph->array[src].head->next; graph->array[src].head->next = newNode; //undirected graph newNode = newAdjListNode(src); newNode->next = graph->array[dest].head->next; graph->array[dest].head->next = newNode; }
// Adds an edge to an undirected graph void addEdge(struct Graph* graph, int src, int dest, int weight) { // Add an edge from src to dest. A new node is added to the adjacency // list of src. The node is added at the begining struct AdjListNode* newNode = newAdjListNode(dest, weight); newNode->next = graph->array[src].head; graph->array[src].head = newNode; // Since graph is undirected, add an edge from dest to src also newNode = newAdjListNode(src, weight); newNode->next = graph->array[dest].head; graph->array[dest].head = newNode; }
void addEdge(struct Graph * graph, int src, int dest) { struct AdjListNode * newNode = newAdjListNode(dest); newNode -> next = graph->array[src].head; graph->array[src].head = newNode; //Undirected newNode = newAdjListNode(src); newNode -> next = graph->array[dest].head; graph->array[dest].head = newNode; }
void addEdge(GRAPH *graph, int src, int dest) { ADJLISTNODE *newNode = newAdjListNode(dest); newNode->next = graph->array[src].head; graph->array[src].head = newNode; //do opposite too newNode = newAdjListNode(src); newNode->next = graph->array[dest].head; graph->array[dest].head = newNode; }
// Adds an edge to an undirected graph void addEdge(struct Graph* graph, int src, int dest,int cost,int *costMatrix,int V) { // Add an edge from src to dest. A new node is added to the adjacency // list of src. The node is added at the begining struct AdjListNode* newNode = newAdjListNode(dest,cost); newNode->next = graph->array[src].head; graph->array[src].head = newNode; // Since graph is undirected, add an edge from dest to src also newNode = newAdjListNode(src,cost); newNode->next = graph->array[dest].head; graph->array[dest].head = newNode; *(costMatrix+V*src+dest)=cost; *(costMatrix+V*dest+src)=cost; }
void addEdge(struct Graph* graph, int src, int dest) { // Add an edge from src to dest. A new node is added to the adjacency // list of src. The node is added at the begining struct AdjListNode* newNode = newAdjListNode(dest); newNode->next = graph->array[src].head; graph->array[src].head = newNode; }
struct Graph* createGraph(int V){ struct Graph* graph = malloc(sizeof(struct Graph)); graph->V = V; graph->array = malloc(V * sizeof(struct AdjList)); int i; for(i = 0; i < V; i++){ // c 用{}来定义block 而不是 缩进.这里忘记了{} struct AdjListNode* node = newAdjListNode(i); graph->array[i].head = node; } return graph; }
// argv = ["randmst", 0, numpoints, numtrials, dimension] int main(int argc, char **argv) { // ensure correct usage if (argc != 5) { printf("Usage: ./randmst 0 numpoints numtrials dimension\n"); return -1; } //TODO: check to see these are integers int dimension = atoi(argv[4]); // the number of verticies int n = atoi(argv[2]); int numtrials = atoi(argv[3]); // seed random number generator srand(time(NULL)); // store the sum of the weights, to be divided by numtrials later float totalweight = 0; float cutoff= 8.0*((float)dimension)/((float) n+1); // the array of vertices (each entry is an array) float** verts = malloc(n*sizeof(float*)); // initialize the vertices array for (int i = 0; i < n; i++) { verts[i] = malloc(dimension*sizeof(float)); } // populate graph Graph vertex* Graph= malloc(n*sizeof(vertex)); // perform numtrials number of trials and add weight to totalweight for (int t = 0; t < numtrials; t++ ) { // initialize the vertices array for (int i = 0; i < n; i++) { for(int j = 0; j < dimension; j++) { verts[i][j] = (float)rand()/(float)RAND_MAX; } } // TODO: define prim queue q = init(n); queue* Q = &q; for (int i = 0; i < n; i++) { vertex v = {INFTY, NULL}; Graph[i]=v; for(int j = 0; j < i; j++) { float squareDistance = sq_dist(verts[i], verts[j], dimension); if(squareDistance<cutoff) { AdjListNode* jNodePtr= newAdjListNode(j, squareDistance); AdjListNode* iNodePtr= newAdjListNode(i, squareDistance); if(!Graph[i].adjacentVertices) { Graph[i].adjacentVertices=jNodePtr; } else { jNodePtr -> next = Graph[i].adjacentVertices; Graph[i].adjacentVertices= jNodePtr; } if(!Graph[j].adjacentVertices) { Graph[j].adjacentVertices=iNodePtr; } else { iNodePtr -> next = Graph[j].adjacentVertices; Graph[j].adjacentVertices=iNodePtr; } } } } // populate the weights array with the euclidean distances // also build adjacency lists float treeweight = Prim(Q, Graph); totalweight += treeweight; // free shit for (int i = 0; i < n; i++) { AdjListNode* trash = Graph[i].adjacentVertices; while(!trash) { AdjListNode* t = trash->next; free(trash); trash = t; } } } // free willy for (int i = 0; i < n; i++) { free(verts[i]); } free(verts); free(Graph); // printf("%f\n", totalweight); // printf("The average weight of a %i-dimensional minimum spanning tree with with %i verticies is: \n", dimension, n); printf("%f %i %i %i \n", totalweight / numtrials, n, numtrials, dimension); }