English 中文(简体)
DAG (unweighed),在2个vert之间找到最短的道路
原标题:Finding Shortest path in DAG (unweighed), between 2 vertices

在Flinoyd-Warshall/Dijkstra对洪水做出答复之前,请让我解释一下这一情况,即确保这两种算法都能够用于本案,而且这并不是一个简便的范例方案(你在java必须保持可以承受的记忆力)。

第3号是第0号至第1号的网页,因为第5号是第5号,第3号是第3号是第4号。 每一“node”在_neighbours[nodeID]和_neighbours[nodeID] 之间都有代表,因此,我们再谈一下诺德。 3. 又注意到在_/out_中,两者都分类(自然分类为5)将一劳永逸地选择其联系,但只有6个选择——这样3个联系——永远不会包含{6、5、7})和c 两者可以包含重复。 (在/外有色体大小的阵列,其中总大小为小或小米,而用户在开始时也规定了N)

无重量。 必须做的是找到平均差异。

public double getAvgDistance() {
    int sum = 0;        

    for (int i=1; i<n; i++) {
        for (int j=0; j < i; j++) {
            sum += dist(i, j);             // there are duplicates, make sure i skip 
        }
    }

    return (double)sum / (double)(  ((n*(n-1)) / 2)  );
}

我迄今的情况是最好的。 说明一只希望找到“j &”之间的距离,即不是同时所有的距离(没有足够记忆,将在m=20 d=1 000上测试)。

private int dist(int i, int j) {
    int dist = 0;

    for (int link : in_neighbours[j]) {
        System.out.print("
Is "+j+" linked to by "+i);
        if (out_neighbours[i].contains(link)) {
            System.out.print(" - yes!");
            dist = 1;
        }
    }

    return dist;
}

因此,不问“fresher”(此时此刻完成图表)是否将“fresher”与任何较老的ud直接联系起来,如果是的话,距离是一环。

如果 no子被 trav倒,这是否只是我,或者说最短的道路永远是第一条道路?

How do i check if its not 1, the "else" after the base case? My math is fairly weak please be gentle :) Any hints how to make use of the fact that the links are sorted?

它不是家庭工作,也不是试图从中挑出来的东西,它并不涉及守则本身,而是必须是一种有用的tool<>em>,“学习”本身就是这样。

这里的图表如何看待数据交换,在m=7 n=13的链接中发现链接(脚注0的周期只是图表的初始化):

0 | 0 0 0 0 0 0 0  | 0 0 0 0 0 0 0 1 1 1 1 1 1 1 2 2 2 2 2 3 4 5 6 6 7 8 9 
1 | 0 0 0 0 0 0 0  | 2 2 3 4 5 5 8 12 
2 | 0 0 0 0 0 1 1  | 3 3 3 3 3 4 4 4 6 7 8 10 
3 | 0 1 2 2 2 2 2  | 4 4 5 5 6 6 7 11 
4 | 0 1 2 2 2 3 3  | 5 5 6 8 9 10 
5 | 0 1 1 3 3 4 4  | 6 7 8 9 9 11 12 
6 | 0 0 2 3 3 4 5  | 7 7 7 8 9 9 12 
7 | 0 2 3 5 6 6 6  | 8 9 10 11 11 12 
8 | 0 1 2 4 5 6 7  | 10 10 10 11 12 
9 | 0 4 5 5 6 6 7  | 10 11 11 
10 | 2 4 7 8 8 8 9  | 12 12 
11 | 3 5 7 7 8 9 9  | 
12 | 1 5 6 7 8 10 10  | 

Sorry for the agonising long read. EDIT: Wrong code in the methods, this is what i think is correct now.

3. 修订 dis2,只是尝试和找到一条道路:

private int dist(int i, int j) {
    int dist = 0, c = 0, count = 0;
    boolean linkExists = false;

    for (int link : in_neighbours[j]) {

        //System.out.print("
Is "+j+" linked to by "+i);
        if (out_neighbours[i].contains(link)) {

            //System.out.print(" - yes!");
            dist = 1; // there is a direct link

        } else {

            while ( c < j ) {
                // if there s a path from 0 up to j, check if  i  links to a node which eventually links to  j 
                if (out_neighbours[i].contains(c) && 
                        (out_neighbours[c].contains(link) || in_neighbours[c].contains(link) )) {

                    count++; // yes. and this is one node we had to step through to get closer
                    linkExists = true;
                } else {
                    linkExists = false; // unreachable, the path was interrupted somewhere on the way
                    break;
                }
                c++; 
            }

            if (linkExists) {
                dist = count-1; // as 2 nodes are linked with 1 edge
            } else {
                dist = 0; // no path was found
            }
        }


    }

    return dist;
}
问题回答

由于所有方面在你的模型中都拥有同样的权重,你可以使用BFS,寻求从S到T找到最短的道路。

This is an iterative process, starting with set #0, containing only the source node ({S}). At each step i, you create set #i by finding all nodes achievable from set (i-1) in one step.

循环终止了两种情况:

(1) 当你发现该编号k含有T时。 在这种情况下,你将返回K-1。

2) 当这块 set空时,即两条 no子是不可击的。

记忆消耗量大约是节点数的两倍,因为每步一时,你就与两组(一至一)合作,受节点总数的约束。

页: 1

这里可以实施(我就此做了一些测试):

private Integer getDist(int i, int j) {
    Set<Integer> currentSet = new HashSet<Integer>();
    currentSet.add(i);
    int dist = 0;
    while (true) {
        Set<Integer> nextSet = new HashSet<Integer>();
        for (Integer currNode : currentSet)
            nextSet.addAll(out[currNode]);

        if (nextSet.isEmpty())
            return null; //i.e. infinite
        if (nextSet.contains(j))
            return dist;
        dist++;
        currentSet = nextSet; 
    }
}

执行假设:inout> >被定义为“清单与附件”;Integer>[],点数从0起就确定。 最低距离是道路上中间点的数量,而不是边缘数目。

你在名单上的重复没有打破任何东西,但与算法无关。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...