本文整理汇总了C#中Graph.AddVertex方法的典型用法代码示例。如果您正苦于以下问题:C# Graph.AddVertex方法的具体用法?C# Graph.AddVertex怎么用?C# Graph.AddVertex使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Graph
的用法示例。
在下文中一共展示了Graph.AddVertex方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MultipleCyclesDirected
public void MultipleCyclesDirected()
{
var graph = new Graph<int>(true);
var vertex1 = graph.AddVertex(1);
var vertex2 = graph.AddVertex(2);
var vertex3 = graph.AddVertex(3);
var vertex4 = graph.AddVertex(4);
var vertex5 = graph.AddVertex(5);
graph.AddEdge(vertex1, vertex2);
graph.AddEdge(vertex2, vertex3);
graph.AddEdge(vertex3, vertex1);
graph.AddEdge(vertex4, vertex5);
graph.AddEdge(vertex5, vertex4);
var cycles = graph.FindCycles(true);
Assert.AreEqual(2, cycles.Count, "There are two cycles");
IList<Vertex<int>> cycle1 = cycles[0];
IList<Vertex<int>> cycle2 = cycles[1];
Assert.IsTrue(((cycle1.Count == 3) && (cycle2.Count == 2)) || ((cycle1.Count == 2) && (cycle2.Count == 3)), "Wrong number of items in the cycles");
var index = (cycle1.Count == 3) ? 0 : 1;
Assert.IsTrue(cycles[index].Any(v => v.Data == 1), "Vertex 1 missing from the cycle");
Assert.IsTrue(cycles[index].Any(v => v.Data == 2), "Vertex 2 missing from the cycle");
Assert.IsTrue(cycles[index].Any(v => v.Data == 3), "Vertex 3 missing from the cycle");
index = (cycle1.Count == 2) ? 0 : 1;
Assert.IsTrue(cycles[index].Any(v => v.Data == 4), "Vertex 1 missing from the cycle");
Assert.IsTrue(cycles[index].Any(v => v.Data == 5), "Vertex 2 missing from the cycle");
}
示例2: Simple
public void Simple()
{
var graph = new Graph<int>(true);
var vertex1 = graph.AddVertex(1);
var vertex2 = graph.AddVertex(2);
var vertex3 = graph.AddVertex(3);
var vertex4 = graph.AddVertex(4);
graph.AddEdge(vertex1, vertex2);
graph.AddEdge(vertex2, vertex3);
graph.AddEdge(vertex2, vertex4);
graph.AddEdge(vertex3, vertex4);
Assert.IsFalse(graph.IsCyclic());
// Add a cycle
graph.AddEdge(vertex4, vertex1);
Assert.IsTrue(graph.IsCyclic());
// Remove the cycle again
graph.RemoveEdge(vertex4, vertex1);
Assert.IsFalse(graph.IsCyclic());
// Add a cycle again
graph.AddEdge(vertex2, vertex1);
Assert.IsTrue(graph.IsCyclic());
// Remove the cycle
graph.RemoveEdge(vertex2, vertex1);
Assert.IsFalse(graph.IsCyclic());
}
示例3: Simple
public void Simple()
{
var vertex1 = new Vertex<int>(1);
var vertex2 = new Vertex<int>(2);
var vertex3 = new Vertex<int>(3);
var vertex4 = new Vertex<int>(4);
var graph = new Graph<int>(true);
graph.AddVertex(vertex1);
graph.AddVertex(vertex2);
graph.AddVertex(vertex3);
graph.AddVertex(vertex4);
graph.AddEdge(vertex1, vertex2);
graph.AddEdge(vertex2, vertex3);
graph.AddEdge(vertex3, vertex1);
graph.AddEdge(vertex4, vertex2);
var vertexList = new List<Vertex<int>>();
foreach (var vertex in graph.Vertices)
{
vertexList.Add(vertex);
}
Assert.IsTrue(vertexList.Contains(vertex1));
Assert.IsTrue(vertexList.Contains(vertex2));
Assert.IsTrue(vertexList.Contains(vertex3));
Assert.IsTrue(vertexList.Contains(vertex4));
}
示例4: Undirected
public void Undirected()
{
var graph = new Graph<int>(false);
var vertex1 = new Vertex<int>(1);
var vertex2 = new Vertex<int>(2);
var vertex3 = new Vertex<int>(3);
graph.AddVertex(vertex1);
graph.AddVertex(vertex2);
graph.AddVertex(vertex3);
graph.AddEdge(vertex1, vertex2);
Assert.AreEqual(vertex1.IncomingEdgeCount, 0);
Assert.AreEqual(vertex2.IncomingEdgeCount, 0);
graph.AddEdge(vertex3, vertex2);
Assert.AreEqual(vertex3.IncomingEdgeCount, 0);
Assert.AreEqual(vertex2.IncomingEdgeCount, 0);
graph.AddEdge(vertex1, vertex3);
Assert.AreEqual(vertex1.IncomingEdgeCount, 0);
Assert.AreEqual(vertex3.IncomingEdgeCount, 0);
}
示例5: Main
public static void Main(string[] args)
{
Graph edgeColorTestGraph = new Graph();
Vertex v1 = new Vertex();
v1.name = "v1";
Vertex v2 = new Vertex();
v2.name = "v2";
Vertex v3 = new Vertex();
v3.name = "v3";
Vertex v4 = new Vertex();
v4.name = "v4";
Vertex v5 = new Vertex();
v5.name = "v5";
edgeColorTestGraph.AddVertex(v1);
edgeColorTestGraph.AddVertex(v2);
edgeColorTestGraph.AddVertex(v3);
edgeColorTestGraph.AddVertex(v4);
edgeColorTestGraph.AddVertex(v5);
edgeColorTestGraph.AddEdge(v1, v2);
edgeColorTestGraph.AddEdge(v1, v3);
edgeColorTestGraph.AddEdge(v1, v4);
edgeColorTestGraph.AddEdge(v2, v5);
edgeColorTestGraph.AddEdge(v3, v4);
edgeColorTestGraph.AddEdge(v5, v4);
edgeColorTestGraph.display();
edgeColorTestGraph.edgeColoring();
edgeColorTestGraph.display();
}
示例6: VertexAndEdge
public void VertexAndEdge()
{
var graph = new Graph<int>(true);
var vertex1 = new Vertex<int>(3);
var vertex2 = new Vertex<int>(4);
var vertex3 = new Vertex<int>(5);
graph.AddVertex(vertex1);
graph.AddVertex(vertex2);
graph.AddVertex(vertex3);
graph.AddEdge(vertex2, vertex1);
vertex2 = SerializeUtil.BinarySerializeDeserialize(vertex1);
TestIsCopy(vertex1, vertex2);
Assert.AreEqual(vertex2.IncidentEdges.Count, 1);
Assert.AreEqual(vertex1.IncidentEdges.Count, 1);
Assert.AreEqual(vertex2.EmanatingEdges.Count, 0);
Assert.AreEqual(vertex1.EmanatingEdges.Count, 0);
TestIsCopy(vertex2.IncidentEdges[0], vertex1.IncidentEdges[0]);
}
示例7: AddEdgeFromVerticesExample
public void AddEdgeFromVerticesExample()
{
// Initialize a new graph instance
var graph = new Graph<int>(true);
// Add two vertices to the graph
var vertex1 = graph.AddVertex(1);
var vertex2 = graph.AddVertex(2);
// Add the edge to the graph
var edge = graph.AddEdge(vertex1, vertex2);
// The from vertex will be vertex1
Assert.AreEqual(edge.FromVertex, vertex1);
// The to vertex will be vertex2
Assert.AreEqual(edge.ToVertex, vertex2);
// Since the graph is directed, the edge will
// be directed as well
Assert.AreEqual(edge.IsDirected, true);
// The edge will be accessible though the edges collection
Assert.AreEqual(graph.Edges.Count, 1);
}
示例8: Directed
public void Directed()
{
var vertex1 = new Vertex<int>(3);
var vertex2 = new Vertex<int>(5);
var vertex3 = new Vertex<int>(8);
var graph = new Graph<int>(true);
graph.AddVertex(vertex1);
graph.AddVertex(vertex2);
graph.AddVertex(vertex3);
graph.AddEdge(vertex1, vertex2);
graph.AddEdge(vertex3, vertex1);
var edgeList = vertex3.EmanatingEdges;
Assert.AreEqual(edgeList.Count, 1);
AssertContainsEdges(edgeList, true,
vertex3.GetEmanatingEdgeTo(vertex1)
);
edgeList = vertex1.EmanatingEdges;
Assert.AreEqual(edgeList.Count, 1);
AssertContainsEdges(edgeList, true,
vertex1.GetEmanatingEdgeTo(vertex2)
);
}
示例9: Undirected
public void Undirected()
{
var graph = new Graph<int>(false);
var vertex1 = new Vertex<int>(1);
var vertex2 = new Vertex<int>(2);
var vertex3 = new Vertex<int>(3);
var vertex4 = new Vertex<int>(4);
graph.AddVertex(vertex1);
graph.AddVertex(vertex2);
graph.AddVertex(vertex3);
graph.AddVertex(vertex4);
graph.AddEdge(vertex1, vertex2);
graph.AddEdge(vertex3, vertex2);
graph.AddEdge(vertex1, vertex3);
Assert.IsFalse(graph.IsStronglyConnected());
graph.AddEdge(vertex2, vertex4);
Assert.IsTrue(graph.IsStronglyConnected());
graph.RemoveEdge(vertex2, vertex3);
Assert.IsTrue(graph.IsStronglyConnected());
graph.RemoveEdge(vertex1, vertex3);
Assert.IsFalse(graph.IsStronglyConnected());
}
示例10: TestAddRemove1
public void TestAddRemove1()
{
using (var graph = new Graph<Edge>(new MemoryMappedStream(new MemoryStream()), 1000, Edge.MapFromDelegate, Edge.MapToDelegate, Edge.SizeUints))
{
uint tagsId = 10;
var vertex1 = graph.AddVertex(51, 1);
var vertex2 = graph.AddVertex(51, 2);
graph.AddEdge(vertex1, vertex2, new Edge()
{
Forward = true,
Tags = tagsId
}, null);
// test forward edge.
var edges = graph.GetEdges(vertex1).ToKeyValuePairs();
Assert.AreEqual(1, edges.Length);
Assert.AreEqual(tagsId, edges[0].Value.Tags);
Assert.AreEqual(vertex2, edges[0].Key);
Assert.AreEqual(true, edges[0].Value.Forward);
// remove edge again.
graph.RemoveEdge(vertex1, vertex2);
// check if the edge is gone.
edges = graph.GetEdges(vertex1).ToKeyValuePairs();
Assert.AreEqual(0, edges.Length);
}
}
示例11: DirectedValue
public void DirectedValue()
{
var graph = new Graph<int>(true);
var vertex1 = new Vertex<int>(1);
var vertex2 = new Vertex<int>(2);
var vertex3 = new Vertex<int>(3);
graph.AddVertex(vertex1);
graph.AddVertex(vertex2);
graph.AddVertex(vertex3);
graph.AddEdge(vertex1, vertex2);
graph.AddEdge(vertex3, vertex2);
graph.AddEdge(vertex1, vertex3);
Assert.AreEqual(graph.Edges.Count, 3);
Assert.AreEqual(graph.Vertices.Count, 3);
Assert.IsTrue(graph.RemoveVertex(1));
Assert.AreEqual(graph.Edges.Count, 1);
Assert.AreEqual(graph.Vertices.Count, 2);
Assert.IsFalse(graph.RemoveVertex(4));
Assert.AreEqual(graph.Edges.Count, 1);
Assert.AreEqual(graph.Vertices.Count, 2);
}
示例12: DegreeExample
public void DegreeExample()
{
// Create two new vertices
var vertex1 = new Vertex<int>(2);
var vertex2 = new Vertex<int>(4);
// The degree of the vertex is the number
// of edges emanating from the vertex. Thus,
// a newly created vertex will have a degree
// of 0.
Assert.AreEqual(vertex1.Degree, 0);
Assert.AreEqual(vertex2.Degree, 0);
// Create a graph, and add the vertices to
// the graph
var graph = new Graph<int>(true);
graph.AddVertex(vertex1);
graph.AddVertex(vertex2);
// Add an edge from vertex1 to vertex vertex2
graph.AddEdge(vertex1, vertex2);
// Since the edge is emanating from vertex1
// (and the graph is directed), vertex1's
// degree will be 1 and vertex2's degree 0.
Assert.AreEqual(vertex1.Degree, 1);
Assert.AreEqual(vertex2.Degree, 0);
}
示例13: TestGraphAddEdge
public void TestGraphAddEdge()
{
var graph = new Graph<Edge>();
var vertex1 = graph.AddVertex(51, 1);
var vertex2 = graph.AddVertex(51, 2);
graph.AddEdge(vertex1, vertex2, new Edge()
{
Forward = true,
Tags = 0
}, null);
var edges = graph.GetEdges(vertex1).ToKeyValuePairs();
Assert.AreEqual(1, edges.Length);
Assert.AreEqual(0, edges[0].Value.Tags);
Assert.AreEqual(vertex2, edges[0].Key);
edges = graph.GetEdges(vertex2).ToKeyValuePairs();
Assert.AreEqual(1, edges.Length);
Assert.AreEqual(0, edges[0].Value.Tags);
Assert.AreEqual(vertex1, edges[0].Key);
Edge edge;
Assert.IsTrue(graph.GetEdge(vertex1, vertex2, out edge));
Assert.AreEqual(0, edge.Tags);
Assert.AreEqual(true, edge.Forward);
Assert.IsTrue(graph.GetEdge(vertex2, vertex1, out edge));
Assert.AreEqual(0, edge.Tags);
Assert.AreEqual(false, edge.Forward);
}
示例14: Main
static void Main(string[] args)
{
Graph UG = new Graph(true);
Vertex a = new Vertex("a");
Vertex b = new Vertex("b");
Vertex c = new Vertex("c");
Vertex d = new Vertex("d");
Vertex e = new Vertex("e");
Vertex f = new Vertex("f");
// Add vertexes
UG.AddVertex(a);
UG.AddVertex(b);
UG.AddVertex(c);
UG.AddVertex(d);
UG.AddVertex(e);
UG.AddEdge(a, b, 3);
UG.AddEdge(a, c, 3);
UG.AddEdge(b, d, 3);
UG.AddEdge(d, e, 3);
UG.BreadthFirstSearch(d);
Console.ReadLine();
}
示例15: TestSparseRemoval1
public void TestSparseRemoval1()
{
// use one edge definition everywhere.
var edge = new Edge();
edge.Forward = true;
edge.Tags = 1;
var graph = new Graph<Edge>();
uint vertex1 = graph.AddVertex(0, 0);
uint vertex2 = graph.AddVertex(1, 1);
uint vertex3 = graph.AddVertex(2, 2);
graph.AddEdge(vertex1, vertex2, edge, null);
graph.AddEdge(vertex2, vertex1, edge, null);
graph.AddEdge(vertex2, vertex3, edge, null);
graph.AddEdge(vertex3, vertex2, edge, null);
// execute pre-processor.
var preProcessor = new GraphSimplificationPreprocessor(graph);
preProcessor.Start();
// test resulting graph.
Assert.AreEqual(2, graph.VertexCount);
Assert.AreEqual(1, graph.GetEdges(1).ToKeyValuePairs().Length);
Assert.AreEqual(2, graph.GetEdges(1).ToKeyValuePairs()[0].Key);
Assert.AreEqual(1, graph.GetEdges(2).ToKeyValuePairs().Length);
Assert.AreEqual(1, graph.GetEdges(2).ToKeyValuePairs()[0].Key);
}