Prev     Next
Express Ideas in Code

Define what a cable object is (typedef struct { int city1, city2, cost } Cable;)
Open data file (fstream fin(argv[1], ios::in); )
Determine number of cables in file (while (fin.getline(buffer, 1024)) count++; )
Close the data file (fin.close();)
Identify a set containing all given cables, and call it GIVEN (Cable given[count]; )
Identify a set of cables and call it SOLUTION (Cable *solution[count]; )
Re-open the data file and load GIVEN (for (int i=0 ; i < count ; i++) fin >> given.city1...; )
Sort GIVEN cables by increasing order of cost (qsort (given, count, sizeof(Cable), cmp); )
Repeat the following until no cables remain in GIVEN: (for (int i=0 ; i < count ; i++) )
         Pull the lowest cost cable from GIVEN, call it CABLE (Cable *cable = &given[i]; )
         Does adding CABLE to SOLUTION cause a cycle? (if (!connect(..., cable)) )
         If not, add CABLE to SOLUTION (solution[sol_index++] = cable; )
Return SOLUTION (return solution; )