algorithm
algorithm
operation of algorithn to
computers can not be uscd Plcase design an /16 n/16| n/16|
string variables with more than 100 bits n/16||n/16|3
for cach. (12)
t 3
4.The following figure shows an iterativc-Mergsort algorithm. ay, a2, .., a, is the set of numbers nceded to5. In the long
be sorted.
Initially, cacch clement of a,, az, , a,is considered as an array separately and cach array is added an Asubscqu
e queue Q. Then, removing two arrays fromthe front of the queue, merging them, and putting theil<i2 <.<
result
array at the end of the queue. The process is iteratively done until there is only one array in the queue. The task is t
When merging two arrays (merge), they are done recursively and the first numbers of all arrays arc subsequence c
compared and the smaller one is removed every time. The operation lnject adds an clement to the end of to find the lor
the queue while eject removes and returns the element at the front of the queuc. Let the unsorted array
contains 34, 81, 15, 24, 3, 25, 71, 8. Please show states of the Q at each step, in other words, show the subsequence
changes of Q(10'). number is rep
of the cdge is
function iterative-mergesort (a|1...n|)
Input: elements aj, 02,... , (ly tO be sorted
Q= (empty queue)
for i =1 to n: 6. When you
inject(Q, [es]) lost, not knov
while Q>1: corner efficie
inject(Q. merge(eject(), eject(Q)
return eject(Q)
aj,az, .
o 5. In the longest increasing subsequence finding problem, the input is a sequence of numbers
a.A subsequence is any subset of these numbers taken in order, of the form a1
, a2 , ..., au where 1
d
eil<i2<.< ik<n, and an increasing subsequence is one in which the numbers
are getting strictly larger.
e. The task is to find the increasing subsequence of greatest length. For instance, the longest increasing
programming based algorithm
resubsequence of 5, 2, 8, 6, 3, 6,9, 7 is 2, 3,6, 9. ( Please design a dynamic
f to find the longest subsequence. (10) What is the relationship between the above fnding the longest
Directed Acyclic Graph (each
subsequence problem and the problem of fnding the longest path in a
there is an edge from a1 to a2 and the length
number is represented by a node. If aË < a2 and il<i2,
1e
4
3 5
B (D
1
2
5
{H
10
procedure dijkstra(G.l. s)
Input : Graph G=(V,E), directed or undirected;
positive edge lengths {le:ee E};
Output : For all vèrtices u reachable fromvertex \EV
to the distance from s s, dist (u) is set
to u.
for all u¬V:
dist(u) =0
prev(u)=nil
dist (s) =0
for all ue V:
ccst(u)=x
prevu) = nil
Pick any initial node ug
cost(se) = 0
2
A
1
1
5
D
B)
1 1
2
5
E H
10