[英]Find if there exists any path between two specified vertices in a weighted digraph
I am trying to implement a method isReachable(E fromKey, E toKey)
to determine if any path exists between the two specified vertices in the graph. 我正在尝试实现isReachable(E fromKey, E toKey)
来确定图中的两个指定顶点之间是否存在任何路径。 I have a generic class Graph<E>
that uses two inner data structures, Vertex
and Edge
, to represent the vertices and edges of the graph. 我有一个通用类Graph<E>
,该类使用两个内部数据结构Vertex
和Edge
来表示图形的顶点和边缘。 Here is the code for that: 这是该代码:
public class Graph<E extends Comparable<E>> implements GraphAPI<E>
{
/*
* number of vertices (size of this graph)
*/
private long order;
/**
* pointer to the list of vertices
*/
private Vertex first;
/**
* A vertex of a graph stores a data item and references
* to its edge list and the succeeding vertex. The data
* object extends the comparable interface.
*/
private class Vertex
{
/**
* pointer to the next vertex
*/
public Vertex pNextVertex;
/**
* the data item
*/
public E data;
/**
* indegree
*/
public long inDeg;
/**
* outdegree
*/
public long outDeg;
/**
* pointer to the edge list
*/
public Edge pEdge;
/**
* Field for tracking vertex accesses
*/
public long processed;
}
/**
* An edge of a graph contains a reference to the destination
* vertex, a reference to the succeeding edge in the edge list and
* the weight of the directed edge.
*/
private class Edge
{
/**
* pointer to the destination vertex
*/
public Vertex destination;
/**
* weight on this edge
*/
public Double weight;
/**
* pointer to the next edge
*/
public Edge pNextEdge;
}
/**
* Constructs an empty weighted directed graph
*/
public Graph()
{
first = null;
order = 0;
}
}
This is my thought process: (1) Walk through the list of vertices until reaching the Vertex containing the specified fromKey
; 这是我的思考过程:(1)遍历顶点列表,直到到达包含指定fromKey
的顶点; (2) add each adjacent Vertex to the fromKey
to a queue; (2)将每个相邻的顶点添加到fromKey
的队列中; (3) while the queue is not empty, retrieve and remove the Vertex at the head of the queue and compare it's key to the toKey
; (3)在队列不为空的情况下,检索并删除队列开头的Vertex,并将其键与toKey
进行比较; and (4) if it's a match return true, otherwise keep searching through the edge list of each adjacent Vertex. (4)如果匹配,则返回true,否则继续搜索每个相邻顶点的边列表。
Here is my code for the method so far: 到目前为止,这是我的方法代码:
/**
* Determines whether there is an outdirected path between two
* vertices.
* @param fromKey - search key of the originating vertex.
* @param toKey - search key of the destination vertex.
* @return true on success or false on failure.
*/
public boolean isReachable(E fromKey, E toKey)
{
ArrayList<Vertex> queue = new ArrayList<Vertex>();
E tmpKey = fromKey;
Edge tmpEdge;
Vertex tmp = first;
while (tmp != null)
{
if (tmp.data.equals(tmpKey))
{
tmpEdge = tmp.pEdge;
while (tmpEdge != null)
{
queue.add(tmpEdge.destination);
tmpEdge = tmpEdge.pNextEdge;
}
tmp = first;
tmpKey = queue.remove(0).data;
if (tmpKey.equals(toKey))
return true;
}
tmp = tmp.pNextVertex;
}
return false;
}
It works when a path between the two specified keys exists, but throws an index out of bounds error when there does not. 当两个指定键之间的路径存在时,它可以工作,但当两个键之间的路径不存在时,它将抛出索引错误。
This is an adjacency list I traced for the sample data I have: 这是我为示例数据跟踪的邻接表:
1 -> null
2 -> 1 -> 11 -> 12 -> null
3 -> 2 -> 4 -> null
4 -> null
5 -> 4 -> null
6 -> 5 -> 7 -> 13 -> 14 -> 15 -> null
7 -> 12 -> null
8 -> 7 -> 9 -> 10 -> 11 -> 12 -> null
9 -> 1 -> null
10 -> null
11 -> 1 -> 12 -> null
12 -> null
13 -> null
14 -> 2 -> 3 -> null
15 -> 3 -> 5 -> 14 -> null
When i call isReachable(5, 3)
, for instance, I get an index out of bounds exception. 例如,当我调用isReachable(5, 3)
,我得到了索引超出范围的异常。 But if i call the method on (15, 2), it returns true. 但是,如果我在(15,2)上调用该方法,它将返回true。
I'm not really sure where to go from here. 我不太确定该从哪里去。 A friend suggested trying a BFS approach to the problem, but I didn't really follow his explanation. 一位朋友建议尝试使用BFS方法解决该问题,但我并没有真正听从他的解释。 Am I on the right track? 我在正确的轨道上吗? Any help is appreciated. 任何帮助表示赞赏。
This is a basic graph search algorithm, google "breadth first search" as a starting point. 这是一种基本的图搜索算法,以Google“宽度优先搜索”为起点。 You need to keep track of nodes you've visited, which I don't see you having done yet. 您需要跟踪您访问过的节点,但我还没有完成。
Also, as I said in my comment, don't use an ArrayList
to maintain a queue, the remove
operation is slow, particularly removing the elements at the start of the array as you have to copy everything across by 1. Use a Queue
directly ( https://docs.oracle.com/javase/7/docs/api/java/util/Queue.html ) 另外,正如我在评论中所说,不要使用ArrayList
来维护队列, remove
操作很慢,尤其是删除数组开始处的元素时,您必须将所有内容复制1。直接使用Queue
( https://docs.oracle.com/javase/7/docs/api/java/util/Queue.html )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.