0% found this document useful (0 votes)
0 views

algorithm

The document contains various algorithmic problems and their solutions, including Big-O notation, iterative merge sort, longest increasing subsequence, Dijkstra's algorithm, and Prim's algorithm. It provides specific examples and asks for the design of algorithms to solve these problems, along with explanations of their workings. Additionally, it discusses the relationship between different algorithmic concepts and their applications in graph theory.

Uploaded by

nouhaila081201
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

algorithm

The document contains various algorithmic problems and their solutions, including Big-O notation, iterative merge sort, longest increasing subsequence, Dijkstra's algorithm, and Prim's algorithm. It provides specific examples and asks for the design of algorithms to solve these problems, along with explanations of their workings. Additionally, it discusses the relationship between different algorithmic concepts and their applications in graph theory.

Uploaded by

nouhaila081201
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

subproblcm.

For example, the tine


t a c k l i n g
a
for
same
istrue
ZU4 B0I, 2024 The
S9 22 A subproblems
answer sheet. in the
means tree?(3")
(which levels
of
Note: Please
s u b p r o b l e m
I. O) (15") many
Let f(n) and gín)
grows no How
write vour soluti on the
be functions
from ons
to positise rcals Weesay fa
)
each
in the k-th level
hat " f grows cXgn)
e (n) size of
relation
pos1tive integers is the intthe k-th level?
(ihe top
sub-problens
no faster What evel
thang")if S Prove
the ) (3)
0suck that/ (n)
O many
here is a constantI c >
c o m p l e x i t y
offthe k-th
How
faster than (n),
we say f Q(g). f= (3). (4) level?(3") is'S
formular. time
Irf- O(g) and f Q(g). we say
ofAn) and g(n) for of cach What is the
the following notations )
questions. (6') Give Big-O
(1) An) =n- 100 complexity (3')
&n) =3n + 20
while
gn) = 9n'+10 263 1,
(2) fn) =n- 100
represent is
can integer
variable
2. In 64-bit
words,
computers, the maximum value that a signcd Lnteger other
. 1. Ia to
an unsigncd integer Value of 264 we want
variable can represent a maximum system. If
binary n/4
bits in I110"
and
"1010...
variables can only represent
numbers with the lerngth Snaller than 64 system like
addition
original
get thc addition result
of two integers with 100 bits of binaryY the
variables),
string two
addition of
the
"1001... 1011" (which are too
large and can only be stored in implement

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

of the edge is one.). (4')

full of tunnels and branching paths, it's very easy to get


6. When you want to explore an underground cave
how to get out. Please design a method to explore every
lost, not knowing if we've searched every coner or
corner efficiently without getting lost. (9)
following graph, starting at node A, (a) Draw one or multip
on the
7.Suppose Dijkstra's algorithm is run nodes at each iteration c
intermediate distance values (dist(v)) and prev values of all the
table showing the
the algorithm. (b) Show the final shortest-path tree. (15')
7 2

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

H=makequeue (V) (using ist-values


while H is not
empty: as keys)
u= deletemin(H)
for all edges (u, v)
if dist(u) > E:
dist(u) +l(u,v):
dist(u) = dist(u) +(u, v)
prev(v) =u
decreasekey(H,u)
to findthe minimum spanning tree, in
r multiple 8.As sbown in the following figures, Prim's algorithm is usually uscd
subtree, and S is chosen to be the set of this tree's
teration of which the intermediate set of edges X always forms a
the lightest edge betweena
On cach iteration, the subtree defined by X grows by one edge, namely
vertices.
the changes of cost and prev values, and
vertex in S and a vertex outside S. 1) Draw a table to show
construct the final Minimum Spanning Tree. (12") 2) What is the
main difference between the Dijkstra's
algorithm and the Prim's algorithm. (3')

procedure prin (G, w)


(V,E) with edge weights We
Input : A connected undirected graph G=
array prev
Output A ninimum spanning tree def ined by the

for all ue V:
ccst(u)=x
prevu) = nil
Pick any initial node ug
cost(se) = 0

(prior1ty queue, using cost-values as


keys)
H= akequeue (V)
vtile H is not empty:
t= eleteuinH)
for each (t,:)¬E:
1t costí:) > w(r, 2):
cost(:) = vu, :)
prev:)=
decreasekey( H.z)

2
A
1
1
5
D
B)
1 1
2
5
E H
10

You might also like