Example #1
0
    public void TestCase2()
    {
        // Total Value = 150
        // Total Wight = 190
        // Item with vi 50 and weight 75
        //              50            59
        //              50            56

        // Arrange
        FileReader fileReader = new FileReader();

        (int, Item[])extractedValues = fileReader.ExtractItems(@"C:\Users\Paul\Documents\Open Source Society for Computer Science (OSSU)\Algorithms Coursera\Programming Assignments\Week 12 Programming Assignment\TestCase\testcase2.txt");
        List <int> indices;

        // Act
        KnapSack knapSack = new KnapSack(extractedValues.Item2, extractedValues.Item1);

        indices = knapSack.Reconstruction();

        // Assert
        Assert.Equal(150, knapSack.GetOptimalSackValue());
        Assert.Equal(4, indices[0]);
        Assert.Equal(1, indices[1]);
        Assert.Equal(0, indices[2]);

        int totalWeight = 0;

        foreach (int ind in indices)
        {
            totalWeight += extractedValues.Item2[ind]._weight;
        }

        Assert.Equal(190, totalWeight);
    }
Example #2
0
        static void Main(string[] args)
        {
            int []   volumes   = { 4, 5, 3 };
            int []   weights   = { 11, 12, 7 };
            int      maxVolume = 10;
            KnapSack ks        = new KnapSack(weights, volumes, maxVolume);

            ks.StartFindBestPile();
        }
Example #3
0
    public void TestCase1()
    {
        // Expected Result 14
        // Arrange
        FileReader fileReader = new FileReader();

        (int, Item[])extractedValues = fileReader.ExtractItems(@"C:\Users\Paul\Documents\Open Source Society for Computer Science (OSSU)\Algorithms Coursera\Programming Assignments\Week 12 Programming Assignment\TestCase\testcase1.txt");

        // Act
        KnapSack knapSack = new KnapSack(extractedValues.Item2, extractedValues.Item1);

        // Assert
        Assert.Equal(14, knapSack.GetOptimalSackValue());
    }
        public void Test_KnapSnack_Calculate()
        {
            List <int> weights = new List <int>()
            {
                2, 3, 1, 4
            };
            List <int> profits = new List <int>()
            {
                4, 5, 3, 7
            };
            int capacity = 5;

            KnapSack obj = new KnapSack(weights, profits, capacity);

            obj.Calculate(capacity);
            int expected   = 10;
            int numberOfIt = obj._numberOfIteration;

            Assert.AreEqual(expected, obj._maxProfit);
        }
Example #5
0
        private static void Main(string[] args)
        {
            //Console.WriteLine("Hello World!");
            List <Food> naps = new List <Food>();

            naps.Add(new Food(100, "Burger", 200));
            naps.Add(new Food(180, "Coke", 90));
            naps.Add(new Food(400, "Cookie", 30));
            naps.Add(new Food(237, "Wine", 300));
            naps.Add(new Food(38, "Apple", 210));
            naps.Add(new Food(487, "Chocolate", 190));
            naps.Add(new Food(20, "Water", 47));
            naps.Add(new Food(90, "Diet Coke", 80));
            naps.Add(new Food(237, "Pineaple Juice", 200));
            naps.Add(new Food(78, "Cranberry Juice", 320));


            MainClass main = new MainClass(naps, 15);

            main.display();
            KnapSack knap = MainClass.solve();

            knap.display();
        }
