Sie sind auf Seite 1von 7

ASSIGNMENT – 4

OF

ADVANCED DATA
STRUCTUTRE

SUBMITTED TO:
SUBMITTED BY:
MR.MANISH SIR.
Sonika Dogra

RG 1005 A 19
Part A
Question 1 . Model a divide and conquer algorithm to
multiply two large integers using only additions and shift
operations.
ANSWER:
Procedure: multiply (A,B)
// assume A, B each have n bits
// the lower order bit of A will be written A[0],
// the highest will be A[n], same for B
let result = 0
let temp = 0
for j = 1 to n do
for i = 1 to n do
temp[i] = B[i] * A[j]
end for
shift temp by (j - 1) bits to the left
set result to result plus temp
set temp = 0
end for
return result

Question 2. Implement a program to display its own file.


All the keywords in the program should be printed in the
upper case letters. While reading file each word should be
matched against a database of keywords to check if it is
to be upper cased or not.
ANSWER:
void mix_up_the_chars(char line[]);
int main()
{
FILE *fp;
char line[80], filename[24];
char *c;

printf("Enter filename -> ");


scanf("%s", filename);
fp = fopen(filename, "r");

do
{
c = fgets(line, 80, fp); /* get a line of text */
if (c != NULL)
{
mix_up_the_chars(line);
}
} while (c != NULL);

fclose(fp);

return 0;
}

/* This function turns all upper case characters into lower case, */
/* and all lower case to upper case. It ignores all other */
/* characters. */
void mix_up_the_chars(char line[])
{
int index;

for (index = 0 ; line[index] != 0 ; index++)


{
if (isupper(line[index])) /* 1 if upper case */
line[index] = tolower(line[index]);
else
{
if (islower(line[index])) /* 1 if lower case */
line[index] = toupper(line[index]);
}
}
printf("%s", line);
}

/* Result of execution

(The selected file is displayed on the monitor with all of


the upper case characters converted to lower case, and all
of the lower case characters converted to upper case.)
*/

3. Draw a huff-man tree of your name for compression.


ANSWER:

T(
5)

T3(3 T1(
) 2)

T2(
2) S(1) A(1)
K(1)

O(1) N(1)
4. Specify a brief account of at least two NP-hard or NP-
Complete practical problems and corresponding heuristics
being employed to solve them.
ANSWER:
The Travelling Salesman Problem (TSP) is an NP-hard
problem in combinatorial optimization studied in operations
research and theoretical computer science. Given a list of cities
and their pair wise distances, the task is to find a shortest
possible tour that visits each city exactly once.
The TSP has several applications even in its purest formulation,
such as planning, logistics, and the manufacture of microchips.
Slightly modified, it appears as a sub-problem in many areas,
such as DNA sequencing. In these applications, the concept city
represents, for example, customers, soldering points, or DNA
fragments, and the concept distance represents travelling times
or cost, or a similarity measure between DNA fragments. In many
applications, additional constraints such as limited resources or
time windows make the problem considerably harder.
Hamiltonian path problem: In the mathematical field of graph
theory the Hamiltonian path problem and the Hamiltonian cycle
problem are problems of determining whether a Hamiltonian path
or a Hamiltonian cycle exists in a given graph (whether directed
or undirected). Both problems are NP-complete. The problem of
finding a Hamiltonian cycle or path is in FNP. There is a simple
relation between the two problems. The Hamiltonian path
problem for graph G is equivalent to the Hamiltonian cycle
problem in a graph H obtained from G by adding a new vertex
and connecting it to all vertices of G. The Hamiltonian cycle
problem is a special case of the traveling salesman problem,
obtained by setting the distance between two cities to a finite
constant if they are adjacent and infinity otherwise.

5. How many spanning trees does a complete graph have?


Provide a proof for the same.
ANSWER:
There may be several minimum spanning trees of the same
weight having a minimum number of edges; in particular, if all the
edge weights of a given graph are the same, then every spanning
tree of that graph is minimum. If there are n vertices in the graph,
then each tree has n-1 edges.
If each edge has a distinct weight then there will only be one,
unique minimum spanning tree. This can be proved by induction
or contradiction. This is true in many realistic situations, such as
the cable TV company example above, where it's unlikely any two
paths have exactly the same cost.

This figure shows that a tree can have different spanning trees.

6. Define any one approach of Hashing algorithm to search


a key.
ANSWER:
DIVISION METHOD: Perhaps the simplest of all the methods of
hashing an integer x is to divide x by M and then to use the
remainder modulo M. This is called the division method of hashing
. In this case, the hash function is

Generally, this approach is quite good for just about any value of
M. However, in certain situations some extra care is needed in the
selection of a suitable value for M. For example, it is often
convenient to make M an even number. But this means that h(x)
is even if x is even; and h(x) is odd if x is odd. If all possible keys
are equiprobable, then this is not a problem. However if, say,
even keys are more likely than odd keys, the function will not
spread the hashed values of those keys evenly.

Das könnte Ihnen auch gefallen