public void When_DestinationNodeIsNotInTheSourceNeighborhood_MinimumPathIsReturned() { var source = new Node("source"); var longPath = new Node("longPath"); var shortPath = new Node("shortPath"); var destination = new Node("destination"); var graph = new DirectedGraph(); graph.AddNode(source); graph.AddNode(longPath); graph.AddNode(shortPath); graph.AddNode(destination); graph.AddEdge(source, longPath, 50); graph.AddEdge(longPath, destination, 2); graph.AddEdge(source, shortPath, 5); graph.AddEdge(shortPath, destination, 2); var dijkstra = new Dijkstra(graph); var expectedPath = new List <Node> { source, shortPath, destination }; List <Node> minimumPath = dijkstra.GetMinimumPathBetween(source, destination).ToList(); CollectionAssert.AreEqual(expectedPath, minimumPath); }
public void TestBAddThreeNodesCount() { DirectedGraph <string, string> g = new DirectedGraph <string, string>(); g.AddNode("add"); g.AddNode("three"); g.AddNode("nodes"); Assert.That(g.NodeCount, Is.EqualTo(3)); }
public void When_EdgeIsDuplicated_ExceptionIsThrown() { var from = new Node("someValue"); var to = new Node("someOtherValue"); var graph = new DirectedGraph(); graph.AddNode(from); graph.AddNode(to); graph.AddEdge(from, to, 5); graph.AddEdge(from, to, 5); }
public void When_DestinationNodeDoesNotExistInGraph_ExceptionIsThrown() { var node = new Node("someValue"); var node2 = new Node("otherValue"); var graph = new DirectedGraph(); graph.AddNode(node); graph.AddNode(node2); var dijkstra = new Dijkstra(graph); dijkstra.GetMinimumPathBetween(node, new Node("notInGraph")); }
/// <summary>Calculate / create a directed graph from model</summary> public void CalculateDirectedGraph() { if (directedGraphInfo == null) { directedGraphInfo = new DirectedGraph(); } directedGraphInfo.Begin(); bool needAtmosphereNode = false; foreach (NutrientPool pool in Apsim.Children(this, typeof(NutrientPool))) { directedGraphInfo.AddNode(pool.Name, ColourUtilities.ChooseColour(3), Color.Black); foreach (CarbonFlow cFlow in Apsim.Children(pool, typeof(CarbonFlow))) { foreach (string destinationName in cFlow.destinationNames) { string destName = destinationName; if (destName == null) { destName = "Atmosphere"; needAtmosphereNode = true; } directedGraphInfo.AddArc(null, pool.Name, destName, Color.Black); } } } foreach (Solute solute in Apsim.Children(this, typeof(Solute))) { directedGraphInfo.AddNode(solute.Name, ColourUtilities.ChooseColour(2), Color.Black); foreach (NFlow nitrogenFlow in Apsim.Children(solute, typeof(NFlow))) { string destName = nitrogenFlow.destinationName; if (destName == null) { destName = "Atmosphere"; needAtmosphereNode = true; } directedGraphInfo.AddArc(null, nitrogenFlow.sourceName, destName, Color.Black); } } if (needAtmosphereNode) { directedGraphInfo.AddTransparentNode("Atmosphere"); } directedGraphInfo.End(); }
public void BuildCyclicGraphViaStrings() { var graph = new DirectedGraph(); graph.AddNode("Start"); graph.AddNode("Middle"); graph.AddNode("End"); graph.AddEdge("Start", "Middle", 1, false); graph.AddEdge("Middle", "Middle", 1, false); graph.AddEdge("Middle", "End", 1, false); Assert.AreEqual(3, graph.Edges.Count()); }
public void When_EdgeIsAdded_ToNodeBecomesNeighborOfFromNode() { var from = new Node("someValue"); var to = new Node("someOtherValue"); var graph = new DirectedGraph(); graph.AddNode(from); graph.AddNode(to); graph.AddEdge(from, to, 5); Assert.IsTrue(from.HasNeighbor(to)); Assert.AreEqual(5, from.GetCostTo(to)); }
public void When_DestinationNodeIsIsolated_EmptyPathIsReturned() { var source = new Node("source"); var node = new Node("someValue"); var destination = new Node("destination"); var graph = new DirectedGraph(); graph.AddNode(source); graph.AddNode(node); graph.AddNode(destination); graph.AddEdge(source, node, 5); var dijkstra = new Dijkstra(graph); Assert.AreEqual(0, dijkstra.GetMinimumPathBetween(source, destination).ToList().Count); }
public void TestBAddThreeNodesIterate() { DirectedGraph <int, string> g = new DirectedGraph <int, string>(); g.AddNode(1); g.AddNode(2); g.AddNode(5); List <int> nodes = new List <int>(); foreach (int i in g.Nodes) { nodes.Add(i); } Assert.That(nodes, Is.EquivalentTo(new int[] { 1, 2, 5 })); }
public void When_NoPathIsFound_ReturnedCostIsZero() { var source = new Node("source"); var node = new Node("someValue"); var destination = new Node("destination"); var graph = new DirectedGraph(); graph.AddNode(source); graph.AddNode(node); graph.AddNode(destination); graph.AddEdge(source, node, 5); var dijkstra = new Dijkstra(graph); List <Node> minimumPath = dijkstra.GetMinimumPathBetween(source, destination).ToList(); Assert.AreEqual(0, dijkstra.GetPathCost(minimumPath)); }
public void AddNode() { var myGraph = new DirectedGraph <int, EdgeData>(); myGraph.AddNode(4); Assert.That(myGraph.Nodes, Does.Contain(4)); }
public static DirectedGraph CreateGraphWithStartNode() { DirectedGraph newGraph = new DirectedGraph(); newGraph.AddNode(new StartNode()); return(newGraph); }
public void AlreadyExistingBidirectedEdge() { var graph = new DirectedGraph(); var start = new DirectedGraphNode("Start"); var middle = new DirectedGraphNode("Middle"); var end = new DirectedGraphNode("End"); graph.AddNode(start); graph.AddNode(middle); graph.AddNode(end); graph.AddEdge(start, middle, 1, true); graph.AddEdge(middle, start, 1, true); graph.AddEdge(middle, end, 1, false); Assert.AreEqual(3, graph.Edges.Count()); }
public void ComplementaryDirectedEdges() { var graph = new DirectedGraph(); var start = new DirectedGraphNode("Start"); var middle = new DirectedGraphNode("Middle"); var end = new DirectedGraphNode("End"); graph.AddNode(start); graph.AddNode(middle); graph.AddNode(end); graph.AddEdge(start, middle, 1, false); graph.AddEdge(middle, start, 1, false); graph.AddEdge(middle, end, 1, false); Assert.AreEqual(3, graph.Edges.Count()); }
public void GenerateImage() { var graph = new DirectedGraph(); graph.AddNode("Start"); graph.AddNode("Middle1"); graph.AddNode("Middle2"); graph.AddNode("End"); graph.TryAddEdge("Start", "End", 2, true, out _); graph.TryAddEdge("Start", "Middle1", 1, true, out _); graph.TryAddEdge("Middle1", "Middle2", 1, true, out _); graph.TryAddEdge("Middle2", "End", 1, true, out _); var image = graph.ToImage(GraphVizPath); Assert.IsNotNull(image); }
public void BuildCyclicGraphViaObjects() { var graph = new DirectedGraph(); var start = new DirectedGraphNode("Start"); var middle = new DirectedGraphNode("Middle"); var end = new DirectedGraphNode("End"); graph.AddNode(start); graph.AddNode(middle); graph.AddNode(end); graph.AddEdge(start, middle, 1, false); graph.AddEdge(middle, middle, 1, false); graph.AddEdge(middle, end, 1, false); Assert.AreEqual(3, graph.Edges.Count()); }
/// <summary> /// Adds all nodes to the graph /// </summary> private void AddNodes() { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { _graph.AddNode(_cellsArray[i, j]); } } }
public void When_EdgeToANonExistingNodeInGraphIsAdded_ExceptionIsThrown() { var from = new Node("someValue"); var to = new Node("someOtherValue"); var graph = new DirectedGraph(); graph.AddNode(from); graph.AddEdge(from, to, 1); }
protected override void OnInit() { if (_systems.Count > 0) { for (var i = 0; i < _systems.Count; i++) { _depGraph.AddNode(_systems[i]); _systems[i].Init(); } } }
public void When_DestinationNodeIsNeighborWithSourceNode_MinimumPathIsReturned() { var source = new Node("source"); var node = new Node("someValue"); var destination = new Node("destination"); var graph = new DirectedGraph(); graph.AddNode(source); graph.AddNode(node); graph.AddNode(destination); graph.AddEdge(source, node, 50); graph.AddEdge(source, destination, 5); var dijkstra = new Dijkstra(graph); var expectedPath = new List <Node> { source, destination }; List <Node> minimumPath = dijkstra.GetMinimumPathBetween(source, destination).ToList(); CollectionAssert.AreEqual(expectedPath, minimumPath); }
public void TestBAddNodeLookItUp() { DirectedGraph <int, string> g = new DirectedGraph <int, string>(); bool before = g.ContainsNode(0); g.AddNode(0); Assert.Multiple(() => { Assert.That(before, Is.False); Assert.That(g.ContainsNode(0), Is.True); }); }
public void When_MinimumPathIsFound_CorrectCostIsReturned() { var source = new Node("source"); var longPath = new Node("longPath"); var shortPath = new Node("shortPath"); var destination = new Node("destination"); var graph = new DirectedGraph(); graph.AddNode(source); graph.AddNode(longPath); graph.AddNode(shortPath); graph.AddNode(destination); graph.AddEdge(source, longPath, 50); graph.AddEdge(longPath, destination, 2); graph.AddEdge(source, shortPath, 5); graph.AddEdge(shortPath, destination, 2); var dijkstra = new Dijkstra(graph); List <Node> minimumPath = dijkstra.GetMinimumPathBetween(source, destination).ToList(); Assert.AreEqual(7, dijkstra.GetPathCost(minimumPath)); }
public void When_NodeIsRemovedFromGraph_GraphNodesDoNotContainRemovedNode() { var node = new Node("someValue"); var graph = new DirectedGraph(); graph.AddNode(node); graph.RemoveNode(node); Assert.IsFalse(graph.HasNode(node)); Assert.AreEqual(0, graph.GetCount()); }
public void SinglePath2() { var graph = new DirectedGraph(); graph.AddNode("Start"); graph.AddNode("Middle1"); graph.AddNode("Middle2"); graph.AddNode("End"); graph.TryAddEdge("Start", "End", 2, true, out _); graph.TryAddEdge("Start", "Middle1", 1, true, out _); graph.TryAddEdge("Middle1", "Middle2", 1, true, out _); graph.TryAddEdge("Middle2", "End", 1, true, out _); var shortestGraph = (new DeepFirstAlgorithm(graph)).GetShortestGraph("Start", "End"); var endNode = shortestGraph.GetDistanceNode("End"); Assert.AreEqual(2, ((DistanceNode)endNode).Distance); Assert.AreEqual(2, shortestGraph.Nodes.Count()); Assert.AreEqual(1, shortestGraph.Edges.Count()); }
public void SinglePath1() { var graph = new DirectedGraph(); graph.AddNode("Start"); graph.AddNode("Middle1"); graph.AddNode("Middle2"); graph.AddNode("End"); graph.TryAddEdge("End", "Start", 5, true, out _); //switches the target and source of the edge in this test graph.TryAddEdge("Start", "Middle1", 1, true, out _); graph.TryAddEdge("Middle1", "Middle2", 1, true, out _); graph.TryAddEdge("Middle2", "End", 1, true, out _); var shortestGraph = (new DeepFirstAlgorithm(graph)).GetShortestGraph("Start", "End"); var endNode = shortestGraph.GetDistanceNode("End"); Assert.AreEqual(3, endNode.Distance); Assert.AreEqual(4, shortestGraph.Nodes.Count()); Assert.AreEqual(3, shortestGraph.Edges.Count()); }
/// <summary> /// Reads in points and populates _graph /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void uxLoad_Click(object sender, EventArgs e) { List <Point> points = new List <Point>(); if (uxFile.ShowDialog() == DialogResult.OK) { uxGraph.Clear(); _graph = new DirectedGraph <Point, double>(); using (StreamReader input = new StreamReader(uxFile.FileName)) { string line = input.ReadLine(); int x, y; LineNums(line, out x, out y); uxGraph.GraphSize = new Size(x, y); while (!input.EndOfStream) { line = input.ReadLine(); LineNums(line, out x, out y); Point p = new Point(x, y); uxGraph.DrawPoint(p); points.Add(p); _graph.AddNode(p); } } } //Done with the file //Write edges to graph foreach (Point p in points) { for (int i = 0; i < points.Count; i++) { if (!p.Equals(points[i])) { double d = GetDistance(p, points[i]); _graph.AddEdge(p, points[i], d); } } } //Find and draw path, or just say 0 if (_graph.NodeCount != 0) { FindShortestPath(points[0]); } else { MessageBox.Show("Total cost: 0"); } }
public void When_DirectedGraphIsComplex_MinimumPathIsReturned() { //testing graph example from https://msdn.microsoft.com/en-US/library/ms379574(v=vs.80).aspx var newYork = new Node("New York"); var chicago = new Node("Chicago"); var sanFrancisco = new Node("San Francisco"); var denver = new Node("Denver"); var losAngeles = new Node("Los Angeles"); var dallas = new Node("Dallas"); var miami = new Node("Miami"); var sanDiego = new Node("San Diego"); var graph = new DirectedGraph(); graph.AddNode(newYork); graph.AddNode(chicago); graph.AddNode(sanFrancisco); graph.AddNode(denver); graph.AddNode(losAngeles); graph.AddNode(dallas); graph.AddNode(miami); graph.AddNode(sanDiego); graph.AddEdge(newYork, chicago, 75); graph.AddEdge(newYork, denver, 100); graph.AddEdge(newYork, dallas, 125); graph.AddEdge(newYork, miami, 90); graph.AddEdge(chicago, sanFrancisco, 25); graph.AddEdge(chicago, denver, 20); graph.AddEdge(sanFrancisco, losAngeles, 45); graph.AddEdge(sanDiego, losAngeles, 45); graph.AddEdge(dallas, sanDiego, 90); graph.AddEdge(dallas, losAngeles, 80); graph.AddEdge(miami, dallas, 50); graph.AddEdge(denver, losAngeles, 100); var dijkstra = new Dijkstra(graph); List <Node> minimumPath = dijkstra.GetMinimumPathBetween(newYork, losAngeles).ToList(); var expectedPath = new List <Node> { newYork, chicago, sanFrancisco, losAngeles }; CollectionAssert.AreEqual(expectedPath, minimumPath); Assert.AreEqual(145, dijkstra.GetPathCost(minimumPath)); }
/// <summary> /// Adds in each node to the directed graph thens adds all of the node's edges. /// Next solves the board. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void uxSolve_Click(object sender, EventArgs e) { //makes a new graph each time _graph = new DirectedGraph <Cell, int>(); //add each one as a node into the graph for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { _graph.AddNode(_cellArray[i, j]); } } // i/row col/j for (int row = 0; row < 9; row++) { for (int col = 0; col < 9; col++) { int rowNum = (row / 3) * 3; int colNum = (col / 3) * 3; addCellEdges(_cellArray[row, col], colNum, rowNum); } } if (SolveSudoku(81, null)) { //updating grid to be the same as the underlying array for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { SetCell(i, j, _cellArray[i, j].Value); } } return; } else { if (!_isEdited) { MessageBox.Show("Initial puzzle does not follow Sudoku rules."); } _isEdited = true; } }
/// <summary> /// Converts a atate machine to a directed graph. /// This allows the usage of all analysis functions of 'mitoSoft.Graphs.Analysis'. /// </summary> public static DirectedGraph ToDirectedGraph(this Workflows.StateMachine stateMachine) { var directedGraph = new DirectedGraph(); foreach (var state in stateMachine.Nodes) { directedGraph.AddNode(state.Name); } foreach (var edge in stateMachine.Edges) { directedGraph.AddEdge(edge.Source.Name, edge.Target.Name, 1, false); var graphEdge = directedGraph.GetEdge(edge.Source.Name, edge.Target.Name); graphEdge.Description = edge.Description; } return(directedGraph); }
/// <summary> /// Gets a graph representing the given maze. The Direction associated with each edge /// is the direction from the source to the destination. /// </summary> /// <param name="maze">The maze to be represented.</param> /// <returns>The graph representation.</returns> private DirectedGraph <Cell, Direction> GetGraph(Maze maze) { DirectedGraph <Cell, Direction> graph = new DirectedGraph <Cell, Direction>(); for (int i = 0; i < maze.MazeHeight; i++) { for (int j = 0; j < maze.MazeWidth; j++) { Cell cell = new Cell(i, j); if (!graph.ContainsNode(cell)) { graph.AddNode(cell); } for (Direction d = Direction.North; d <= Direction.West; d++) { if (maze.IsClear(cell, d)) { graph.AddEdge(cell, maze.Step(cell, d), d); } } } } return(graph); }
private void buildGraph(List<XerisModule> modules) { graph = new DirectedGraph<XerisModule>(); graph.AddNode(beforeAll); graph.AddNode(before); graph.AddNode(afterAll); graph.AddNode(after); graph.AddEdge(before, after); graph.AddEdge(beforeAll, before); graph.AddEdge(after, afterAll); foreach(XerisModule m in modules) { graph.AddNode(m); } foreach (XerisModule m in modules) { if (m.IsImmutable) { // Immutable mods are always before everything graph.AddEdge(beforeAll, m); graph.AddEdge(m, before); continue; } bool preDepAdded = false; bool postDepAdded = false; foreach (XerisModule dep in m.Dependencies) { preDepAdded = true; if (dep.Name.Equals("*")) { // We are "after" everything graph.AddEdge(m, afterAll); graph.AddEdge(after, m); postDepAdded = true; } else { graph.AddEdge(before, m); if (modules.Contains<XerisModule>(dep)) graph.AddEdge(dep, m); } } foreach (XerisModule dep in m.Dependents) { if (dep == null) { continue; } postDepAdded = true; if (dep.Name.Equals("*")) { // We are "before" everything graph.AddEdge(beforeAll, m); graph.AddEdge(m, before); preDepAdded = true; } else { graph.AddEdge(m, after); if (modules.Contains<XerisModule>(dep)) graph.AddEdge(m, dep); } } if (!preDepAdded) graph.AddEdge(before, m); if (!postDepAdded) graph.AddEdge(m, after); } }
public void ShuffleTreasures(MT19337 rng) { DirectedGraph<byte> graph = new DirectedGraph<byte>(); var treasureBlob = Get(TreasureOffset, TreasureSize * TreasureCount); var usedIndices = Enumerable.Range(0, TreasureCount).Except(TreasureConditions.NotUsed).ToList(); var usedTreasures = usedIndices.Select(i => treasureBlob[i]).ToList(); bool tofrQuestItem; do { usedTreasures.Shuffle(rng); for (int i = 0; i < usedIndices.Count; i++) { treasureBlob[usedIndices[i]] = usedTreasures[i]; } // ToFR is only exitable using WARP or EXIT, so we don't want these items showing up there. // Especially not the TAIL, as that would make class change impossible. And the CROWN being // here could block a LOT of valuable loot if you don't have a WW or BW. tofrQuestItem = TreasureConditions.ToFR.Contains(treasureBlob.IndexOf((byte)QuestItems.Crown)) || TreasureConditions.ToFR.Contains(treasureBlob.IndexOf((byte)QuestItems.Tail)) || TreasureConditions.ToFR.Contains(treasureBlob.IndexOf((byte)QuestItems.Adamant)); if (tofrQuestItem) { continue; } var blockages = new List<Tuple<byte, int, List<int>>> { Tuple.Create((byte)QuestItems.Crown, treasureBlob.IndexOf((byte)QuestItems.Crown), TreasureConditions.CrownBlocked), Tuple.Create((byte)QuestItems.Tnt, treasureBlob.IndexOf((byte)QuestItems.Tnt), TreasureConditions.TntBlocked), Tuple.Create((byte)QuestItems.Ruby, treasureBlob.IndexOf((byte)QuestItems.Ruby), TreasureConditions.RubyBlocked), Tuple.Create((byte)QuestItems.Floater, treasureBlob.IndexOf((byte)QuestItems.Floater), TreasureConditions.FloaterBlocked), Tuple.Create((byte)QuestItems.Slab, treasureBlob.IndexOf((byte)QuestItems.Slab), TreasureConditions.SlabBlocked) }; graph = new DirectedGraph<byte>(); foreach (var blockage in blockages) { graph.AddNode(blockage.Item1); } foreach (var blocker in blockages) { foreach (var blockee in blockages) { if (blocker.Item3.Contains(blockee.Item2)) { graph.AddEdge(blockee.Item1, blocker.Item1); } } } } while (tofrQuestItem || graph.HasCycles()); Put(TreasureOffset, treasureBlob); }