public static List <WeightedNode <T> > WeightedDepthFirstSearch <T>(WeightedNode <T> start) { List <WeightedNode <T> > nodes = new List <WeightedNode <T> >(); HashSet <WeightedNode <T> > visited = new HashSet <WeightedNode <T> >(); DepthFirstSearch.TraverseWeightedDepthFirstSearch(start, nodes, visited); return(nodes); }
public void addEdge(int firstVertex, int secondVertex, double weight) { WeightedNode first = (WeightedNode)mVerticies[firstVertex]; WeightedNode second = (WeightedNode)mVerticies [secondVertex]; WeightedEdge edge = new WeightedEdge(first, second, weight); first.getNeighbors().Add(edge); second.getNeighbors().Add(edge); }
public MapGraphNode NextNode(MapGraphNode targetNode) { List <WeightedNode> openNodes = new List <WeightedNode>(); List <WeightedNode> closedNodes = new List <WeightedNode>(); openNodes.Add(new WeightedNode(mapMovingEntity.nodeFrom, targetNode)); while (openNodes.First().node != targetNode) { openNodes.OrderBy(n => n.TotalCost); closedNodes.Add(openNodes.First()); openNodes.RemoveAt(0); for (int i = 0; i < 4; i++) { var neighbouringNode = closedNodes.Last().node.NodeDirection((Direction)i); if (neighbouringNode == null) { continue; } WeightedNode neighbourWeightedNode = closedNodes.Find(n => n.node == neighbouringNode); if (neighbourWeightedNode != null) { continue; } neighbourWeightedNode = openNodes.Find(n => n.node == neighbouringNode); if (neighbourWeightedNode == null) { openNodes.Add(new WeightedNode(neighbouringNode, targetNode, closedNodes.Last())); } else { float currentParentDistance = (neighbourWeightedNode.node.transform.position - closedNodes.Last().node.transform.position).sqrMagnitude; if (neighbourWeightedNode.pathDistance > closedNodes.Last().pathDistance + currentParentDistance) { neighbourWeightedNode.parentNode = closedNodes.Last(); neighbourWeightedNode.parentDistance = currentParentDistance; neighbourWeightedNode.pathDistance = closedNodes.Last().pathDistance + currentParentDistance; } } } } WeightedNode currentNode = openNodes.First(); while (currentNode.parentNode.parentNode != null) { currentNode = currentNode.parentNode; } return(currentNode.node); }
public DijkstraDistance[] getShortestPathsFrom(int source) { var vertexes = new List <DijkstraDistance> (); DijkstraDistance[] distances = new DijkstraDistance[mVerticies.Count]; distances[source] = new DijkstraDistance(source, 0); for (int i = 0; i < distances.Length; i++) { if (i != source) { distances[i] = new DijkstraDistance(i, Int32.MaxValue); } vertexes.Add(distances[i]); } while (vertexes.Count > 0) { vertexes.Sort(); DijkstraDistance smallest = vertexes [0]; vertexes.Remove(smallest); WeightedNode n = (WeightedNode)mVerticies [smallest.mVertex]; double dist; foreach (WeightedEdge e in n.getNeighbors()) { if (n == e.getFirst()) { dist = distances [n.getIndex()].mCurrentDistance + e.getWeight(); if (dist < distances [e.getSecond().getIndex()].mCurrentDistance) { distances [e.getSecond().getIndex()].mCurrentDistance = dist; } } else { dist = distances [n.getIndex()].mCurrentDistance + e.getWeight(); if (dist < distances [e.getFirst().getIndex()].mCurrentDistance) { distances [e.getFirst().getIndex()].mCurrentDistance = dist; } } } } return(distances); }
private static void TraverseWeightedDepthFirstSearch <T>(WeightedNode <T> start, List <WeightedNode <T> > nodes, HashSet <WeightedNode <T> > visited) { nodes.Add(start); visited.Add(start); foreach (WeightedNode <T> node in start.GetNeighborsInOrder()) { if (!visited.Contains(node)) { DepthFirstSearch.TraverseWeightedDepthFirstSearch(node, nodes, visited); } } }
static void Main(string[] args) { Console.WriteLine("Hello World!"); Graph graph = new Graph(); WeightedNode n1 = new WeightedNode("1"); WeightedNode n2 = new WeightedNode("2"); WeightedNode n3 = new WeightedNode("3"); WeightedNode n4 = new WeightedNode("4"); WeightedNode n5 = new WeightedNode("5"); WeightedNode n6 = new WeightedNode("6"); WeightedNode n7 = new WeightedNode("7"); WeightedNode n8 = new WeightedNode("8"); WeightedNode n9 = new WeightedNode("9"); WeightedNode n10 = new WeightedNode("10"); graph.AddEdge(n1, n2, 2); graph.AddEdge(n1, n3, 3); graph.AddEdge(n2, n4, 4); graph.AddEdge(n4, n5, 5); graph.AddEdge(n3, n6, 6); graph.AddEdge(n1, n6, 7); graph.AddEdge(n7, n8, 8); graph.AddEdge(n7, n9, 9); graph.AddEdge(n1, n10, 10); graph.AddEdge(n4, n10, 11); graph.AddEdge(n8, n10, 12); graph.AddEdge(n3, n10, 13); Dictionary <bool, int> valuePair = graph.GetEdge(n1, "1", "10"); Console.WriteLine($"{valuePair[true]} is the value true for {n1.Value}-{n10.Value}"); Dictionary <bool, int> valuePair2 = graph.GetEdge(n1, "1", "9"); Console.WriteLine($"{valuePair2[false]} is the value false for {n1.Value}-{n9.Value}"); Graph newGraph = new Graph(); Dictionary <bool, int> notTrue = graph.GetEdge(n1, "hi", "what's up"); Console.WriteLine($"{notTrue[false]} is the value for an empty graph"); }
private void BuildDijkstraVisitation(WeightedNode node, List <WeightedNode> visitedNodes) { WeightedNode shortestPathNode = null; TWeight shortestPath = default(TWeight); foreach (var path in node.Paths) { if (!visitedNodes.Contains(path.Key)) { if (shortestPathNode == null || path.Value.CompareTo(shortestPath) <= 0) { shortestPathNode = path.Key; shortestPath = path.Value; } } } if (shortestPathNode != null) { visitedNodes.Add(shortestPathNode); this.BuildDijkstraVisitation(shortestPathNode, visitedNodes); } }
public void GetEdgesGivesCorrectResponseWithIncorrectValuesTest() { // Arrange WeightedNode n1 = new WeightedNode("Seattle"); WeightedNode n2 = new WeightedNode("New York"); WeightedNode n3 = new WeightedNode("Chicago"); WeightedNode n4 = new WeightedNode("Austin"); Graph graph = new Graph(); graph.AddEdge(n1, n2, 20); graph.AddEdge(n1, n3, 10); graph.AddEdge(n2, n3, 10); graph.AddEdge(n2, n4, 16); graph.AddEdge(n3, n4, 8); // Act Dictionary <bool, int> testTravel = graph.GetEdge(n1, "Seattle", "Austin"); // Assert Assert.Equal(new Dictionary <bool, int> { { false, 0 } }, testTravel); }
static void Main(string[] args) { Graph graph = new Graph(); WeightedNode metro = new WeightedNode("Metroville"); WeightedNode narnia = new WeightedNode("Narnia"); WeightedNode naboo = new WeightedNode("Naboo"); WeightedNode monstro = new WeightedNode("Monstropolis"); WeightedNode arend = new WeightedNode("Arendelle"); WeightedNode pand = new WeightedNode("Pandora"); graph.AddWeightedEdge(metro, narnia, 37); graph.AddWeightedEdge(metro, naboo, 26); graph.AddWeightedEdge(metro, monstro, 105); graph.AddWeightedEdge(metro, arend, 99); graph.AddWeightedEdge(metro, pand, 82); graph.AddWeightedEdge(narnia, naboo, 250); graph.AddWeightedEdge(naboo, monstro, 73); graph.AddWeightedEdge(monstro, arend, 42); graph.AddWeightedEdge(arend, pand, 150); Object result = GetEdges(graph, new string[] { "Metroville", "Pandora" }); Console.WriteLine(result); Console.WriteLine("---"); result = GetEdges(graph, new string[] { "Arendelle", "Monstropolis", "Naboo" }); Console.WriteLine(result); Console.WriteLine("---"); result = GetEdges(graph, new string[] { "Naboo", "Pandora" }); Console.WriteLine(result); Console.WriteLine("---"); result = GetEdges(graph, new string[] { "Narnia", "Arendelle", "Naboo" }); Console.WriteLine(result); Console.WriteLine("---"); }
private List <WeightedNode> GetDepthFirstVisitation() { var visitedNodes = new List <WeightedNode>(); var nodes = new Stack <WeightedNode>(); nodes.Push(this.StartNode); while (nodes.Count > 0) { WeightedNode dequeued = nodes.Pop(); visitedNodes.Add(dequeued); foreach (var path in dequeued.Paths) { if (!visitedNodes.Contains(path.Key)) { nodes.Push(path.Key); } } } return(visitedNodes); }
public Edge(double distance, WeightedNode <T> from, WeightedNode <T> to) { this.Distance = distance; this.From = from; this.To = to; }
public WeightedEdge(WeightedNode first, WeightedNode second, double weight) { mFirst = first; mSecond = second; mWeight = weight; }
public void Connect(WeightedNode endpoint1, WeightedNode endpoint2, TWeight weight) { endpoint1.Paths.Add(endpoint2, weight); endpoint2.Paths.Add(endpoint1, weight); }
public WeightedGraph getMinimumSpanningTree() { WeightedGraph mst = new WeightedGraph(6); ArrayList marked = new ArrayList(); bool added = false; int count = 0; while (!added) { WeightedNode first = (WeightedNode)mVerticies[count]; if (first.mNeighbors.Count > 0) { WeightedEdge edge = (WeightedEdge)first.mNeighbors [0]; mst.addEdge(first.getIndex(), edge.getSecond().getIndex(), edge.getWeight()); marked.Add(first.getIndex()); marked.Add(edge.getSecond().getIndex()); added = true; } } count = 1; while (count < (mVerticies.Count - 1)) { added = false; foreach (int i in marked) { WeightedNode n = (WeightedNode)mVerticies [i]; foreach (WeightedEdge e in n.mNeighbors) { if (n.getIndex() == e.getFirst().getIndex()) { if (!marked.Contains(e.getSecond().getIndex())) { marked.Add(e.getSecond().getIndex()); mst.addEdge(e.getFirst().getIndex(), e.getSecond().getIndex(), e.getWeight()); count++; added = true; } } else { if (!marked.Contains(e.getFirst().getIndex())) { marked.Add(e.getFirst().getIndex()); mst.addEdge(e.getFirst().getIndex(), e.getSecond().getIndex(), e.getWeight()); count++; added = true; } } if (added) { break; } } if (added) { break; } } } return(mst); }
public static Node GenerateHuffmanTree(FrequencyTable frequencyTable, out int height) { //TODO enhance this code (assuming that both arrays have the same lenght) /* check frequency table */ if (frequencyTable == null) { throw new ArgumentNullException(nameof(frequencyTable), "The provided frequency table is null."); } /* set initial height */ height = 0; /* the array contaning leaves of a tree */ var leaves = PackNodes(frequencyTable); Array.Sort(leaves); if (leaves.Length == 1) { return(leaves[0].node); height = 1; } if (leaves.Length == 0) { return(null); } /* the array containing internal nodes of a tree */ var internalNodes = new WeightedNode[leaves.Length - 1]; // (because Huffman tree is a full binary tree the array containg leaves-1 elements) /* fill internailNodes with null values (null means node of infinite weight) */ FillWithNull(ref internalNodes); /* the index of the current element of the leaves array */ var leavesIndex = 0; /* the index of the current element of the internalNodes array */ var internalNodesIndex = 0; var internalNodesEnd = 0; /* current sum value */ long currentSum; /* current (withing a loop iteration) min sum value */ long minSum; /* special marker variable for determining the exact two elements of the min sum */ var status = ""; for (var i = 0; i <= internalNodes.Length - 1; i++) /* while */ { height += 1; minSum = long.MaxValue; if (leavesIndex + 1 < leaves.Length) /* if two sequential elements (relating to leavesIndex) of leaves array exist */ { /* check if their sum is less than the min sum (and set it as the new min sum if so) */ currentSum = leaves[leavesIndex].weight + leaves[leavesIndex + 1].weight; if (currentSum < minSum) { minSum = currentSum; status = "leaves + leaves"; } } if (leavesIndex < leaves.Length && internalNodes[internalNodesIndex] != null) /* if current internal node's weight is not equal to infinity */ { /* check if sum of current leaf and internal node is less than the min sum (and set it as the new min sum if so) */ currentSum = leaves[leavesIndex].weight + internalNodes[internalNodesIndex].weight; if (currentSum < minSum) { minSum = currentSum; status = "leaves + internalNodes"; } } if (internalNodes[internalNodesIndex] != null && internalNodesIndex + 1 < internalNodes.Length) { /* if two sequential elements (relating to internalNodesIndex) of internalNodes array exist */ if (internalNodes[internalNodesIndex + 1] != null) { /* check if sum of current leaf and internal node is less than the min sum (and set it as the new min sum if so) */ currentSum = internalNodes[internalNodesIndex].weight + internalNodes[internalNodesIndex + 1].weight; if (currentSum < minSum) { minSum = currentSum; status = "internalNodes + internalNodes"; } } } WeightedNode w1, w2; switch (status) { case "leaves + leaves": w1 = leaves[leavesIndex]; w2 = leaves[leavesIndex + 1]; internalNodes[internalNodesEnd++] = new WeightedNode { node = new InternalNode(w1.node, w2.node), weight = w1.weight + w2.weight }; leavesIndex += 2; break; case "leaves + internalNodes": w1 = leaves[leavesIndex]; w2 = internalNodes[internalNodesIndex]; internalNodes[internalNodesEnd++] = new WeightedNode { node = new InternalNode(w1.node, w2.node), weight = w1.weight + w2.weight }; leavesIndex += 1; internalNodesIndex += 1; break; case "internalNodes + internalNodes": w1 = internalNodes[internalNodesIndex]; w2 = internalNodes[internalNodesIndex + 1]; internalNodes[internalNodesEnd++] = new WeightedNode { node = new InternalNode(w1.node, w2.node), weight = w1.weight + w2.weight }; internalNodesIndex += 2; break; default: throw new NotImplementedException(); } } return(internalNodes[internalNodes.Length - 1].node); }
void AnimateMovement(WeightedNode<MazeCell, MazeCellEdgeWeight> groundNode, WeightedNode<MazeCell, MazeCellEdgeWeight> wallNode, WeightedNode<MazeCell, MazeCellEdgeWeight> connection) { var ground1 = Maze.entities[groundNode.Data.Position.X, groundNode.Data.Position.Y]; var wall = Maze.entities[wallNode.Data.Position.X, wallNode.Data.Position.Y]; var old_pos = ground1.GetComponent<TransformComponent>().Position; var wall_transform = wall.GetComponent<TransformComponent>(); wall.GetComponent<WallComponent>().IsMoving = true; var new_ground_position = new Vector3 (wall_transform.Position.X, 0, wall_transform.Position.Z); var ground2 = EntityFactory.Instance.CreateWith ("ground_temp" + temp_counter++, GameState.MessageProxy, systems: new[] { typeof (ModelSystem), typeof (PhysicsSystem) }); var ground2_model = new ModelSceneObject ("lib/Renderer/TestGraphics/Ground/ground.xml"); ground2.GetComponent<ModelComponent>().Model = ground2_model; var transform = ground2.GetComponent<TransformComponent>(); transform.Position = old_pos; transform.Scale = ground1.GetComponent<TransformComponent>().Scale; ground2_model.Position = transform.Position; var body = new RigidBody(new BoxShape (2.0f * transform.Scale.X, 0.2f, 2.0f * transform.Scale.Y)); body.Position = transform.Position.ToJitterVector (); body.Material.Restitution = -10; body.IsStatic = true; ground2.GetComponent<PhysicsComponent>().RigidBody = body; ground2.GetComponent<PhysicsComponent>().World = GameState.PhysicsManager.World; ground2.GetComponent<PhysicsComponent>().PhysicsApplying = AffectedByPhysics.Orientation | AffectedByPhysics.Position; GameState.PhysicsManager.World.AddBody (body); GameState.Scene.AddObject(ground2_model); ground1.GetComponent<TransformComponent>().Position = new_ground_position; ground1.GetComponent<PhysicsComponent>().RigidBody.Position = new_ground_position.ToJitterVector(); var position = old_pos; position = new Vector3 (position.X, -0.5f, position.Z); Maze.entities[wallNode.Data.Position.X, wallNode.Data.Position.Y] = ground1; Maze.entities[groundNode.Data.Position.X, groundNode.Data.Position.Y] = wall; var tmp_wall_position = Maze.entities[connection.Data.Position.X, connection.Data.Position.Y] .GetComponent<TransformComponent>().Position; tmp_wall_position = new Vector3 (tmp_wall_position.X, -0.5f, tmp_wall_position.Z); wall.WakeUp(); MoveEntityTo (wall, tmp_wall_position, position, () => { GameState.Scene.RemoveObject(ground2_model); wall.GetComponent<WallComponent>().IsMoving = false; wall.Suspend(); ground2.Destroy(); if(MessageCreated != null) MessageCreated(new EndWallMovementMessage(wall)); }); }
/// <summary> /// Adds an element to the Queue /// </summary> /// <param name="element"></param> public void Enqueue(WeightedNode <T> element) { _queue.Add(element); }
public WeightedEdge(WeightedNode first, WeightedNode second, double weight) { this.mFirst = first; this.mSecond = second; this.weight = weight; }