Beispiel #1
0
 public void Should_not_allow_creating_heap_with_null_collection()
 {
     Assert.Throws <ArgumentNullException>(() =>
     {
         // ReSharper disable once UnusedVariable
         var heap = new ArrayHeap <int, int>(Comparer <int> .Default, 2, null);
     });
 }
Beispiel #2
0
 /// <summary>
 /// 释放资源
 /// </summary>
 public void Dispose()
 {
     if (taskHeap != null)
     {
         taskHeap.Dispose();
         taskHeap = null;
     }
 }
Beispiel #3
0
        public void Should_not_allow_creating_heap_with_arity_less_than_1()
        {
            Assert.Throws <ArgumentOutOfRangeException>(() =>
            {
                // ReSharper disable once UnusedVariable
                var heap = new ArrayHeap <int, string>(Comparer <int> .Default, 0);
            });

            // TODO: add does not throw
            var heap2 = new ArrayHeap <int, string>(Comparer <int> .Default, 1);
        }
Beispiel #4
0
        //Deikstra Shortest paths from vertex.
        public CSharpDataStructures.Structures.Lists.LinkedList <Int32> DSP(Int32 vertex)
        {
            CSharpDataStructures.Structures.Lists.LinkedList <Int32> paths = new  CSharpDataStructures.Structures.Lists.LinkedList <Int32>();
            Double[] D = new Double[_n];
            Int32[]  P = new Int32[_n];
            ArrayHeap <GraphNode <T> > Q = new ArrayHeap <GraphNode <T> >(x => x.Weight);

            for (Int32 i = vertex + 1; i <= _n; i++)
            {
                D[i - 1] = ((GraphNode <T>)_adj[0][i]).Weight;
                Q.Add((GraphNode <T>)_adj[i - 1][1]);
            }


            for (Int32 i = 1; i < _n; i++)
            {
                GraphNode <T> w = Q.DeleteMin();
                Int32         vi;
                Int32         wi;
                if (Int32.TryParse(w.Name, out wi))
                {
                    foreach (GraphNode <T> v in Q)
                    {
                        if (Int32.TryParse(v.Name, out vi))
                        {
                            if (D[wi - 1] + v.Weight < D[vi - 1])
                            {
                                P[vi - 1] = wi;
                                D[vi - 1] = D[wi - 1] + v.Weight;
                            }
                        }
                    }
                }
            }
            paths.Add(_n - 1);
            for (Int32 j = _n - 1; j > 1; j = P[j])
            {
                paths.Add(P[j]);
            }

            return(paths);
        }
        public static void DisplayArrayHeap()
        {
            ArrayHeap <char> heap = new ArrayHeap <char>(100);

            heap.InsertValue('A', 1);
            heap.InsertValue('B', 2);
            heap.InsertValue('C', 3);

            Console.WriteLine(heap.Delete());

            heap.InsertValue('A', 1);
            heap.InsertValue('B', 2);
            heap.InsertValue('C', 3);

            Console.WriteLine(heap.Delete());

            while (!heap.IsEmpty())
            {
                Console.WriteLine(heap.Delete());
            }
        }
Beispiel #6
0
        public void Execute()
        {
            Func <Int32, Int32> f = P;

            Console.WriteLine("\n---- Heap Sort(Integers) ----");
            ArrayHeap <Int32> tree = new ArrayHeap <Int32>((it) => it);

            //Algorithm O(N*logN)
            Int32[] arrayToS = new Int32[] { 3, 2, 7, 1, 8, 9, 4, 5, 13, 2, 4, 5, 7 }; //for x in L
            for (Int32 i = 0; i < arrayToS.Length; i++)                                //INSERT(x,S)
            {
                tree.Add(arrayToS[i]);
            }

            Console.WriteLine("Sorting...");
            while (tree.GetCount() != 0)    //while NOT EMPTY(S)
            {
                Int32 y = tree.DeleteMin(); //DELETE(y) and RETURN MIN(y)
                Console.Write(y + " ");
            }
            Console.WriteLine("\n");
        }
