Блог пользователя drexdelta

Автор drexdelta, история, 21 месяц назад, По-английски

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 )
  • Проголосовать: нравится
  • +6
  • Проголосовать: не нравится

»
21 месяц назад, скрыть # |
 
Проголосовать: нравится +15 Проголосовать: не нравится

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 месяц назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    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 месяц назад, скрыть # ^ |
       
      Проголосовать: нравится +15 Проголосовать: не нравится

      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 месяц назад, скрыть # ^ |
        Rev. 3  
        Проголосовать: нравится 0 Проголосовать: не нравится

        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 месяц назад, скрыть # ^ |
           
          Проголосовать: нравится -13 Проголосовать: не нравится

          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 месяц назад, скрыть # ^ |
           
          Проголосовать: нравится +17 Проголосовать: не нравится

          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 месяц назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

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.