Sie sind auf Seite 1von 12

Algorithm: Roughly speaking An algorithm is a set of rules for carrying out calculation either by hand or on a machine.

. An algorithm is a finite step-by-step procedure to achieve a required result. An algorithm is a sequence of computational steps that transform the input into the output. An algorithm is a sequence of operations performed on data that have to be organized in data structures. An algorithm is an abstraction of a program to be executed on a physical machine (model of Computation). Characteristic of an algorithm: 1. Input: zero or more quantities are externally supplied. 2. Output: at least one quantity is produced. 3. Definiteness: each instruction is clear and unambiguous. 4. Finiteness: if we trace out an algorithm, then for all cases, the algorithm terminates after a finite number of steps. 5. Effectiveness: every instruction must be very basic so that it can be carried out feasibly. The Classic Multiplication Algorithm 1. Multiplication, the American way: Multiply the multiplicand one after another by each digit of the multiplier taken from right to left.

2. Multiplication, the English way: Multiply the multiplicand one after another by each digit of the multiplier taken from left to right.

3rd way of multiplication:


981 490 245 122 61 30 15 7 3 1 1234 2468 4936 9872 19744 39488 78976 157952 315904 631808 1234 4936 19744 78976 157952 315904 631808 1210554 1

Still 4th method is Arabic method and 5th is divide-and-conquer. Algorithms can be broadly classified into two categoris: Deterministic: the output for the same input is always same irrespective of space & time Nondeterministic: different outputs can be obtained with the same set of inputs while using the same algorithm. designing and analyzing computer algorithms 1. The design pertain to i. The description of algorithm at an abstract level by means of a pseudo language, & ii. Proof of correctness that is, the algorithm solves the given problem in all cases. There are number of standard methods to design an algorithm are: brute-force method(trial-&-error method-leads ti inefficient algorithms), divide and conquer method, greedy method, dynamic programming method, branch-n-bound method, approximation method, etc. 2. The analysis deals with performance evaluation (complexity analysis). Algortihmic specification (Syntax) 1.Comments begins with // 2. Blocks {------------} 3. Asssignment := 4. Operators true, false, and, or, not, <, >, <=, >=, =, = 5. Loops

While () do { } For var:=val1 to val2 step _ do { } Repeat Stmt..... Until (condition) If stmt then stmt else stmt Case { ..... }

Algorithm's Performance

Two important ways to characterize the effectiveness of an algorithm are its space complexity and time complexity. Time complexity of an algorithm concerns determining an expression of the number of steps needed as a function of the problem size. Since the step count measure is somewhat coarse, one does not aim at obtaining an exact step count. Instead, one attempts only to get asymptotic bounds on the step count. Asymptotic analysis makes use of the O (Big Oh) notation. Two other notational constructs used by computer scientists in the analysis of algorithms are (Big Theta) notation and (Big Omega) notation. The performance evaluation of an algorithm is obtained by totalling the number of occurrences of each operation when running the algorithm. The performance of an algorithm is evaluated as a function of the input size n and is to be considered modulo a multiplicative constant. The following notations are commonly use notations in performance analysis and used to characterize the complexity of an algorithm. -Notation (Same order) This notation bounds a function to within constant factors. We say f(n) = (g(n)) if there exist positive constants n0, c1 and c2 such that to the right of n0 the value of f(n) always lies between c1 g(n) and c2 g(n) inclusive. In the set notation, we write as follows: (g(n)) = {f(n) : there exist positive constants c1, c2, and n0 such that 0 c1 g(n) f(n) c2 g(n) for all n n0} We say that is g(n) an asymptotically tight bound for f(n).

Graphically, for all values of n to the right of n0, the value of f(n) lies at or above c1 g(n) and at or below c2 g(n). In other words, for all n n0, the function f(n) is equal to g(n) to within a constant factor. We say that g(n) is an asymptotically tight bound for f(n). In the set terminology, f(n) is said to be a member of the set (g(n)) of functions. In other words, because O(g(n)) is a set, we could write f(n) (g(n)) to indicate that f(n) is a member of (g(n)). Instead, we write f(n) = (g(n)) to express the same notation. -Notation (Upper Bound) This notation gives an upper bound for a function to within a constant factor. We write f(n) = O(g(n)) if there are positive constants n0 and c such that to the right of n0, the value of f(n) always lies on or below c g(n).
3

