Exemplo n.º 1
0
        public override bool RemoveVertex(DVertex <T> vertex)
        {
            if (!Vertices.Contains(vertex))
            {
                return(false);
            }

            for (int i = 0; i < Edges.Count; i++)
            {
                if (Edges[i].StartingPoint == vertex)
                {
                    Edges.RemoveAt(i);
                    i--;
                }
                else if (Edges[i].EndingPoint == vertex)
                {
                    Edges[i].StartingPoint.Neighbors.RemoveAt(Edges[i].StartingPoint.Neighbors.FindIndex((a) => { return(a.EndingPoint == vertex); }));
                    Edges.RemoveAt(i);
                    i--;
                }
            }

            Vertices.Remove(vertex);

            return(true);
        }
Exemplo n.º 2
0
        public void BreadthFirstTraversal(DVertex <T> start)
        {
            breadthFirstTraversal(start, new Queue <DVertex <T> >(new DVertex <T>[] { start }));

            for (int i = 0; i < Count; i++)
            {
                this[i].IsVisited = false;
            }

            void breadthFirstTraversal(DVertex <T> current, Queue <DVertex <T> > queue)
            {
                current.IsVisited = true;

                foreach (var neighbor in current.Neighbors)
                {
                    if (!neighbor.EndingPoint.IsVisited && !queue.Contains(neighbor.EndingPoint))
                    {
                        queue.Enqueue(neighbor.EndingPoint);
                    }
                }

                queue.Dequeue();

                if (queue.Count == 0)
                {
                    return;
                }

                Console.WriteLine($"\t{current.Value}->{queue.Peek().Value}");

                breadthFirstTraversal(queue.Peek(), queue);
            }
        }
Exemplo n.º 3
0
        public void DepthFirstSearch(DVertex <T> start)
        {
            depthFirstSearch(start);

            for (int i = 0; i < Count; i++)
            {
                this[i].IsVisited = false;
            }

            void depthFirstSearch(DVertex <T> current)
            {
                current.IsVisited = true;

                foreach (var neighbor in current.Neighbors)
                {
                    if (!neighbor.EndingPoint.IsVisited)
                    {
                        Console.WriteLine($"\t{current.Value}->{neighbor.EndingPoint.Value}");
                        depthFirstSearch(neighbor.EndingPoint);
                    }
                }

                return;
            }
        }
Exemplo n.º 4
0
        public override void AddVertex(DVertex <T> vertex)
        {
            if (Vertices.Contains(vertex))
            {
                throw new Exception("Vertex already exists");
            }

            Vertices.Add(vertex);
        }
Exemplo n.º 5
0
        public override bool AddEdge(DVertex <T> a, DVertex <T> b, int distance)
        {
            if (GetEdge(a, b) != null)
            {
                return(false);
            }

            a.Neighbors.Add(new Edge <T>(a, b, distance));
            Edges.Add(a.Neighbors[a.Count - 1]);
            return(true);
        }
Exemplo n.º 6
0
        public override bool RemoveEdge(DVertex <T> a, DVertex <T> b)
        {
            Edge <T> temp = GetEdge(a, b);

            if (temp == null)
            {
                return(false);
            }

            a.Neighbors.Remove(temp);
            Edges.Remove(temp);
            return(true);
        }
Exemplo n.º 7
0
        public Edge <T> GetEdge(DVertex <T> a, DVertex <T> b)
        {
            if (!(Vertices.Contains(a) && Vertices.Contains(b)))
            {
                return(null);
            }

            for (int i = 0; i < Edges.Count; i++)
            {
                if (Edges[i].StartingPoint == a && Edges[i].EndingPoint == b)
                {
                    return(Edges[i]);
                }
            }

            return(null);
        }
Exemplo n.º 8
0
 public Edge(DVertex <T> startingPoint, DVertex <T> endingPoint, int distance)
 {
     StartingPoint = startingPoint;
     EndingPoint   = endingPoint;
     Distance      = distance;
 }
Exemplo n.º 9
0
 public bool CombineVerticies(DVertex <T> a, DVertex <T> b)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 10
0
        public IEnumerable <DVertex <T> > Dijkstra(DVertex <T> start, DVertex <T> end)
        {
            if (!Vertices.Contains(start) && !Vertices.Contains(end))
            {
                return(null);
            }

            var info = new Dictionary <DVertex <T>, (DVertex <T> founder, int distance)>();

            var queue = new PriorityQueue <DVertex <T> >(Comparer <DVertex <T> > .Create((a, b) => info[a].distance.CompareTo(info[b].distance)));

            for (int i = 0; i < Count; i++)
            {
                this[i].IsVisited = false;
                info.Add(this[i], (null, int.MaxValue));
            }

            info[start] = (null, 0);
            queue.Enqueue(start);

            while (!queue.IsEmpty())
            {
                var vertex = queue.Dequeue();
                vertex.IsVisited = true;

                //if infinite graph, break if you just popped the end

                //find tentative distances
                foreach (var edge in vertex.Neighbors)
                {
                    var neighbor  = edge.EndingPoint;
                    int tentative = edge.Distance + info[vertex].distance;
                    if (tentative < info[neighbor].distance)
                    {
                        info[neighbor]     = (vertex, tentative);
                        neighbor.IsVisited = false;
                    }

                    //if they are not in the queue AND have not been visited, add them to the queue
                    if (!queue.Contains(neighbor) && !neighbor.IsVisited)
                    {
                        queue.Enqueue(neighbor);
                    }
                }
            }

            //start at end and build stack of founders back to beginning
            Stack <DVertex <T> > stack = new Stack <DVertex <T> >();

            var current = info.ElementAt(info.Count - 1);

            while (true)
            {
                stack.Push(current.Key);

                if (current.Value.founder == null)
                {
                    break;
                }

                current = new KeyValuePair <DVertex <T>, (DVertex <T> founder, int distance)>(current.Value.founder, info[current.Value.founder]);
            }

            return(stack);
        }