本文整理汇总了C#中AdjacencyGraph.ContainsEdge方法的典型用法代码示例。如果您正苦于以下问题:C# AdjacencyGraph.ContainsEdge方法的具体用法?C# AdjacencyGraph.ContainsEdge怎么用?C# AdjacencyGraph.ContainsEdge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AdjacencyGraph
的用法示例。
在下文中一共展示了AdjacencyGraph.ContainsEdge方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Removing_Explicit_Edges_2
public void Removing_Explicit_Edges_2()
{
var graph = new AdjacencyGraph<int, Edge<int>>();
graph.AddVertex(1);
graph.AddVertex(2);
graph.AddVertex(3);
graph.AddVertex(4);
graph.AddVertex(5);
graph.AddVertex(6);
graph.AddVertex(7);
graph.AddEdge(new Edge<int>(1, 3));
graph.AddEdge(new Edge<int>(1, 4));
graph.AddEdge(new Edge<int>(1, 6));
graph.AddEdge(new Edge<int>(3, 6));
graph.AddEdge(new Edge<int>(4, 6));
graph.AddEdge(new Edge<int>(2, 4));
graph.AddEdge(new Edge<int>(2, 5));
graph.AddEdge(new Edge<int>(2, 7));
graph.AddEdge(new Edge<int>(4, 7));
graph.AddEdge(new Edge<int>(5, 7));
GraphHelper.RemoveExplicitEdges(graph);
Assert.AreEqual(8, graph.EdgeCount);
Assert.IsTrue(graph.ContainsEdge(1, 3));
Assert.IsTrue(graph.ContainsEdge(1, 4));
Assert.IsTrue(graph.ContainsEdge(2, 4));
Assert.IsTrue(graph.ContainsEdge(2, 5));
Assert.IsTrue(graph.ContainsEdge(3, 6));
Assert.IsTrue(graph.ContainsEdge(4, 6));
Assert.IsTrue(graph.ContainsEdge(4, 7));
Assert.IsTrue(graph.ContainsEdge(5, 7));
}
示例2: Removing_Explicit_Edges_1
public void Removing_Explicit_Edges_1()
{
var graph = new AdjacencyGraph<int, Edge<int>>();
graph.AddVertex(1);
graph.AddVertex(2);
graph.AddVertex(3);
graph.AddEdge(new Edge<int>(1, 2));
graph.AddEdge(new Edge<int>(2, 3));
graph.AddEdge(new Edge<int>(1, 3));
GraphHelper.RemoveExplicitEdges(graph);
Assert.AreEqual(2, graph.EdgeCount);
Assert.IsTrue(graph.ContainsEdge(1, 2));
Assert.IsTrue(graph.ContainsEdge(2, 3));
}
示例3: CreateGraphFromNode
private AdjacencyGraph<Node, Edge<Node>> CreateGraphFromNode(XDocument xDocument, XNode parent)
{
var graph = new AdjacencyGraph<Node, Edge<Node>>();
var nodes = xDocument.Descendants().Where(x => x.Parent != null && (x.Parent.Parent != null && (x.Parent != null && x.Parent.Parent.Parent == parent)));
var fromList = nodes.Where(x => x.Parent != null && (x.Name.LocalName == "ControlFlow" && x.Parent.Name.LocalName =="FromSimpleRelationships") );
var toList = nodes.Where(x => x.Parent != null && (x.Name.LocalName == "ControlFlow" && x.Parent.Name.LocalName =="ToSimpleRelationships") );
foreach (var fromNode in fromList)
{
var xNode1 = fromNode.Parent.Parent;
string idref = fromNode.Attribute("Idref").Value;
var xNode2 =
toList.Where(x => x.Parent != null && (x.Attribute("Idref").Value.ToString() == idref))
.Select(x => x.Parent.Parent).FirstOrDefault();
if (xNode1 == null || xNode2 == null)
continue;
Node node1 = new Node(xNode1.Attribute("Name").Value, GetNodeType(xNode1));
Node node2 = new Node(xNode2.Attribute("Name").Value, GetNodeType(xNode2));
if (!graph.Vertices.Any(x => x.Name==node1.Name && x.Type==node1.Type))
{
graph.AddVertex(node1);
}
else
{
node1 = graph.Vertices.FirstOrDefault(x => x.Name == node1.Name && x.Type == node1.Type);
}
if (!graph.Vertices.Any(x => x.Name == node2.Name && x.Type == node2.Type))
{
graph.AddVertex(node2);
}
else
{
node2 = graph.Vertices.FirstOrDefault(x => x.Name == node2.Name && x.Type == node2.Type);
}
var newEdge = new Edge<Node>(node1, node2);
if(!graph.ContainsEdge(newEdge))
graph.AddEdge(newEdge);
}
return graph;
}
示例4: ShortestPhrase
/// <summary>
/// Time complexity n + logn?
/// http://bigocheatsheet.com/
///
/// Used a directed graph acyclic
/// http://en.wikipedia.org/wiki/Directed_acyclic_graph
///
/// The use topological sorting to determine order
/// https://quickgraph.codeplex.com/wikipage?title=Topological%20Sort
/// http://en.wikipedia.org/wiki/Topological_sorting#Algorithms
/// </summary>
/// <param name="logins"></param>
/// <returns></returns>
public static int ShortestPhrase(string[] logins)
{
var graph = new AdjacencyGraph<int, SEdge<int>>();
foreach (var item in logins)
{
for (int i = 0; i < item.Length; i++)
{
var number = Convert.ToInt32(item.Substring(i, 1));
if (!graph.ContainsVertex(number))
{
graph.AddVertex(number);
}
if (i + 1 == item.Length)
break;
var next = Convert.ToInt32(item.Substring(i + 1, 1));
if (!graph.ContainsEdge(number, next))
{
graph.AddEdge(new SEdge<int>(number, next));
}
}
}
var shortestPath = graph.TopologicalSort(); // O(log(n))
var builder = new StringBuilder();
shortestPath.ForEach(item => builder.Append(item));
return Convert.ToInt32(builder.ToString());
}
示例5: DeserializeFromGraphMLNorth
public void DeserializeFromGraphMLNorth()
{
foreach (var graphmlFile in TestGraphFactory.GetFileNames())
{
Console.Write(graphmlFile);
var g = new AdjacencyGraph<string, Edge<string>>();
using (var reader = new StreamReader(graphmlFile))
{
g.DeserializeFromGraphML(
reader,
id => id,
(source, target, id) => new Edge<string>(source, target)
);
}
Console.Write(": {0} vertices, {1} edges", g.VertexCount, g.EdgeCount);
var vertices = new Dictionary<string, string>();
foreach(var v in g.Vertices)
vertices.Add(v, v);
// check all nodes are loaded
var settings = new XmlReaderSettings();
settings.XmlResolver = new GraphMLXmlResolver();
settings.DtdProcessing = DtdProcessing.Ignore;
settings.ValidationFlags = System.Xml.Schema.XmlSchemaValidationFlags.None;
using(var xreader = XmlReader.Create(graphmlFile, settings))
{
var doc = new XPathDocument(xreader);
foreach (XPathNavigator node in doc.CreateNavigator().Select("/graphml/graph/node"))
{
string id = node.GetAttribute("id", "");
Assert.IsTrue(vertices.ContainsKey(id));
}
TestConsole.Write(", vertices ok");
// check all edges are loaded
foreach (XPathNavigator node in doc.CreateNavigator().Select("/graphml/graph/edge"))
{
string source = node.GetAttribute("source", "");
string target = node.GetAttribute("target", "");
Assert.IsTrue(g.ContainsEdge(vertices[source], vertices[target]));
}
TestConsole.Write(", edges ok");
}
TestConsole.WriteLine();
}
}
示例6: create_part_cycle_graph
public AdjacencyGraph<string, Edge<string>> create_part_cycle_graph(){
var g = new AdjacencyGraph<string, Edge<string>> ();
foreach (rule r in rules) {
b_rule b = r as b_rule;
if (b != null) {
string a1 = b.reactants.a.ToString ();
string a2 = b.reactants.b.ToString ();
string b1 = b.products.a.ToString ();
string b2 = b.products.b.ToString ();
if (!g.ContainsVertex (a1)) {
g.AddVertex (a1);
// Console.WriteLine (a1);
}
if (!g.ContainsVertex (a2)) {
g.AddVertex (a2);
// Console.WriteLine (a2);
}
if (!g.ContainsVertex (b1)) {
g.AddVertex (b1);
// Console.WriteLine (b1);
}
if (!g.ContainsVertex (b2)) {
g.AddVertex (b2);
// Console.WriteLine (b2);
}
if (!g.ContainsEdge (a1, b1)) {
Edge<string> e = new Edge<string> (a1, b1);
g.AddEdge (e);
// Console.WriteLine(a1+"->"+b1);
}
if (!g.ContainsEdge (a2, b2)) {
Edge<string> f = new Edge<string> (a2, b2);
g.AddEdge (f);
// Console.WriteLine(a2+"->"+b2);
}
}
}
return g;
}