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;
}
Example #2
0
	virtual void SetUp() {
		A = g.addVert('A'); B = g.addVert('B'); C = g.addVert('C');
		D = g.addVert('D'); E = g.addVert('E'); F = g.addVert('F');
		G = g.addVert('G'); H = g.addVert('H');

		ab = g.addEdge(A, B, "ab"); ac = g.addEdge(A, C, "ac");
		bc = g.addEdge(B, C, "bc"); bd = g.addEdge(B, D, "bd"); bh = g.addEdge(B, H, "bh");
		cd = g.addEdge(D, C, "cd"); ce = g.addEdge(C, E, "ce");
		de = g.addEdge(D, E, "de"); df = g.addEdge(F, D, "df");

		vertMap[A] = 1.0; vertMap[B] = 2.0; vertMap[C] = 4.0; vertMap[D] = 3.0;
		vertMap[E] = 3.0; vertMap[F] = 1.0; vertMap[G] = 1.0; vertMap[H] = 2.0;

		misWeight = 0.0;
	}