Graph Theory37 sections · 1633 units
Open in Course

Implementation - Flight Routes Check

Kosaraju with example output

Here is the SCC-based implementation:

function flightRoutesCheck(n, adj):
    sccId = kosarajuSCC(adj)
    numSCCs = max(sccId) + 1

    if numSCCs == 1:
        print "YES"
        return

    // Find nodes in different SCCs
    a = any node with sccId[a] == 0
    b = any node with sccId[b] == 1

    // Check reachability with BFS
    canReach = BFS(adj, a, b)

    print "NO"
    if canReach:
        print b, a
    else:
        print a, b

Time: O(V+E)O(V + E). Space: O(V)O(V).