In the set notation, we write as follows: For a given function g(n), the set of functions (g(n)) = {f(n): there exist positive constants c and n0 such that 0 f(n) c g(n) for all n n0} We say that the function g(n) is an asymptotic upper bound for the function f(n). We use -notation to give an upper bound on a function, to within a constant factor.

Graphically, for all values of n to the right of n0, the value of the function f(n) is on or below g(n). We write f(n) = O(g(n)) to indicate that a function f(n) is a member of the set (g(n)) i.e. f(n) (g(n)) Note that f(n) = (g(n)) implies f(n) = (g(n)), since -notation is a stronger notation than -notation. Equivalently, we may also define f is of order g as follows: If f(n) and g(n) are functions defined on the positive integers, then f(n) is (g(n)) if and only if there is a c > 0 and an n0 > 0 such that | f(n) | | g(n) | for all n n0 The concept of order(Big O) is important because a. it can be used to decide the best algorithm that solves a given problem b. It determines the maximum size of a problem that can be solved in a given system, in a given amount of time Big-O expressions and commonly used names
O(1) O(log n) constant (an increase in the amount of data (n) has no effect) logarithmic (operations increase once each time n doubles)

O(n) linear O(n log n) n log n O(n2) O(n ) O(2 )


n 3

quadratic cubic exponential

Question: Arrange following rate of growth in increasing order. 2N, n log n, n2, 1 , n, log n, n!, n3
Answer: 1, log n, n, n log n, n2, n3, 2N, n!

Example : Take the following list of functions and arrange them in ascending order of growth rate. That is, if function g(n) immediately follows function f(n) in your list, then it should be the case that f(n) is O(g(n)). g1(n) = 2n , g2(n) = n4/3 , g3(n) = n(log n)3 , g4(n) = nlog n ,g5(n) = 22n , g6(n) = 2n2 Solutions: Here are the functions ordered in ascending order of growth rate:

g3(n) = n(log n)3 g2(n) = n4/3 g4(n) = nlog n g1(n) = 2n g6(n) = 2n2 g5(n) = 22n

-Notation (Lower Bound) This notation gives a lower bound for a function to within a constant factor. We write f(n) = (g(n)) if there are positive constants n0 and c such that to the right of n0, the value of f(n) always lies on or above c g(n). In the set notation, we write as follows: For a given function g(n), the set of functions (g(n)) = {f(n) : there exist positive constants c and n0 such that 0 c g(n) f(n) for all n n0} We say that the function g(n) is an asymptotic lower bound for the function f(n).

Algorithm Analysis

The complexity of an algorithm is a function g(n) that gives the upper bound of the number of operation (or running time) performed by an algorithm when the input size is n. There are two interpretations of upper bound. Worst-case Complexity The running time for any given size input will be lower than the upper bound except possibly for some values of the input where the maximum is reached. Average-case Complexity The running time for any given size input will be the average number of operations over all problem instances for a given size.
5

Because, it is quite difficult to estimate the statistical behavior of the input, most of the time we content ourselves to a worst case behavior. Most of the time, the complexity of g(n) is approximated by its family o(f(n)) where f(n) is one of the following functions. n (linear complexity), log n (logarithmic complexity), na where a 2 (polynomial complexity), an (exponential complexity). Optimality Once the complexity of an algorithm has been estimated, the question arises whether this algorithm is optimal. An algorithm for a given problem is optimal if its complexity reaches the lower bound over all the algorithms solving this problem. For example, any algorithm solving the intersection of n segments problem will execute at least n2 operations in the worst case even if it does nothing but print the output. This is abbreviated by saying that the problem has (n2) complexity. If one finds an O(n2) algorithm that solve this problem, it will be optimal and of complexity (n2). Reduction Another technique for estimating the complexity of a problem is the transformation of problems, also called problem reduction. As an example, suppose we know a lower bound for a problem A, and that we would like to estimate a lower bound for a problem B. If we can transform A into B by a transformation step whose cost is less than that for solving A, then B has the same bound as A. The Convex hull problem nicely illustrates "reduction" technique. A lower bound of Convex-hull problem established by reducing the sorting problem (complexity: (n log n)) to the Convex hull problem. Elementary operation. Answer the following: (07) (i) What is relation? Explain equivalence relation. (ii) Explain linear inequality and equations. Answer the following: (07) (i) Explain asymptotic analysis of algorithm. (ii) Let f(n) and g(n) be asymptotically positive functions. Prove or disprove following. f(n) + g(n) = (min(f(n), g(n))). Write selection sort algorithm. And compute running time of algorithm. (07) Answer the following: (07) (i) Prove that (n + a)b = ( nb) , b>0 (ii) Find big oh() notation for following: (1) f(n) = 6993 (2) f(n) = 6n2 + 135 (b) Answer the following (i) Find big theta() and big omega() notation. (1) f(n) = 14 * 7 + 83. (2) f(n) = 83n3 + 84n (ii) Is 2n+1 = (2n) ? Explain.

