需要优化Dijkstra(唯一算法)

时间:2015-04-20 20:38:06

标签: java algorithm optimization data-structures dijkstra

编辑:[已解决]

在这一页上看了我的答案


好吧,在我努力在JAVA中实现O(N.lgN)Dijkstra解决方案时,我意识到首先,很难为每个顶点创建相邻顶点和权重的元组。这很容易在C ++中使用:

完成
pair<int adv_vertex,int weight>   

如果你不知道我在说什么,请看看这个解决方案: http://qr.ae/LoESY

现在,不顾一切,我发现了一个类似于&#39;对&lt;&gt;&#39;在JAVA。 作为(1,1)的一个例子,它声明为:

Map.Entry<Integer, Integer> pair=new AbstractMap.SimpleEntry<Integer,Integer>(1,1);

请在此处阅读详细信息:https://stackoverflow.com/a/11253710/4258892

现在,我成功地使用此DataStructure实现了Dijkstra算法。我使用了一个带有自定义比较器的TreeSet。这几乎与PriorityQueue类似,它首先提取具有最小权重的顶点。这个问题可以被视为以下的扩展版本:

Having trouble Implementing a Custom Comparator for a TreeSet (Dijkstra's)

完成后,我试着解决了这个问题:

http://www.spoj.com/problems/EZDIJKST/

它清除了样本测试用例,但我在提交时获得了TLE()TIme Limit Exceeded)。对于实施Dijkstra来说,就像没有人一样。

以下是获得TLE的代码:http://ideone.com/wAQfBu

这是我的实际Dijkstra实现与解释。我提交给SPOJ的那个没有评论和输出提示。 你可能需要一段时间才能解决问题。我曾尝试在何时何地发表评论。

