Example #1
0
        public IList <DirectedEdge> [] PathTo(int v)
        {
            verticesToCover = new IndexedPriorityQueue <DirectedEdge>(graph.VerticesCount);
            verticesToCover.Add(v, new DirectedEdge(0, 0, 0.0));
            distTo[v] = 0;
            while (!verticesToCover.IsEmpty())
            {
                var v1 = verticesToCover.DeleteMin();
                var adjacentEdgeList = graph.Adj(v1.Key);
                foreach (var edge in adjacentEdgeList)
                {
                    //if(marked[edge.To]) continue;
                    if (distTo[edge.To] > (distTo[v1.Key] + edge.Weight))
                    {
                        distTo[edge.To]  = distTo[v1.Key] + edge.Weight;
                        marked[edge.To]  = true;
                        hasPath[edge.To] = true;
                        verticesToCover.Add(edge.To, edge);

                        foreach (var item in listOfEdges[v1.Key])
                        {
                            listOfEdges[edge.To].Add(item);
                        }
                        if (!listOfEdges[edge.To].Contains(edge))
                        {
                            listOfEdges[edge.To].Add(edge);
                        }
                    }
                }
            }
            return(listOfEdges);
        }
        public void Run()
        {
            //var iterator = graph.GetIterator(0).GetIterator().ToArray().Select(x => x as Edge).ToList();
            //iterator.Sort();
            var first = new Edge(0, 0, 0);

            minHeap.Add(0, first);
            while (!minHeap.IsEmpty())
            {
                var min = minHeap.DeleteMin();
                Console.WriteLine("Adding edge " + min);
                listOfEdges.Add(min.Value);
                Run(min.Key);
            }
            foreach (var edge in listOfEdges)
            {
                Console.Write(edge + " > ");
            }
        }