Sets
A set is a collection of different things (distinguishable objects or distinct objects) represented as a unit. The objects in a set are called its elements or members. If an object x is a member of a set S, we write x S. On the the hand, if x is not a member of S, we write z S. A set cannot contain the same object more than once, and its elements are not ordered. For example, consider the set S= {7, 21, 57}. Then 7 {7, 21, 57} and 8 {7, 21, 57} or equivalently, 7 S and 8 S. We can also describe a set containing elements according to some rule. We write {n : rule about n} Thus, {n : n = m2 for some m N } means that a set of perfect squares.

Set Cardinality The number of elements in a set is called cardinality or size of the set, denoted |S| or sometimes n(S). The two sets have same cardinality if their elements can be put into a one-to-one correspondence. It is easy to see that the cardinality of an empty set is zero i.e., | | . Multiset If we do want to take the number of occurrences of members into account, we call the group a multiset. For example, {7} and {7, 7} are identical as set but {7} and {7, 7} are different as multiset. Infinite Set :A set contains infinite elements. For example, set of negative integers, set of integers, etc. Empty Set Set contain no member, denoted as or {}.

Subset For two sets A and B, we say that A is a subset of B, written A B, if every member of A also is a member of B. Formally, A B if x A implies x B Written x A => x B. Proper Subset Set A is a proper subset of B, written A is, A set A is proper subset of B if A B but A B. B, if A is a subset of B and not equal to B. That

Equal Sets The sets A and B are equal, written A = B, if each is a subset of the other. Rephrased definition, let A and B be sets. A = B if A B and B A. Power Set Let A be the set. The power of A, written P(A) or 2A, is the set of all subsets of A. That is, P(A) = {B : B A}. For example, consider A={0, 1}. The power set of A is {{}, {0}, {1}, {0, 1}}. And the power set of A is the set of all pairs (2-tuples) whose elements are 0 and 1 is {(0, 0), (0, 1), (1, 0), (1, 1)}. Disjoint Sets Let A and B be sets. A and B are disjoint if A B = .

Union of Sets The union of A and B, written A B, is the set we get by combining all elements in A and B into a single set. That is, A B = { x : x A or x B}. We can conclude |B| |A B| |A| + |B|. That is, if |A B| = 0 then |A B| = |A| + |B| and if A B then |A|

Intersection Sets The intersection of set set A and B, written A B, is the set of elements that are both in A and in B. That is, A intersection B = { x : x A and x B}.
7

Difference of Sets Let A and B be sets. The difference of A and B is A - B = {x : x A and x B}. For example, let A = {1, 2, 3} and B = {2, 4, 6, 8}. The set difference A - B = {1, 3} while B-A = {4, 6, 8}. Complement of a Set All set under consideration are subset of some large set U called universal set. Given a universal set U, the complement of A, written A', is the set of all elements under consideration that are not in A. Formally, let A be a subset of universal set U. The complement of A in U is A' = A - U OR A' = {x : x For any set A i. A'' = A U, we have following laws ii. A intersection A' = . iii. A A' = U U and x A}.

Cartesian Product or Cross Product If A and B are two sets, the cross product of A and B, written AB, is the set of all pairs wherein the first element is a member of the set A and the second element is a member of the set B. Formally, AB = {(a, b) : a A, b B}. For example, let A = {1, 2} and B = {x, y, z}. Then AB = {(1, x), (1, y), (1, z), (2, x), (2, y), (2, z)}. When A and B are finite sets, the cardinality of their product is |AB| = |A| . |B|

