Exemple #1
0
        void TestDirectedGraph()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("Begin Testing Directed Graph").AppendLine();

            RGVertex <string> V1 = new RGVertex <string>("v1");
            RGVertex <string> V2 = new RGVertex <string>("v2");
            RGVertex <string> V3 = new RGVertex <string>("v3");
            RGVertex <string> V4 = new RGVertex <string>("v4");

            List <RGVertex <string> > Vertices = new List <RGVertex <string> > {
                V1, V2, V3, V4
            };

            RGGraph <string> graph = new RGGraph <string>(Vertices);

            graph.CreateDirectedEdge(V1, V2, 3);
            graph.CreateDirectedEdge(V4, V1, 1);
            graph.CreateDirectedEdge(V2, V3, 1);
            graph.CreateDirectedEdge(V2, V4, -5);

            sb.Append(graph.ToString()).AppendLine();

            sb.Append("End of Testing Directed Graph").AppendLine();

            debugClass.WriteStringToFile(DirectedGraphFile, sb.ToString());
        }
Exemple #2
0
        public static void Dijkstra <T>(RGGraph <T> graph, RGVertex <T> source)
        {
            List <RGVertex <T> > unfinishedVertices = new List <RGVertex <T> >();

            foreach (RGVertex <T> vertex in graph.Vertices)
            {
                vertex.Distance = int.MaxValue;
                vertex.Parent   = null;
                unfinishedVertices.Add(vertex);
            }
            source.Distance = 0;
            while (unfinishedVertices.Count > 0)
            {
                RGVertex <T> vertex = GetClosestVertex(unfinishedVertices);
                unfinishedVertices.Remove(vertex);
                foreach (RGVertex <T> adjVertex in graph.GetAdjacentVertices(vertex))
                {
                    if (adjVertex.Distance > vertex.Distance + graph.GetEdgeWeight(vertex, adjVertex))
                    {
                        adjVertex.Distance = vertex.Distance + graph.GetEdgeWeight(vertex, adjVertex);
                        adjVertex.Parent   = vertex;
                    }
                }
            }
        }
Exemple #3
0
        public static void BreadthFirstSearch <T>(RGGraph <T> graph, RGVertex <T> sourceVertex)
        {
            foreach (RGVertex <T> vertex in graph.Vertices)
            {
                vertex.Parent   = null;
                vertex.Distance = 0;
                vertex.Visited  = false;
            }

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

            queue.Enqueue(sourceVertex);

            while (queue.Count > 0)
            {
                RGVertex <T> vertex = queue.Dequeue();
                foreach (RGVertex <T> neighbour in graph.GetAdjacentVertices(vertex))
                {
                    if (!neighbour.Visited)
                    {
                        neighbour.Parent   = vertex;
                        neighbour.Distance = vertex.Distance + graph.GetEdgeWeight(vertex, neighbour);
                        neighbour.Visited  = true;

                        queue.Enqueue(neighbour);
                    }
                }

                vertex.Visited = true;
            }
        }
Exemple #4
0
        public static List <RGVertex <T> > DepthFirstSearchWithGoal <T>(RGGraph <T> graph, RGVertex <T> sourceVertex, RGVertex <T> goalVertex, bool reverseNeighbours = false)
        {
            if (sourceVertex.Equals(goalVertex))
            {
                return(new List <RGVertex <T> > {
                    sourceVertex
                });
            }

            foreach (RGVertex <T> vertex in graph.Vertices)
            {
                vertex.Parent   = null;
                vertex.Distance = 0;
                vertex.Visited  = false;
            }

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

            stack.Push(sourceVertex);


            while (stack.Count > 0)
            {
                RGVertex <T> vertex = stack.Pop();

                List <RGVertex <T> > neighbours = graph.GetAdjacentVertices(vertex);
                if (reverseNeighbours)
                {
                    neighbours.Reverse();
                }
                foreach (RGVertex <T> neighbour in neighbours)
                {
                    if (!neighbour.Visited)
                    {
                        neighbour.Parent   = vertex;
                        neighbour.Distance = vertex.Distance + graph.GetEdgeWeight(vertex, neighbour);
                        neighbour.Visited  = true;

                        if (neighbour.Equals(goalVertex))
                        {
                            return(GetPathToSource <T>(neighbour));
                        }

                        stack.Push(neighbour);
                    }
                }
                vertex.Visited = true;
            }

            // no path found
            return(null);
        }