Beispiel #7
0
        static void Main(string[] args)
        {
            //Console.WriteLine("Input link: ");
            //String s = Console.ReadLine();
            //Console.WriteLine("Input Directory to save: ");
            //String d = Console.ReadLine();

            String startupPath = System.IO.Directory.GetCurrentDirectory();

            //startupPath = Path.Combine(startupPath, d);


            dsFiles = Path.Combine(startupPath, "Data");//Path to source files.
            //String dict = Path.Combine(startupPath,"LDict.txt");//Dictionary of lemmas

            /* STEP 1. PARSE HTML
             * ParserWorker<String[]> parser = new ParserWorker<String[]>(new NekdoParser());
             *
             * parser.SetSettings(new NekdoSettings(1,100));
             * parser.OnNewData += NewData;
             * parser.OnComplete += Complete;
             * dir = new DirectoryInfo(startupPath);
             * try{
             *  dir.Create();
             * }
             * catch(IOException){
             *  Console.WriteLine("This directory has already exist. Continue work with this directory");
             * }
             * parser.Start();
             * while(parser.IsActive()){//awaiting parser...
             *
             * }
             *
             *
             *
             * CreateIndexF(parser.GetUrls());
             */


            //STEP 2 STEMMING

            /*
             * TrainDataParser TDP = new TrainDataParser();
             *
             *
             * Lemmatization(TDP);
             *
             * Console.WriteLine("");
             */
            //STEP 3 CREATING INDEX.
            String indexFileP = Path.Combine(startupPath, "Indexer", "inventIndex.txt");

            Console.WriteLine("===STEP 3 ===");

            IndexBuilder builder = new IndexBuilder();

            Console.WriteLine("Source: {0} ", builder.Source);
            Console.WriteLine("Dest: {0}", indexFileP);


            LinkedDictionary <String, IndexEntry> indexer = builder.ReadData();//INDEX


            // UNCOMMENT FOR VECTOR RETRIEVAL (STEP 5)

            foreach (KeyValuePair <String, IndexEntry> p in indexer)
            {
                Double I = Math.Round(100.0 / p.Value.Ids.Count, 5);
                p.Value.IDF = I;//Math.Log(100.0/p.Value.Ids.Count, 10.0);

                foreach (Double prob in p.Value.Probs)
                {
                    p.Value.Weights.Add(prob * I); //tf(t,d)*idf(t,D) = tf-idf(t,d,D)
                }
                //String data = p.Key +" : "+ p.Value;
                //__CreateIFile(indexFileP, data);//read Data from indexer to file.
            }

            Console.WriteLine("Done.");



            IStemmer         stem   = new RussianStemmer();                                    //STEMMER
            BoolSyntaxParser bp     = new BoolSyntaxParser();                                  //PARSER OF BOOLEAN EXPRESSIONS
            ILemmatizer      lemmer = new LemmatizerPrebuiltCompact(LanguagePrebuilt.Russian); //LEMMATIZER.


            //STEP 4. BOOLEAN SEARCH BY(indexer)

            /*
             * while(true){
             *  Console.WriteLine("Input search str...");
             *  String ui = Console.ReadLine();
             *
             *  String[] u = ui.ToLower().Replace('ё','е').Split(new Char[]{' ' , ',', '.', ';', '-', ':','?','!','\"'},StringSplitOptions.RemoveEmptyEntries);
             *  LinkedStack<String> ui_w =  bp.GetInput(u);//GET EXPRESSION IN POLISH NOTATION
             *
             *  String[] ui_wa = ui_w.ToArray();//SAVE IT INTO ARRAY
             *  foreach(String it2 in ui_wa){
             *      Console.WriteLine(it2);
             *  }
             *  SimpleTextCrawler.Structures.LinkedList<Int32> idsOf = __GetIds(lemmer, indexer, ui_wa);
             *  __FindLinks(idsOf);
             *
             * }*/


            //STEP 5 Vector SEARCH BY(indexer).

            ArrayHeap <HeapEntry> PQ = new ArrayHeap <HeapEntry>(x => x.Relevance);//HEAP SORT.

            Console.WriteLine("VECTOR SEARCH...\n");
            while (true)
            {
                PQ.Clear();
                Console.WriteLine("Input search str...");
                String   ui    = Console.ReadLine();
                Double[] score = new Double[101];
                //Double[] lengths = new Double[101];//ST_C
                Double[] lengths = builder.GetLens();//ST_UC
                Double   q_w     = 0.0;
                String[] u       = ui.ToLower().Replace('ё', 'е').Split(new Char[] { ' ', ',', '.', ';', '-', ':', '?', '!', '\"' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (String t in u)
                {
                    IndexEntry te;
                    if (indexer.TryGetValue(lemmer.Lemmatize(t), out te))
                    {
                        q_w += te.IDF * te.IDF;
                        Int32 i = 1;
                        foreach (Int32 id in te.Ids)
                        {
                            score[id] += te.Weights[i];
                            //lengths[id] += te.Probs[i]*te.Probs[i];//ST_C
                            i++;
                        }
                    }
                }
                q_w = Math.Sqrt(q_w);
                if (q_w == 0.0)
                {
                    Console.WriteLine("NOT FOUND");
                }
                else
                {
                    for (Int32 k = 1; k < 101; k++)
                    {
                        if (lengths[k - 1] == 0) //ST_C
                        {
                            continue;            //ST_C
                        }
                        //lengths[k] = lengths[k] > 0 ? Math.Sqrt(lengths[k]) : 1;//ST_C
                        //score[k] = score[k]/(lengths[k]*q_w);//ST_C
                        score[k] = score[k] / (lengths[k - 1] * q_w);// 0 /1 => 0.
                        if (score[k] == 0.0)
                        {
                            continue;
                        }
                        PQ.Add(new HeapEntry()
                        {
                            Relevance = 1d / score[k], Id = k
                        });                                                      //ASC ORDER
                    }
                    SimpleTextCrawler.Structures.LinkedList <Int32> docIds = new SimpleTextCrawler.Structures.LinkedList <Int32>();
                    Int32 KM = 5;
                    while (!PQ.IsEmpty() && KM > 0)
                    {
                        HeapEntry et = PQ.DeleteMin();
                        Console.WriteLine("{0} : {1} ", et.Id, 1d / et.Relevance);
                        docIds.Add(et.Id);
                        KM--;
                    }
                    Console.WriteLine("");
                    __FindLinksV(docIds);
                }
            }
        }
Beispiel #8
0
 private void initializeData(
     Models.Node start, 
     IEnumerable<Models.FeatureType> pathFeatures)
 {
     createFeatureMap(pathFeatures);
     queue = HeapFactory.NewRawArrayHeap<State, double>(HEAP_CHILDREN);
     discovered = new Dictionary<State, PathData>();
     done = new HashSet<State>();
     updateState(new State(start, featureMap));
 }
Beispiel #9
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="env"></param>
 /// <param name="capacity"></param>
 internal PreemptiveResource(SimEnvironment env, int capacity) : base(env)
 {
     _capacity     = capacity;
     _requestQueue = HeapFactory.NewRawBinaryHeap <RequestEvent, ReqPriority>(ReqPriority.Comparer);
     _users        = HeapFactory.NewRawBinaryHeap <RequestEvent, ReqPriority>(ReqPriority.ReverseComparer);
 }
        static void Main(string[] args)
        {
            Transaction t1 = new Transaction("001", "8/10/2012", 78900.00);
            Transaction t2 = new Transaction("002", "9/10/2012", 451900.00);
            t1.showTransaction();
            t2.showTransaction();
            Console.ReadKey();

            Int32[] arrInt = new Int32[] { 7, 6, 9, 65, 8, 3, 5, 4 };

            Int32[] arrIntCopy = new Int32[arrInt.Length];
            //arrInt.CopyTo(arrIntCopy, 0);
            ArraySelection<Int32> arrIntSel = new ArraySelection<Int32>();
            arrInt.CopyTo(arrIntCopy, 0);
            arrIntSel.ElementsCollection = arrIntCopy;
            arrIntSel.Sort();
            Console.Write("array selection sort:\n");
            for (int i = 0; i < arrIntSel.ElementsCollection.Length; ++i)
            {
                Console.Write("{0} ", arrIntSel.ElementsCollection.GetValue(i).ToString());
            }
            Console.WriteLine();
            Console.ReadKey();

            ArrayInsert<Int32> arrIntIns = new ArrayInsert<Int32>();
            arrInt.CopyTo(arrIntCopy, 0);
            arrIntIns.ElementsCollection = arrIntCopy;
            arrIntIns.Sort();
            Console.Write("array Insert sort:\n");
            for (int i = 0; i < arrIntIns.ElementsCollection.Length; ++i)
            {
                Console.Write("{0} ", arrIntIns.ElementsCollection.GetValue(i).ToString());
            }
            Console.WriteLine();
            Console.ReadKey();

            ArrayBubble<Int32> arrIntBub = new ArrayBubble<Int32>();
            arrInt.CopyTo(arrIntCopy, 0);
            arrIntBub.ElementsCollection = arrIntCopy;
            arrIntBub.Sort();
            Console.Write("array Buble sort:\n");
            for (int i = 0; i < arrIntBub.ElementsCollection.Length; ++i)
            {
                Console.Write("{0} ", arrIntBub.ElementsCollection.GetValue(i).ToString());
            }
            Console.WriteLine();
            Console.ReadKey();

            ArrayShell<Int32> arrIntShe = new ArrayShell<Int32>();
            arrInt.CopyTo(arrIntCopy, 0);
            arrIntShe.ElementsCollection = arrIntCopy;
            arrIntShe.Sort();
            Console.Write("array Shell sort:\n");
            for (int i = 0; i < arrIntShe.ElementsCollection.Length; ++i)
            {
                Console.Write("{0} ", arrIntShe.ElementsCollection.GetValue(i).ToString());
            }
            Console.WriteLine();
            Console.ReadKey();

            ArrayMerge<Int32> arrIntMer = new ArrayMerge<Int32>();
            arrInt.CopyTo(arrIntCopy, 0);
            arrIntMer.ElementsCollection = arrIntCopy;
            arrIntMer.Sort();
            Console.Write("array Merge sort:\n");
            for (int i = 0; i < arrIntMer.ElementsCollection.Length; ++i)
            {
                Console.Write("{0} ", arrIntMer.ElementsCollection.GetValue(i).ToString());
            }
            Console.WriteLine();
            Console.ReadKey();

            ArrayQuick<Int32> arrIntQui = new ArrayQuick<Int32>();
            arrInt.CopyTo(arrIntCopy, 0);
            arrIntQui.ElementsCollection = arrIntCopy;
            arrIntQui.Sort();
            Console.Write("array Quick sort:\n");
            for (int i = 0; i < arrIntQui.ElementsCollection.Length; ++i)
            {
                Console.Write("{0} ", arrIntQui.ElementsCollection.GetValue(i).ToString());
            }
            Console.WriteLine();
            Console.ReadKey();

            ArrayHeap<Int32> arrIntHea = new ArrayHeap<Int32>();
            arrInt.CopyTo(arrIntCopy, 0);
            arrIntHea.ElementsCollection = arrIntCopy;
            arrIntHea.Sort();
            Console.Write("array Heap sort:\n");
            for (int i = 0; i < arrIntHea.ElementsCollection.Length; ++i)
            {
                Console.Write("{0} ", arrIntHea.ElementsCollection.GetValue(i).ToString());
            }
            Console.WriteLine();
            Console.ReadKey();
        }