本文整理汇总了C#中Graph.AddNode方法的典型用法代码示例。如果您正苦于以下问题:C# Graph.AddNode方法的具体用法?C# Graph.AddNode怎么用?C# Graph.AddNode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Graph
的用法示例。
在下文中一共展示了Graph.AddNode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Analysis
/// <summary>
/// 1. Create Structs
/// 2. Separators unitys
/// 2.1
/// </summary>
/// <param name="sourcecode"></param>
/// <returns></returns>
public override Graph<Token, Token> Analysis(string sourcecode)
{
if (string.IsNullOrWhiteSpace(sourcecode))
sourcecode = string.Empty;
sourcecode = sourcecode.Replace("\r", "");
List<string> tokens = Tokenizer(sourcecode);
GraphTokens = new Graph<Token, Token>();
List<Token> lastTokens = new List<Token>();
int lineActual = 0;
int columnActual = 0;
var root = GraphTokens.AddNode("Root");
GraphTokens.Root = root;
root.Info = new Token(0, 0, 0, null, "R");
Node<Token, Token> lastNode = root;
for (int i = 0; i < tokens.Count; i++)
{
if (i == 23)
{
}
columnActual = i;
RecognitionToken rule = FindRules(tokens[i]).Max();
if (rule != null)
{
var node = GraphTokens.AddNode(NextIdNode());
Token token = new Token(NextIdToken(), lineActual, columnActual, rule, tokens[i]);
node.Info = token;
var edge = lastNode.AddEdge(token, node, rule.LastMatchValue);
lastNode = node;
}
}
return GraphTokens;
}
示例2: Main
static void Main()
{
Graph graph = new Graph();
string[] townInfo = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string numberOfPoints = townInfo[0];
int numberOfRoads = int.Parse(townInfo[1]);
HashSet<string> hospitals =
new HashSet<string>(Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
for (int i = 0; i < numberOfRoads; i++)
{
string[] roadsInfo = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string from = roadsInfo[0];
string to = roadsInfo[1];
int distance = int.Parse(roadsInfo[2]);
if (!graph.Nodes.ContainsKey(from))
{
graph.AddNode(from);
}
if (!graph.Nodes.ContainsKey(to))
{
graph.AddNode(to);
}
graph.AddConnection(from, to, distance, true);
if (hospitals.Contains(from))
{
graph.Nodes[from].IsHospital = true;
}
if (hospitals.Contains(to))
{
graph.Nodes[to].IsHospital = true;
}
}
//Console.WriteLine(graph);
int minDistance = int.MaxValue;
foreach (var hospital in hospitals)
{
int currentDistance = Dijkstra(graph.Nodes, hospital);
if (currentDistance < minDistance)
{
minDistance = currentDistance;
}
}
Console.WriteLine(minDistance);
}
示例3: ShouldAnnotateNodesAndLinksReachableFromGivenPoint
public void ShouldAnnotateNodesAndLinksReachableFromGivenPoint()
{
// arrange
var graph = new Graph();
var start = graph.AddNode(new SampleNode("start"));
var node1 = graph.AddNode(new SampleNode("node1"));
var node2 = graph.AddNode(new SampleNode("node2"));
var node3 = graph.AddNode(new SampleNode("node3"));
var unreachable = graph.AddNode(new SampleNode("unreachable"));
var link1 = graph.AddLink(start, node1, new SampleLink());
var link2 = graph.AddLink(node1, node2, new SampleLink());
var link3 = graph.AddLink(node2, node3, new SampleLink());
// act
var walkAndAnnotate = new WalkAndAnnotate<Node, Link>
{
NodeAnnotation = _ => "node annotation",
LinkAnnotation = _ => "link annotation"
};
walkAndAnnotate.Walk(graph, start);
// assert
Assert.That(start.HasAnnotation<string>(), Is.True);
Assert.That(node1.HasAnnotation<string>(), Is.True);
Assert.That(node2.HasAnnotation<string>(), Is.True);
Assert.That(node3.HasAnnotation<string>(), Is.True);
Assert.That(unreachable.HasAnnotation<string>(), Is.False);
Assert.That(link1.HasAnnotation<string>(), Is.True);
Assert.That(link2.HasAnnotation<string>(), Is.True);
Assert.That(link3.HasAnnotation<string>(), Is.True);
}
示例4: ShouldReplaceLinkInAll
public void ShouldReplaceLinkInAll()
{
// arrange
var source = new SampleNode("source");
var target = new SampleNode("target");
var graph = new Graph();
graph.AddNode(source);
graph.AddNode(target);
var firstLink = new SampleLink();
graph.AddLink(source, target, firstLink);
var secondLink = new SecondLink();
// act
graph.ReplaceLink(firstLink, secondLink);
// Assert
Assert.That(graph.Links, Has.Count.EqualTo(1));
Assert.That(graph.Links, Has.No.Member(firstLink));
Assert.That(graph.Links, Has.Member(secondLink));
Assert.That(secondLink.Source, Is.SameAs(source));
Assert.That(secondLink.Target, Is.SameAs(target));
Assert.That(source.OutboundLinks, Has.No.Member(firstLink));
Assert.That(target.InboundLinks, Has.No.Member(firstLink));
Assert.That(source.OutboundLinks, Has.Member(secondLink));
Assert.That(target.InboundLinks, Has.Member(secondLink));
}
示例5: TestFindingPaths
public void TestFindingPaths(string[] z)
{
var graphPaths = new List<string>(z);
// arrange
var graph = new Graph();
var start = graph.AddNode(new SampleNode("start"));
var end = graph.AddNode(new SampleNode("end"));
var nodes = new Dictionary<string, Node>
{
{"start", start},
{"end", end},
};
foreach (var graphPath in graphPaths)
{
var hops = graphPath.Split(new[] { "->" }, StringSplitOptions.None).ToList();
for (int i = 1; i < hops.Count; i++)
{
var hop = hops[i];
if (!nodes.ContainsKey(hop))
{
nodes[hop] = graph.AddNode(new SampleNode(hop));
}
graph.AddLink(nodes[hops[i - 1]], nodes[hop], new SampleLink());
}
}
// act
var foundPaths = FindAllPaths.BetweenNodes(start, end).ToList();
foreach (var foundPath in foundPaths)
{
Console.WriteLine("Found path: {0}", string.Join("->", foundPath.Select(x => x.Id)));
}
// assert
foreach (var foundPath in foundPaths)
{
var spec = string.Join("->", foundPath.Select(x => x.Id));
if (!graphPaths.Contains(spec))
{
Assert.Fail("Found path " + spec + " is unknown");
}
else
{
graphPaths.Remove(spec);
}
}
foreach (var notFoundPath in graphPaths)
{
Assert.Fail("Path " + notFoundPath + " was not found");
}
}
示例6: should_add_the_to_node_from_nodes_neighborlist
public void should_add_the_to_node_from_nodes_neighborlist()
{
var graph = new Graph<string>();
graph.AddNode("Berlin");
graph.AddNode("Paris");
graph.AddDirectedEdge("Berlin", "Paris", 0);
Assert.That(graph.NodeCount, Is.EqualTo(2));
}
示例7: should_also_add_the_weight_to_the_edge
public void should_also_add_the_weight_to_the_edge()
{
var graph = new Graph<string>();
graph.AddNode("Berlin");
graph.AddNode("Paris");
graph.AddUndirectedEdge("Berlin", "Paris", 0);
Assert.That(graph.NodeCount, Is.EqualTo(2));
}
示例8: EdgeTests
public EdgeTests()
{
_graph = _graphProvider.ProvideValid();
_validNode1 = _graph.AddNode("1");
_validNode2 = _graph.AddNode("2");
_validNode3 = _graph.AddNode("3");
_otherGraphNode = _graphProvider.ProvideValid().AddNode("1");
_notAddedNode = new Node("5", _graph);
}
示例9: Solve
static void Solve(string[] args)
{
Graph<int> graph = new Graph<int>();
graph.AddNode(1);
graph.AddNode(2);
GraphNodeDictionary<int> gNodes = graph.Nodes;
graph.AddDirectedEdge(gNodes[1], gNodes[2]);
}
示例10: Graph_can_contain_null
public void Graph_can_contain_null()
{
var graph = new Graph<object>(EqualityComparer<object>.Default);
var obj = new object();
graph.AddNode(null);
graph.AddNode(obj);
graph.AddEdge(null, obj);
Assert.Equal(2, graph.ValueNodes.Count());
}
示例11: AcyclicGraph
public void AcyclicGraph()
{
// arrange
var graph = new Graph();
var a = graph.AddNode(new SampleNode("a"));
var b = graph.AddNode(new SampleNode("b"));
var c = graph.AddNode(new SampleNode("c"));
var d = graph.AddNode(new SampleNode("d"));
var e = graph.AddNode(new SampleNode("e"));
var f = graph.AddNode(new SampleNode("f"));
var g = graph.AddNode(new SampleNode("g"));
var h = graph.AddNode(new SampleNode("h"));
graph.AddLink(a, b, new SampleLink());
graph.AddLink(a, c, new SampleLink());
graph.AddLink(b, d, new SampleLink());
graph.AddLink(b, e, new SampleLink());
graph.AddLink(c, f, new SampleLink());
graph.AddLink(c, g, new SampleLink());
graph.AddLink(e, h, new SampleLink());
// act
var path = new List<Node>();
var collectVisitedNodes = new LambdaBreadthFirstSearch<Node, Link>
{
HandlingNode = (node, links) => path.Add(node)
};
collectVisitedNodes.Walk(a);
// assert
Assert.That(path, Is.EqualTo(new[] { a, b, c, d, e, f, g, h }));
}
示例12: SplitAndJoinOnHop
public void SplitAndJoinOnHop()
{
// arrange
var graph = new Graph();
var start = graph.AddNode(new SampleNode("start"));
var end = graph.AddNode(new SampleNode("end"));
var hop1 = graph.AddNode(new SampleNode("hop1"));
var split = graph.AddNode(new SampleNode("split"));
var hop3 = graph.AddNode(new SampleNode("hop3"));
var hop4 = graph.AddNode(new SampleNode("hop4"));
var join = graph.AddNode(new SampleNode("join"));
var hop2 = graph.AddNode(new SampleNode("hop2"));
var l1 = graph.AddLink(start, hop1, new SampleLink());
var l2 = graph.AddLink(hop1, split, new SampleLink());
var l5 = graph.AddLink(split, join, new SampleLink());
var l6 = graph.AddLink(join, end, new SampleLink());
var l3 = graph.AddLink(split, hop3, new SampleLink());
var l4 = graph.AddLink(hop3, join, new SampleLink());
// act
var paths = FindAllPaths.BetweenNodes(start, end);
// assert
Assert.That(paths, Has.Count.EqualTo(2));
Assert.That(paths, Has.Exactly(1).EqualTo(new[] { start, hop1, split, hop3, join, end }));
Assert.That(paths, Has.Exactly(1).EqualTo(new[] { start, hop1, split, join, end }));
}
示例13: ColoringTest
public void ColoringTest()
{
//http://en.wikipedia.org/wiki/File:4CT_Non-Counterexample_1.svg
var states = new Graph();
states.AddNode("9");
states.AddNode("1");
states.AddNode("2");
states.AddNode("3");
states.AddNode("4");
states.AddNode("5");
states.AddNode("6");
states.AddNode("7");
states.AddNode("8");
//TODO: add all states
states["1"].AddEdge(states["2"]);
states["1"].AddEdge(states["3"]);
states["1"].AddEdge(states["4"]);
states["1"].AddEdge(states["5"]);
states["2"].AddEdge(states["3"]);
states["2"].AddEdge(states["5"]);
states["2"].AddEdge(states["6"]);
states["2"].AddEdge(states["9"]);
states["3"].AddEdge(states["4"]);
states["3"].AddEdge(states["6"]);
states["3"].AddEdge(states["7"]);
states["4"].AddEdge(states["5"]);
states["4"].AddEdge(states["7"]);
states["4"].AddEdge(states["8"]);
states["5"].AddEdge(states["8"]);
states["5"].AddEdge(states["9"]);
states["6"].AddEdge(states["7"]);
states["6"].AddEdge(states["9"]);
states["7"].AddEdge(states["8"]);
states["8"].AddEdge(states["9"]);
//TODO: add all edges for adjacent states
var bfsGraphColorizer = new BfsGraphColorizer();
bfsGraphColorizer.Color(states.Values);
Debug.Print("Using BFS algorithm:");
foreach (var state in states)
Debug.Print("{0}: {1}", state.Key, state.Value.Color);
var bruteForceGraphColorizer = new BruteForceGraphColorizer();
bruteForceGraphColorizer.Color(states);
Debug.Print("Using Brute Force algorithm:");
foreach (var state in states)
Debug.Print("{0}: {1}", state.Key, state.Value.Color);
}
示例14: Build
public static Graph<ASNode, ConnectionSchedule> Build(NodeNameMapper nodeNameMapper)
{
Logger.Log("Building graph", Logger.TabChange.Increase);
var graph = new Graph<ASNode, ConnectionSchedule>();
for (int i = 0; i < InputData.TrainingSets.Length; i++)
{
foreach (var names in InputData.TrainingSets[i].Select(s => s.Split('|').Select(n => n.Trim()).ToArray()))
{
foreach (var name in names.Take(2))
{
if (nodeNameMapper.Get(name) == null)
{
var node = nodeNameMapper.Create(name);
graph.AddNode(node);
}
}
var from = nodeNameMapper.Get(names[0]);
var to = nodeNameMapper.Get(names[1]);
var edge = from.GetEdge(to);
if (edge == null)
{
edge = new Edge<ConnectionSchedule>(new ConnectionSchedule());
from.AddEdge(to, edge);
}
edge.Value.Schedule[i] = double.Parse(names[2]);
}
}
Logger.Log(graph.Nodes.Count + " nodes and " + graph.Nodes.Sum(n => n.Edges.Count) + " edges added from training sets");
foreach (var name in InputData.Paths.SelectMany(p => p.Split('|').Select(n => n.Trim())))
{
if (nodeNameMapper.Get(name) == null)
{
var node = nodeNameMapper.Create(name);
graph.AddNode(node);
}
}
Logger.Log(graph.Nodes.Count + " nodes added from paths");
Logger.Log("Graph built", Logger.TabChange.Decrease);
return graph;
}
示例15: SetProgram
public void SetProgram(List<Loop> loops, List<BasicBlock> blocks)
{
// build DAG of basic blocks
// excluding backedges
Graph g = new Graph();
Dictionary<BasicBlock, Node> NodeMap = new Dictionary<BasicBlock, Node>();
foreach (BasicBlock b in blocks)
{
Node n = new Node(b);
g.AddNode(n);
NodeMap.Add(b,n);
}
foreach (BasicBlock b in blocks)
foreach (BasicBlock s in b.Successors)
if (!s.Dominates(b))
g.AddEdge(NodeMap[b], NodeMap[s]);
// walk over loops inner to outer
// for each loop, replace its contents with a single "super-node"
foreach( Loop l in loops )
{
Node[] loopNodes = new Node[l.BlockCount];
int n = 0;
foreach (BasicBlock b in l.Blocks)
loopNodes[n++] = NodeMap[b];
g.CombineNodes(loopNodes,l);
}
// now populate the tree view with loops and branches
TreeNode root = new TreeNode("program");
BuildTree(root, g);
treeView1.Nodes.Add(root);
}