Function and relations A function assigns outputs to inputs. More precisely, a function assigns to each element of a set of so-called input values a single output value. Familiar examples of functions are mother, father, and date of birth. In these examples, the input is a person and the output is some characteristic of that person; in all cases, the output is uniquely dened by the inputwe all have just one mother, one father and one date of birth. Other familiar examples of functions are age and weight; because our age and weight change over time, the input is a combination of a person and a time. (For example, the age of Jenny on Wednesday was 61.) Nationality is an example of a characteristic of a person that is not a function because some people have more than one nationality. Spouse is also not a function because some people do not have a spouse and, in polygamous societies, some have more than one. Nationality and spouse are examples of relations. In mathematics, there are lots of well-known functions that map real numbers to real numbers. Negation is the function that maps, for example, 0.1 to 0.1 and 2 to 2. The square function maps, for example, 1 to 1, 2 to 4, 10 to 100, and 0.1 to 0.01. Note that the square function maps both 1 and 1 to 1; a function maps each input value to a single output value, but several input values may be mapped to the same output value. Other well-known functions are the sine, cosine and tangent functions, which map angles to reals, and area and volume. Area maps a two-dimensional object to a real number; Volume does the same to a threedimensional object. In mathematics, we often investigate relationships between certain objects (numbers, functions, sets, figures, etc.). If an element a of a set A is related to an element b of a set B, we might write a is related to b or shortly a related b or even more shortly a R b.
8

The essential point is that we have two objects, a and b, that are related in some way. Also, we say "a is related to b", not "b is related to a", so the order of a and b is important. In other words, the ordered pair (a,b) is distinguished by the relation. This observation suggests the following formal definition of a relation. Definition: Let A and B be two sets. A relation R from A into B is a subset of the cartesian product A B. Equivalence relations constitute a very important type of relations on a set. Definition: Let A be a nonempty set. A relation R on A (that is, a subset R of A A) is called an equivalence relation on A if the following hold. (i) (a,a) R for all a A, (ii) if (a,b) R, then (b,a) R (for all a,b A) (iii) if (a,b) R and (b,c) R, then (a,c) R (for all a,b,c R). We prefer to write a b, or a b, or a b or some similar symbolism instead of (a,b) express that a,b are related by an equivalence relation R. R in order to

Vectors and Matrices

A vector, u, means a list (or n-tuple) of numbers: u = (u1, u2, . . . , un) where ui are called the components of u. If all the ui are zero i.e., ui = 0, then u is called the zero vector. Given vectors u and v are equal i.e., u = v, if they have the same number of components and if corresponding components are equal. Addition of Two Vectors If two vectors, u and v, have the number of components, their sum, u + v, is the vector obtained by adding corresponding components from u and v. u + v = (u1, u2, . . . , un) + (v1, v2, . . . , vn) = (u1 + v1 + u2 + v2, . . . , un + vn)

Multiplication of a vector by a Scalar The product of a scalar k and a vector u i.e., ku, is the vector obtained by multiplying each component of u by k: ku = k(u1, u2, . . . , un) = ku1, ku2, . . . , kun. Here, we define -u = (-1)u and u-v = u +(-v). It is not difficult to see k(u + v) = ku + kv where k is a scalar and u and v are vectors. In algorithmic (study of algorithms), we like to write a matrix A as A(aij). Column Vector A matrix with only one column is called a column vector. Zero Matrix A matrix whose entries are all zero is called a zero matrix and denoted by 0. Matrix Addition Let A and B be two matrices of the same size. The sum of A and B is written as A + B and obtained by adding corresponding elements from A and B. Scalar Multiplication The product of a scalar k and a matrix A, written kA or Ak, is the matrix obtained by multiplying each element of A by k. Here, we define -A = (-1)A and A - B = A + (-B). Note that -A is the negative of the matrix A.

Properties of Matrix under Addition and Multiplication Let A, B, and C be matrices of same size and let k and l be scalars. Then i. ii. iii. iv. v. vi. vii. viii. (A + B) + A + (B + C) A+B=B+A A+0=0+A=A A + (-A) = (-A) + A = 0 k(A + B) = kA + kB (k + l)A = kA + lA (kl)A = k(lA) lA = A

Matrix Multiplication Suppose A and B are two matrices such that the number of columns of A is equal to number of rows of B. Say matrix A is an mp matrix and matrix B is a pn matrix. Then the product of A and B is the mn matrix whose ij-entry is obtained by multiplying the elements of the ith row of a by the corresponding elements of the jth column of B and then adding them. It is important to note that if the number of columns of A is not equal to the number of rows of B, then the product AB is not defined. Properties of Matrix Multiplication Let A, B, and C be matrices and let k be a scalar. Then i. ii. iii. iv. (AB)C = A(BC) A(B+C) = AB + AC (B+C)A = BA + CA k(AB) = (kB)B = A(kB)

