Objectives

  • representing a problem as a graph.
  • coding a graph algorithm
  • writing a program to find a computer network of minimal cost, given a set of computers and existing connection costs between those computers.

Problem Statement

You are to write a program that takes a single additional command line argument (for a total of 2 arguments, including the command name) that gives the name of an input file:

The file will be a sequence of connections between computers and their associated costs. Each connection will be on its own line and appear as follows:

< name1 > < name2 > < cost >

Where

  • < name1 > will be a string (containing no spaces) representing the name of a computer
  • < name2 > will be a string (containing no spaces) representing the name of a computer. This name will be a different name than < name1 >.
  • < cost > will be a positive integer representing the cost of a communications line between the two computers

So, one example input file might look like:

Allentown Bethlehem 12
Allentown Indianapolis 25
Bethlehem Chicago 10
Bethlehem Hartford 40
Bethlehem Indianapolis 8
Chicago Detroit 18
Chicago GreenBay 55
Detroit Edwardsville 44
Edwardsville Fargo 60
Edwardsville GreenBay 38
GreenBay Hartford 35
Hartford Indianapolis 35

Note that all communication lines are undirected, so the connection from Bethlehem to Hartford at a cost of 40 means that Hartford can also communicate with Bethlehem using the same line.

The program should then print out the total cost of a minimum cost network that would allow all computers to be connected, but not necessarily with direct connections between all computers. For the example above, the resulting cost would be 216.

Your program must run in better than m2 time, where is the number of connections specified in the input file !!!!

Example Executions

Suppose you have the above example stored an an input file called simple.txt. Then you could run the program as (assuming your executable name is p6sol) :

./p6sol simple.txt

And the resulting output would be:

216
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.