Example #6
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            int[] arr = { 1, 2, 3, 3, 4, 5, 5, 8, 10, 10 };
            //var a = BubbleSort.GetSorted(arr);
            // QuickSort.GetSorted(arr);
            MyHeap.BuildHead();
            //foreach (var x in a)
            //{
            //    Console.WriteLine(x);
            //}

            var a = removeDuplicates(arr, arr.Length);

            // Print updated array
            for (int i = 0; i < a; i++)
            {
                Console.Write(arr[i] + " ");
            }

            var b = BianrySearch(arr, 225, 0, arr.Length - 1);

            Console.WriteLine(b);

            BinaryTree bt    = new BinaryTree();
            BinaryTree bt1   = new BinaryTree();
            var        root  = bt.Insert(null, 10);
            var        root1 = bt1.Insert(null, 10);

            /*root = bt.Insert(root, 20);
             * root = bt.Insert(root, 8);
             * root = bt.Insert(root, 9);
             * root = bt.Insert(root, 15);
             * root = bt.Insert(root, 25);
             */
            bt.Insert(root, 20);
            bt.Insert(root, 8);
            bt.Insert(root, 9);
            bt.Insert(root, 15);
            bt.Insert(root, 25);
            bt.Insert(root, 12);
            bt.Insert(root, 5);

            bt1.Insert(root1, 20);
            bt1.Insert(root1, 8);
            bt1.Insert(root1, 9);
            bt1.Insert(root1, 15);
            bt1.Insert(root1, 25);
            //bt1.Insert(root1, 12);
            bt1.Insert(root1, 7);

            TwoNodes tn = new TwoNodes {
                First = bt.RootNode, Second = bt1.RootNode
            };

            Console.WriteLine("Is_BST_Identical_Iterative  ::{0} ", bt.Is_BST_Identical_Iterative(tn));
            Console.WriteLine("Is_BST_Identical  ::{0} ", bt.isIdentical(bt.RootNode, bt1.RootNode));
            bt1.Height_Tree_Iterative(bt1.RootNode);

            Console.WriteLine("PreOrder");
            bt.PreOrder(bt.RootNode);
            Console.WriteLine("");
            Console.WriteLine("InOrder");
            bt.InOrder(bt.RootNode);
            Console.WriteLine("");
            Console.WriteLine("PostOrder");
            bt.PostOrder(bt.RootNode);

            bt.CreateBinaryTreeFromInAndPreOrder();

            Console.WriteLine("");
            Console.WriteLine("Find");
            var f = bt.Find(115, bt.RootNode);

            Console.WriteLine(f != null ? f.data : 0);

            Console.WriteLine("");
            Console.WriteLine("MinValue");

            var m = bt.GetMinValue(bt.RootNode);

            Console.WriteLine(m);
            int pos = 6;

            bt.GetKthMinValue(bt.RootNode, ref pos);
            //bt.LevelOrder(bt.RootNode);
            //bt.Inorder_Iterative(bt.RootNode);
            //bt.Preorder_Iterative(bt.RootNode);
            //bt.Postorder_Iterative(bt.RootNode);
            //bt.Get_Inorder_Predecessor(bt.RootNode);
            //bt.Get_Inorder_Successor(bt.RootNode);

            Console.WriteLine("\nInvert binary");
            bt.InvertBinary(bt.RootNode);
            bt.LevelOrder(bt.RootNode);

            Node ln = new Node {
                data = 20
            };
            Node rn = new Node {
                data = 30
            };
            Node no = new Node {
                data = 10, Left = ln, Right = rn
            };

            // Console.WriteLine("isBST :: {0}", bt.isBST(no, int.MinValue) );
            Console.WriteLine("isBST :: {0}", bt.isBinarySearch(no));

            AVLTree at = new AVLTree();

            at.Avl_execute();

            int[] arr1 = { 5, 6, 8, 9, 10, 12, 15, 20 };

            GetTriplets(arr1, 30);
            GetSumDobles(arr1, 15);
            GetSumDobles_Linear(arr1, 15);

            Console.WriteLine("KnapSack");
            int[] val = new int[] { 60, 120, 100 };
            int[] wt  = new int[] { 10, 30, 20 };
            int   Cap = 50;
            int   n   = val.Length;

            int[,] dp = new int[n + 1, Cap + 1];
            KnapSack k = new KnapSack();

            Console.WriteLine(k.KnapSack_Binary(Cap, wt, val, n, dp));
            k.KnapSack_Fraction(Cap, wt, val, n);

            Console.WriteLine("Pattern");
            Pattern.SearchPattern("AABAACAADAABAAABAACAAD", "CAAD");
            Pattern.Binary_MultipleOf3("110100000101");

            Console.WriteLine("Largest Sum Contiguous Subarray");
            int[] aa = { 1, 2, 3, -2, 5 };
            Pattern.maxSubArraySum(aa);
            Console.WriteLine("Missing elements");

            int[] aa1 = { 1, 2, 3, 5, 7, 8, 10 };
            Pattern.FindMissingElements(aa1);

            int[] aa2 = { 1, 2, 3, 1, 1 };
            Pattern.FindMajorityElement(aa2);

            //Search in a Rotated Array
            int[] aa3 = { 5, 6, 7, 8, 9, 10, 1, 2, 3, 4 };
            Pattern.SearchRotatedArray(aa3, 10);

            //Linked List
            LinkedList lst = new LinkedList();

            lst.InsertFront(10);
            lst.InsertLast(20);
            lst.InsertFront(5);
            lst.InsertLast(25);
            lst.InsertLast(35);
            lst.PrintList(lst);

            lst.FindMiddleElement(lst);

            Problem_30 p30 = new Problem_30();

            p30.FindSingeNumber();
            p30.HappyNumber();
            p30.Longest_Sum_Subarray();
            p30.FindSubarraySum();
            p30.MoveZeroRight();
            p30.MaxProfit();
            p30.IsAnagram();
            p30.StringSorting("kgadeA");
            p30.GroupingAnagrams();
            p30.CountingElement();
            p30.BackspaceStingCompare();
            //p30.LastStoneWeight();
            p30.SubArrayWithEqualZeroAndOne();
            p30.StringShif();
            p30.ProductOfArrayExceptSlef();
            Console.WriteLine("\nValid parenthesis:: {0}", p30.ValidParenthesis());
            p30.NumberOfIsland();
            p30.LeftmostColumnWithAtleastOne();
            p30.BitwiseAnd();
            p30.JumpingProblem();
            p30.LongestcommonSubsequence();
            p30.CountPairsGivenSum();
            p30.MaximumPathSum(bt.RootNode);
            p30.ValidSequence(bt.RootNode);
            p30.MinFromSatck();
            p30.MinimumPathSum();
            p30.HistogramMaxRectangle();
            //p30.Print_Combination_R_Elements_Array();

            ///All Array Problem
            ///
            ArrayProblem ar = new ArrayProblem();

            ar.Find_And_Print_SubArraySum();
            ar.SortBinaryArray();
            ar.FindDuplicate();
            ar.LongestConsecutiveSubArray();
            ar.MaximumSumSubArray();
            ar.MinimuSumSubArray();
            ar.SubArrayWithEqualZeroAndOne();
            ar.SortArrayOnlyZeroOneTwos();
            ar.Merge2SortedArray();
            ar.MergeArray_ReplaceZeroValue();
            ar.Find_Index_MaxLength_ContinuousOnes();
            ar.ProductOfTwoIntArray();
            ar.Suffle();
            ar.RearrangeArrayHighAndLow();
            ar.EquilibriumIndex();
            ar.FindMajorityElement();
            ar.MoveAllZeroToEnd();
            ar.ProductOfArrayWithotSlef();
            ar.BitonicSubArray();
            ar.FindMaximumDifference();
            ar.MaximumSumCircularSubArray();
            ar.FindSumMimumSubArray_Given_Size();
            ar.FindSumSubArray_Given_Sum();
            ar.LengthOfSmallestSubarray_SumOfElements_GreaterThanNumber();
            ar.Find_Smallest_Window_Will_Make_Entire_Array_Sorted();
            ar.TrappingRainWaterwithin_given_set_bars();
            ar.Find_Maximum_Sum_Subsequence_With_NO_Adjacent();
            ar.MinimumNumber_Platform();
            ar.LengthOf_Continuous_Same_Sum_Two_Binary_Array();
            ar.Find_Number_Rotations();
            ar.RodCutting();
            ar.Find_kth_Smallest_Element();
            ar.FindAllCombinations();

            //Trie data structure
            Trie tr = new Trie();

            string[] words = { "abc", "abgl", "cdf", "abcd", "lmn" };
            //string[] words = { "abc"};
            foreach (string st in words)
            {
                tr.Insert(st);
            }

            Console.WriteLine("\nIs string present {0} ", tr.Search("cdf"));
            tr.AutoComplete("ab");

            // List of graph edges as per above diagram
            List <Edge> edges = new List <Edge> {
                // Notice that node 0 is unconnected node
                new Edge(1, 2), new Edge(1, 7), new Edge(1, 8),
                new Edge(2, 3), new Edge(2, 6), new Edge(3, 4),
                new Edge(3, 5), new Edge(8, 9), new Edge(8, 12),
                new Edge(9, 10), new Edge(9, 11)
            };

            // Set number of vertices in the graph (0-12)
            int N = 13;

            // create a graph from edges
            Graph graph = new Graph(edges, N);

            // stores vertex is discovered or not
            bool[] discovered = new bool[N];

            // Do DFS traversal from all undiscovered nodes to
            // cover all unconnected components of graph
            for (int i = 0; i < N; i++)
            {
                if (!discovered[i])
                {
                    graph.DFS(graph, i, discovered);
                }
            }

            //Matrix
            Matrix mt = new Matrix();

            mt.Print_SpiralOrder();
            mt.Rotation_90();
            mt.FloodFill();
        }
Example #7
0
 public void GetMaxCostFractionalTest0()
 {
     var knapSack = new KnapSack();
     var res = knapSack.GetMaxCostFractional(new int[] { 60, 100, 120 }, new int[] { 10, 20, 30 }, 50);
     Assert.Equal(240, res);
 }
Example #8
0
 public void GetMaxCostDiscreteTest0()
 {
     var knapSack = new KnapSack();
     var res = knapSack.GetMaxCostDiscrete(new int[] { 60, 100, 120 }, new int[] { 10, 20, 30 }, 50);
     Assert.Equal(220, res);
 }