Imagine I have a weighted complete directed graph $G$ with $d$ vertices(so $d(d-1)$ edges) and I want to do the following:
- Set $D$ to be a DAG with the same set of vertices but without any edges
- sort the edge weights $c_{ji}$ of the original graph by size
- From the biggest to the lowest edge weight: Add an edge $(j,i)$ to the DAG $D$ if we do not cause a cycle with it
Now I see two approaches to implement this.the first one is:
- Set $D$ to be a DAG with the same set of vertices but without any edges
- sort the edge weights $c_{ji}$ of the original graph by size
- From the biggest to the lowest edge weight: Add an edge $(j,i)$ to the DAG $D$ if there is no path from $i$ to $j$
Now sorting can be neglected in complexity ( $O(d^2\log(d))$)
And number three is performed at most $d(d-1)$ times and to check if a path exists we can use BFS or DFS and the number of edges, unfortunately, can be as big as $d^2$ so we have $d^4$ in total
My second idea: Work with a reachability matrix and update the reachability matrix in every iteration of the loop... But then updating the reachability matrix is again of complexity $d^2$ because if we set an edge from $j$ to $i$ then denoting the ancestors of $j$ by $k_1$ and the descendants of $i$ by $k_2$ we need to set $R(u,w)=1$, $R(u,i)=1$, $R(j,w)=1$ for any $u$ in $k_1$ and $w$ in $k_2$ .... Assuming that in the worst case that each of $k_1$ and $k_2$ have $d/2-1$ vertices we get a complexity of $d^2$ That we need to update the reachability matrix more than $O(d)$ times can e.g. be seen by looking at bipartite graphs... So I'm again stuck with the same complexity... Is there actually a more efficient solution?