Linear Inequalities and Linear Equations

Inequalities The term inequality is applied to any statement involving one of the symbols <, >, Examples of inequalities are: i. x 1 ii. x + y + 2z > 16 iii. p2 + q2 1/2 iv. a2 + ab > 1 Fundamental Properties of Inequalities 1. If a b and c is any real number, then a + c b + c. For example, -3 -1 implies -3+4 -1 + 4. 2. If a b and c is positive, then ac bc. For example, 2 3 implies 2(4) 3(4). 3. If a b and c is negative, then ac bc. For example, 3 9 implies 3(-2) 9(-2). 4. If a b and b c, then a c. For example, -1/2 2 and 2 8/3 imply -1/2 8/3.

10

Solution of Inequality By solution of the one variable inequality 2x + 3 7 we mean any number which substituted for x yields a true statement. For example, 1 is a solution of 2x + 3 7 since 2(1) + 3 = 5 and 5 is less than and equal to 7. By a solution of the two variable inequality x y 5 we mean any ordered pair of numbers which when substituted for x and y, respectively, yields a true statement. For example, (2, 1) is a solution of x y 5 because 2-1 = 1 and 1 5. By a solution of the three variable inequality 2x - y + z 3 we means an ordered triple of number which when substituted for x, y and z respectively, yields a true statement. For example, (2, 0, 1) is a solution of 2x - y + z 3. A solution of an inequality is said to satisfy the inequality. For example, (2, 1) is satisfy x y 5. Two or more inequalities, each with the same variables, considered as a unit, are said to form a system of inequalities. For example, x 0, y 0, 2x + y 4

Note that the notion of a system of inequalities is analogous to that of a solution of a system of equations. Any solution common to all of the inequalities of a system of inequalities is said to be a solution of that system of inequalities. A system of inequalities, each of whose members is linear, is said to be a system of linear inequalities. Geometric Interpretation of Inequalities An inequality in two variable x and y describes a region in the x-y plane (called its graph), namely, the set of all points whose coordinates satisfy the inequality. The y-axis divide, the xy-plane into two regions, called half-planes.

Right half-plane : The region of points whose coordinates satisfy inequality x > 0. Left half-plane : The region of points whose coordinates satisfy inequality x < 0.

Similarly, the x-axis divides the xy-plane into two half-planes.


Upper half-plane : In which inequality y > 0 is true. Lower half-plane : In which inequality y < 0 is true.

What is x-axis and y-axis? They are simply lines. So, the above arguments can be applied to any line. Every line ax + by = c divides the xy-plane into two regions called its half-planes.
11

On one half-plane ax + by > c is true. On the other half-plane ax + by < c is true.

Linear Equations
One Unknown A linear equation in one unknown can always be stated into the standard form ax = b where x is an unknown and a and b are constants. If a is not equal to zero, this equation has a unique solution x = b/a Two Unknowns A linear equation in two unknown, x and y, can be put into the form ax + by = c where x and y are two unknowns and a, b, c are real numbers. Also, we assume that a and b are no zero. Solution of Linear Equation A solution of the equation consists of a pair of number, u = (k1, k2), which satisfies the equation ax + by = c. Mathematically speaking, a solution consists of u = (k1, k2) such that ak1 + bk2 = c. Solution of the equation can be found by assigning arbitrary values to x and solving for y OR assigning arbitrary values to y and solving for x. Geometrically, any solution u = (k1, k2) of the linear equation ax + by = c determine a point in the cartesian plane. Since a and b are not zero, the solution u correspond precisely to the points on a straight line. Two Equations in the Two Unknowns A system of two linear equations in the two unknowns x and y is a1x + b1x = c1 a2x + b2x = c2 Where a1, a2, b1, b2 are not zero. A pair of numbers which satisfies both equations is called a simultaneous solution of the given equations or a solution of the system of equations. Geometrically, there are three cases of a simultaneous solution 1. If the system has exactly one solution, the graph of the linear equations intersect in one point. 2. If the system has no solutions, the graphs of the linear equations are parallel. 3. If the system has an infinite number of solutions, the graphs of the linear equations coincide. The special cases (2) and (3) can only occur when the coefficient of x and y in the two linear equations are proportional.

12