Graph::isContainCycle

Function


bool isContainCycle();

Returns whether this graph contain cycle or not.

Parameters

None

Return value

Return true if there exists cycle in the graph, false otherwise.

Example

#include "HKUAL_graph.h"
#include <iostream>
using namespace std;
using namespace HKUAL;
int main(){
    Graph<string> g;
    g.addVertex("A");
    g.addVertex("B");
    g.addVertex("C");
    g.addEdge("A","B");
    g.addEdge("A","C");
    g.addEdge("B","C");

    if (g.isContainCycle())
        cout <<"Yes" << endl;
    else
        cout <<"No" << endl;
        
    Graph<string> g2;
    g2.addVertex("A");
    g2.addVertex("B");
    g2.addVertex("C");
    g2.addEdge("A","B");
    g2.addEdge("A","C");

    if (g2.isContainCycle())
        cout <<"Yes" << endl;
    else
        cout <<"No" << endl;
    return 0;
}

Output

Yes
No

Time Complexity

, for is number of edges and is total number of vertives.

© The University of Hong Kong Algorithms Library - hkual@cs.hku.hk