本文整理汇总了C#中Graph.AddEdge方法的典型用法代码示例。如果您正苦于以下问题:C# Graph.AddEdge方法的具体用法?C# Graph.AddEdge怎么用?C# Graph.AddEdge使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Graph
的用法示例。
在下文中一共展示了Graph.AddEdge方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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);
}
示例2: 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)
);
}
示例3: 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);
}
示例4: Graph_can_be_converted_to_acyclic_graph_02
public void Graph_can_be_converted_to_acyclic_graph_02()
{
// ARRANGE
var expectedValuesInOrder = new[] {0, 1, 2, 1};
var graph = new Graph<int>(EqualityComparer<int>.Default);
// ACT
graph.AddEdgeFromStartNode(0);
graph.AddEdge(0, 1);
graph.AddEdge(1, 2);
graph.AddEdge(2, 1);
var nodes = graph.ToAcyclicGraph().ValueNodes.ToArray();
// ASSERT
Assert.Equal(4, nodes.Length);
Assert.Equal(expectedValuesInOrder, nodes.Select(n => n.Value).ToArray());
// expected Graph 0 -> 1 -> 2 -> 1 (1 was added twice to prevent a cycle)
Assert.Equal(1, nodes[0].Successors.Single().Value);
Assert.Equal(2, nodes[1].Successors.Single().Value);
Assert.Equal(1, nodes[2].Successors.Single().Value);
Assert.Empty(nodes[3].Successors);
}
示例5: 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);
}
示例6: CreateGraph
public Graph<Chapter> CreateGraph(IEnumerable<Chapter> chapters)
{
var graph = new Graph<Chapter>();
foreach (var chapter in chapters)
{
graph.AddVertex(chapter);
}
foreach (var chapter in chapters)
{
foreach (var precedingChapterString in chapter.PrecedingChapterReferences)
{
Chapter preceedingChapter = this.findChapterByTitle(chapters, precedingChapterString);
if (preceedingChapter != null)
{
graph.AddEdge(new Edge<Chapter>(preceedingChapter, chapter, destinationArrow: new PrecedingArrow()));
}
}
foreach (var succeedingChapterString in chapter.SucceedingChapterReferences)
{
Chapter succeedingChapter = this.findChapterByTitle(chapters, succeedingChapterString);
if (succeedingChapter != null)
{
graph.AddEdge(new Edge<Chapter>(chapter, succeedingChapter, destinationArrow: new SucceedingArrow()));
}
}
}
return graph;
}
示例7: Analyze
private void Analyze(string dll)
{
if (!string.IsNullOrEmpty(dll))
{
toolStripStatusLabel1.Text = dll;
var graph = new Graph("glee");
richTextBox1.Clear();
richTextBox2.Clear();
foreach (string relation in _references)
{
if (relation.StartsWith(dll))
{
string[] strs = relation.Split(',');
graph.AddEdge(strs[0], strs[1]).Attr.Color=Microsoft.Glee.Drawing.Color.DarkBlue;
richTextBox1.AppendText(strs[1] + "\n");
graph.FindNode(strs[1]).Attr.Fillcolor = Microsoft.Glee.Drawing.Color.Blue;
}else if(relation.EndsWith(dll))
{
string[] strs = relation.Split(',');
graph.AddEdge(strs[0], strs[1]).Attr.Color = Microsoft.Glee.Drawing.Color.DarkRed;
richTextBox2.AppendText(strs[0] + "\n");
graph.FindNode(strs[0]).Attr.Fillcolor = Microsoft.Glee.Drawing.Color.Red;
}
}
var node=graph.FindNode(dll);
if(node !=null)node.Attr.Fillcolor = Microsoft.Glee.Drawing.Color.Green;
_viewer.Graph = graph;
_viewer.CalculateLayout(graph);
}
}
示例8: 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);
}
示例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: InterproceduralReachabilityGraph
public InterproceduralReachabilityGraph(Program prog) {
this.prog = prog;
originalToNew = new Dictionary<Block,Block>();
newProcedureEntryNodes = new Dictionary<string,Block>();
newProcedureExitNodes = new Dictionary<string,Block>();
nodes = new HashSet<Block>();
ProcessImplementations();
ProcessBodilessProcedures();
PatchUpGotoTargets();
AddCallAndReturnEdges();
reachabilityGraph = new Graph<Block>();
foreach(var n in nodes) {
GotoCmd gotoCmd = n.TransferCmd as GotoCmd;
if(gotoCmd != null) {
foreach(Block b in gotoCmd.labelTargets) {
reachabilityGraph.AddEdge(n, b);
}
}
}
foreach(var n in nodes) {
// If there are disconnected nodes, put them into the
// graph as self-loops so that every node is represented in
// the graph
if(!reachabilityGraph.Nodes.Contains(n)) {
reachabilityGraph.AddEdge(n, n);
}
}
}
示例11: 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();
}
示例12: 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");
}
示例13: 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));
}
示例14: TopolgicalOrder_Cyclic_THrowsException
public void TopolgicalOrder_Cyclic_THrowsException()
{
var g = new Graph<string>();
g.AddEdge("jquery", "bootstrap");
g.AddEdge("bootstrap", "jquery");
g.SortTopological().ToList();
}
示例15: 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;
}