Пример #1
0
/**
 * Entry point into the netplan program.
 *
 * -Reads a file from the filesystem according to the specification for
 *  PA3, creating an UndirectedGraph.
 * -Finds the total cost & ping time of the graph as presented in the input
 *  file.
 * -Determines the minimum cost graph from the original graph.
 * -Finds the total cost & ping time of the minimum cost graph.
 * -Finds the change of cost & ping time from the original graph to the
 *  minimum cost graph.
 * -Prints the results to stdout.
 *
 * Usage:
 *   ./netplan infile
 *
 */
int main(int argc, char **argv) {
    // Data Structs to hold the variables
    vector<string> to;
    vector<string> from;
    vector<unsigned int> cost;
    vector<unsigned int> length;

    // if number of arguments passed in is not 2, print usage
    if (argc != 2) {
        std::cerr << "Usage: " << argv[0] << " infile" << std::endl;
        return EXIT_FAILURE;
    }
    
    std::ifstream in(argv[1]);
    if (!in) {
        std::cerr << "Unable to open file for reading." << std::endl;
        return EXIT_FAILURE;
    }

    // string and int variables for adding to the vectors
    string str;
    unsigned int i;

    /**
     * while file is not empty, parse input so that we can make a graph from
     * the input
     */
    while(true){
        in >> str;
        if(in.eof()) break;
        to.push_back(str);

        in >> str;
        from.push_back(str);

        in >> i;
        cost.push_back(i);

        in >> i;
        length.push_back(i);
    }

    /**
     * create undirected graph from the input file
     */
    UndirectedGraph *bob = new UndirectedGraph();
    for(unsigned int j = 0; j < to.size(); j++){
        bob->addEdge(to[j], from[j], cost[j], length[j]);
    }

    // get total edge cost of inital graph
    unsigned int totalCost = bob->totalEdgeCost();

    // get total distance of inital graph, by using Dijkstra's algorithm on 
    // all the vertices
    unsigned int totalTime = bob->totalDistance();

    // create minimum spanning tree of the inital graph, using Prim's algorithm
    bob->minSpanningTree();

    // get total edge cost of minimum spanning tree
    unsigned int mstCost = bob->totalEdgeCost();

    // get total distance of minimum spanning tree, using Dijkstra's algorithm
    // on all the vertices
    unsigned int mstTime = bob->totalDistance();

    // print out all the costs and distances
    cout << totalCost << endl;
    cout << mstCost << endl;
    cout << totalCost - mstCost << endl;
    cout << totalTime << endl;
    cout << mstTime << endl;
    cout << mstTime - totalTime << endl;

    // delete graph
    delete(bob);

    return EXIT_SUCCESS;
}
Пример #2
0
int main() {
    cout << "--------------GRAPHTESTERFILE-----------------" << endl;

    cout << "CREATING GRAPH" << endl;
    UndirectedGraph testG = UndirectedGraph();

    cout << "ADDING EDGE WITHOUT ANY VERTICES EXISTING" << endl;
    testG.addEdge("Yahoo","Google",13,13);

    cout << "total edge cost: ";
    cout << testG.totalEdgeCost() << endl;

    cout << "ADDING DUPLICATE" << endl;
    testG.addEdge("Yahoo","Google",9,9);

    cout << "total edge cost: ";
    cout << testG.totalEdgeCost() << endl;

    cout << "ADDING REVERSED DUPLICATE" << endl;
    testG.addEdge("Google","Yahoo",7,7);

    cout << "total edge cost: ";
    cout << testG.totalEdgeCost() << endl;    

    cout << "ADDING EDGE" << endl;
    testG.addEdge("Yahoo","Microsoft",71,71);
    cout << "total edge cost: ";    
    cout << testG.totalEdgeCost() << endl;

    cout << "ADDING UNCONNECTED EDGE" << endl;
    testG.addEdge("Netflix","Yelp",21,21);
    cout << "total edge cost: ";    
    cout << testG.totalEdgeCost() << endl;

    cout << "SETTING UP NON-MST" << endl;
    testG.addEdge("Google","Yahoo",2,2);
    testG.addEdge("Yahoo","Microsoft",3,3);
    testG.addEdge("Microsoft","Netflix",5,5);
    testG.addEdge("Netflix","Yelp",7,7);
    testG.addEdge("Yelp","Google",11,11);
    testG.addEdge("Netflix","Google",13,13);
    testG.addEdge("Google","Microsoft",17,17);

    cout << "total edge cost: ";    
    cout << testG.totalEdgeCost() << endl;

    cout << "CREATING MST" << endl;
    testG.minSpanningTree();
    cout << "total edge cost: ";        
    cout << testG.totalEdgeCost() << endl;

    cout << "TEST TOTAL DISTANCE" << endl;
    cout << "total distance: ";
    cout << testG.totalDistance("Google") << endl;

    cout << "TEST TOTAL DISTANCE NEW GRAPH" << endl;
    UndirectedGraph graphTwo = UndirectedGraph();
    graphTwo.addEdge("Google","Yahoo",2,2);
    graphTwo.addEdge("Yahoo","Microsoft",3,3);    
    cout << graphTwo.totalDistance("Google") << endl;
    cout << graphTwo.totalDistance("Yahoo") << endl;
    cout << graphTwo.totalDistance("Microsoft") << endl;
    cout << graphTwo.totalDistance() << endl;

    return 1;
}