public class BreadthFirstDirectedPaths
{
private static readonly Int32 INFINITY = Int32.MaxValue;
private Boolean[] _marked; //keep track of whether vertex v is reachable from s
private int[] _edgeTo; //keep track of the last edge on path s to v
private int[] _distTo; //keep track of length of shortest s->v path
//Single source breadth first search
public BreadthFirstDirectedPaths(Digraph G, int s)
{
_marked = new Boolean[G.V()]; //create a boolean array for all vertices
_distTo = new int[G.V()]; //create a boolean array for all vertices
_edgeTo = new int[G.V()]; //create a boolean array for all vertices
//initialize each element of _distTo to Int32.MaxValue
for (int v = 0; v < G.V(); v++) _distTo[v] = INFINITY;
BFS(G, s);
}
/*
* A function to do breadth first search. In this case we use a for loop rather than a
* recursive function to find the shortest path from s to v.
* We start with the source vertex s. Rather than "fanning out" from each vertex recursively
* we travel along a single path (in turn) adding connected vertices to the q Queue until
* all vertices have been reached.
* We avoid "going backwards" or needlessly looking at all paths by keeping track of
* which vertices we've already visited using the _marked[] array.
* We keep track of how we're moving through the graph (from s to v) using _edgeTo[].
* We keep track of how far we've traveled using _distTo[w].
*/
private void BFS(Digraph G, int s)
{
/*
* Helps us keep track of what path to go down.
* Add each new connected vertex to the end of the
* queue. Once we travel down it
* remove it from the queue.
* */
Queue<Int32> q = new Queue<Int32>();
_marked[s] = true;
_distTo[s] = 0;
q.Enqueue(s);
while (q.Count > 0)
{
int v = q.Dequeue();
foreach (int w in G.Adj(v))
{
if (!_marked[w])
{
_edgeTo[w] = v;
_distTo[w] = _distTo[v] + 1;
_marked[w] = true;
q.Enqueue(w);
}
}
}
}
/*
* In the BFS method we've kept track of the shortest path from s to all connected vertices
* using the _distTo[] array.
* */
public int DistTo(int v)
{
return _distTo[v];
}
/*
* In the BFS method we've kept track of vertices connected to the source s
* using the _marked[] array.
* */
public Boolean HasPathTo(int v)
{
return _marked[v];
}
/*
* We can find the path from s to v working backwards using the _edgeTo array.
* For example, if we want to find the path from 3 to 0. We look at _edgeTo[0] which gives us
* a vertex, say 2. We then look at _edgeTo[2] and so on until _edgeTo[x] equals 3 (our
* source vertex).
* */
public IEnumerable<Int32> PathTo(int v)
{
if (!HasPathTo(v)) return null;
Stack path = new Stack();
int x;
for (x = v; _distTo[x] != 0; x = _edgeTo[x])
path.Push(x);
path.Push(x);
return path; **Problem is here**
}
}我发现这段来自这里 Digraph g = new Digraph(dsayisi);图的代码已经完成。当我试图用图和源顶点调用bfs类时。获得输出。当我希望源到目的地的特定目标路径
BreadthFirstDirectedPaths bfs = new BreadthFirstDirectedPaths(g,source-1);
bfs.HasPathTo(destination - 1);
var path = bfs.PathTo(destination - 1);
/*output of path它会产生错误
public IEnumerable<Int32> PathTo(int v)
{
if (!HasPathTo(v)) return null;
Stack path = new Stack();
int x;
for (x = v; _distTo[x] != 0; x = _edgeTo[x])
path.Push(x);
path.Push(x);
**return path;**
}错误1不能隐式地将'System.Collections.Stack‘类型转换为’System.Collection tions.Generic.IEnDigable‘。存在显式转换(是否缺少强制转换?)
发布于 2015-12-12 17:09:48
.NET有两个名为Stack的类--一个泛型类(即Stack<T>)和一个非泛型类(即没有<T>的“普通”Stack )。您的代码在必须返回Stack<Int32>的上下文中使用非泛型堆栈,这会造成问题。
使用
var path = new Stack<Int32>();将修复编译问题,因为Stack<T>实现了IEnumerable<T>。
https://stackoverflow.com/questions/34242556
复制相似问题