Блог пользователя papa-ka-para

Автор papa-ka-para, история, 11 дней назад, По-английски

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

»
11 дней назад, # |
  Проголосовать: нравится +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).

  • »
    »
    11 дней назад, # ^ |
      Проголосовать: нравится 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

    • »
      »
      »
      11 дней назад, # ^ |
        Проголосовать: нравится +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.

      • »
        »
        »
        »
        11 дней назад, # ^ |
        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.)

        • »
          »
          »
          »
          »
          11 дней назад, # ^ |
            Проголосовать: нравится -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://mirror.codeforces.com/blog/entry/60440#:~:text=1)%20Choose%20a%20random%20node,%2Cv)%20is%20the%20diameter.

          • »
            »
            »
            »
            »
            »
            11 дней назад, # ^ |
            Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

            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.

            • »
              »
              »
              »
              »
              »
              »
              11 дней назад, # ^ |
                Проголосовать: нравится -10 Проголосовать: не нравится

              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.

              • »
                »
                »
                »
                »
                »
                »
                »
                11 дней назад, # ^ |
                  Проголосовать: нравится 0 Проголосовать: не нравится

                How will that help ?

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

              • »
                »
                »
                »
                »
                »
                »
                »
                10 дней назад, # ^ |
                Rev. 5   Проголосовать: нравится 0 Проголосовать: не нравится

                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.

        • »
          »
          »
          »
          »
          11 дней назад, # ^ |
            Проголосовать: нравится +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
»
11 дней назад, # |
Rev. 3   Проголосовать: нравится -32 Проголосовать: не нравится

//(c) A+ Computer Science //www.apluscompsci.com //Name - Srinivas Potnuru //stores a thing and a count of how many times the thing is there package LinkedList; public class ThingCount implements Comparable { private int count; private Object thing; public ThingCount() { this(null, 0); } public ThingCount(Object thang, int cnt) { thing = thang; count = cnt; } public int getCount() { return count; } public void setCount(int cnt) { count = cnt; } public void setThing(Object obj) { thing = obj; } public Object getThing() { return thing; } public boolean equals(Object obj) { ThingCount other = (ThingCount)obj; return other.getThing().equals(getThing()); } public int compareTo(Object obj) { ThingCount other = (ThingCount)obj; if (!getThing().getClass().equals(other.getThing().getClass())) { throw new RuntimeException("both objects are not of the same type"); } return getThing().toString().compareTo(other.getThing().toString()); } public String toString() { return ""+ getThing() + " - " + getCount(); } }
»
11 дней назад, # |
  Проголосовать: нравится +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.