private readonly decimal[] _distTo; // distTo[v] = distance  of shortest s->v path

        #endregion Fields

        #region Constructors

        /**
         * Computes a shortest paths tree from <tt>s</tt> to every other vertex in
         * the edge-weighted digraph <tt>G</tt>.
         * @param G the edge-weighted digraph
         * @param s the source vertex
         * @throws IllegalArgumentException if an edge weight is negative
         * @throws IllegalArgumentException unless 0 &le; <tt>s</tt> &le; <tt>V</tt> - 1
         */
        public ShortestNeuronPath(NeuronGraph G, int s)
        {
            _distTo = new decimal[G.V()];
            edgeTo = new DirectedEdge[G.V()];
            for (int v = 0; v < G.V(); v++)
                _distTo[v] = decimal.MaxValue;
            _distTo[s] = 0.0M;

            // relax vertices in order of distance from s
            pq = new IndexMinPQ<decimal>(G.V());
            pq.Insert(s, _distTo[s]);
            while (!pq.IsEmpty())
            {
                int v = pq.DelMin();
                foreach (DirectedEdge e in G.adj(v))
                    relax(e);
            }
        }
Beispiel #2
0
        private Queue<Int32> queue; // queue of vertices to relax

        #endregion Fields

        #region Constructors

        public BellmanFordSP(NeuronGraph G, int s)
        {
            distTo  = new decimal[G.V()];
            edgeTo  = new DirectedEdge[G.V()];
            onQueue = new Boolean[G.V()];
            for (int v = 0; v < G.V(); v++)
            distTo[v] = decimal.MaxValue;
            distTo[s] = 0.0M;

            // Bellman-Ford algorithm
            queue = new Queue<Int32>();
            queue.Enqueue(s);
            onQueue[s] = true;
            while (queue.Count != 0) {
            int v = queue.Dequeue();
               // Console.WriteLine("Relaxing edge {0}", v);

            onQueue[v] = false;
            relax(G, v);
            }
        }
Beispiel #3
0
        static void Main(String[] args)
        {
            var reader = new LRNReader("data.lrn");
            var songs = reader.ReadSongs().ToArray();
            Console.WriteLine("LRN file read...");
            var bestMatches = ESOMDataWrapper.BestMatches.FromFile("bigmap.bm");
            Console.WriteLine("BM file read...");
            var ESOM = ESOMDataWrapper.ESOM.Fromfile("bigmap.wts");
            var UMat = ESOMDataWrapper.UMatrix.FromFile("bigmap.umx");
            Console.WriteLine("UMatrix file read...");
            var graph = new NeuronGraph(UMat.Heights, 0);
            Console.WriteLine("Built read...");
            int from = 23;
            var fromCoord = bestMatches.Index2BestMatch[from];
            int translatefrom = graph.translate(fromCoord);

               // var shortestPath = new ShortestNeuronPath(graph, translatefrom);
            var shortestPath = new ShortestNeuronPath(graph, translatefrom);
            // Console.Write("Bellman Ford completed...");
            var tocoord = bestMatches.Index2BestMatch[296];
            var to = graph.translate(tocoord);
            // Console.Write(shortestPath.HasPathTo(to));
            var result = shortestPath.HasPathTo(to) ? shortestPath.pathTo(to) : null;

            Song source = songs[from];

            double accumulatedDistance = 0.0;

            List<Song> resultSongs = new List<Song>();

            foreach (var directedEdge in result)
            {
                var coord = graph.translate(directedEdge.@from());
                // Console.Out.Write(directedEdge.@from());
                if (bestMatches.BestMatch2Index.ContainsKey(coord))
                {

                    //var resultint = bestMatches.BestMatch2Index[coord].First();
                    //Console.Out.WriteLine("Length on BM list:" + bestMatches.BestMatch2Index[coord].Count);
                    var next = KNNBestMatches.FindKBestMatches(songs, bestMatches.BestMatch2Index[coord], source, 1);
                    source = next.FirstOrDefault() ?? source;
                    resultSongs.Add(source);
                    if (next.Count >= 1)
                    {
                        foreach (var resultint in bestMatches.BestMatch2Index[coord])
                        {
                           Console.Out.WriteLine("Song Name: {0} ... Artist Name: {1}",
                            songs[resultint].TrackName, songs[resultint].ArtistName);
                        }
                    }

                }

            }
            double min = Double.MaxValue;
            double max = 0.0;
            for (int i = 0; i < resultSongs.Count - 1; i++)
            {
                double dist = Utilities.EuclideanDistance.Distance(resultSongs[i], resultSongs[i + 1]);
                if (dist > max) max = dist;
                if (dist < min && dist != 0) min = dist;
                accumulatedDistance += dist;
            }
            var avgdist = accumulatedDistance / resultSongs.Count;
            Console.Out.WriteLine("Total euclidean distance: {0} and average distance: {1} ", accumulatedDistance, avgdist);
            Console.Out.WriteLine("Max distance: {0}... Min distance: {1}", max, min);
        }
Beispiel #4
0
 // relax vertex v and put other endpoints on queue if changed
 private void relax(NeuronGraph G, int v)
 {
     foreach(DirectedEdge e in G.adj(v)) {
     int w = e.to();
     decimal wdist = distTo[w];
     decimal vdist = distTo[v];
     if (distTo[w] > distTo[v] + e.weight()) {
      //   Console.WriteLine("LOL");
         distTo[w] = distTo[v] + e.weight();
         edgeTo[w] = e;
         if (w == 2000)
         {
             Console.WriteLine(distTo[w]);
         }
         if (!onQueue[w]) {
             queue.Enqueue(w);
             onQueue[w] = true;
         }
     }
     if (cost++%G.V() == 0)
     {
        // Console.Write("Cost is {0} and we're hitting this, finding negative cycle", cost);
         findNegativeCycle();
     }
     }
 }