Пример #1
0
        public T[] DepthFirstTraversal(WeightedDirectedVertex <T> startVertex)
        {
            List <T> returnList = new List <T>(VertexCount);

            Stack <WeightedDirectedVertex <T> > stack = new Stack <WeightedDirectedVertex <T> >();

            stack.Push(startVertex);

            while (stack.Count != 0)
            {
                WeightedDirectedVertex <T> currentVertex = stack.Pop();
                returnList.Add(currentVertex.Value);

                foreach (KeyValuePair <WeightedDirectedVertex <T>, float> vertex in currentVertex.Edges)
                {
                    if (returnList.Contains(vertex.Key.Value))
                    {
                        continue;
                    }

                    stack.Push(vertex.Key);
                }
            }

            return(returnList.ToArray());
        }
Пример #2
0
        public T[] BreadthFirstTraversal(WeightedDirectedVertex <T> startVertex)
        {
            List <T> returnList = new List <T>(VertexCount);

            Queue <WeightedDirectedVertex <T> > queue = new Queue <WeightedDirectedVertex <T> >();

            queue.Enqueue(startVertex);

            while (queue.Count != 0)
            {
                WeightedDirectedVertex <T> currentVertex = queue.Dequeue();
                returnList.Add(currentVertex.Value);

                foreach (KeyValuePair <WeightedDirectedVertex <T>, float> vertex in currentVertex.Edges)
                {
                    if (returnList.Contains(vertex.Key.Value))
                    {
                        continue;
                    }

                    queue.Enqueue(vertex.Key);
                }
            }

            return(returnList.ToArray());
        }
Пример #3
0
        void DepthFirstTraversalHelper(WeightedDirectedVertex <T> thisNode, T searchVal)
        {
            if (item != null)
            {
                return;
            }

            thisNode.edges.Sort((x, y) => x.Item2.CompareTo(y.Item2));
            foreach (var t in thisNode.edges)
            {
                var n = t.Item1;
                if (visited.Contains(n))
                {
                    continue;
                }
                if (n.val.CompareTo(searchVal) == 0)
                {
                    item = n;
                    return;
                }

                visited.Add(n);
                DepthFirstTraversalHelper(n, searchVal);
            }
        }
Пример #4
0
        public WeightedDirectedVertex <T> BreadthFirstTraversal(WeightedDirectedVertex <T> root, T searchVal)
        {
            visited = new List <WeightedDirectedVertex <T> >();
            Queue <WeightedDirectedVertex <T> > q = new Queue <WeightedDirectedVertex <T> >();

            q.Enqueue(root);
            while (q.Count != 0)
            {
                var n = q.Peek();
                if (n.val.CompareTo(searchVal) == 0)
                {
                    return(n);
                }
                q.Dequeue();

                n.edges.Sort((x, y) => x.Item2.CompareTo(y.Item2));
                foreach (var i in n.edges)
                {
                    if (!visited.Contains(i.Item1))
                    {
                        q.Enqueue(i.Item1);
                    }
                }
                visited.Add(n);
            }

            Console.WriteLine("hi!");
            return(null);
        }
Пример #5
0
        public void AddEdge(WeightedDirectedVertex <T> fromNode, WeightedDirectedVertex <T> toNode, float weight)
        {
            WeightedDirectedVertex <T> aVertex = vertices.Find(i => i == fromNode);
            WeightedDirectedVertex <T> bVertex = vertices.Find(i => i == toNode);

            aVertex.edges.Add(new Tuple <WeightedDirectedVertex <T>, float>(bVertex, weight));
        }
Пример #6
0
 public WeightedDirectedVertex <T> DepthFirstTraversal(WeightedDirectedVertex <T> root, T searchVal)
 {
     item    = null;
     visited = new List <WeightedDirectedVertex <T> >();
     DepthFirstTraversalHelper(root, searchVal);
     return(item);
 }
Пример #7
0
        public void RemoveVertex(WeightedDirectedVertex <T> vertex)
        {
            foreach (WeightedDirectedVertex <T> vtx in vertices)
            {
                vtx.edges.Remove(vtx.edges.Find(t => t.Item1 == vertex));
            }

            vertices.Remove(vertex);
        }
Пример #8
0
 public bool TryGetEdgeWeight(WeightedDirectedVertex <T> startValue, WeightedDirectedVertex <T> endValue, out float weight)
 {
     if (startValue.Edges.ContainsKey(endValue))
     {
         weight = startValue.Edges[endValue];
         return(true);
     }
     weight = float.NaN;
     return(false);
 }
Пример #9
0
        public void AddEdge(WeightedDirectedVertex <T> v, WeightedDirectedVertex <T> w, float weight)
        {
            if (v == null || w == null)
            {
                throw new Exception("One of the parameters passed was null");
            }

            Debug.WriteLine($"{ v.Value }, { w.Value }");

            v.Edges.Add(w, weight);
            EdgeCount++;
        }