import java.io.*;
import java.util.*;

    /**
     * Created by Shreyans on 3/25/2015 at 7:26 PM using IntelliJ IDEA (Fast IO Template)
     */

    class DIJKSTA_TRY
    {
        public static void main(String[] args) throws Exception
        {
            InputReader in = new InputReader(System.in);
            //Initializing Graph

            //AbstractMap.SimpleEntry<Integer,Integer> is similar to pair<int a,int b> in C++

            List<ArrayList<AbstractMap.SimpleEntry<Integer,Integer>>>gr=new ArrayList<ArrayList<AbstractMap.SimpleEntry<Integer, Integer>>>();//AbstractMap.SimpleEntry<Integer,Integer> is similar to pair<int a,int b> in C++
            System.out.println("Enter no of Vertices");
            int v=in.readInt();
            System.out.println("Enter no of Edges");
            int e=in.readInt();
            Set<Integer>vertices=new HashSet<Integer>();
            for(int i=0;i<=v;i++)//Initializing rows for each vertex
            {
                vertices.add(i);
                gr.add(new ArrayList<AbstractMap.SimpleEntry<Integer, Integer>>());
            }
            vertices.remove(0);//Since 0 was added in advertantly
            System.out.println("Enter <Vertex> <Adjacent Vertex> <Weight>");
            for(int i=0;i<e;i++)
            {
                int a = in.readInt();
                int b = in.readInt();
                int c = in.readInt();
                gr.get(a).add(new AbstractMap.SimpleEntry<Integer, Integer>(b, c));
            }
            System.out.println("Enter Source");
            int s=in.readInt();
            Comparator<AbstractMap.SimpleEntry<Integer, Integer>> comparator=new WeightComparator();
            TreeSet<AbstractMap.SimpleEntry<Integer, Integer>>ts=new TreeSet<AbstractMap.SimpleEntry<Integer, Integer>>(comparator);
            int[]d=new int[v+1];
            Arrays.fill(d,Integer.MAX_VALUE);//Initial distance INFINITY
            d[s]=0;//Setting distance of source from source 0
            vertices.remove(s);
            for(AbstractMap.SimpleEntry<Integer, Integer> pair:gr.get(s))//Storing vertices adjacent to source
            {
                ts.add(pair);
                d[pair.getKey()]=pair.getValue();
            }
            while(!vertices.isEmpty()&&!ts.isEmpty())
            {
                AbstractMap.SimpleEntry<Integer, Integer> curr=ts.pollFirst();//Removing that element
                int V=curr.getKey();//Got adjacent vertex;
                int W=curr.getValue();//Got weight
                //vertices.remove();
                for(AbstractMap.SimpleEntry<Integer, Integer> pair:gr.get(V))//Now traversing vertives adjacent to V
                {
                    int v1=pair.getKey();
                    int w1=pair.getValue();
                    if(d[v1]>W+w1)//setting distance if shorted
                    {
                        d[v1]=W+w1;
                    }
                    ts.add(pair);//Adding to TreeSet
                }
                vertices.remove(V);//Removing V from Vertices set
            }
            System.out.println("Single Source Shortest Distance from Vertex "+(s)+" is:");
            for(int i=1;i<=v;i++)
            {
                System.out.println("Shortest Distance from Source Vertex "+s+" is: "+d[i]);
            }
        }

        static public class WeightComparator implements
                Comparator<AbstractMap.SimpleEntry<Integer, Integer>>
        {
            @Override
            public int compare(AbstractMap.SimpleEntry<Integer, Integer> one,
                               AbstractMap.SimpleEntry<Integer, Integer> two)
            {
                return Integer.compare(one.getValue(), two.getValue());
            }
        }
        //**FAST IO. THAT'S IT. NOTHING DOWN HERE**
        private static class InputReader
        {
            private InputStream stream;
            private byte[] buf = new byte[1024];
            private int curChar;
            private int numChars;
            private SpaceCharFilter filter;

            public InputReader(InputStream stream)
            {
                this.stream = stream;
            }

            public int read()
            {
                if (numChars == -1)
                    throw new InputMismatchException();
                if (curChar >= numChars)
                {
                    curChar = 0;
                    try
                    {
                        numChars = stream.read(buf);
                    } catch (IOException e)
                    {
                        throw new InputMismatchException();
                    }
                    if (numChars <= 0)
                        return -1;
                }
                return buf[curChar++];
            }

            public int readInt()
            {
                int c = read();
                while (isSpaceChar(c))
                    c = read();
                int sgn = 1;
                if (c == '-')
                {
                    sgn = -1;
                    c = read();
                }
                int res = 0;
                do
                {
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    res *= 10;
                    res += c - '0';
                    c = read();
                } while (!isSpaceChar(c));
                return res * sgn;
            }

            public String readString()
            {
                int c = read();
                while (isSpaceChar(c))
                    c = read();
                StringBuilder res = new StringBuilder();
                do
                {
                    res.appendCodePoint(c);
                    c = read();
                } while (!isSpaceChar(c));
                return res.toString();
            }

            public double readDouble()
            {
                int c = read();
                while (isSpaceChar(c))
                    c = read();
                int sgn = 1;
                if (c == '-')
                {
                    sgn = -1;
                    c = read();
                }
                double res = 0;
                while (!isSpaceChar(c) && c != '.')
                {
                    if (c == 'e' || c == 'E')
                        return res * Math.pow(10, readInt());
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    res *= 10;
                    res += c - '0';
                    c = read();
                }
                if (c == '.')
                {
                    c = read();
                    double m = 1;
                    while (!isSpaceChar(c))
                    {
                        if (c == 'e' || c == 'E')
                            return res * Math.pow(10, readInt());
                        if (c < '0' || c > '9')
                            throw new InputMismatchException();
                        m /= 10;
                        res += (c - '0') * m;
                        c = read();
                    }
                }
                return res * sgn;
            }

            public long readLong()
            {
                int c = read();
                while (isSpaceChar(c))
                    c = read();
                int sgn = 1;
                if (c == '-')
                {
                    sgn = -1;
                    c = read();
                }
                long res = 0;
                do
                {
                    if (c < '0' || c > '9')
                        throw new InputMismatchException();
                    res *= 10;
                    res += c - '0';
                    c = read();
                } while (!isSpaceChar(c));
                return res * sgn;
            }

            public boolean isSpaceChar(int c)
            {
                if (filter != null)
                    return filter.isSpaceChar(c);
                return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
            }

            public String next()
            {
                return readString();
            }

            public interface SpaceCharFilter
            {
                public boolean isSpaceChar(int ch);
            }
        }

        private static class OutputWriter
        {
            private final PrintWriter writer;

            public OutputWriter(OutputStream outputStream)
            {
                writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
            }

            public OutputWriter(Writer writer)
            {
                this.writer = new PrintWriter(writer);
            }

            public void print(Object... objects)
            {
                for (int i = 0; i < objects.length; i++)
                {
                    if (i != 0)
                        writer.print(' ');
                    writer.print(objects[i]);
                }
            }

            public void printLine(Object... objects)
            {
                print(objects);
                writer.println();
            }

            public void close()
            {
                writer.close();
            }

            public void flush()
            {
                writer.flush();
            }
        }
    }

