Ejemplo n.º 1
0
        //A* is the exact same impleamentation as dijkstra below except queue.Dequeue() and queue.Enqueue takes into account the extra
        //hueristic data A* star graphs have which is edge weight and an overall distance.

        public static HashSet <WeightedVertex> DijkstraShortestPath(WeightedGraph graph, int startVertex, int endVertex)
        {
            var            visited = new HashSet <WeightedVertex>();
            WeightedVertex start   = new WeightedVertex(startVertex, 0); //make start vertex with weight 0.

            if (!graph.AdjacencyList.ContainsKey(start.Vertex))
            {
                return(visited);
            }

            var heap = new MinHeap();

            heap.Add(start);

            while (heap.Count > 0)
            {
                WeightedVertex current = heap.PopMin();

                //if vistited.Contains(weightedVertex)
                if (visited.Any(x => x.Vertex == current.Vertex))
                {
                    continue;
                }

                visited.Add(current);

                if (current.Vertex == endVertex)
                {
                    break;
                }

                foreach (var neighbor in graph.AdjacencyList[current.Vertex])
                {
                    if (!visited.Any(x => x.Vertex == neighbor.Vertex))
                    {
                        heap.Add(neighbor);
                    }
                }
            }

            return(visited);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            System.Diagnostics.Stopwatch t = new System.Diagnostics.Stopwatch();
            t.Start();

            //Test
            // col {a, c, y, i, t}, word = “cat” - true
            // col {a, c, y, t, i, t}, word = “catty” - true
            // col {A, c, y, i, t}, word = “cat” - false
            // col {} || null, word = “cat” -  false (empty or null collection)
            // col {a, g, h}, word = null || empty - false
            // col {D, o, ‘ ’, h, ‘ ’, ‘ ’, g} = “   ” - True (3 space character)
            // col {D, o, ‘ ’, h, g, B, u, l, e} = “Blue Dog” - True
            // col {D, o, h, g, B, u, l, e} = “Blue Dog” - False (no space to support phrase)

            //List<char> chars = new List<char>() { 'a', 'c', 'y', 't', 'i', 't' };
            //string word = "catty";

            //Console.WriteLine(GoogleInterview1.CanBuildWord(chars, word));

            //int[] nums = new int[] { 4,5,2,8,999,92,47,42,99,-12,-5,1001,123456,9,2,2,0,3,6,11,15,4245,0,1,45,75,245,121,333 };

            //Sort.MergeSort(nums);

            //foreach (int i in nums)
            //{
            //    Console.WriteLine(i);
            //}


            //List<int> vertcies = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8 };
            //List<Edge> edges = new List<Edge>()
            //{
            //    new Edge(1,2),
            //    new Edge(1,5),
            //    new Edge(1,7),
            //    new Edge(2,3),
            //    new Edge(3,7),
            //    new Edge(7,6),
            //    new Edge(5,8),
            //    new Edge(6,4),
            //    new Edge(4,8)
            //};

            //Graph graph = new Graph(vertcies, edges);

            ////HashSet<int> path = BFS.FindShortestPath(graph, 2, 8);
            //HashSet<int> path = DFS.FindPath(graph, 2, 8);

            //foreach (var i in path)
            //    Console.WriteLine("   " + i);


            List <int> vertcies = new List <int>()
            {
                1, 2, 3, 4, 5, 6, 7, 8
            };
            List <WeightedEdge> edges = new List <WeightedEdge>()
            {
                new WeightedEdge(1, 2, 2),
                new WeightedEdge(1, 5, 2),
                new WeightedEdge(1, 7, 3),
                new WeightedEdge(2, 3, 1),
                new WeightedEdge(3, 7, 2),
                new WeightedEdge(7, 6, 1),
                new WeightedEdge(5, 8, 4),
                new WeightedEdge(6, 4, 5),
                new WeightedEdge(4, 8, 1)
            };

            WeightedGraph graph = new WeightedGraph(vertcies, edges);

            //HashSet<int> path = BFS.FindShortestPath(graph, 2, 8);
            HashSet <WeightedVertex> path = Dijkstra.DijkstraShortestPath(graph, 6, 8);

            foreach (var i in path)
            {
                Console.WriteLine("   " + i.Vertex + " -> w: " + i.Weight);
            }



            t.Stop();
            //Console.WriteLine(t.ElapsedMilliseconds);

            Console.ReadKey();
        }