Posts

Matrix Chain Multiplication

Image
Given a sequence of matrices, find the most efficient way to multiply these matrices together. The problem is not actually to perform the multiplications, but merely to decide in which order to perform the multiplications.  We have many options to multiply a chain of matrices because matrix multiplication is associative. In other words, no matter how we parenthesize the product, the result will be the same. For example, if we had four matrices A, B, C, and D, we would have:    (ABC)D = (AB)(CD) = A(BCD) = ....  However, the order in which we parenthesize the product affects the number of simple arithmetic operations needed to compute the product, or the efficiency. For example, suppose A is a 10 × 30 matrix, B is a 30 × 5 matrix, and C is a 5 × 60 matrix.  (AB)C = (10×30×5) + (10×5×60) = 1500 + 3000 = 4500 operations A(BC) = (30×5×60) + (10×30×60) = 9000 + 18000 = 27000 operations. Clearly the first parenthesization requires less number of operations. Given an a...

Tower of Hanoi

Image
Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs) and more than one rings is as depicted − Tower Of Hanoi These rings are of different sizes and stacked upon in an ascending order, i.e. the smaller one sits over the larger one. There are other variations of the puzzle where the number of disks increase, but the tower count remains the same. Rules The mission is to move all the disks to some another tower without violating the sequence of arrangement. A few rules to be followed for Tower of Hanoi are − Only one disk can be moved among the towers at any given time. Only the "top" disk can be removed. No large disk can sit over a small disk. Following is an animated representation of solving a Tower of Hanoi puzzle with three disks. Tower Of Hanoi Tower of Hanoi puzzle with n disks can be solved in minimum 2n−1 steps. This presentation shows that a puzzle with 3 disks has taken 23 - 1 = 7 steps. Algorithm To write an algorithm for Tower of H...

Breadth First Search (BFS)

Image
Breadth-first search assigns two values to each vertex v: A distance, giving the minimum number of edges in any path from the source vertex to vertex vvv. The predecessor vertex of vvv along some shortest path from the source vertex. The source vertex's predecessor is some special value, such as null, indicating that it has no predecessor. If there is no path from the source vertex to vertex vvv, then vvv's distance is infinite and its predecessor has the same special value as the source's predecessor. For example, here's an undirected graph with eight vertices, numbered 0 to 7, with vertex numbers appearing above or below the vertices. Inside each vertex are two numbers: its distance from the source, which is vertex 3, followed by its predecessor on a shortest path from vertex 3. A dash indicates null: In BFS, we initially set the distance and predecessor of each vertex to the special value (null). We start the search at the source and assign it a distance of 0. ...

Recursion

Image
What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function. Using recursive algorithm, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc. A Mathematical Interpretation Let us consider a problem that a programmer have to determine the sum of first n natural numbers, there are several ways of doing that but the simplest approach is simply add the numbers starting from 1 to n. So the function simply looks like, approach(1) – Simply adding one by one f(n) = 1 + 2 + 3 +……..+ n but there is another mathematical approach of representing this, approach(2) – Recursive adding f(n) = 1 n=1 f(n) = n + f(n-1) n>1 There is a simple difference between the approach (1) and approach(2) and that is in approach(2) the function “ f( ) ” itself is being...

Understanding React and ReactDOM

Image
The first line — import React from 'react'; - brings in the React module from your React library. It also creates an Object called React which you can tap into pre-written methods. For example, createElement() is a method that belongs in the React object. createElement() is also how React renders things into HTML. When you're using JSX, JSX compiles and transforms your code into a React.createElement() call. Note: You can write React ‘code’ without JSX, but it means that you will need to format everything to fit with createElement() method requirements. The next line after import React from 'react'; is import ReactDOM from 'react-dom';. This line imports methods that are available from react-dom and makes it accessible through the Object named ReactDOM. When you are rendering your React component, you are doing it via ReactDOM. Note: the DOM is not something that is new or exclusive to React. It is part of HTML and lets you hook into different parts of ...

Java Programming Language

Image
Java is one of the most popular and widely used programming language. Java has been one of the most popular programming language for many years. Java is Object Oriented. However it is not considered as pure object oriented as it provides support for primitive data types (like int, char, etc) The Java codes are first compiled into byte code (machine independent code). Then the byte code runs on Java Virtual Machine (JVM) regardless of the underlying architecture. Java syntax is similar to C/C++. But Java does not provide low level programming functionalities like pointers. Also, Java codes are always written in the form of classes and objects. Java is used in all kind of applications like Mobile Applications (Android is Java based), desktop applications, web applications, client server applications, enterprise applications and many more. When compared with C++, Java codes are generally more maintainable because Java does not allow many things which may lead bad/inefficient program...