// Memoryless version of Prim's algorithm // See CSE 2320, Notes 15. // This is coded with everything in main() because it is very slow, O(VE). // Also, the edges are stored in input order. #include #include // Simulated infinity value #define oo (1000000000) typedef enum {s,t} stType; main() { int numVertices,numEdges; int i,j,MSTedgeCount=0,MSTweight=0,smallestWeight,smallestEdgeNumber; stType *flag; // flag[i] indicates which set vertex i is in. int *vertex1,*vertex2,*weight; // Input tables scanf("%d %d",&numVertices,&numEdges); flag=(stType*) malloc(numVertices*sizeof(stType)); // Three parallel arrays instead of array of structs vertex1=(int*) malloc(numEdges*sizeof(int)); vertex2=(int*) malloc(numEdges*sizeof(int)); weight=(int*) malloc(numEdges*sizeof(int)); if (!flag || !vertex1 || !vertex2 || !weight) { printf("malloc() failed %d\n",__LINE__); exit(0); } // Get input undirected edges. Store in simple tables. for (i=0;inumVertices-1 || vertex2[i]<0 || vertex2[i]>numVertices-1 || weight[i]<=0) { printf("Bad input %d\n",__LINE__); exit(0); } } // Vertex 0 starts in S. Other vertices start in T. flag[0]=s; for (i=1;i