示例#1
0
        public void Add_VertexNode_ReturnBool()
        {
            adjList.Add("A");
            adjList.Add("B");
            adjList.Add("C");
            adjList.Add("D");
            adjList.Add("E");

            Assert.AreEqual(true, adjList.Contains(adjList[0]));
            Assert.AreEqual(true, adjList.Contains(adjList.Find("A")));
        }
示例#2
0
    private static bool AStar(AdjacencyList <Vector3> graph, Vector3 start, Vector3 goal, out List <Vector3> path)
    {
        //make sure that the start and end points are valid points in the graph
        if (!graph.Contains(start) || !graph.Contains(goal))
        {
            path = new List <Vector3>();
            return(false);
        }

        bool success = false;
        Dictionary <Vector3, Vector3> from   = new Dictionary <Vector3, Vector3>();
        HashSet <Vector3>             closed = new HashSet <Vector3>();
        HashSet <Vector3>             open   = new HashSet <Vector3> {
            start
        };

        Dictionary <Vector3, float>          gScore = new Dictionary <Vector3, float>();
        SimplePriorityQueue <Vector3, float> fScore = new SimplePriorityQueue <Vector3, float>();

        gScore.Add(start, 0);
        fScore.Enqueue(start, Vector3.Distance(start, goal));

        while (open.Count > 0)
        {
            Vector3 current = fScore.Dequeue();
            if (current == goal)
            {
                success = true;
                break;
            }

            open.Remove(current);
            closed.Add(current);

            foreach (Vector3 neighbor in graph.FindNeighbours(current))
            {
                if (closed.Contains(neighbor))
                {
                    continue;
                }

                float dist = gScore[current] + Vector3.Distance(current, neighbor);

                if (!open.Contains(neighbor))
                {
                    open.Add(neighbor);
                }
                else if (gScore.ContainsKey(neighbor) && dist >= gScore[neighbor])
                {
                    continue;
                }

                from[neighbor]   = current;
                gScore[neighbor] = dist;
                fScore.Enqueue(neighbor, dist + Vector3.Distance(neighbor, goal));
            }
        }

        path = FormPath(from, goal);
        return(success);
    }
示例#3
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="node"></param>
 /// <returns></returns>
 public bool ContainsAdjacentNode(INode <KEY> node)
 {
     return(AdjacencyList.Contains(node));
 }
示例#4
0
        public void AddEdge(string name1, string name2, int length)
        {
            if (list.Contains(name1) && list.Contains(name2))
            {
                Vertex first  = list.GetVertex(name1);
                Vertex second = list.GetVertex(name2);

                if (list[first, second] == null)
                {
                    Edge edge = new Edge(second, length);
                    list.AddLink(first, edge);
                }
            }
            else
            {
                throw new VertexDoesNotExistException();
            }
        }