Sunteți pe pagina 1din 11

UNIT 1

Purpose Of Database – Overall System Structure – Entity Relationship Model – Mapping


Constraints – Keys – ER Diagram
Informal Definition
An Algorithm is any well-defined computational procedure that takes some value or
set of values as Input and produces a set of values or some value as output. Thus algorithm is a
sequence of computational steps that transforms the input into the output.
Formal Definition
An Algorithm is a finite set of instructions that, if followed, accomplishes a
particular task. In addition, all algorithms should satisfy the following criteria.
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 the instructions of an algorithm, then for all cases, the
algorithm terminates after a finite number of steps.
5. EFFECTIVENESS  Every instruction must very basic so that it can be carried out, in
principle, by a person using only pencil & paper.
Issues or study of Algorithm
 How to device or design an algorithm  creating and algorithm.
 How to express an algorithm  definiteness.
 How to analysis an algorithm  time and space complexity.
 How to validate an algorithm  fitness.
 Testing the algorithm  checking for error.

Algorithm Specification
Algorithm can be described in three ways.
1. Natural language like English
When this way is chose care should be taken, we should ensure that each & every
statement are definite.
2. Graphic representation called flowchart
This method will work well when the algorithm is small& simple.
3. Pseudo-code Method
In this method, we should typically describe algorithms as program, which
resembles language like Pascal & algorithm.
Efficiency of an Algorithm
The efficiency of the algorithms can be listed by following cases. They are
 Empirical analysis
 Theoretical analysis
 Hybrid analysis
The efficiency of an algorithm can be anglicized by means of Time efficiency and Space efficiency

ICW/CS/DAA/Unit 1 Page 1
Time efficiency
The efficiency of an algorithm depends on the time it takes to perform a task.
Space efficiency
The efficiency of an algorithm depends on the memory space it require to perform a task.
Pseudo-Code Conventions
1. Comments begin with // and continue until the end of line.
2. Blocks are indicated with matching braces {and}.
3. An identifier begins with a letter. The data types of variables are not explicitly declared.
4. Compound data types can be formed with records. Here is an example,
Node Record
{
data type – 1 data-1;
.
.
.
data type – n data – n;
node * link;
}
Here link is a pointer to the record type node. Individual data items of a record can
be accessed with  and period.
5. Assignment of values to variables is done using the assignment statement.
<Variable>:= <expression>;
6. There are two Boolean values TRUE and FALSE.
 Logical Operators AND, OR, NOT
Relational Operators <, <=,>,>=, =,!=
7. The following looping statements are employed.
For, while and repeat-until
While Loop
While < condition > do
{
<statement-1>
.
.
<statement-n>
}
For Loop
For variable: = value-1 to value-2 step step do
{
<statement-1>
.
.
.
<statement-n>
}
ICW/CS/DAA/Unit 1 Page 2
repeat-until
repeat
<statement-1>
.
.
.
<statement-n>
until<condition>
8. A conditional statement has the following forms.
 If <condition> then <statement>
 If <condition> then <statement-1>
Else <statement-1>
Case statement
Case
{
: <condition-1> : <statement-1>
.
.
.
: <condition-n> : <statement-n>
: else : <statement-n+1>
}
9. Input and output are done using the instructions read & write.
10. There is only one type of procedure:
Algorithm, the heading takes the form,
Algorithm Name (Parameter lists)
 As an example, the following algorithm fields & returns the maximum of ‘n’ given
numbers:
1. algorithm Max(A,n)
2. // A is an array of size n
3. {
4. Result := A[1];
5. for I:= 2 to n do
6. if A[I] > Result then
7. Result :=A[I];
8. return Result;
9. }
In this algorithm (named Max), A & n are procedure parameters. Result & I are Local variables.
Recursive Algorithms
 A Recursive function is a function that is defined in terms of itself.
 Similarly, an algorithm is said to be recursive if the same algorithm is invoked in the body.

