int main() {

    MyGraph<char> *mg = new DirectedGraph<char>();
    stack<int> path;
    for (int i = 0; i < 6; i++)
        mg->addVertex('A' + i);
    mg->addEdge('A', 'B');
    mg->addEdge('B', 'C');
    mg->addEdge('C', 'D');
    mg->addEdge('D', 'B'); // create a circle
    mg->addEdge('B', 'E');
    mg->addEdge('E', 'D'); // create another circle
    mg->addEdge('E', 'F');
    mg->printGraph();

    cout << "reachable? DFS: "
        << (isReachable_DFS(mg->getGraph(), mg->size(), 0, 5, path) == 1 ?
                "TRUE" : "FALSE") << endl;
    cout << "path: A -> ";
    while (!path.empty()) {
        cout << (char) ('A' + path.top()) << (path.size() > 1 ? " -> " : "");
        path.pop();
    }
    cout << endl;

    cout << "reachable? BFS: "
        << (isReachable_BFS(dynamic_cast<DirectedGraph<char> *>(mg),'A', 'F') == 1 ? "TRUE" : "FALSE") << endl;

    return 0;
}
Exemple #2
0
/**
 * Kompilace: $ g++ -std=c++11 -Wall -pedantic main.cpp mygraph.cpp edge.cpp node.cpp dijkstra.cpp -o graph
 * ./graph [string] ... Spusti program, nacte ze souboru.
 * ./graph [string] [unsigned int] [unsigned int] ... Generuje graf do souboru,
 * druhy parametr je pocet uzlu a treti parametr pocet hran, ktere v prumeru
 * vedou z nejakeho uzlu.
 */
int main(int argc, char* const argv[]) {
    ios::sync_with_stdio(false);
    MyGraph * graph = new MyGraph();

    try {
        switch(argc) {
            case 2: {
                graph->load(argv[1]);

                //ofstream ofs;

                //ofs.open("dijkstra.out");
                //if(!ofs.is_open()) throw IOException();

                double ** matrix;
                CDijkstra dijkstra = CDijkstra(graph);
                clock_t begin = clock();
                //matrix = dijkstra.CalculateDistanceMatrix();
                clock_t end = clock();
                //printMatrix(ofs, matrix, graph->size());

                //ofs.close();

                double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;

                cout << "Dijkstra:\t" << elapsed_secs << " sec" << endl;

                //ofs.open("floydwarshall.out");
                //if(!ofs.is_open()) throw IOException();

                int blockSize = 36;
                while(graph->size() % blockSize != 0) ++blockSize;

                begin = clock();
                matrix = floydWarschall(graph, blockSize);
                end = clock();
                //printMatrix(ofs, matrix, graph->size());
                //ofs.close();
                elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;

                cout << "FloydWarshall:\t" << elapsed_secs << " sec" << endl;

                /*double ** matrix;

                for(unsigned i = 1; i < 1080; ++i) {
                    if(1080 % i == 0)
                    {
                        clock_t begin = clock();
                        matrix = floydWarschall(graph, i);
                        clock_t end = clock();
                        cout << i << " " << double(end - begin) / CLOCKS_PER_SEC << endl;
                    }
                }*/

                break;
            }
            case 4: {
                unsigned int n, pocetHran;
                string arg = argv[2];
                arg += " ";
                arg += argv[3];
                arg += " ";
                stringstream ss(arg);
                ss >> n;
                ss >> pocetHran;
                if(ss.eof() || ss.fail()) throw IOException();
                graph->graphGen(n, pocetHran, argv[1]);
                break;
            }
            default: {
                throw IllegalArgumentException();
                break;
            }
        }
    }
    catch(IOException) {}
    catch(IllegalArgumentException) {}
    catch(...) {
        cerr << "Neco se podelalo." << endl;
    }

    delete graph;

    return 0;
}