drexdelta's blog

By drexdelta, history, 21 month(s) ago, In English

Hi all, I have just now solved this leetcode problem in O(N^3).

I am curious if we can solve this problem in O ( N ^ 2 ) ?

For those, who are interested in understanding O ( N ^ 3 ) approach, please let me know, I will try my best to explain.

MY code for O ( N ^ 3 )
  • Vote: I like it
  • +6
  • Vote: I do not like it

| Write comment?
»
21 month(s) ago, hide # |
 
Vote: I like it +15 Vote: I do not like it

Prety sure that the result is the diameter of the graph. As far as I know you cannot do that faster than $$$O(N*M)$$$, but there exist approximate algorithms that run in $$$O(N+M)$$$ and have an approximation factor of $$$2$$$. (I don't know if there are other algorithms that have been discovered/invented for graph diameters that are better).

  • »
    »
    21 month(s) ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    I am not that expert in the graph theory. How to find diameter in such a cyclic graph ?

    Whats-App-Image-2024-12-09-at-9-58-12-PM
    case

    • »
      »
      »
      21 month(s) ago, hide # ^ |
       
      Vote: I like it +15 Vote: I do not like it

      What you drew is not the diameter. In a general graph the diameter is defined as the maximum minimum distance between two nodes. That is: Take all pairs of nodes and compute the distance between them. The maximum of these values is the diameter.

      • »
        »
        »
        »
        21 month(s) ago, hide # ^ |
        Rev. 3  
        Vote: I like it 0 Vote: I do not like it

        understood. Thanks. Helpful. I saw one solution, which did floyd-warshal to find distances of each pair, and then took maximum of it. I understood it now, earlier I couldn't .

        Now, Coming to the above diagram, (just in case if you have time)

        how to find length of longest path in undirected graph, where every node is visited only once the path. ( I can't think anything but brute-dfs from each node.)

        • »
          »
          »
          »
          »
          21 month(s) ago, hide # ^ |
           
          Vote: I like it -13 Vote: I do not like it

          It's a classic problem that can be solved in O(n + m). One of the ways you can solve it is by DFS from an arbitrary node u and finding any farthest node from it. Let such node be v. Now, the claim is that the farthest node from v is the diameter of the graph. So, calling DFS twice finds such longest simple path.

          This might help: https://codeforces.me/blog/entry/60440#:~:text=1)%20Choose%20a%20random%20node,%2Cv)%20is%20the%20diameter.

          • »
            »
            »
            »
            »
            »
            21 month(s) ago, hide # ^ |
            Rev. 2  
            Vote: I like it 0 Vote: I do not like it

            Try running your algorithm on the graph I have drawn above. Take left most node as initial random node, and see, if it it works.

            The algo you shared, it works when you don't have cycles.

            • »
              »
              »
              »
              »
              »
              »
              21 month(s) ago, hide # ^ |
               
              Vote: I like it -10 Vote: I do not like it

              Oh sorry about it. You solve the cyclic version in O(N^2 log n) by running dijkstra starting at every node. I'm not sure if you can do better with edge weights <= 10^4.

              • »
                »
                »
                »
                »
                »
                »
                »
                21 month(s) ago, hide # ^ |
                 
                Vote: I like it 0 Vote: I do not like it

                How will that help ?

                If you can provide some POC, i will be grateful.

              • »
                »
                »
                »
                »
                »
                »
                »
                21 month(s) ago, hide # ^ |
                Rev. 5  
                Vote: I like it 0 Vote: I do not like it

                Guys I thought he meant longest minimum path (aka diameter). I was suggesting a way to find the diameter of a cyclic graph by running a dijkstra from every node. But it's ok.

        • »
          »
          »
          »
          »
          21 month(s) ago, hide # ^ |
           
          Vote: I like it +17 Vote: I do not like it

          Finding a longest path is NP-Hard: https://en.wikipedia.org/wiki/Longest_path_problem

          Probably the best algorithm to find a longest path is a dp with running time O(2^n * n^2).

          Where for every vertex v (n choices) and subset of vertices S (2^n choices) you set dp[v][S] = true, if there exists a path that ends in v and visits exactly the vertices in S.

          A convenient way to implement this, is using bitmasks.

          Code
»
21 month(s) ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

If you have a cycle with an odd length, then such a grouping is not possible. You can check that with a simple bipartite-checking algorithm, which runs in $$$O(n+m)$$$. Now that we know that the graph is bipartite, the answer is the diameter of the graph. A simple approach would be to start a BFS from each node, which would run in $$$O(n*m)$$$. Maybe we can use the bipartite property to calculate the diameter of the graph more efficiently, however I don't know how that would be done.