Exemple #5
0
        public static List <RGVertex <T> > GetPathToSource <T>(RGVertex <T> from)
        {
            List <RGVertex <T> > path = new List <RGVertex <T> >();
            RGVertex <T>         next = from;

            while (next != null)
            {
                path.Add(next);
                next = next.Parent;
            }

            return(path);
        }
Exemple #6
0
        private static RGVertex <T> GetClosestVertex <T>(List <RGVertex <T> > list)
        {
            RGVertex <T> candidate = list[0];

            foreach (RGVertex <T> vertex in list)
            {
                if (vertex.Distance < candidate.Distance)
                {
                    // I can use binomial heap if required
                    candidate = vertex;
                }
            }

            return(candidate);
        }
Exemple #7
0
        public static List <RGVertex <T> > BreadthFirstSearchWithGoal <T>(RGGraph <T> graph, RGVertex <T> sourceVertex, RGVertex <T> goalVertex)
        {
            if (sourceVertex.Equals(goalVertex))
            {
                return(new List <RGVertex <T> > {
                    sourceVertex
                });
            }
            foreach (RGVertex <T> vertex in graph.Vertices)
            {
                vertex.Parent   = null;
                vertex.Distance = 0;
                vertex.Visited  = false;
            }

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

            queue.Enqueue(sourceVertex);

            while (queue.Count > 0)
            {
                RGVertex <T> vertex = queue.Dequeue();

                foreach (RGVertex <T> neighbour in graph.GetAdjacentVertices(vertex))
                {
                    if (!neighbour.Visited)
                    {
                        neighbour.Parent   = vertex;
                        neighbour.Distance = vertex.Distance + graph.GetEdgeWeight(vertex, neighbour);
                        neighbour.Visited  = true;

                        if (neighbour.Equals(goalVertex))
                        {
                            return(GetPathToSource <T>(neighbour));
                        }

                        queue.Enqueue(neighbour);
                    }
                }
                vertex.Visited = true;
            }

            // no path to goal vertex
            return(null);
        }
Exemple #8
0
        public static List <RGVertex <T> > DijkstraWithGoal <T>(RGGraph <T> graph, RGVertex <T> source, RGVertex <T> goal)
        {
            if (source.Equals(goal))
            {
                return(new List <RGVertex <T> > {
                    source
                });
            }

            List <RGVertex <T> > unfinishedVertices = new List <RGVertex <T> >();

            foreach (RGVertex <T> vertex in graph.Vertices)
            {
                vertex.Distance = int.MaxValue;
                vertex.Parent   = null;
                unfinishedVertices.Add(vertex);
            }
            source.Distance = 0;
            while (unfinishedVertices.Count > 0)
            {
                RGVertex <T> vertex = GetClosestVertex(unfinishedVertices);
                unfinishedVertices.Remove(vertex);
                if (vertex.Equals(goal))
                {
                    return(GetPathToSource(vertex));
                }
                foreach (RGVertex <T> adjVertex in graph.GetAdjacentVertices(vertex))
                {
                    if (adjVertex.Distance > vertex.Distance + graph.GetEdgeWeight(vertex, adjVertex))
                    {
                        adjVertex.Distance = vertex.Distance + graph.GetEdgeWeight(vertex, adjVertex);
                        adjVertex.Parent   = vertex;
                    }
                }
            }
            return(null);
        }
