Exemple #1
0
        /// <summary>
        /// Uses the Dijkstra Search Algorithm to find from one Vertex to another in the given Graph.
        /// </summary>
        /// <param name="graph"></param>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <returns></returns>
        public static Vertex[] Search(Graph g, Vertex from, Vertex to)
        {
            //Initialize
            Dictionary <Vertex, int>    distances   = new Dictionary <Vertex, int>();
            Dictionary <Vertex, Vertex> predecessor = new Dictionary <Vertex, Vertex>();

            bool found = false;

            distances.Add(from, 0);
            predecessor.Add(from, from);

            foreach (Vertex v in g.Vertices)
            {
                if (!v.Equals(from))
                {
                    distances.Add(v, Int32.MaxValue);
                    predecessor.Add(v, null);
                }
            }

            PriorityQueue <Vertex> q = new PriorityQueue <Vertex>();

            q.Insert(from, 0);

            //Algorithm
            while (q.Count != 0)
            {
                Vertex u = q.PopLowest();

                //Goal Vertex found
                if (u.Equals(to))
                {
                    found = true;
                    break;
                }

                //Find all neighbors of u
                Vertex[] neighbors = GraphHelper.FindAdjacentVertices(g, u);

                //Analyse the neighbors and update their distances and predecessors accordingly
                foreach (Vertex v in neighbors)
                {
                    if (predecessor[v] != null)
                    {
                        //update distance
                        if (distances[u] + 1 < distances[v])
                        {
                            distances[v]   = distances[u] + 1;
                            predecessor[v] = u;
                            q.InsertOverride(v, distances[u] + 1);
                        }
                    }
                    else
                    {
                        distances[v]   = distances[u] + 1;
                        predecessor[v] = u;
                        q.InsertOverride(v, distances[u] + 1);
                    }
                }
            }

            if (found)
            {
                return(GetPath(predecessor, to));
            }
            else
            {
                return(null);
            }
        }
Exemple #2
0
        private static void Main(string[] args)
        {
            GraphHelper graphHelper = new GraphHelper();

            Graph graph  = new Graph(GraphTypeEnum.Empty, 10);
            Graph graph2 = new Graph(GraphTypeEnum.Complete, 10);
            Graph graph3 = new Graph(GraphTypeEnum.Random, 4);



            graphHelper.DepthFirstSearch(graph2);


            //2,4
            //2,3,4
            //0,1,3
            //1,2,4
            //0,1,3

            Graph andreea = new Graph(GraphTypeEnum.Empty, 5);

            andreea.AdjacencyLists[0].Add(andreea.Nodes[2]);
            andreea.AdjacencyLists[0].Add(andreea.Nodes[4]);

            andreea.AdjacencyLists[1].Add(andreea.Nodes[2]);
            andreea.AdjacencyLists[1].Add(andreea.Nodes[3]);
            andreea.AdjacencyLists[1].Add(andreea.Nodes[4]);

            andreea.AdjacencyLists[2].Add(andreea.Nodes[0]);
            andreea.AdjacencyLists[2].Add(andreea.Nodes[1]);
            andreea.AdjacencyLists[2].Add(andreea.Nodes[3]);

            andreea.AdjacencyLists[3].Add(andreea.Nodes[1]);
            andreea.AdjacencyLists[3].Add(andreea.Nodes[2]);
            andreea.AdjacencyLists[3].Add(andreea.Nodes[4]);

            andreea.AdjacencyLists[4].Add(andreea.Nodes[0]);
            andreea.AdjacencyLists[4].Add(andreea.Nodes[1]);
            andreea.AdjacencyLists[4].Add(andreea.Nodes[3]);

            graphHelper.DepthFirstSearch(andreea);
            graphHelper.GetConnectedComponents(andreea);
            graphHelper.Euler(andreea);
            graphHelper.Hamilton(andreea);

            Graph graph4 = new Graph(GraphTypeEnum.Empty, 4);

            graph4.AdjacencyLists[0].Add(graph4.Nodes[1]);
            graph4.AdjacencyLists[1].Add(graph4.Nodes[0]);
            graph4.AdjacencyLists[1].Add(graph4.Nodes[2]);
            graph4.AdjacencyLists[2].Add(graph4.Nodes[1]);
            graph4.AdjacencyLists[2].Add(graph4.Nodes[3]);
            graph4.AdjacencyLists[3].Add(graph4.Nodes[2]);

            graphHelper.DepthFirstSearch(graph4);
            graphHelper.Euler(graph4);

            Graph graph5 = new Graph(GraphTypeEnum.Empty, 4);

            graph5.AdjacencyLists[0].Add(graph5.Nodes[1]);
            graph5.AdjacencyLists[0].Add(graph5.Nodes[3]);
            graph5.AdjacencyLists[1].Add(graph5.Nodes[0]);
            graph5.AdjacencyLists[1].Add(graph5.Nodes[2]);
            graph5.AdjacencyLists[2].Add(graph5.Nodes[1]);
            graph5.AdjacencyLists[2].Add(graph5.Nodes[3]);
            graph5.AdjacencyLists[3].Add(graph5.Nodes[2]);
            graph5.AdjacencyLists[3].Add(graph5.Nodes[0]);

            graph5.GetEdges();
            graphHelper.DepthFirstSearch(graph5);
            graphHelper.Euler(graph5);
            graphHelper.Hamilton(graph5);

            graph3.GetNumberOfEdges();

            for (var i = 0; i < graph3.Nodes.Count; i++)
            {
                for (int j = 0; j < graph3.Nodes.Count; j++)
                {
                    Console.Write(graph3.GetAdjacentyMatrix()[i, j] + " ");
                }

                Console.WriteLine();
            }

            graphHelper.DepthFirstSearch(graph3);

            graph3.ConnectedComponents = graphHelper.GetConnectedComponents(graph3);

            foreach (Edge graph3Bridge in graph3.Bridges)
            {
                Console.WriteLine(
                    graph3Bridge.ToString());
            }

            graphHelper.Euler(graph3);
            graphHelper.Hamilton(graph3);
        }