本文整理汇总了C#中Graph.AddLink方法的典型用法代码示例。如果您正苦于以下问题:C# Graph.AddLink方法的具体用法?C# Graph.AddLink怎么用?C# Graph.AddLink使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Graph
的用法示例。
在下文中一共展示了Graph.AddLink方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ShouldAnnotateNodesAndLinksReachableFromGivenPoint
public void ShouldAnnotateNodesAndLinksReachableFromGivenPoint()
{
// arrange
var graph = new Graph();
var start = graph.AddNode(new SampleNode("start"));
var node1 = graph.AddNode(new SampleNode("node1"));
var node2 = graph.AddNode(new SampleNode("node2"));
var node3 = graph.AddNode(new SampleNode("node3"));
var unreachable = graph.AddNode(new SampleNode("unreachable"));
var link1 = graph.AddLink(start, node1, new SampleLink());
var link2 = graph.AddLink(node1, node2, new SampleLink());
var link3 = graph.AddLink(node2, node3, new SampleLink());
// act
var walkAndAnnotate = new WalkAndAnnotate<Node, Link>
{
NodeAnnotation = _ => "node annotation",
LinkAnnotation = _ => "link annotation"
};
walkAndAnnotate.Walk(graph, start);
// assert
Assert.That(start.HasAnnotation<string>(), Is.True);
Assert.That(node1.HasAnnotation<string>(), Is.True);
Assert.That(node2.HasAnnotation<string>(), Is.True);
Assert.That(node3.HasAnnotation<string>(), Is.True);
Assert.That(unreachable.HasAnnotation<string>(), Is.False);
Assert.That(link1.HasAnnotation<string>(), Is.True);
Assert.That(link2.HasAnnotation<string>(), Is.True);
Assert.That(link3.HasAnnotation<string>(), Is.True);
}
示例2: ShouldReplaceLinkInAll
public void ShouldReplaceLinkInAll()
{
// arrange
var source = new SampleNode("source");
var target = new SampleNode("target");
var graph = new Graph();
graph.AddNode(source);
graph.AddNode(target);
var firstLink = new SampleLink();
graph.AddLink(source, target, firstLink);
var secondLink = new SecondLink();
// act
graph.ReplaceLink(firstLink, secondLink);
// Assert
Assert.That(graph.Links, Has.Count.EqualTo(1));
Assert.That(graph.Links, Has.No.Member(firstLink));
Assert.That(graph.Links, Has.Member(secondLink));
Assert.That(secondLink.Source, Is.SameAs(source));
Assert.That(secondLink.Target, Is.SameAs(target));
Assert.That(source.OutboundLinks, Has.No.Member(firstLink));
Assert.That(target.InboundLinks, Has.No.Member(firstLink));
Assert.That(source.OutboundLinks, Has.Member(secondLink));
Assert.That(target.InboundLinks, Has.Member(secondLink));
}
示例3: TestFindingPaths
public void TestFindingPaths(string[] z)
{
var graphPaths = new List<string>(z);
// arrange
var graph = new Graph();
var start = graph.AddNode(new SampleNode("start"));
var end = graph.AddNode(new SampleNode("end"));
var nodes = new Dictionary<string, Node>
{
{"start", start},
{"end", end},
};
foreach (var graphPath in graphPaths)
{
var hops = graphPath.Split(new[] { "->" }, StringSplitOptions.None).ToList();
for (int i = 1; i < hops.Count; i++)
{
var hop = hops[i];
if (!nodes.ContainsKey(hop))
{
nodes[hop] = graph.AddNode(new SampleNode(hop));
}
graph.AddLink(nodes[hops[i - 1]], nodes[hop], new SampleLink());
}
}
// act
var foundPaths = FindAllPaths.BetweenNodes(start, end).ToList();
foreach (var foundPath in foundPaths)
{
Console.WriteLine("Found path: {0}", string.Join("->", foundPath.Select(x => x.Id)));
}
// assert
foreach (var foundPath in foundPaths)
{
var spec = string.Join("->", foundPath.Select(x => x.Id));
if (!graphPaths.Contains(spec))
{
Assert.Fail("Found path " + spec + " is unknown");
}
else
{
graphPaths.Remove(spec);
}
}
foreach (var notFoundPath in graphPaths)
{
Assert.Fail("Path " + notFoundPath + " was not found");
}
}
示例4: AcyclicGraph
public void AcyclicGraph()
{
// arrange
var graph = new Graph();
var a = graph.AddNode(new SampleNode("a"));
var b = graph.AddNode(new SampleNode("b"));
var c = graph.AddNode(new SampleNode("c"));
var d = graph.AddNode(new SampleNode("d"));
var e = graph.AddNode(new SampleNode("e"));
var f = graph.AddNode(new SampleNode("f"));
var g = graph.AddNode(new SampleNode("g"));
var h = graph.AddNode(new SampleNode("h"));
graph.AddLink(a, b, new SampleLink());
graph.AddLink(a, c, new SampleLink());
graph.AddLink(b, d, new SampleLink());
graph.AddLink(b, e, new SampleLink());
graph.AddLink(c, f, new SampleLink());
graph.AddLink(c, g, new SampleLink());
graph.AddLink(e, h, new SampleLink());
// act
var path = new List<Node>();
var collectVisitedNodes = new LambdaBreadthFirstSearch<Node, Link>
{
HandlingNode = (node, links) => path.Add(node)
};
collectVisitedNodes.Walk(a);
// assert
Assert.That(path, Is.EqualTo(new[] { a, b, c, d, e, f, g, h }));
}
示例5: NodeViewShouldNotIncludeLinksNotMatchingPredicate
public void NodeViewShouldNotIncludeLinksNotMatchingPredicate()
{
// arrange
var graph = new Graph();
var nodeA1 = graph.AddNode(new SampleNode("A1"));
var nodeA2 = graph.AddNode(new SampleNode("A2"));
var link1 = graph.AddLink(nodeA1, nodeA2, new SampleLink("1"));
var link2 = graph.AddLink(nodeA1, nodeA2, new SampleLink("2"));
// act
var view = graph.PrepareView(x => x.Id.StartsWith("A"), x => ((SampleLink)x).Name != "1");
// assert
var nodeA1View = view.Nodes.Single(x => x.Node == nodeA1);
Assert.That(nodeA1View.OutboundLinks, Has.No.Member(link1));
}
示例6: ShouldFilterOutLinks
public void ShouldFilterOutLinks()
{
// arrange
var graph = new Graph();
var nodeA1 = graph.AddNode(new SampleNode("A1"));
var nodeA2 = graph.AddNode(new SampleNode("A2"));
var link1 = graph.AddLink(nodeA1, nodeA2, new SampleLink("1"));
var link2 = graph.AddLink(nodeA1, nodeA2, new SampleLink("2"));
// act
var view = graph.PrepareView(x => true, x => (x is SampleLink) && ((SampleLink)x).Name == "1");
// assert
Assert.That(view.Links, Has
.Member(link1)
.And.Not.Member(link2),
"Nodes has not been filtered");
}
示例7: ShouldSortGraphTopologically
public void ShouldSortGraphTopologically()
{
// arrange
var graph = new Graph<SampleNode, SampleLink>();
var node1 = graph.AddNode(new SampleNode("1"));
var node2 = graph.AddNode(new SampleNode("2"));
var node3 = graph.AddNode(new SampleNode("3"));
var node4 = graph.AddNode(new SampleNode("4"));
var node5 = graph.AddNode(new SampleNode("5"));
graph.AddLink(node1, node2, new SampleLink());
graph.AddLink(node1, node5, new SampleLink());
graph.AddLink(node3, node5, new SampleLink());
graph.AddLink(node5, node4, new SampleLink());
// act
var sorted = TopologySort.SortGraph(graph);
// assert
Assert.That(sorted, Is.EqualTo(new[] {node3, node1, node5, node4, node2}));
}
示例8: ShouldWalkThroughAllNodes
public void ShouldWalkThroughAllNodes()
{
// arrange
var graph = new Graph();
var a = graph.AddNode(new SampleNode("A"));
var b = graph.AddNode(new SampleNode("B"));
var c = graph.AddNode(new SampleNode("C"));
var d = graph.AddNode(new SampleNode("D"));
var e = graph.AddNode(new SampleNode("E"));
var f = graph.AddNode(new SampleNode("F"));
var g = graph.AddNode(new SampleNode("G"));
graph.AddLink(a, b, new SampleLink());
graph.AddLink(a, c, new SampleLink());
graph.AddLink(a, d, new SampleLink());
graph.AddLink(c, e, new SampleLink());
graph.AddLink(e, f, new SampleLink());
graph.AddLink(e, g, new SampleLink());
var enterSequence = new List<SampleNode>();
var leaveSequence = new List<SampleNode>();
var dfs = new LambdaDepthFirstSearch
{
EnteringNode = (node, links) => enterSequence.Add((SampleNode)node),
LeavingNode = (node, links) => leaveSequence.Add((SampleNode)node)
};
// act
dfs.Walk(a);
// assert
Assert.That(enterSequence, Is.EqualTo(new[] {a, b, c, e, f, g, d}), "Enter node sequence is correct");
Assert.That(leaveSequence, Is.EqualTo(new[] {b, f, g, e, c, d, a}), "Leave node sequence is correct");
}
示例9: SplitAndJoinOnHop
public void SplitAndJoinOnHop()
{
// arrange
var graph = new Graph();
var start = graph.AddNode(new SampleNode("start"));
var end = graph.AddNode(new SampleNode("end"));
var hop1 = graph.AddNode(new SampleNode("hop1"));
var split = graph.AddNode(new SampleNode("split"));
var hop3 = graph.AddNode(new SampleNode("hop3"));
var hop4 = graph.AddNode(new SampleNode("hop4"));
var join = graph.AddNode(new SampleNode("join"));
var hop2 = graph.AddNode(new SampleNode("hop2"));
var l1 = graph.AddLink(start, hop1, new SampleLink());
var l2 = graph.AddLink(hop1, split, new SampleLink());
var l5 = graph.AddLink(split, join, new SampleLink());
var l6 = graph.AddLink(join, end, new SampleLink());
var l3 = graph.AddLink(split, hop3, new SampleLink());
var l4 = graph.AddLink(hop3, join, new SampleLink());
// act
var paths = FindAllPaths.BetweenNodes(start, end);
// assert
Assert.That(paths, Has.Count.EqualTo(2));
Assert.That(paths, Has.Exactly(1).EqualTo(new[] { start, hop1, split, hop3, join, end }));
Assert.That(paths, Has.Exactly(1).EqualTo(new[] { start, hop1, split, join, end }));
}
示例10: ShouldRemoveNodeInAll
public void ShouldRemoveNodeInAll()
{
// arrange
var graph = new Graph();
var node1 = new SampleNode("node1");
var node2 = new SampleNode("node2");
graph.AddNode(node1);
graph.AddNode(node2);
var inboundLink = new SampleLink();
graph.AddLink(node2, node1, inboundLink);
var outboundLink = new SampleLink();
graph.AddLink(node1, node2, outboundLink);
// act
graph.RemoveNode(node1);
// assert
Assert.That(graph.Nodes, Has.No.Member(node1));
Assert.That(node2.InboundLinks, Has.No.Member(outboundLink));
Assert.That(node2.OutboundLinks, Has.No.Member(inboundLink));
}
示例11: ShouldRemoveLinkFromAll
public void ShouldRemoveLinkFromAll()
{
// arrange
var source = new SampleNode("source");
var target = new SampleNode("target");
var graph = new Graph();
graph.AddNode(source);
graph.AddNode(target);
var link = new SampleLink();
graph.AddLink(source, target, link);
// act
graph.RemoveLink(link);
// Assert
Assert.That(graph.Links, Has.Count.EqualTo(0));
Assert.That(source.OutboundLinks, Has.No.Member(link));
Assert.That(target.InboundLinks, Has.No.Member(link));
}
示例12: ShouldThrowWhenGraphHaveCycle
public void ShouldThrowWhenGraphHaveCycle()
{
// arrange
var graph = new Graph<SampleNode, SampleLink>();
var node1 = graph.AddNode(new SampleNode("1"));
var node2 = graph.AddNode(new SampleNode("2"));
var node3 = graph.AddNode(new SampleNode("3"));
var node4 = graph.AddNode(new SampleNode("4"));
graph.AddLink(node4, node1, new SampleLink());
graph.AddLink(node4, node2, new SampleLink());
graph.AddLink(node2, node1, new SampleLink());
graph.AddLink(node1, node2, new SampleLink());
graph.AddLink(node1, node3, new SampleLink());
graph.AddLink(node2, node3, new SampleLink());
// act
Assert.That(() => TopologySort.SortGraph(graph), Throws.InstanceOf<CannotSortGraphException>());
}
示例13: ShouldTrackAddedLinks
public void ShouldTrackAddedLinks()
{
// arrange
var source = new SampleNode("source");
var target = new SampleNode("target");
var graph = new Graph();
graph.AddNode(source);
graph.AddNode(target);
var link = new SampleLink();
// act
graph.AddLink(source, target, link);
// Assert
Assert.That(graph.Links, Has.Count.EqualTo(1));
Assert.That(link.Source, Is.SameAs(source));
Assert.That(link.Target, Is.SameAs(target));
Assert.That(source.OutboundLinks, Has.Member(link));
Assert.That(target.InboundLinks, Has.Member(link));
}
示例14: ShouldNotIncludeLinksWithEndNotMatchingPredicate
public void ShouldNotIncludeLinksWithEndNotMatchingPredicate()
{
// arrange
var graph = new Graph();
var nodeA1 = graph.AddNode(new SampleNode("A1"));
var nodeA2 = graph.AddNode(new SampleNode("A2"));
var nodeB1 = graph.AddNode(new SampleNode("B1"));
var nodeB2 = graph.AddNode(new SampleNode("B2"));
var link1 = graph.AddLink(nodeA1, nodeB2, new SampleLink("1"));
var link2 = graph.AddLink(nodeB1, nodeA2, new SampleLink("2"));
var link3 = graph.AddLink(nodeA1, nodeA2, new SampleLink("3"));
// act
var view = graph.PrepareView(x => x.Id.StartsWith("A"), x => true);
// assert
Assert.That(view.Links, Has
.No.Member(link1)
.And.No.Member(link2)
.And.Member(link3));
}
示例15: Main
static void Main(string[] args)
{
string[] inputs;
inputs = Console.ReadLine().Split(' ');
int nodeNumber = int.Parse(inputs[0]); // the total number of nodes in the level, including the gateways
int linksNumber = int.Parse(inputs[1]); // the number of links
int gatewayNumbers = int.Parse(inputs[2]); // the number of exit gateways
var graph = new Graph(nodeNumber);
for (int i = 0; i < linksNumber; i++)
{
inputs = Console.ReadLine().Split(' ');
int nodeNumber1 = int.Parse(inputs[0]); // N1 and N2 defines a link between these nodes
int nodeNumber2 = int.Parse(inputs[1]);
graph.AddLink(nodeNumber1, nodeNumber2);
Console.Error.Write(nodeNumber1 + " " + nodeNumber2 + " | ");
}
for (int i = 0; i < gatewayNumbers; i++)
{
int eI = int.Parse(Console.ReadLine()); // the index of a gateway node
graph.gatewayNumbers.Add(eI);
Console.Error.Write(eI + " | ");
}
Console.Error.WriteLine("node numbers :{0} | Gateway number :{1}", graph.nodes.Count, graph.gatewayNumbers.Count);
var path = new int[] {0, 1};
// game loop
while (true)
{
int sI = int.Parse(Console.ReadLine()); // The index of the node on which the Skynet agent is positioned this turn
var minPathLengthArray = graph.GetNodePathLengthArray(sI);
var nearestGatewayNumber = -1;
foreach (var gatewayNumber in graph.gatewayNumbers)
{
var exitNodeNumber = graph.GetNodeIndex(gatewayNumber);
Console.Error.WriteLine("miPathLEngthArray : {0}", minPathLengthArray[exitNodeNumber]);
if (minPathLengthArray[exitNodeNumber] == -1)
continue;
if (nearestGatewayNumber == -1)
{
nearestGatewayNumber = gatewayNumber;
}
else
{
var minExitNodeIndex = graph.GetNodeIndex(nearestGatewayNumber);
if (minPathLengthArray[exitNodeNumber] < minPathLengthArray[minExitNodeIndex])
nearestGatewayNumber = gatewayNumber;
}
}
Console.Error.WriteLine("agent Node: {0} | nearestGateway number: {1} | minPathLength : {2}", sI, nearestGatewayNumber, minPathLengthArray[graph.GetNodeIndex(nearestGatewayNumber)] + 1);
path = graph.GetShortestPath(sI, nearestGatewayNumber, minPathLengthArray[graph.GetNodeIndex(nearestGatewayNumber)] + 1);
Console.Error.WriteLine("Number of nodes in path:{0} | agentIndex: {1} | path[0]: {2}", path.Count(), sI, path[0]);
graph.RemoveLink(path[0], path[1]);
Console.WriteLine("{0} {1}", path[0], path[1]); // Example: 0 1 are the indices of the nodes you wish to sever the link between
}
}