处理文件比邻接链接列表图的O(MN)时间更快

时间:2015-07-25 23:04:49

标签: java time-complexity

这篇文章与我在第一篇文章中的解决方案有关

现在我已经解决了my first post中的重复问题,我想知道我是否能够以超过O(MN)时间的速度处理我的文件(假设只有循环和遍历文件会增加复杂性),那就是我的分析是正确的。这不是一个功课问题,这纯粹是我的兴趣。

我说O(MN)是因为我走了M线并且在每个线路的最坏情况下将其斩断为N个令牌。这是正确的吗?我原来说O(N ^ 2)。不过,有没有更有效的方法?

这里的方法在清晰度方面稍微改进了我以前的方法,但我真的想处理这个文件而不必走线和标记每一行,但我不是那种经验丰富的文件处理顺序有这种聪明才智,我也不知道该找什么。我希望我能看到摆脱嵌套while循环的方法。有人知道这样做的方法吗? try-catch块纯粹是为了捕捉问题,但绝对会被删除,因为这不是一个真实的世界实现,也不是为了适应性。

private void processFile(File file)
{   
    Scanner sc;

    /* First we must try to create a scanner from the file */
    try 
    {
        sc = new Scanner(file);

        /* Initializing the hashmap with empty lists in this loop */
        while (sc.hasNext())
        {
            String from = new String();

            /* Here we are skipping over integers for now */
            try
            {
                from = sc.next();
                Integer.parseInt(from);
            }
            /* If parsing the int failed, it must be an airport id */
            catch(NumberFormatException e)
            {
                /* Make sure it does not already contain the key in the hashmap */
                if (this.airports.containsKey(from))
                    continue;

                /* Add the key and value to the hashmap */
                AirportVertex source = new AirportVertex(from);
                airports.put(from, source);
                this.numNodes++;
            }
            catch(NoSuchElementException e)
            {
                System.err.println("There are no more tokens available");
            }
        }

        /* Reinitialize the scanner */
        sc.close();
        sc = new Scanner(file);
        String line = new String();
        String from = new String();

        /* Adding adjacent airports */
        while(sc.hasNextLine())
        {   
            /* Try to create Scanner for the line read from the file */
            try
            {
                line = sc.nextLine();
                Scanner tokens = new Scanner(line);

                /* Try to read tokens */
                try
                {
                    from = tokens.next();
                }
                catch(NoSuchElementException e)
                {
                    System.err.println("There are no more tokens available to scan for the ");
                }

                /* The first token is the source airport */
                AirportVertex source = this.airports.get(from);


                /* Read the rest of the line */
                while (tokens.hasNext())
                {
                    /* The file has a pattern of alternating strings and ints after the first string read on each line */
                    AirportVertex dest = this.airports.get(tokens.next());

                    int cost = tokens.nextInt();

                    /* You cannot only go one way, must assume both paths */
                    if (!source.adj_lst.contains(dest) && !dest.adj_lst.contains(source))
                    {
                        source.adj_lst.add(dest);
                        dest.adj_lst.add(source);
                        AirportEdge edge = new AirportEdge(source, dest, cost);
                        source.edge_lst.add(edge);
                        dest.edge_lst.add(edge);
                    }

                    /* The destination is now the source for the next destination */
                    source = dest;
                }
            }
            catch(NoSuchElementException e)
            {
                System.err.println("No line could be found");
            }
        }
    } 
    catch (FileNotFoundException e) 
    {
        System.err.println("File could not be found");
        e.printStackTrace();
    }

    printGraph();
    System.out.println("\n");
    printEdgeInfo();
}

printGraph();printEdgeInfo();对此代码的输出如下所示

SHV|  OKC SFO DFW
MOB|  DFW ATL
LAX|  HOU LIT DFW
OKC|  MSY SHV DFW
BOS|  ATL DFW AUS HOU
SAT|  HOU DFW AUS
HOU|  DFW SAT BOS LAX AUS
MSY|  LIT OKC DFW
DFW|  BOS MOB AUS HOU ATL SAT LAX SFO LIT MSY OKC SHV
LIT|  LAX MSY DFW
ATL|  BOS DFW AUS MOB
SFO|  SHV DFW
AUS|  DFW ATL BOS HOU SAT


SHV| OKC --> SHV Cost: 59 SHV --> SFO Cost: 1200 SHV --> DFW Cost: 59 
MOB| DFW --> MOB Cost: 59 MOB --> ATL Cost: 59 
LAX| HOU --> LAX Cost: 1000 LAX --> LIT Cost: 59 LAX --> DFW Cost: 1000 
OKC| MSY --> OKC Cost: 59 OKC --> SHV Cost: 59 OKC --> DFW Cost: 59 
BOS| ATL --> BOS Cost: 250 BOS --> DFW Cost: 250 AUS --> BOS Cost: 250 BOS --> HOU Cost: 128 
SAT| HOU --> SAT Cost: 59 DFW --> SAT Cost: 59 SAT --> AUS Cost: 59 
HOU| DFW --> HOU Cost: 59 HOU --> SAT Cost: 59 BOS --> HOU Cost: 128 HOU --> LAX Cost: 1000 HOU --> AUS Cost: 59 
MSY| LIT --> MSY Cost: 128 MSY --> OKC Cost: 59 MSY --> DFW Cost: 128 
DFW| BOS --> DFW Cost: 250 DFW --> MOB Cost: 59 AUS --> DFW Cost: 59 DFW --> HOU Cost: 59 ATL --> DFW Cost: 250 DFW --> SAT Cost: 59 LAX --> DFW Cost: 1000 DFW --> SFO Cost: 100 LIT --> DFW Cost: 59 MSY --> DFW Cost: 128 OKC --> DFW Cost: 59 SHV --> DFW Cost: 59 
LIT| LAX --> LIT Cost: 59 LIT --> MSY Cost: 128 LIT --> DFW Cost: 59 
ATL| ATL --> BOS Cost: 250 ATL --> DFW Cost: 250 ATL --> AUS Cost: 59 MOB --> ATL Cost: 59 
SFO| SHV --> SFO Cost: 1200 DFW --> SFO Cost: 100 
AUS| AUS --> DFW Cost: 59 ATL --> AUS Cost: 59 AUS --> BOS Cost: 250 HOU --> AUS Cost: 59 SAT --> AUS Cost: 59

1 个答案:

答案 0 :(得分:0)

这不是O(n²),它是O(n),因为你只需要每个令牌固定次数,即使在另一个while循环中有一个while循环。

对于它是O(n²),对于每个令牌,您必须访问扫描仪中的所有(或一小部分)其他令牌。