Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 10

15.01.

2009

1.
) h(k ) = k m
) h( k ) = k mod m
) h(k ) = m (k A mod 1)
(0.5)
2.

)
)
)
(0.5)
3.

? (
), +1 ( 1
).
) heap
)
)
(0.5)
4.
(Merge Sort)?
) O ( n log 2 n)
) O (n 2 )
) O (2 n )
(0.5)
5. ,
O(n)?
) (Insertion Sort)
) (Counting Sort)
) heap (Heap Sort)
(0.5)
6.
( )
?
)
) - (Backtracking and alpha-beta
pruning)
) (Greedy Algorithms)
(0.5)
7.

) (Dynamic Programming)
) (Divide and Conquer)
) (Greedy Algorithms)
(0.5)

8.
?
void hanoj(n,poc,kraj,sred)
int n;
char poc, sred, kraj;
{
if(n = = 1)
cout <<"\nOd "<<poc<<" na "<< kraj;
else
{
hanoj(n-1, poc, sred, kraj);
cout <<"\nOd "<<poc<<" na "<<kraj;
hanoj(n-1, sred, kraj, poc);
}
}
) O ( n log 2 n)
) O (n 2 )

) O (2 n )
(0.5)

9. ?

(0.5)
)
)
)
10.
)
)

)
(0.5)
11. .
(2)

.
(chaining).


, .

. : h(k,i) = (k+i) mod m
12.
(Breadth First Search).
(2)
BFS(G,s)
1 for ekoe teme u
V[G] - {s}
2
do color[u] WHITE
3
d[u]
4
[u] NIL
5 color[s] GRAY
6 d[s] 0
7 [s] NIL
8 Q {s}
9 whileQ
10
do u head[Q]
11
for sekoe v Adj[u]
12
do if color[v] = WHITE
13
then color[v] GRAY
14
d[v] d[u] + 1
15
[v] u
16
ENQUEUE(Q,v)
17
DEQUEUE(Q)
18
color[u] BLACK

BFS .
color[u],
[u]. ( )
[u] = NIL. u
d[u]. Q (first-in, first-out queue)
.
13.
(depth first search).
.
(2)

a
b

c
e

:
1. DFS G
.
2. Gr G.
3. DFS Gr
1. DFS
DFS .
4.
G.
a

5
e
4
f

a
3

5
b

4
2
c

{}, {} {b, d, c, f}
14. 1
Dijkstra.
(2)

40

70

10

100

20

30
5

30

50

:
40

70

V2(70,V1)
20

V4(40,V1)
4

10

100

V6(,V1)

30
5

30

50
V5(30,V1)

V6(60,V5)

S={V1,V5,V4,V6,V2,V3}

40

70

2
30

4
6

30

V2(,V1)

20

3
5

V2(90,V2)

15.
(backtracking) - .
(2)
max

min

max E

N
1

max E

6
L
6

T
4

G
N

V
2

H
P

C 3

K
3

:
max

min

R
5

J
T

16. 1 , 50 , 10
. 500
(, F/E ).
5 .
) 1000 (buckets),
?
) ,
?
) )
?
(5)

V
2

:
Nz=1 000 000
Lz=50B

Lk=10B

LB=500B

Lp=5B

B=1000

Nzb
/
Nbb
/
Nbk

) Nbk=B*Lp/LB=1000*5/500=10
B=1000 , 10 .
) Nzb=Nz/B=1 000 000/1000=1000 /
1000 .
1000 .
Nbb=Nzb/(Lb/Lz)=1000/(500/50)=100 /
100 , 10 .
) =1+(Nbb-1)/2=1+99/2=50.5
:
.
17.
6 / 65 . 65
65=16+15+14+12+8.
(5)
:
1
2
3
4
5
6

1
116
18
14
12
11

2
115
17
13
11
651

3
114
16
12
331

4
112
14
172
171

5
18
94
92
91

6
58
54
52
51

65
40
36
34
33
65

18. binaren_string(n) n .
a[1..n]. pecati(a)
. .

void binaren_string(int n){


if (n= = -1)
pecati();
else
{
a[n]=0; binaren_string(n-1);
a[n]=1; binaren_string(n-1);
}
}
(5)
:
a[1]=0
a[2]=0 bin(1)
a[3]=0 bi(2)

a[1]=1
a[2]=1 ..

c...ako...n = 1

T ( n) =
2T ( n 1) + d ..za..n > 1

T(n)=2T(n-1)+d
T(n-1)=2T(n-2)+d
T(n-2)=2T(n-3)+d

T(n)=2T(n-1)+d=2[2T(n-2)+d]+d=4T(n-2)+2d+d=4[2T(n-3)+d]+2d+d
=8T(n-3)+4d+2d+d

i 1

= 2 i T (n i) + d 2 j =
j =0

n 2

= 2 n 1 T (1) + d 2 j =
j =0

=2n-1c+d(1+2++2n-2)=
= 2 n 1 c + d

2 n 1 1
=
2 1

=2n-1(c+d)-d
O(2n)
19. C++
(Insertion Sort).
.
,
?

(5)
# include <iostream>
void InsertionSort( int A[ ], int N )
{
int j, i;
int Tmp;
for( i = 1; i < N; i++ )
{
Tmp = A[i];
for( j = i; j > 0 && A[j - 1] > Tmp; j-- )
A[j] = A[j - 1];
A[j] = Tmp;
}
}
void main() {
int A[10];
int n, i;
cout <<"Vnesete broj na elementi na poleto n=";
cin>>n;
cout <<"Vnesete gi elementite na poleto"<<endl;
for (i=0; i<n; i++)
{
cout<<" A["<<i<<"]=";
cin>>A[i];
}
InsertionSort(A,n);
cout <<"Sortirano pole "<<endl;
for (i=0; i<n; i++)
cout<<" A["<<i<<"]="<< A[i]<<endl;
}
(n2).
T(n)=(n).

n 1

T (n) = i = 1 + 2 + 3 + ... + n = (n 2 )
i =1


n 1

T (n) = i / 2 = ( n 2 )
i =1

20.) C++
.

.
(divide and conquer) .
) ,
?
(5)
)
int najdi(int elem,int A[],int l,int r){
int m;
m=(l+r)/2;
if (elem==A[m]) return(m);
else
if (elem<A[m]) najdi(elem, A, 1, m-1);
else najdi(elem, A, m+1, r);
}
)
T(1)=c
T(n)=d+T(n/2)
T(n/2)=T(n/4)+d
T(n/4)=T(n/8)+d
.
T(n)=T(n/2)+d=T(n/4)+2d=T(n/8)+3d=.=T(n/2i)+id
n/2i =1 T(1)=c i=log n
T(n)=T(1)+ d log n=c+d log n
O(log n)

You might also like