当我完成编码时,我真的被激怒了。花了将近5个小时来找到所有的资源并将它们放在一起来实现这一目标,但是当它无法及时清除测试用例时,我感到非常失望。

有人可以建议另一种方式(可能是数据结构)/优化吗?

此外,这个算法的运行时间是多少?你把它改成了什么? (如果有)

3 个答案:

答案 0 :(得分:3)

  1. 您确定您的IO功能实际上比Java提供的更快吗?

  2. 代码似乎是O((M + N) log N),虽然有很大的常量。首先是因为你使用了一个集合而不是一个优先级队列(比如一个二进制堆),第二个是因为你创建了许多实例的AbstractMap.SimpleEntry。然后是因为你的arraylists列表,也可能不是很快。

  3. 在C ++中,你可以经常使用它(但并非总是如此;在C ++中,Dijkstra通常使用其优先级队列实现而不是其集合实现来实现)。对于Java,如果您想进行在线判断问题,我建议您尽可能少地使用现有的类SimpleEntry

    我要做的是自己编写一个binary heap实现并使用它而不是你的集合,除了所有的SimpleEntries,因为你有更多的控制与你自己的堆实现,你可以只使用一个简单的没有比较器的class Pair { public int node; public int weight; }

    如果你不想走那么远,也许你可以先尝试用Java自己PriorityQueue替换你的套装(小心使用右边的O(log N)删除方法!)。

答案 1 :(得分:2)

这不是Dijstra的算法。 Dijkstra的算法枚举从源开始的所有最短路径,而您的算法枚举从源开始的所有路径,甚至包含循环的路径。

例如,考虑图表

1 <----> 2 <----> 3 <----> 4 <----> 5

Dijstra会列举以下路径:

[1]
[1,2]
[1,2,3]
[1,2,3,4]
[1,2,3,4,5]

,而您的算法枚举

[1]
[1,2]
[1,2,1]
[1,2,3]
[1,2,1,2]
[1,2,3,2]
[1,2,3,4]
[1,2,1,2,1]
[1,2,1,2,3]
[1,2,3,2,1]
[1,2,3,2,3]
[1,2,3,4,3]
[1,2,3,4,5]

......如果图表更大,差异就更大了。例如,考虑顶点集合为整数的图形,当且仅当x + 1 = y或x = y + 1时,两个顶点x,y相邻。设0为源,n为目标。通过构造,从0到n的最短路径具有长度n。 Dijkstra将从-n到n的所有节点访问一次。您的算法将访问最多n个长度的所有路径,其中有2 ^ n(包括路径,如0,1,0,1,0,1,0,1,0,1,....)。对于n = 20,Dijkstra将访问41条路径,而您的代码将访问大约一百万条!

因此,在您摆弄快速I / O例程或尝试使用更快的树之前,请正确使用算法!

PS:除了速度慢之外,还有一些错误导致您的实现不正确。例如,如果我们有一个长度为n的路径,并添加一个长度为s的步长,则新路径的长度应为n + s,而不是s ...

答案 2 :(得分:1)

好吧,真的!所以,这是一个清洁和(希望)FAST Dijkstra的实施。