ICW/CS/DAA/Unit 1 Page 3
 An algorithm that calls itself is Direct Recursive.
 Algorithm ‘A’ is said to be Indirect Recursive if it calls another algorithm which in turns
calls ‘A’.
 The Recursive mechanism, are externally powerful, but even more importantly, many times
they can express an otherwise complex process very clearly.
The following example shows how to develop a recursive algorithms. We consider the Towers of
Hanoi problem
1. Towers of Hanoi
 We have 3 towers as Tower A Tower B Tower C. It is Fashioned after the ancient tower of
Brahma ritual.
 According to legend, at the time the world was created, there was a diamond tower (labelled
A) with 64 golden disks.
 The disks were of decreasing size and were stacked on the tower in decreasing order of size
bottom to top.

 Besides these tower there were two other diamond towers (labelled B & C)
 Since the time of creation, Brahman priests have been attempting to move the disks from
tower A to tower B using tower C, for intermediate storage.
 As the disks are very heavy, they can be moved only one at a time. In addition, at no time
can a disk be on top of a smaller disk.
 According to legend, the world will come to an end when the priest have completed this
task.
 A very elegant solution results from the use of recursion. Assume that the number of disks
is ‘n’.
 To get the largest disk to the bottom of tower B, we move the remaining ‘n-1’ disks to
tower C and then move the largest to tower B.
 Now we are left with the tasks of moving the disks from tower C to B. To do this, we have
tower A and B available.
 The fact, that towers B has a disk on it can be ignored as the disks large than the disks being
moved from tower C and so any disk scan be placed on top of it.

ICW/CS/DAA/Unit 1 Page 4
Algorithm
Algorithm TowersofHanoi(n,x,y,z)
//Move the top ‘n’ disks from tower x to tower y.
{
if(n>=1) then
{
TowersofHanoi(n-1,x,z,y);
Write(“move top disk from tower “ X ,”to top of tower “ ,Y);
Towersofhanoi(n-1,z,y,x);
}
}
Define input size
The input size of any instance characteristics that is frequently used in the life afire is the
input size. The input size of any instance of a problem is defined to be the no. of words to describe
that instance.
Performance Analysis
The performance of a program is the amount of computer memory and time needed to run a
program.
1. Space Complexity
The space complexity of an algorithm is the amount of money it needs to run to
compilation. The space need by a program has the following components:
Instruction space: Instruction space is the space needed to store the compiled version of the
program instructions.
Data space: Data space is the space needed to store all constant and variable values. Data space has
two components:
 Space needed by constants and simple variables in program.
 Space needed by dynamically allocated objects such as arrays and class instances.

ICW/CS/DAA/Unit 1 Page 5
Environment stack space: The environment stack is used to save information needed to resume
execution of partially completed functions.
Instruction Space: The amount of instructions space that is needed depends on factors such as:
 The compiler used to complete the program into machine code.
 The compiler options in effect at the time of compilation
 The target computer.
2. Time Complexity
The time complexity of an algorithm is the amount of computer time it needs to
run to compilation.
Space Complexity
Space Complexity Example:
Algorithm abc(a,b,c)
{
return a+b++*c+(a+b-c)/(a+b) +4.0;
}
 The Space needed by each of these algorithms is seen to be the sum of the following
component.
A fixed part that is independent of the characteristics (eg: number, size)of the inputs and outputs.
The part typically includes the instruction space (ie. Space for the code), space for simple variable
and fixed-size component variables (also called aggregate) space for constants, and so on.
A variable part that consists of the space needed by component variables whose size is dependent
on the particular problem instance being solved, the space needed by referenced variables (to the
extent that is depends on instance characteristics), and the recursion stack space.
a. The space requirement s(p) of any algorithm p may therefore be written as,
S(P) = c+ Sp(Instance characteristics)
Where ‘c’ is a constant.
Example 1
Algorithm sum(a,n)
{
s=0.0;
for I=1 to n do
s= s+a[I];
return s;
}
 The problem instances for this algorithm are characterized by n, the number of elements
