Skip to main content

Posts

Optimal Binary Search using Dynamic Programming

An optimal binary search tree is a binary search tree for which the nodes are arranged on levels such that the tree cost is minimum. If the probabilities of searching for elements of a set are known from accumulated data from past searches, then Binary Search Tree (BST) should be such that the average number of comparisons in a search should be minimum. eg. Lets the elements to be searched are A, B, C, D and probabilities of searching these items are 0.1, 0.2, 0.4 and 0.3 respectively. Lets consider 2 out of 14 possible BST containing these keys. Figure 1 Figure 2 Average number of comparison is calculated as sum of level*probability(key element) for each element of the tree. Lets the level of tree start from 1. Therefore, for figure 1 -     Average number of comparison = 1*0.1 +2*0.2 +3*0.4 +4*0.3  = 2.9                                  ...

Knapsack Problem and Solution using Dynamic Programming

The knapsack problem or rucksack problem is a problem in combinatorial optimization: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible Given a knapsack of capacity m and number of items n of weight w1, w2, w3 ... , wn with profits p1, p2, p3..., pn. Let x1,x2,...,xn is an array that represents the items has been selected or not. If the item i is selected, then xi = 1 If the item i is not selected then x i = 0 In 0/1 knapsack, the item can be selected or completely rejected. The items are not allowed to be broken into smaller parts. The main objective is to place the items into the knapsack so that maximum profit is obtained or find the most valuable subset of items that fits into the knapsack. Constraints: The weight of the items chosen should not exceed the capacity of knapsack. Obj...

Understanding Concurrency, Threading and Synchronization

Thread: A thread is defined at the operating system level. It is a single execution of a program. A thread is a set of instruction. An application can be composed of several threads. Different threads can be executed at the same time. The JVM works with several threads like Garbage collector, JIT etc. What does mean by "At the same time"? In a single core of CPU, the threads cannot works at the same time, it give us a feel that it is running at same but in reality it work one by one and the context switch happen very fast in milliseconds which we can't observe. On a multi-core CPU, things happens at the same time. A challenge while this is the read and write operation involve while running different thread at same time. Who is responsible for the CPU sharing? There is a special element who does this, is called scheduler.there are three reasons for a scheduled to pause a thread -  The CPU should be shared equally among threads. The thread might be waiting ...

ClassNotFoundException and NoClassDefFoundError in Java

As the name suggest ClassNotFoundException is of Exception type and the NoClassDefFoundError is a type of Error. ClassNotFoundException can be handled inside code while NoClassDefFoundError can't. ClassNotFoundException and NoClassDefFoundError occur when the particular required class is not found during run-time. It occurs in different scenarios. ClassNotFoundException thrown when an application tries to load in a class through its string name using: The forName method in class Class. The findSystemClass method in class ClassLoader . The loadClass method in class ClassLoader. but not available during run time. For example, you might have come across this exception when you would have tried to connect to MySQL or Oracle databases and you have not updated the classpath with required JAR files. Most of the time, this exception occurs when you try to run an application without updating the classpath with required JAR files. For example, the below program will throw ClassN...

Basic Unix Commands quick reference

This quick reference lists commands, including a syntax diagram and brief description. […] indicates an optional part of the command. For more detail, use: man command 1. Files 1.1. Filename Substitution Wild Cards                ? * Character Class (c is any single character) [c…] Range                 [c-c] Home Directory         ~ Home Directory of Another User ~user List Files in Current Directory ls [-l] List Hidden Files                 ls -[l]a 1.2. File Manipulation Display File Contents         cat filename Copy                 cp source destination Move (Rename)         mv oldname newname Remove (Delete)         rm filename 1.3. File Pr...