Exemple #9
0
        // DFS BEGINS

        public static void DepthFirstSearch <T>(RGGraph <T> graph, RGVertex <T> sourceVertex, bool reverseNeighbours = false)
        {
            foreach (RGVertex <T> vertex in graph.Vertices)
            {
                vertex.Parent   = null;
                vertex.Distance = 0;
                vertex.Visited  = false;
            }


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

            stack.Push(sourceVertex);

            while (stack.Count > 0)
            {
                RGVertex <T> vertex = stack.Pop();

                List <RGVertex <T> > neighbours = graph.GetAdjacentVertices(vertex);
                if (reverseNeighbours)
                {
                    neighbours.Reverse();
                }
                foreach (RGVertex <T> neighbour in neighbours)
                {
                    if (!neighbour.Visited)
                    {
                        neighbour.Parent   = vertex;
                        neighbour.Distance = vertex.Distance + graph.GetEdgeWeight(vertex, neighbour);
                        neighbour.Visited  = true;

                        stack.Push(neighbour);
                    }
                }
                vertex.Visited = true;
            }
        }
Exemple #10
0
 public float GetEdgeWeight(RGVertex <T> V1, RGVertex <T> V2)
 {
     return(_adjacencyMatrix.GetEdgeWeight(V1.Index, V2.Index));
 }
Exemple #11
0
 public List <RGVertex <T> > GetAdjacentVertices(RGVertex <T> source)
 {
     return(GetAdjacentVertices(source.Index));
 }
Exemple #12
0
 public void CreateUnDirectedEdge(RGVertex <T> from, RGVertex <T> to, float weight = 1)
 {
     this.CreateUnDirectedEdge(from.Index, to.Index, weight);
 }
Exemple #13
0
        void TestBFS()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("Begin BFS test").AppendLine();

            RGVertex <string> V1 = new RGVertex <string>("V1");
            RGVertex <string> V2 = new RGVertex <string>("V2");
            RGVertex <string> V3 = new RGVertex <string>("V3");
            RGVertex <string> V4 = new RGVertex <string>("V4");
            RGVertex <string> V5 = new RGVertex <string>("V5");
            RGVertex <string> V6 = new RGVertex <string>("V6");
            RGVertex <string> V7 = new RGVertex <string>("V7");

            List <RGVertex <string> > vertices = new List <RGVertex <string> >
            {
                V1, V2, V3, V4, V5, V6, V7
            };

            RGGraph <string> graph = new RGGraph <string>(vertices);

            graph.CreateUnDirectedEdge(V4, V5);
            graph.CreateUnDirectedEdge(V4, V2);
            graph.CreateUnDirectedEdge(V4, V1);
            graph.CreateUnDirectedEdge(V5, V6);
            graph.CreateUnDirectedEdge(V2, V5);
            graph.CreateUnDirectedEdge(V2, V7);
            graph.CreateUnDirectedEdge(V2, V1);
            graph.CreateUnDirectedEdge(V1, V3);
            graph.CreateUnDirectedEdge(V1, V7);
            graph.CreateUnDirectedEdge(V7, V6);

            string graphData = graph.ToString();

            sb.Append(graphData).AppendLine();

            sb.Append("Running BFS from : ").Append(V4.ToString()).AppendLine();
            RGSearchAlgorithms.BreadthFirstSearch <string>(graph, V4);

            List <RGVertex <string> > fromV6 = RGSearchAlgorithms.GetPathToSource <string>(V6);

            sb.Append("Start vertex : V6").AppendLine();
            foreach (RGVertex <string> vertex in fromV6)
            {
                sb.Append(vertex.ToString());
            }

            sb.AppendLine();

            List <RGVertex <string> > path = RGSearchAlgorithms.BreadthFirstSearchWithGoal <string>(graph, V6, V3);

            sb.Append("Path from v3 to v6").AppendLine();
            foreach (RGVertex <string> vertex in path)
            {
                sb.Append(vertex.ToString());
            }

            sb.AppendLine();
            sb.Append("End BFS test").AppendLine();

            debugClass.WriteStringToFile(BFSFile, sb.ToString());
        }