to be summed. The space needed d by ‘n’ is one word, since it is of type integer.
 The space needed by ‘a’a is the space needed by variables of type array of floating point
numbers.
 This is atleast ‘n’ words, since ‘a’ must be large enough to hold the ‘n’ elements to be
summed.
 So, we obtain sum(n)>=(n+s) [ n for a[],one each for n,I a& s]

ICW/CS/DAA/Unit 1 Page 6
Time Complexity
The time T(p) taken by a program P is the sum of the compile time and the run
time(execution time)
The compile time does not depend on the instance characteristics. Also we may assume that a
compiled program will be run several times without recompilation .This rum time is denoted by tp
(instance characteristics).
 The number of steps any problem statement t is assigned depends on the kind of statement.
For example, comments  0 steps.
Assignment statements  1 steps.[Which does not involve any calls to other algorithms]
Interactive statement such as for, while & repeat-until Control part of the statement.
We introduce a variable, count into the program statement to increment count with initial
value 0.Statement to increment count by the appropriate amount are introduced into the program.
This is done so that each time a statement in the original program is executes count is incremented
by the step count of that statement.
Algorithm
Algorithm sum(a,n)
{
s= 0.0;
count = count+1;
for I=1 to n do
{
count =count+1;
s=s+a[I];
count=count+1;
}
count=count+1;
count=count+1;
return s;
}
 If the count is zero to start with, then it will be 2n+3 on termination. So each invocation
if sum execute a total of 2n+3 steps.
First determine the number of steps per execution (s/e) of the statement and the total
number of times (ie., frequency) each statement is executed. The second method to determine the
step count of an algorithm is to build a table in which we list the total number of steps contributes
by each statement.
By combining these two quantities, the total contribution of all statements, the step count
for the entire algorithm is obtained.
Statement S/e Frequency Total
1. Algorithm Sum(a,n) 0 - 0
2.{ 0 - 0
3. S=0.0; 1 1 1
4. for I=1 to n do 1 n+1 n+1
5. s=s+a[I]; 1 n n
6. return s; 1 1 1
7. } 0 - 0

ICW/CS/DAA/Unit 1 Page 7
Total 2n+3

Complexity of Algorithms
The complexity of an algorithm M is the function f(n) which gives the running time and/or
storage space requirement of the algorithm in terms of the size ‘n’ of the input data. Mostly, the
storage space required by an algorithm is simply a multiple of the data size ‘n’. Complexity shall
refer to the running time of the algorithm.
The function f(n), gives the running time of an algorithm, depends not only on the size ‘n’ of the
input data but also on the particular data. The complexity function f(n) for certain cases are:
 Best Case: The minimum possible value of f(n) is called the best case.
 Average Case : The expected value of f(n).
 Worst Case: The maximum value of f(n) for any key possible input.
Asymptotic Notations
The following notations are commonly use notations in performance analysis and used to
characterize the complexity of an algorithm:
 Big–OH (O) ,
 Big–OMEGA (Ω),

 Big–THETA ( ) and
 Little–OH (o)
Big Oh Notation, Ο
The notation Ο(n) is the formal way to express the upper bound of an algorithm's running time. It
measures the worst case time complexity or the longest amount of time an algorithm can possibly
take to complete.
For example, for a function f(n)
Ο(f(n)) = { g(n) : there exists c > 0 and n0 such that f(n) ≤
c.g(n) for all n > n0. }

Omega Notation, Ω
The notation Ω(n) is the formal way to express the lower bound of an algorithm's running time. It
measures the best case time complexity or the best amount of time an algorithm can possibly take
to complete.

For example, for a function f(n)


Ω(f(n)) ≥ { g(n) : there exists c > 0 and n0 such that g(n)
≤ c.f(n) for all n > n0. }

ICW/CS/DAA/Unit 1 Page 8
Theta Notation, θ
The notation θ(n) is the formal way to express both the lower bound and the upper bound of an
algorithm's running time. It is represented as follows −

θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) =


