本文整理汇总了C#中Graph.ContainsEdge方法的典型用法代码示例。如果您正苦于以下问题:C# Graph.ContainsEdge方法的具体用法?C# Graph.ContainsEdge怎么用?C# Graph.ContainsEdge使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Graph
的用法示例。
在下文中一共展示了Graph.ContainsEdge方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DuplicateEdge
public void DuplicateEdge()
{
var graph = new Graph<int>(false);
var random = new Random();
var vertices = new List<Vertex<int>>();
for (var i = 0; i < 100; i++)
{
vertices.Add(graph.AddVertex(i));
}
var edges = new List<Edge<int>>();
for (var i = 0; i < 2000; i++)
{
var fromVertex = random.Next(100);
var toVertex = random.Next(100);
if (!graph.ContainsEdge(vertices[fromVertex], vertices[toVertex]))
{
edges.Add(
graph.AddEdge(vertices[fromVertex], vertices[toVertex])
);
}
}
foreach (var edge in edges)
{
Assert.IsTrue(graph.ContainsEdge(edge));
Assert.IsTrue(graph.ContainsEdge(edge.FromVertex, edge.ToVertex));
Assert.IsTrue(graph.ContainsEdge(edge.FromVertex.Data, edge.ToVertex.Data));
}
}
示例2: ProvideNodeCollection
/// <summary>
/// Generates collection of nodes of given amount and maximum neighbours count
/// </summary>
/// <param name="parent">Nodes parent graph</param>
/// <param name="nodeCount">Nodes amount to generate</param>
/// <param name="maxNeighbours">Maximum neighbours per node</param>
/// <returns></returns>
public ICollection<Node> ProvideNodeCollection(Graph parent, uint nodeCount, uint maxNeighbours = 2)
{
if (maxNeighbours >= int.MaxValue || nodeCount >= int.MaxValue)
throw new ArgumentOutOfRangeException(nameof(maxNeighbours));
if (nodeCount <= maxNeighbours)
throw new ArgumentOutOfRangeException(nameof(nodeCount), "You cannot have less or equal amount of nodes than max neighbours possible!");
List<Node> nodes = new List<Node>();
//Create random nodes
while (nodes.Count < nodeCount)
{
var node = parent.AddNode(_random.Next().ToString());
if (node != null) nodes.Add(node);
}
//Fill neighbours for each one of them
for (int i = 0; i < nodeCount; i++)
{
var howManyNeigbhours = _random.Next((int)maxNeighbours) - nodes[i].Neighbours.Count;
HashSet<int> alreadyPickedIndexes = new HashSet<int>();
while (nodes[i].Neighbours.Count < howManyNeigbhours)
{
var pick = _random.Next((int)nodeCount);
uint attempts = 0;
while (pick == i
|| alreadyPickedIndexes.Contains(pick)
|| nodes[pick].Neighbours.Count == maxNeighbours
|| parent.ContainsEdge(nodes[pick], nodes[i]))
{
pick = _random.Next((int) nodeCount);
attempts++;
//Give up if rolling takes too long
if (attempts == RollsBeforeGivingUp)
break;
}
//Give up if rolling takes too long
if (attempts == RollsBeforeGivingUp)
break;
parent.AddEdge(nodes[i].Key, nodes[pick].Key);
alreadyPickedIndexes.Add(pick);
}
//Make sure to not produce dead end
if (nodes[i].Neighbours.Count == 0)
{
var pick = _random.Next((int)nodeCount);
while (pick == i
|| alreadyPickedIndexes.Contains(pick)
|| nodes[pick].Neighbours.Count == maxNeighbours)
pick = _random.Next((int)nodeCount);
parent.AddEdge(nodes[i].Key, nodes[pick].Key);
}
}
return nodes;
}
示例3: AsDomainModel
public static Graph AsDomainModel(this GraphEntity entity)
{
List<EdgeMapping> edgesToAdd = new List<EdgeMapping>();
var graph = new Graph(entity.Id);
foreach (var node in entity.Nodes.OrderBy(e => e.Position))
{
graph.AddNode(node.Key, node.Id);
edgesToAdd.AddRange(node.Neighbours
.Where(ngh => ngh != null)
.Select(ngh => new EdgeMapping { FromKey = node.Key, ToKey = ngh.Key }));
}
foreach (var edge in edgesToAdd.Where(edge => !graph.ContainsEdge(edge.FromKey, edge.ToKey)))
{
graph.AddEdge(edge.FromKey, edge.ToKey);
}
return graph;
}
示例4: ContainsEdgeFromVerticeValuesExample
public void ContainsEdgeFromVerticeValuesExample()
{
// Initialize a new graph instance
var graph = new Graph<int>(true);
// Add three vertices to the graph
var vertex1 = graph.AddVertex(1);
var vertex2 = graph.AddVertex(2);
var vertex3 = graph.AddVertex(3);
// Add edges between the vertices
graph.AddEdge(vertex1, vertex2);
graph.AddEdge(vertex2, vertex3);
// There will be edges between vertex1 and vertex2, and vertex2 and vertex3
Assert.IsTrue(graph.ContainsEdge(1, 2));
Assert.IsTrue(graph.ContainsEdge(2, 3));
// But not between vertex1 and vertex3
Assert.IsFalse(graph.ContainsEdge(1, 3));
}
示例5: ContainsEdgeExample
public void ContainsEdgeExample()
{
// Initialize a new graph instance
var graph = new Graph<int>(true);
// Add three vertices to the graph
var vertex1 = graph.AddVertex(1);
var vertex2 = graph.AddVertex(2);
var vertex3 = graph.AddVertex(3);
// Add edges between the vertices
var edge1 = graph.AddEdge(vertex1, vertex2);
var edge2 = graph.AddEdge(vertex2, vertex3);
// Add another edge that's not part of the graph
var edge3 = new Edge<int>(vertex1, vertex3, true);
// edge1 and edge2 is contained in the graph
Assert.IsTrue(graph.ContainsEdge(edge1));
Assert.IsTrue(graph.ContainsEdge(edge2));
// edge3 is not
Assert.IsFalse(graph.ContainsEdge(edge3));
}
示例6: VerticesAndWeightUndirected
public void VerticesAndWeightUndirected()
{
var graph = new Graph<int>(false);
var vertex1 = new Vertex<int>(4);
var vertex2 = new Vertex<int>(5);
var vertex3 = new Vertex<int>(6);
graph.AddVertex(vertex1);
graph.AddVertex(vertex2);
graph.AddVertex(vertex3);
graph.AddEdge(vertex1, vertex2, 10);
graph.AddEdge(vertex1, vertex3, 12);
graph.AddEdge(vertex2, vertex3, 14);
Assert.AreEqual(graph.Edges.Count, 3);
Assert.IsTrue(vertex1.HasEmanatingEdgeTo(vertex2));
Assert.IsTrue(vertex1.HasEmanatingEdgeTo(vertex3));
Assert.IsTrue(vertex2.HasEmanatingEdgeTo(vertex3));
Assert.IsTrue(vertex2.HasEmanatingEdgeTo(vertex1));
Assert.IsTrue(vertex3.HasEmanatingEdgeTo(vertex1));
Assert.IsTrue(vertex3.HasEmanatingEdgeTo(vertex2));
Assert.IsTrue(graph.ContainsEdge(vertex1, vertex2));
Assert.IsTrue(graph.ContainsEdge(vertex1, vertex3));
Assert.IsTrue(graph.ContainsEdge(vertex2, vertex3));
Assert.IsTrue(graph.ContainsEdge(vertex2, vertex1));
Assert.IsTrue(graph.ContainsEdge(vertex3, vertex1));
Assert.IsTrue(graph.ContainsEdge(vertex3, vertex2));
}
示例7: Undirected
public void Undirected()
{
var graph = new Graph<int>(false);
var vertices = new Vertex<int>[20];
for (var i = 0; i < 20; i++)
{
vertices[i] = new Vertex<int>(i);
graph.AddVertex(vertices[i]);
}
var counter = 0;
for (var i = 0; i < 17; i += 2)
{
var edge = new Edge<int>(vertices[i], vertices[i + 2], false);
graph.AddEdge(edge);
counter++;
Assert.AreEqual(graph.Edges.Count, counter);
Assert.IsTrue(graph.ContainsEdge(edge));
Assert.IsTrue(vertices[i].HasEmanatingEdgeTo(vertices[i + 2]));
Assert.IsTrue(vertices[i].HasIncidentEdgeWith(vertices[i + 2]));
}
}