Array is a container which can hold a fix number of items and these items should be of the same type. Most of the data structures make use of arrays to implement their algorithms. Following are the important terms to understand the concept of array.
1. GREEDY METHOD:THE GENERAL METHO
OPTIMAL STORAGE ON TAPES
KNAPSACK PROBLEM
JOB SEQUENCING WITH DEADLINES
OPTIMAL MERGE PATTERNS
2. The greedy method suggests that one can devise an
algorithm that works in stages, considering one input
at a time.
At each stage, a decision is made regarding whether a
particular input is in an optimal solution.
This is done by considering the inputs in an order
determined by some selection procedure.
The greedy method is perhaps the most straight
forward design technique we consider in this text, and
what’s more it can be applied to a wide variety of
problems.
3. 1.Algorithm Greedy(a,n)
2.//a[1:n]contains the n inputs.
3.{
4. solution:=0;//Intialize the solution.
5. for i:=1 to n do
6. {
7. x:=select(a);
8. if Feasible(solution,x)then
9. solution:=union(solution,x);
10. }
11. return solution;
12.}
4. A large ship is to be loaded with cargo. The cargo is
containerized, and all containers are the same size. Different
containers may have different weights.
The sip may be loaded in stages; one container per stage. At
each stage we need to select a container to load.
For this decision we may use the greedy criterion: from the
remaining containers, select the one with least weight.
This order of selection will keep the total weight of the selected
containers minimum and hence leave maximum capacity for
loading more containers.
Using the greedy algorithm just outlined, we first select the
container that has least weight, then the one with the next
smallest weight, and so on until either all containers have been
loaded or there isn’t enough capacity for the next one.
5. {
//sort into increasing order
sort (c,numberO f Containers);
n:=numberO f Containers;
//initialize x
for i:=1 to n do
x[i]:=0;
//select containers in order of weight
i:=1;
While(i<n&&c[i]weight<capacity)
{
//enough capacity for container c[i].id
x[c[i].id]:=1;
Capacity--=c[i].weight;
//remaining capacity
i++
}
}
6. 1.Suppose you have 6 containers whose weights are
50,10,30,20,60,and 5 and a ship whose capacity is 100.
use algorithm 4.2 to find an optimal solution to this
instance of the container loading problem.
2.Extend the greedy solution for the container loading
problem to the case when there are two ships. Does
your algorithm always generate optimal solutions?
3.We are given n tasks to perform is sequence. Suppose
that task needs it units of time. If the tasks are done in
the order 1,2,….,n, then task I completes at time
chi=∑j
i=1 tj. The average completion time (ACT) is
1/n∑n
i=1ci.
7. `Optimal merge pattern is a pattern that relates to the
merging of two or more sorted files in a single sorted file.
This type of merging can be done by the two-way merging
method.
If we have two sorted files containing n and m records
respectively then they could be merged together, to obtain
one sorted file in time O (n+m).
There are many ways in which pairwise merge can be done
to get a single sorted file. Different pairings require a
different amount of computing time.
8. The formula of external merging cost is:
Where, f (i) represents the number of records in each
file and d (i) represents the depth.
The main thing is to pairwise merge the n
sorted files so that the number of comparisons
will be less.
9. Tree(n)
//list is a global list of n single node
{
For i=1 to i= n-1 do
{
// get a new tree node
Pt: new treenode;
// merge two trees with smallest length
(Pt = lchild) = least(list);
(Pt = rchild) = least(list);
(Pt =weight) = ((Pt = lchild) = weight) = ((Pt = rchild)
= weight);
Insert (list , Pt);
}
// tree left in list
Return least(list);
}
10. Given a set of unsorted files: 5, 3, 2, 7, 9, 13
Now, arrange these elements in ascending order: 2, 3,
5, 7, 9, 13
After this, pick two smallest numbers and repeat this
until we left with only one number.
15. The sequencing of jobs on a single processor with deadline
constraints is called as Job Sequencing with Deadlines.
You are given a set of jobs.
Each job has a defined deadline and some profit associated
with it.
The profit of a job is given only when that job is completed
within its deadline.
Only one processor is available for processing all the jobs.
Processor takes one unit of time to complete a job.
16. A feasible solution would be a subset of jobs where
each job of the subset gets completed within its
deadline.
Value of the feasible solution would be the sum of
profit of all the jobs contained in the subset.
An optimal solution of the problem would be a feasible
solution which gives the maximum profit.
17. Greedy Algorithm is adopted to determine how the next job is selected for an
optimal solution.
The greedy algorithm described below always gives an optimal solution to the
job sequencing problem-
Step-01:
Sort all the given jobs in decreasing order of their profit.
Step-02:
Check the value of maximum deadline.
Draw a Gantt chart where maximum time on Gantt chart is the value of
maximum deadline.
Step-03:
Pick up the jobs one by one.
Put the job on Gantt chart as far as possible from 0 ensuring that the job gets
completed before its deadline.
18. Problem-
Given the jobs, their deadlines and associated profits as
shown-
Jobs J1 J2 J3 J4 J5 J6
Deadlines 5 3 3 2 4 2
Profits 200 180 190 300 120 100
19. Answer the following questions-
1.Write the optimal schedule that gives maximum profit.
2.Are all the jobs completed in the optimal schedule?
3.What is the maximum earned profit?
Jobs J4 J1 J3 J2 J5 J6
Deadlines 2 5 3 3 4 2
Profits 300 200 190 180 120 100
Solution-
Step-01:
Sort all the given jobs in decreasing order of their profit-
20. Step-02:
Value of maximum deadline = 5.
So, draw a Gantt chart with maximum time on Gantt chart = 5 units as shown-
Now,
•We take each job one by one in the order they appear in Step-01.
•We place the job on Gantt chart as far as possible from 0.
21. Step-03:
•We take job J4.
•Since its deadline is 2, so we place it in the first empty cell before deadline 2 as-
22. Step-04:
•We take job J1.
•Since its deadline is 5, so we place it in the first empty cell before deadline 5 as-
23. Step-05:
•We take job J3.
•Since its deadline is 3, so we place it in the first empty cell
before deadline 3 as-
24. Step-06:
•We take job J2.
•Since its deadline is 3, so we place it in the first empty cell before deadline 3.
•Since the second and third cells are already filled, so we place job J2 in the
first cell as-
25. Step-07:
•Now, we take job J5.
•Since its deadline is 4, so we place it in the first empty cell before deadline 4 as-
26. Now,
The only job left is job J6 whose deadline is 2.
All the slots before deadline 2 are already occupied.
Thus, job J6 can not be completed.
Now,
The given questions may be answered as-
27. Part-01:
The optimal schedule is-
J2 , J4 , J3 , J5 , J1
This is the required order in which the jobs must be
completed in order to obtain the maximum profit.
Part-02:
•All the jobs are not completed in optimal schedule.
•This is because job J6 could not be completed
within its deadline.
28. Part-03:
Maximum earned profit
= Sum of profit of all the jobs in
optimal schedule
= Profit of job J2 + Profit of job J4
+ Profit of job J3 + Profit of job J5
+ Profit of job J1
= 180 + 300 + 190 + 120 + 200
= 990 units