English 中文(简体)
在实际最短的道路上只回收vert
原标题:Returning only the vertices in the actual shortest path

我知道这个标题是微不足道的,但我不知道如何更好地解释。

www.un.org/Depts/DGACM/index_spanish.htm 我想要做的是:。

利用文本档案中的图表,发现并打印出最短的道路(最小的vert量),从A到B。

注:使用广度第一搜寻,而不是Dijkstra。

www.un.org/Depts/DGACM/index_spanish.htm The I ve got:

实用算法在图表上适用BAFS,但实际上没有最短的道路。

I m having a hard time distinguishing a vertex in the shortest path from one that is simply run through the algorithm, but not in the shortest path.

For example: Find the shortest path between 0 and 4. 0 connects to 1,2 and 3. 1 connects to 4. My path turns out to be [0,1,2,3,4] instead of [0,1,4].

我没有能够找到任何线索来提出同样的问题,也没有看到任何穿过包括这个问题的BAFS的行走,因此我不敢确定我是否这样做。 这比现在更加困难?

<<>Edit: Code for those who may be interest (not 当然,如果我避免圈子的话)

Edit 2: Changed the way I store my path to a Stack.

public String findPath(int v, int w) {
    Queue<Integer> q = new LinkedList<Integer>();
    boolean[] visited = new boolean[g.numVertices()];

    q.add(v);
    Stack<Integer> path = new Stack<Integer>();
    while(q.peek() != null) {
        runBFS(q.poll(),w,visited,q,path);
    }
    return path.toString();
} 

private void runBFS(int v, int w, boolean[] visited, Queue<Integer> q, Stack<Integer> path) {
    if(visited[v]) {
    }
    else if(v == w) {
        path.add(v);
        q.clear();
    }
    else {
        path.add(v);
        visited[v] = true;
        VertexIterator vi = g.adjacentVertices(v);
        while(vi.hasNext()) {
                q.add(vi.next());
        }
    }
}

对变量和方法的一些解释:

v = 来源的垂直

w = 目标垂直

g = 图表

vi = 一种正常的变压器,在高射线邻国上空扩散

www.un.org/Depts/DGACM/index_spanish.htm 感谢阅读!

最佳回答

You will have to have specific path field for each vertex. That way you can keep track of the paths you ve chosen, hence the short path found. I will use an String array, just like you used the Boolean array for storing visited vertices.

public String findPath(int v, int w) {
    Queue<Integer> q = new LinkedList<Integer>();
    boolean[] visited = new boolean[g.numVertices()];
    String[] pathTo = new String[g.numVertices()];

    q.add(v);
    pathTo[v] = v+" ";
    while(q.peek() != null) {
        if(runBFS(q.poll(),w,visited,q,pathTo))
        break;
    }
    return pathTo[w];
}

private boolean runBFS(int v, int w, boolean[] visited, Queue<Integer> q, String[] pathTo) {
    if(visited[v]) {
    }
    else if(v == w)
        return true; 
    }
    else {
        visited[v] = true;
        VertexIterator vi = g.adjacentVertices(v);
        while(vi.hasNext()) {
            int nextVertex = vi.next();
            pathTo[nextVertex] = pathTo[v] + nextVertex + " ";
            q.add(nextVertex);
        }
    }
    return false;
}
问题回答

我们的助理们建议的另一项契约(空间-噪音)解决办法是,每个仓库只能存放从中减去。 可以通过将访问清单改成分类阵列(<>编码>访问)。

第1步:开始访问名单,以便每个要素为<条码>-1,或“无人注意”

step 2: mark the first node as visited by itself visited[v] = v;

(如你所做的那样)

移至-> v_next:

if(visited[v_next] == -1)
{
  visited[v_next] = v;
  q.put(v_next);
}
// else skip it, it s already been visited

这样,如果可以接触到,就能够储存它从那代之来的空洞,你可以回过来,最后按相反的顺序打印。 (采用打字或复读印刷方法进行)

希望具有意义:

当你在BFS的电离层中存储外溢时,你还需要储存已到达的道路的复印件,以便一旦电离层出水时即可。 既然现在,你的法典没有把任何类型的道路信息保存在被 que弄的vert子上,它只保留了它访问的节点清单。

例如,你可以使用一个将同时处理的不同地点,在这种地方,你将储存目前的道路,然后在你把下个外部线排出后再找回。

You need to push your current node onto a stack, and only print the whole stack out once you reach the destination.





相关问题
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 ...

热门标签