[英]Trouble with Graph Data Structure C#
I've just started learning how to program and have been following along with this tutorial on graphs but I've run into some issues using the given我刚刚开始学习如何编程,并一直在学习有关图形的本教程,但我在使用给定的方法时遇到了一些问题
AddDirectedEdge(GraphNode<T> from, GraphNode<T> to, int cost)
It fails to build using the default from the tutorial, for example例如,使用教程中的默认设置无法构建
web.AddDirectedEdge("People.aspx", "Privacy.htm"); // People -> Privacy
This gives the error "No overload for method 'AddDirectedEdge' takes 2 arguments" .这给出了错误“方法 'AddDirectedEdge' 没有重载需要 2 个参数” 。 Adding an integer as a third argument does not help either, it just gives another error "Argument#: cannot convert from 'string' to 'GraphTest.GraphNode< string>'"
添加一个整数作为第三个参数也无济于事,它只会给出另一个错误“Argument#: cannot convert from 'string' to 'GraphTest.GraphNode< string>'”
I'm not sure how to fix this, any help/insight regarding it would be appreciated.我不知道如何解决这个问题,任何有关它的帮助/见解将不胜感激。 For ease of viewing the graph and graph node classes are shown below (these are all available through the above link as well):
为了便于查看图形和图形节点类如下所示(这些都可以通过上面的链接获得):
public class GraphNode<T> : Node<T>
{
private List<int> costs;
public GraphNode() : base() { }
public GraphNode(T value) : base(value) { }
public GraphNode(T value, NodeList<T> neighbors) : base(value, neighbors) { }
new public NodeList<T> Neighbors
{
get
{
if (base.Neighbors == null)
base.Neighbors = new NodeList<T>();
return base.Neighbors;
}
}
public List<int> Costs
{
get
{
if (costs == null)
costs = new List<int>();
return costs;
}
}
}
. .
public class Graph<T> : IEnumerable<T>
{
private NodeList<T> nodeSet;
public Graph() : this(null) { }
public Graph(NodeList<T> nodeSet)
{
if (nodeSet == null)
this.nodeSet = new NodeList<T>();
else
this.nodeSet = nodeSet;
}
public void AddNode(GraphNode<T> node)
{
// adds a node to the graph
nodeSet.Add(node);
}
public void AddNode(T value)
{
// adds a node to the graph
nodeSet.Add(new GraphNode<T>(value));
}
public void AddDirectedEdge(GraphNode<T> from, GraphNode<T> to, int cost)
{
from.Neighbors.Add(to);
from.Costs.Add(cost);
}
public void AddUndirectedEdge(GraphNode<T> from, GraphNode<T> to, int cost)
{
from.Neighbors.Add(to);
from.Costs.Add(cost);
to.Neighbors.Add(from);
to.Costs.Add(cost);
}
public bool Contains(T value)
{
return nodeSet.FindByValue(value) != null;
}
public bool Remove(T value)
{
// first remove the node from the nodeset
GraphNode<T> nodeToRemove = (GraphNode<T>)nodeSet.FindByValue(value);
if (nodeToRemove == null)
// node wasn't found
return false;
// otherwise, the node was found
nodeSet.Remove(nodeToRemove);
// enumerate through each node in the nodeSet, removing edges to this node
foreach (GraphNode<T> gnode in nodeSet)
{
int index = gnode.Neighbors.IndexOf(nodeToRemove);
if (index != -1)
{
// remove the reference to the node and associated cost
gnode.Neighbors.RemoveAt(index);
gnode.Costs.RemoveAt(index);
}
}
return true;
}
public NodeList<T> Nodes
{
get
{
return nodeSet;
}
}
public int Count
{
get { return nodeSet.Count; }
}
public IEnumerator<T> GetEnumerator()
{
throw new NotImplementedException();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
throw new NotImplementedException();
}
}
Well, obviously it is complaining about type mismatch:好吧,显然它在抱怨类型不匹配:
you are supplying string
type instead of GraphNode<T>
.您提供的是
string
类型而不是GraphNode<T>
。
Instead you should call it as:相反,您应该将其称为:
web.AddDirectedEdge(new GraphNode("People.aspx"), new GraphNode("Privacy.htm"), 1);
where 1
is a cost, which is required, or其中
1
是成本,这是必需的,或
var ppl = new GraphNode("People.aspx");
var prv = new GraphNode("Privacy.htm");
web.AddDirectedEdge(ppl, prv, 1);
if you like to reuse nodes further in code.如果您想在代码中进一步重用节点。
As a side note, it seems you are making some trivial mistakes.作为旁注,您似乎犯了一些小错误。 Before making it further I strongly recommend you to take at least basic course of C#.
在进一步学习之前,我强烈建议您至少学习 C# 的基础课程。 There is plenty of then available for free in the Web, this for example .
网络上有很多免费提供,例如。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.