Ω(f(n)) for all n > n0. }

Amortized Analysis
Amortized Analysis is used for algorithms where an occasional operation is
very slow, but most of the other operations are faster.
The actual complexity of a sequence of operations may be determined by adding
together the step counts for the individual operations in the sequence.
In determining the complexity of a sequence of operations, we can obtain tighter
bounds using amortized complexity rather than worst-case complexity.
The amortized complexity of an operation could be anything. The only
requirement is that the sum of the amortized complexities of all operations in the sequence be
greater than or equal to the sum of the actual complexities. That is
∑𝟏≤𝒊≤𝒏 𝒂𝒎𝒐𝒓𝒕𝒊𝒛𝒆𝒅(𝒊) >= ∑𝟏≤𝒊≤𝒏 𝒂𝒄𝒕𝒖𝒂𝒍(𝒊) ------------ (1)

where amortized(i) and actual(i), respectively, denote the amortized and actual complexities of the
ith operation in a sequence of n operations.
Relative to the actual and amortized costs of each operation in a sequence of n
operations, we define a potential function P(i) as below
𝑷(𝒊) = 𝒂𝒎𝒐𝒓𝒕𝒊𝒛𝒆𝒅 (𝒊) − 𝒂𝒄𝒕𝒖𝒂𝒍 (𝒊) + 𝑷(𝒊 − 𝟏) ------------------------(2)

