本文整理汇总了C#中Graph.GetEdge方法的典型用法代码示例。如果您正苦于以下问题:C# Graph.GetEdge方法的具体用法?C# Graph.GetEdge怎么用?C# Graph.GetEdge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Graph
的用法示例。
在下文中一共展示了Graph.GetEdge方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
}
示例2: TestArguments
public void TestArguments()
{
using (var graph = new Graph<Edge>(new MemoryMappedStream(new MemoryStream()), 1000,
Edge.MapFromDelegate, Edge.MapToDelegate, Edge.SizeUints))
{
uint vertex1 = graph.AddVertex(0, 0);
uint vertex2 = graph.AddVertex(0, 0);
uint vertex3 = 3;
Assert.Catch<ArgumentOutOfRangeException>(() =>
{
graph.AddEdge(vertex3, vertex1, new Edge(), null);
});
Assert.Catch<ArgumentOutOfRangeException>(() =>
{
graph.AddEdge(vertex1, vertex3, new Edge(), null);
});
Assert.Catch<ArgumentException>(() =>
{
graph.AddEdge(vertex1, vertex1, new Edge(), null);
});
Assert.Catch<ArgumentException>(() =>
{
graph.AddEdge(vertex1, vertex1, new Edge(), null);
});
Assert.Catch<ArgumentOutOfRangeException>(() =>
{
graph.ContainsEdges(vertex3, vertex1);
});
Assert.Catch<ArgumentOutOfRangeException>(() =>
{
graph.ContainsEdges(vertex1, vertex3);
});
Edge edge;
Assert.Catch<ArgumentOutOfRangeException>(() =>
{
graph.GetEdge(vertex3, vertex1, out edge);
});
Assert.Catch<ArgumentOutOfRangeException>(() =>
{
graph.GetEdge(vertex1, vertex3, out edge);
});
Assert.Catch<ArgumentOutOfRangeException>(() =>
{
graph.GetEdges(vertex3);
});
Assert.Catch<ArgumentOutOfRangeException>(() =>
{
graph.SetVertex(vertex3, 10, 10);
});
}
}
示例3: TestGraphArguments
public void TestGraphArguments()
{
// create graph with one vertex and start adding vertex2.
var graph = new Graph<Edge>();
uint vertex1 = graph.AddVertex(0, 0);
uint vertex2 = graph.AddVertex(0, 0);
uint vertex3 = 3;
Assert.Catch<ArgumentOutOfRangeException>(() => {
graph.AddEdge(vertex3, vertex1, new Edge(), null);
});
Assert.Catch<ArgumentOutOfRangeException>(() =>
{
graph.AddEdge(vertex1, vertex3, new Edge(), null);
});
Assert.Catch<ArgumentException>(() =>
{
graph.AddEdge(vertex1, vertex1, new Edge(), null);
});
Assert.Catch<ArgumentException>(() =>
{
graph.AddEdge(vertex1, vertex1, new Edge(), null);
});
Assert.Catch<ArgumentOutOfRangeException>(() =>
{
graph.ContainsEdges(vertex3, vertex1);
});
Assert.Catch<ArgumentOutOfRangeException>(() =>
{
graph.ContainsEdges(vertex1, vertex3);
});
Edge edge;
Assert.Catch<ArgumentOutOfRangeException>(() =>
{
graph.GetEdge(vertex3, vertex1, out edge);
});
Assert.Catch<ArgumentOutOfRangeException>(() =>
{
graph.GetEdge(vertex1, vertex3, out edge);
});
Assert.Catch<ArgumentOutOfRangeException>(() =>
{
graph.GetEdges(vertex3);
});
Assert.Catch<ArgumentOutOfRangeException>(() =>
{
graph.SetVertex(vertex3, 10, 10);
});
}
示例4: ComputeGraph
public static void ComputeGraph(IEnumerable<KeyValuePair<string, Table>> tables1,
IEnumerable<ForeignKeyConstraint> realforeignKeys,
IEnumerable<ForeignKeyConstraint> probableForeignKeys)
{
var g = new Graph<string>(false);
foreach (var tbl in tables1)
{
g.AddVertex(tbl.Key.ToLower());
}
foreach (var key in realforeignKeys)
{
g.AddEdge(g.GetVertex(key.ConstraintKeys.First().From.TableName.ToLower()),
g.GetVertex(key.ConstraintKeys.First().To.TableName.ToLower()), 1);
}
foreach (var key in probableForeignKeys)
{
var first = key.ConstraintKeys.First();
var vertex = g.GetVertex(first.From.TableName.ToLower());
var vertex1 = g.GetVertex(first.To.TableName.ToLower());
if (g.GetEdge(vertex, vertex1) == null)
g.AddEdge(vertex, vertex1, 2);
}
//v.Compute();
//var v = GraphAlgorithms.KruskalsAlgorithm(g);
}
示例5: 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);
graph.AddEdge(vertex3, vertex2);
graph.AddEdge(vertex1, vertex3);
var edge = graph.GetEdge(vertex1, vertex2);
Assert.IsNotNull(edge);
Assert.AreEqual(edge.FromVertex, vertex1);
Assert.AreEqual(edge.ToVertex, vertex2);
edge = graph.GetEdge(vertex3, vertex2);
Assert.IsNotNull(edge);
Assert.AreEqual(edge.FromVertex, vertex3);
Assert.AreEqual(edge.ToVertex, vertex2);
edge = graph.GetEdge(vertex1, vertex3);
Assert.IsNotNull(edge);
Assert.AreEqual(edge.FromVertex, vertex1);
Assert.AreEqual(edge.ToVertex, vertex3);
edge = graph.GetEdge(vertex2, vertex1);
Assert.IsNotNull(edge);
Assert.AreEqual(edge.FromVertex, vertex1);
Assert.AreEqual(edge.ToVertex, vertex2);
edge = graph.GetEdge(vertex2, vertex3);
Assert.IsNotNull(edge);
Assert.AreEqual(edge.FromVertex, vertex3);
Assert.AreEqual(edge.ToVertex, vertex2);
edge = graph.GetEdge(vertex3, vertex1);
Assert.IsNotNull(edge);
Assert.AreEqual(edge.FromVertex, vertex1);
Assert.AreEqual(edge.ToVertex, vertex3);
}
示例6: Graph
private static Graph<string> Graph(IEnumerable<ForeignKeyConstraint> constraints, IEnumerable<Table> tables)
{
var g = new Graph<string> (false);
foreach (var tbl in tables) {
g.AddVertex (tbl.TableName.ToLower ());
}
foreach (var key in constraints) {
var @from = g.GetVertex (key.FromTableName ().ToLower ());
var to = g.GetVertex (key.ToTableName ().ToLower ());
Edge<string> edge;
if ((edge = g.GetEdge (@from, to)) == null)
edge = g.AddEdge (@from, to, 1);
edge.Tag = string.Join (", ", key.Columns.Select (c => c.From.ColumnName).ToArray ());
}
return g;
}
示例7: GetEdgeFromVertexValueExample
public void GetEdgeFromVertexValueExample()
{
// 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);
// A GetEdge operation with values 1 and 2 will return edge edge1
Assert.AreSame(graph.GetEdge(1, 2), edge1);
// A GetEdge operation with values 2 and 3 will return edge2
Assert.AreSame(graph.GetEdge(2, 3), edge2);
// A GetEdge operation with values 1 and 3 will return null -
// no such edge exists in the graph.
Assert.IsNull(graph.GetEdge(1, 3));
}
示例8: TestGraphAddReverse
public void TestGraphAddReverse()
{
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 = 1
}, null);
graph.AddEdge(vertex2, vertex1, new Edge()
{
Forward = true,
Tags = 2
}, null);
Edge edge;
Assert.IsTrue(graph.GetEdge(vertex1, vertex2, out edge));
Assert.AreEqual(2, edge.Tags);
Assert.IsTrue(graph.GetEdge(vertex2, vertex1, out edge));
Assert.AreEqual(2, edge.Tags);
}
示例9: TestGraphCompressEdges
public void TestGraphCompressEdges()
{
var graph = new Graph<Edge>();
var vertex1 = graph.AddVertex(51, 1);
var vertex2 = graph.AddVertex(51, 2);
var vertex3 = graph.AddVertex(51, 3);
var vertex4 = graph.AddVertex(51, 3);
graph.AddEdge(vertex1, vertex2, new Edge()
{
Forward = true,
Tags = 1
}, null);
graph.AddEdge(vertex2, vertex3, new Edge()
{
Forward = true,
Tags = 2
}, null);
graph.AddEdge(vertex3, vertex4, new Edge()
{
Forward = true,
Tags = 3
}, null);
graph.AddEdge(vertex4, vertex2, new Edge()
{
Forward = true,
Tags = 4
}, null);
graph.RemoveEdge(vertex2, vertex3);
graph.Compress();
Assert.IsFalse(graph.ContainsEdges(vertex2, vertex3));
Assert.IsFalse(graph.ContainsEdges(vertex3, vertex2));
Assert.AreEqual(graph.GetEdges(vertex1).ToKeyValuePairs().Length, 1);
Assert.AreEqual(graph.GetEdges(vertex2).ToKeyValuePairs().Length, 2);
Assert.AreEqual(graph.GetEdges(vertex3).ToKeyValuePairs().Length, 1);
Assert.AreEqual(graph.GetEdges(vertex4).ToKeyValuePairs().Length, 2);
graph = new Graph<Edge>();
vertex1 = graph.AddVertex(51, 1);
vertex2 = graph.AddVertex(51, 2);
vertex3 = graph.AddVertex(51, 3);
vertex4 = graph.AddVertex(51, 3);
graph.AddEdge(vertex1, vertex2, new Edge()
{
Forward = true,
Tags = 1
}, null);
graph.AddEdge(vertex2, vertex3, new Edge()
{
Forward = true,
Tags = 2
}, null);
graph.AddEdge(vertex3, vertex4, new Edge()
{
Forward = true,
Tags = 3
}, null);
graph.AddEdge(vertex4, vertex2, new Edge()
{
Forward = true,
Tags = 4
}, null);
graph.RemoveEdge(vertex3, vertex4);
graph.Compress();
Assert.IsFalse(graph.ContainsEdges(vertex3, vertex4));
Assert.IsFalse(graph.ContainsEdges(vertex4, vertex3));
Assert.AreEqual(graph.GetEdges(vertex1).ToKeyValuePairs().Length, 1);
Assert.AreEqual(graph.GetEdges(vertex2).ToKeyValuePairs().Length, 3);
Assert.AreEqual(graph.GetEdges(vertex3).ToKeyValuePairs().Length, 1);
Assert.AreEqual(graph.GetEdges(vertex4).ToKeyValuePairs().Length, 1);
Edge edge;
Assert.IsTrue(graph.GetEdge(vertex1, vertex2, out edge));
Assert.AreEqual(1, edge.Tags);
Assert.IsTrue(graph.GetEdge(vertex2, vertex3, out edge));
Assert.AreEqual(2, edge.Tags);
Assert.IsTrue(graph.GetEdge(vertex4, vertex2, out edge));
Assert.AreEqual(4, edge.Tags);
}
示例10: Run
// This would be much more effective as a unit test.
public void Run(Graph<Node, Edge> graph)
{
// Add nodes 0 and 1, and an curEdge between them.
graph.AddNode(new Node(graph.AvailableNodeIndex));
graph.AddNode(new Node(graph.AvailableNodeIndex));
graph.AddEdge(new Edge(0, 1));
// Add nodes 2 and 3.
graph.AddNode(new Node(graph.AvailableNodeIndex));
graph.AddNode(new Node(graph.AvailableNodeIndex));
// Connect 0-2, 0-3, 2-3, and change 2-3's actualWeight.
graph.AddEdge(new Edge(0, 2));
graph.AddEdge(new Edge(0, 3));
graph.AddEdge(new Edge(2, 3));
graph.ChangeEdgeWeight(3, 0, 32.0);
// Should print all nodes (0, 1, 2, and 3)
SysDbg.WriteLine("Nodes before first delete.");
foreach (Node node in graph.Nodes)
SysDbg.WriteLine("\tNode #" + node.Index);
// Nodes 0 and 1 should be removed as well as corresponding edges.
graph.RemoveNode(0);
graph.RemoveNode(1);
// Should print nodes 2 and 3.
SysDbg.WriteLine("Nodes after first delete.");
foreach (Node node in graph.Nodes)
SysDbg.WriteLine("\tNode #" + node.Index);
// Re-add node 0, connect 0-2. (Connect 0-1 and 1-0 not allowed)
// Also, change 0-2 actualWeight.
graph.AddNode(new Node(0));
graph.AddEdge(new Edge(0, 1));
graph.AddEdge(new Edge(2, 0));
graph.AddEdge(new Edge(1, 0));
graph.ChangeEdgeWeight(0, 2, 42.0);
// Should print 0-2, 2-3, 2-0, and 3-2.
SysDbg.WriteLine("Current edges.");
foreach (Edge edge in graph.Edges)
SysDbg.WriteLine("\tEdge " + edge.NodeFrom + " -> " + edge.NodeTo);
// Remove 0-2 and 2-0.
graph.RemoveEdge(2, 0);
// Should print 2-3 and 3-2.
SysDbg.WriteLine("Current edges.");
foreach (Edge edge in graph.Edges)
SysDbg.WriteLine("\tEdge " + edge.NodeFrom + " -> " + edge.NodeTo);
SysDbg.WriteLine("Node 3 exists? " + graph.NodeExists(3));
SysDbg.WriteLine("Node 1 exists? " + graph.NodeExists(1));
SysDbg.WriteLine("Edge 2-3 exists? " + graph.EdgeExists(2, 3));
SysDbg.WriteLine("Edge 3-2 exists? " + graph.EdgeExists(3, 2));
SysDbg.WriteLine("Edge 0-1 exists? " + graph.EdgeExists(0, 1));
SysDbg.WriteLine("Edge 1-0 exists? " + graph.EdgeExists(1, 0));
graph.AddNode(new Node(1));
graph.AddNode(new Node(4));
graph.AddNode(new Node(5));
graph.AddNode(new Node(6));
graph.AddEdge(new Edge(2, 2, 3));
for (int i = 2; i < 20; i++)
graph.AddEdge(new Edge(i, 1, 9.999));
// Should print nodes 0-6.
SysDbg.WriteLine("Nodes");
foreach (Node node in graph.Nodes)
SysDbg.WriteLine("\tNode #" + node.Index);
SysDbg.WriteLine("Current edges.");
foreach (Edge edge in graph.Edges)
SysDbg.WriteLine("\tEdge " + edge.NodeFrom + " -> " + edge.NodeTo);
SysDbg.WriteLine("Current edges from node 1.");
foreach (Edge edge in graph.EdgesFromNode(1))
SysDbg.WriteLine("\tEdge " + edge.NodeFrom + " -> " + edge.NodeTo);
graph.RemoveEdge(0, 5);
graph.RemoveEdge(3, 2);
// These all change the actualWeight.
graph.ChangeEdgeWeight(1, 2, 9001);
graph.GetEdge(6, 1).Weight = 33.2112;
graph.GetEdge(1, 6).Weight = 2112.33;
// Check for curEdge modification by iteration.
foreach (Edge e in graph.Edges)
e.Weight = 1.0;
double weightMod = 10.0;
foreach (Edge e in graph.Edges)
{
e.Weight += weightMod;
weightMod += 10.0;
}
// Revert all weights back to 1.0.
//.........这里部分代码省略.........
示例11: TestCompressEdges
public void TestCompressEdges()
{
using (var graph = new Graph<Edge>(new MemoryMappedStream(new MemoryStream()), 1000,
Edge.MapFromDelegate, Edge.MapToDelegate, Edge.SizeUints))
{
var vertex1 = graph.AddVertex(51, 1);
var vertex2 = graph.AddVertex(51, 2);
var vertex3 = graph.AddVertex(51, 3);
var vertex4 = graph.AddVertex(51, 3);
graph.AddEdge(vertex1, vertex2, new Edge()
{
Forward = true,
Tags = 1
}, null);
graph.AddEdge(vertex2, vertex3, new Edge()
{
Forward = true,
Tags = 2
}, null);
graph.AddEdge(vertex3, vertex4, new Edge()
{
Forward = true,
Tags = 3
}, null);
graph.AddEdge(vertex4, vertex2, new Edge()
{
Forward = true,
Tags = 4
}, null);
graph.RemoveEdge(vertex2, vertex3);
graph.Compress();
Assert.IsFalse(graph.ContainsEdges(vertex2, vertex3));
Assert.IsFalse(graph.ContainsEdges(vertex3, vertex2));
Assert.AreEqual(graph.GetEdges(vertex1).ToKeyValuePairs().Length, 1);
Assert.AreEqual(graph.GetEdges(vertex2).ToKeyValuePairs().Length, 2);
Assert.AreEqual(graph.GetEdges(vertex3).ToKeyValuePairs().Length, 1);
Assert.AreEqual(graph.GetEdges(vertex4).ToKeyValuePairs().Length, 2);
}
using (var graph = new Graph<Edge>(new MemoryMappedStream(new MemoryStream()), 1000,
Edge.MapFromDelegate, Edge.MapToDelegate, Edge.SizeUints))
{
uint vertex1 = graph.AddVertex(51, 1);
uint vertex2 = graph.AddVertex(51, 2);
uint vertex3 = graph.AddVertex(51, 3);
uint vertex4 = graph.AddVertex(51, 3);
graph.AddEdge(vertex1, vertex2, new Edge()
{
Forward = true,
Tags = 1
}, null);
graph.AddEdge(vertex2, vertex3, new Edge()
{
Forward = true,
Tags = 2
}, null);
graph.AddEdge(vertex3, vertex4, new Edge()
{
Forward = true,
Tags = 3
}, null);
graph.AddEdge(vertex4, vertex2, new Edge()
{
Forward = true,
Tags = 4
}, null);
graph.RemoveEdge(vertex3, vertex4);
graph.Compress();
Assert.IsFalse(graph.ContainsEdges(vertex3, vertex4));
Assert.IsFalse(graph.ContainsEdges(vertex4, vertex3));
Assert.AreEqual(graph.GetEdges(vertex1).ToKeyValuePairs().Length, 1);
Assert.AreEqual(graph.GetEdges(vertex2).ToKeyValuePairs().Length, 3);
Assert.AreEqual(graph.GetEdges(vertex3).ToKeyValuePairs().Length, 1);
Assert.AreEqual(graph.GetEdges(vertex4).ToKeyValuePairs().Length, 1);
Edge edge;
Assert.IsTrue(graph.GetEdge(vertex1, vertex2, out edge));
Assert.AreEqual(1, edge.Tags);
Assert.IsTrue(graph.GetEdge(vertex2, vertex3, out edge));
Assert.AreEqual(2, edge.Tags);
Assert.IsTrue(graph.GetEdge(vertex4, vertex2, out edge));
Assert.AreEqual(4, edge.Tags);
}
}
示例12: TestEdge
public void TestEdge()
{
using (var graph = new Graph<Edge>(new MemoryMappedStream(new MemoryStream()), 1000, Edge.MapFromDelegate, Edge.MapToDelegate, Edge.SizeUints))
{
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);
}
}