Пример #10
0
 public WeightedDirectedVertex <T> AddVertex(WeightedDirectedVertex <T> newVertex)
 {
     vertices.Add(newVertex);
     foreach (WeightedDirectedVertex <T> i in vertices)
     {
         if (i == newVertex)
         {
             return(i);
         }
     }
     throw new Exception("Can't find the newly added Vertex!");
 }
Пример #11
0
 public bool RemoveEdge(WeightedDirectedVertex <T> v, WeightedDirectedVertex <T> w)
 {
     //and should work, maybe want or
     return(v.Edges.Remove(w) && w.Edges.Remove(v));
 }
Пример #12
0
        public WeightedDirectedVertex <T>[] GetPathBF(WeightedDirectedVertex <T> startVertex, WeightedDirectedVertex <T> endVertex)
        {
            if (startVertex is null)
            {
                throw new ArgumentNullException(nameof(startVertex));
            }

            List <(WeightedDirectedVertex <T>, int)> data = new List <(WeightedDirectedVertex <T>, int)>();

            Queue <(WeightedDirectedVertex <T>, int)> queue = new Queue <(WeightedDirectedVertex <T>, int)>();

            queue.Enqueue((startVertex, 0));

            List <WeightedDirectedVertex <T> > reversedPath = new List <WeightedDirectedVertex <T> >();

            int GetIndex(WeightedDirectedVertex <T> vertex)
            {
                for (int i = 0; i < data.Count; i++)
                {
                    if (data[i].Item1 == vertex)
                    {
                        return(i);
                    }
                }
                return(-1);
            }

            while (queue.Count > 0)
            {
                var vertex = queue.Dequeue();
                data.Add((vertex.Item1, vertex.Item2));
                if (vertex.Item1 == endVertex)
                {
                    //follow parents back up the list until -1

                    while (vertex.Item2 != GetIndex(vertex.Item1))
                    {
                        reversedPath.Add(vertex.Item1);
                        vertex = data[vertex.Item2];
                    }
                    reversedPath.Add(vertex.Item1);

                    break;
                }
                else
                {
                    //push all edges with the index of vertex.Item2

                    foreach (var item in vertex.Item1.Edges)
                    {
                        if (GetIndex(item.Key) == -1)
                        {
                            queue.Enqueue((item.Key, GetIndex(vertex.Item1)));
                        }
                    }
                }
            }

            if (reversedPath.Count == 0)
            {
                return(null);
            }

            WeightedDirectedVertex <T>[] returnList = new WeightedDirectedVertex <T> [reversedPath.Count];
            for (int i = 0; i < returnList.Length; i++)
            {
                returnList[i] = reversedPath[reversedPath.Count - 1 - i];
            }

            return(returnList);
        }
Пример #13
0
        public WeightedDirectedVertex <T>[] GetPathDF(WeightedDirectedVertex <T> startVertex, WeightedDirectedVertex <T> endVertex)
        {
            List <(WeightedDirectedVertex <T>, int)> data = new List <(WeightedDirectedVertex <T>, int)>();

            Stack <(WeightedDirectedVertex <T>, int)> stack = new Stack <(WeightedDirectedVertex <T>, int)>();

            stack.Push((startVertex, 0));

            List <WeightedDirectedVertex <T> > reversedPath = new List <WeightedDirectedVertex <T> >();

            int GetIndex(WeightedDirectedVertex <T> vertex)
            {
                for (int i = 0; i < data.Count; i++)
                {
                    if (data[i].Item1 == vertex)
                    {
                        return(i);
                    }
                }
                return(-1);
            }

            while (stack.Count > 0)
            {
                var vertex = stack.Pop();
                data.Add((vertex.Item1, vertex.Item2));
                if (vertex.Item1 == endVertex)
                {
                    //follow parents back up the list until -1

                    while (vertex.Item2 != GetIndex(vertex.Item1))
                    {
                        reversedPath.Add(vertex.Item1);
                        vertex = data[vertex.Item2];
                    }
                    reversedPath.Add(vertex.Item1);

                    break;
                }
                else
                {
                    //push all edges with the index of vertex.Item2

                    foreach (var item in vertex.Item1.Edges)
                    {
                        if (GetIndex(item.Key) == -1)
                        {
                            stack.Push((item.Key, GetIndex(vertex.Item1)));
                        }
                    }
                }
            }

            if (reversedPath.Count == 0)
            {
                return(null);
            }

            WeightedDirectedVertex <T>[] returnList = new WeightedDirectedVertex <T> [reversedPath.Count];
            for (int i = 0; i < returnList.Length; i++)
            {
                returnList[i] = reversedPath[reversedPath.Count - 1 - i];
            }

            return(returnList);
        }
Пример #14
0
 public bool HasVertex(WeightedDirectedVertex <T> vertex) => vertices.Contains(vertex);