正如 IVlad 和其他一些人所建议的那样,而不是使用:

AbstractMap.SimpleEntry

我创建了一个名为&#39; Node&#39;的自定义数据类型。它存储2个整数元组

Node x=new Node(AdjacentVertex,Weight) 

我为这个&#39; Node&#39;实现了一个比较器。根据指定的权重对其进行排序的类。减轻重量 - &gt;更优先。

最后,我成功实现了一个优先级队列,用于存储“节点”的对象。根据比较器设定的上述顺序。

Queue<Node> pq=new PriorityQueue<Node>(new Node());

我在SPOJ上获得了我的解决方案AC(已接受)http://www.spoj.com/status/EZDIJKST,bholagabbar/

这是SPOJ代码(与下面的代码相同,但更快的IO和没有提示语句):http://ideone.com/p7u5vN

切入追逐,这是我的最终,简单,正确的&#39;和原始的实施

import java.util.*;

/**
 * Created by Shreyans on 4/21/2015 at 6:32 PM using IntelliJ IDEA (Fast IO Template)
 */

class DIJKSTRA
{
    public static void main(String[] args) throws Exception
    {
        Scanner sc=new Scanner(System.in);
        List<ArrayList<Node>> gr=new ArrayList<ArrayList<Node>>();//Initialising Adj list to store graph
        System.out.println("Enter Number of Vertices");
        int v=sc.nextInt();
        for(int i=0;i<=v;i++)
        {
            gr.add(new ArrayList<Node>());
        }
        System.out.println("Enter Number of Edges");
        int e=sc.nextInt();
        System.out.println("Enter <Vertex> <Adjacent Vertex> <Weight>");
        for(int i=0;i<e;i++)
        {
            int a=sc.nextInt();
            int b=sc.nextInt();
            int c=sc.nextInt();
            gr.get(a).add(new Node(b,c));
        }//Built Graph
        System.out.println("Enter Source");
        int s=sc.nextInt();
        //int des=sc.nextInt();//Entering Destination
        Queue<Node> pq=new PriorityQueue<Node>(new Node());//Heap to extract value
        boolean[]checked=new boolean[v+1];//Keeping track of checked values
        int[]d=new int[v+1];//Keeping track of distances
        Arrays.fill(d,Integer.MAX_VALUE);
        d[s]=0;
        pq.clear();
        pq.offer(new Node(s,0));
        while(!pq.isEmpty())
        {
            Node x=pq.poll();
            int V=x.node;//Getting next node from heap
            int W=x.cost;//Getting cost
            checked[V]=true;
            for(int i=0;i<gr.get(V).size();i++)
            {
                Node z=gr.get(V).get(i);//Getting all adjacent Vertices
                if(!checked[(z.node)])//Not checking visited Vertices
                {
                    int v1=z.node;
                    int w1=z.cost;
                    if(d[v1]>W+w1)//Checking for min weight
                    {
                        d[v1]=W+w1;
                    }
                    pq.offer(new Node(v1,d[v1]));//Adding element to PriorityQueue
                }
            }
        }
        for(int i=1;i<=v;i++)//Printing Shortest Distances. Ignore ones with Integer.MAV_VALUE. Meh
        {
            if(d[i]==Integer.MAX_VALUE)
            {
                System.out.println("No Path connecting Source Vertex "+s+" to Vertex "+i);
            }
            else
            {
                System.out.println("Shortest distance from  Source Vertex "+s+" to Vertex "+i+" is: "+d[i]);
            }
        }
    }
}
class Node implements Comparator<Node>
{
    public int node;
    public int cost;

    public Node(){}

    public Node(int node, int cost)
    {
        this.node = node;
        this.cost = cost;
    }

    @Override
    public int compare(Node node1, Node node2)
    {
        if (node1.cost < node2.cost)
            return -1;
        if (node1.cost > node2.cost)
            return 1;
        return 0;
    }
}

有人能告诉我这次实施的时间和空间复杂性是什么? 我需要这些细节来完成我正在做的项目。我的实施可以进一步改进吗?建议最受欢迎

相关问题