for an assignment I am asked by to find find an algorithm that calculates the transitive closure of a directed graph using O(n 4 ) time. We already learned about the floyd warshall algorithm, which is a lot better, so can someone help me create one that runs in O(n4) time? is there such an algorithm?
I know it seems dumb of a question. I dont really understand why we are being asked to find the slower way to do it.
Floyd Warshall is
O(n^3), and sinceO(n^3)is a subset ofO(n^4), it is alsoO(n^4).Thus, by setting a new graph
G'=(V,E',w')whereE' = V x V(clique, complete graph) andw'(u,v) = 1 if (u,v) is in E, otherwise INFINITY– by using Floyd-Warshall algorithm, each pair(u,v)that ends up with a value less then infinity is in the closure.A Theta(n^4) solution:
Complexity is trivially
Theta(n^4), we only need to show it indeed finds the transitive closure.By induction:
since (u,v) is in E.
k:Each pair
(u,v)with shortest path of lengthk>1– there iswsuch that there is a pathu -> ... -> w, and an edge(w,v). From induction hypothesis, in previous iterations we added(u,w)toQ, and thus the condition will yield true, and we will add(u,v)to the resulting setQ.Similary show that if some pair
(u,v)was added to Q, then there is a pathu->..->w->v, and thus it was rightfully added.A second
Theta(n^4)solution:Set
G'as described above, and for each vertexvinVrun Bellman Ford fromv.Each run of BF is
Theta(n^3)1, running itntimes isTheta(n^4)(1) Technically it is
O(VE), but for none sparse graphsEis inTheta(V^2)