Пример #15
0
        public List <WeightedDirectedVertex <T> > AStarFind(WeightedDirectedVertex <T> root, WeightedDirectedVertex <T> endpoint, Dictionary <WeightedDirectedVertex <T>, Vector> positions)
        {
            var vertexInfo = new Dictionary <WeightedDirectedVertex <T>, Tuple <WeightedDirectedVertex <T>, float, float> >();
            var searched   = new List <WeightedDirectedVertex <T> >();
            var q          = new List <WeightedDirectedVertex <T> >();
            WeightedDirectedVertex <T> nodeToFind;

            q.Add(root);

            vertexInfo.Add(root, new Tuple <WeightedDirectedVertex <T>, float, float>(null, 0, ManhattanHeuristic(positions[root], positions[endpoint])));

            while (q.Count != 0)
            {
                //sort q such that the vertex with the smallest cumulative cost is first
                q.Sort((x, y) => (vertexInfo[x].Item3 + vertexInfo[x].Item2).CompareTo(vertexInfo[y].Item3 + vertexInfo[y].Item2));


                var n = q[0];

                q.RemoveAt(0);

                if (n == endpoint)
                {
                    break;
                }

                n.edges.Sort((x, y) => x.Item2.CompareTo(y.Item2));
                foreach (var i in n.edges)
                {
                    //Item 1 here is the Vertex which represents neighbor
                    if (!searched.Contains(i.Item1))
                    {
                        continue;
                    }

                    q.Add(i.Item1);
                    if (vertexInfo.ContainsKey(i.Item1))
                    {
                        if (vertexInfo[i.Item1].Item2 > vertexInfo[n].Item2 + i.Item2)
                        {
                            vertexInfo[i.Item1] = new Tuple <WeightedDirectedVertex <T>, float, float>(n, vertexInfo[n].Item2 + i.Item2, vertexInfo[i.Item1].Item3);
                        }
                    }
                    else
                    {
                        vertexInfo.Add(i.Item1, new Tuple <WeightedDirectedVertex <T>, float, float>(n, vertexInfo[n].Item2 + i.Item2, ManhattanHeuristic(positions[i.Item1], positions[endpoint])));
                        if (i.Item1 == endpoint)
                        {
                            nodeToFind = i.Item1;
                        }
                    }
                }
                searched.Add(n);
            }

            var path = new List <WeightedDirectedVertex <T> >();

            while (true)
            {
                var dictEntry = vertexInfo[path[path.Count - 1]];
                if (dictEntry.Item2 == 0)
                {
                    return(path);
                }
                else
                {
                    path.Add(dictEntry.Item1);
                }
            }

            Console.WriteLine("hi!");
            return(null);
        }
Пример #16
0
        public List <WeightedDirectedVertex <T> > DijkstraFind(WeightedDirectedVertex <T> root, T searchVal)
        {
            var vertexInfo = new Dictionary <WeightedDirectedVertex <T>, Tuple <WeightedDirectedVertex <T>, float> >();
            var searched   = new List <WeightedDirectedVertex <T> >();

            var q = new List <WeightedDirectedVertex <T> >();
            WeightedDirectedVertex <T> nodeToFind;

            q.Add(root);

            vertexInfo.Add(root, new Tuple <WeightedDirectedVertex <T>, float>(null, 0));

            while (q.Count != 0)
            {
                //sort q such that the vertex with the smallest cumulative cost is first
                q.Sort((x, y) => vertexInfo[x].Item2.CompareTo(vertexInfo[y].Item2));
                var n = q[0];

                q.RemoveAt(0);

                n.edges.Sort((x, y) => x.Item2.CompareTo(y.Item2));
                foreach (var i in n.edges)
                {
                    //Item 1 here is the Vertex which represents neighbor
                    if (!searched.Contains(i.Item1))
                    {
                        q.Add(i.Item1);
                        if (vertexInfo.ContainsKey(i.Item1))
                        {
                            if (vertexInfo[i.Item1].Item2 > vertexInfo[n].Item2 + i.Item2)
                            {
                                vertexInfo[i.Item1] = new Tuple <WeightedDirectedVertex <T>, float>(n, vertexInfo[n].Item2 + i.Item2);
                            }
                        }
                        else
                        {
                            vertexInfo.Add(i.Item1, new Tuple <WeightedDirectedVertex <T>, float>(n, vertexInfo[n].Item2 + i.Item2));
                            if (i.Item1.val.CompareTo(searchVal) == 0)
                            {
                                nodeToFind = i.Item1;
                            }
                        }
                    }
                }
                searched.Add(n);

                if (q.Count == 0)
                {
                    var path = new List <WeightedDirectedVertex <T> >();
                    while (true)
                    {
                        var dictEntry = vertexInfo[path[path.Count - 1]];
                        if (dictEntry.Item2 == 0)
                        {
                            return(path);
                        }
                        else
                        {
                            path.Add(dictEntry.Item1);
                        }
                    }
                }
            }

            Console.WriteLine("hi!");
            return(null);
        }