That is, the ith operation causes the potential function to change by the difference between the
amortized and actual costs of that operation. If we sum Equation (2) for 1 ≤ 𝑖 ≤ 𝑛, we get
∑1≤𝑖≤𝑛 𝑃(𝑖)=∑1≤𝑖≤𝑛(𝑎𝑚𝑜𝑟𝑡𝑖𝑧𝑒𝑑(𝑖) − 𝑎𝑐𝑡𝑢𝑎𝑙(𝑖) + 𝑃(𝑖 − 1))
or
∑1≤𝑖≤𝑛(𝑃(𝑖) − 𝑃(𝑖 − 1__𝑎𝑚𝑜𝑟𝑡𝑖𝑧𝑒𝑑(𝑖)=∑1≤𝑖≤𝑛(𝑎𝑚𝑜𝑟𝑡𝑖𝑧𝑒𝑑(𝑖) − 𝑎𝑐𝑡𝑢𝑎𝑙(𝑖))
or
𝑃(𝑛) − 𝑃(0)=∑1≤𝑖≤𝑛(𝑎𝑚𝑜𝑟𝑡𝑖𝑧𝑒𝑑(𝑖) − 𝑎𝑐𝑡𝑢𝑎𝑙(𝑖))

From Equation (1), it follows that


𝑷(𝒏) − 𝑷(𝟎) ≥ 𝟎 --------------------------------------------------------------(3)
Under the assumption that 𝑃(0) = 0, the potential P(i) is the amount by which the first i operations
have been overcharged.
Equation (3) must hold for all non negative integers.
The following three methods to arrive at amortized costs for operations
AggregateMethod
In the aggregate method, we determine an upper bound UpperBoundOnSumOfActualCosts(n) for
the sum of the actual costs of the n operations. The amortized cost of each operation is set equal to
UpperBoundOnSumOfActualCosts(n)/n. You may verify that this assignment of amortized costs
satisfies Equation (1) and is, therefore, valid.
ICW/CS/DAA/Unit 1 Page 9
AccountingMethod
In this method, we assign amortized costs to the operations, compute the P(i)s using Equation (2),
and show that 𝑃(𝑛) − 𝑃(0) ≥ 0.
PotentialMethod
Here, we start with a potential function that satisfies Equation (3), and compute the amortized
complexities using Equation (2).
ProblemDefinition
Consider the car maintenance contract: $50 each month other than March, June, September and
December, $100 every March, June, and September and $200 every December. We are to obtain an
upper bound on the cost of this maintenance contract as a function of the number of months.

Worst-CaseMethod
we can bound the contract cost for the first n months by taking the product of n and the maximum
cost incurred in any month (i.e., $200). The upper bound is correct because the actual cost for n
months does not exceed $200n.

AggregateMethod
To use the aggregate method for amortized complexity, we first determine an upper bound on the
sum of the costs for the first n months. As tight a bound as is possible is desired. The sum of the
actual monthly costs of the contract for the first n months is
200*floor(n/12) + 100*(floor(n/3) - floor(n/12)) + 50*(n - floor(n/3))
= 100*floor(n/12) + 50*floor(n/3) + 50*n
<= 100*n/12 + 50*n/3 + 50*n
= 50n(1/6 + 1/3 + 1)
= 50n(3/2)
= 75n
The amortized cost for each month is set to $75. The table below shows the actual costs,
theamortized costs, and the potential function value (assuming P(0) = 0) for the first 16 months of
the contract.
month | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
actual cost | 50 50 100 50 50 100 50 50 100 50 50 200 50 50 100 50
amortized cost | 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75
P() | 25 50 25 50 75 50 75 100 75 100 125 0 25 50 25 50

Notice that some months are charged more than their actual costs and others are charged less than
their actual cost.
AccountingMethod
From the above table, we see that using any cost less than $75 will result in P(n) - P(0) < 0 for
some values of n. Therefore, the smallest assignable amortized cost consistent with Equation (3) is
$75.
sum1 <= i <= k f(i) * amortized(i)
where k is the number of different operation types and f(i) is the frequency of operation type i.

For our maintenance contract example, we might try an amortized cost of $70. When we use this
amortized cost, we discover that Equation (3) is not satisifed for n = 12 (for example) and so $70 is
an invalid amortized cost assignment.

ICW/CS/DAA/Unit 1 Page 10
PotentialMethod
We first define a potential function for the analysis using Equation (3), and then use the potential
function and the actual operation cost to verify the amortized costs.

If we are extremely experienced, we might start with the potential function

P(0) = 0
P(n) = 0 for n mod 12 = 0
P(n) = 25 for n mod 12 = 1 or 3
P(n) = 50 for n mod 12 = 2, 4 or 6
P(n) = 75 for n mod 12 = 5, 7 or 9
P(n) = 100 for n mod 12 = 8, or 10
P(n) = 125 for n mod 12 = 11

we proceed to use Equation (2) to determine the amortized costs.

From Equation (2), we obtain


𝑎𝑚𝑜𝑟𝑡𝑖𝑧𝑒𝑑(𝑖) = 𝑎𝑐𝑡𝑢𝑎𝑙(𝑖) + 𝑃(𝑖) − 𝑃(𝑖 − 1))

Therefore,
amortized(1) = actual(1) + P(1) - P(0) = 50 + 25 - 0 = 75,
amortized(2) = actual(2) + P(2) - P(1) = 50 + 50 - 25 = 75,
amortized(3) = actual(3) + P(3) - P(2) = 100 + 25 - 50 = 75, and so on.
Therefore, the amortized cost for each month is $75. So, the actual cost for n months is at most
$75n.
Randomized Algorithms

A randomized algorithm is a technique that uses a source of randomness as part of its logic. It is
typically used to reduce either the running time, or time complexity; or the memory used, or space
complexity, in a standard algorithm. The algorithm works by generating a random number, , within
a specified range of numbers, and making decisions based on value.
A randomized algorithm could help in a situation of doubt by flipping a coin or a drawing a card
from a deck in order to make a decision.
Randomized algorithms are usually designed in one of two common forms: as a Las Vegas or as a
Monte Carlo algorithm.
A Las Vegas algorithm runs within a specified amount of time. If it finds a solution within that
timeframe, the solution will be exactly correct; however, it is possible that it runs out of time and
does not find any solutions.
A Monte Carlo algorithm is a probabilistic algorithm which, depending on the input, has a slight
probability of producing an incorrect result or failing to produce a result altogether.

ICW/CS/DAA/Unit 1 Page 11

S-ar putea să vă placă și