Você está na página 1de 6

Tennessee Tech.

University
Design of Algorithms
CSC 2400-001

Jessica Maria Evangelista de Souza


Program 1a
Due: October 8, 2014

Unique Element Problem


1. Analysis Algorithm
This report have the goal that focus on different algorithmic solutions to the unique element
problem. This analyze was performed focusing on worst-case analysis.
The natural measure of this inputs size in both algorithms is n, the number of elements on the
array. Since the innermost loop contains a single operation, which I consider it as the algorithms basic
operation. Below, we can observe the analysis for each algorithm:
Algorithm 1
1: ALGORITHM Unique1(A[0 . . . n 1])
2: // Determines whether all of the elements in A are Distinct
3: // Input: An array A of n times
4: // Output: Return true if all elements are distinct, otherwise return false
5: for i 0 to n 1 do
6:
for j 0 to n 1 do
7:
if i j and A[i] = A[j] then
8:
return false
9:
end if
10:
end for
11: end for
12: return true
13: end ALGORITHM

a. What is the basic operation?


i j and A[i] = A[j]
b. Set up a sum expressing the number of times the algorithms basic operation is executed.
1 1

1
=0 =0

c. How many times is the basic operation executed?


1
1
1
2
1
=0 =0 1 = =0 = n =0 1 = n * n = n

d. What is the efficiency class of this algorithm?


(g(x)) = (n2)
Algorithm 2
1: ALGORITHM Unique2(A[0 . . . n 1])
2: // Determines whether all of the elements in A are Distinct
3: // Input: An array A of n times
4: // Output: Return true if all elements are distinct, otherwise return false
5: for i 0 to n 2 do
6:
for j i + 1 to n 1 do
7:
if A[i] = A[j] then
8:
return false
9:
end if
10:
end for
11: end for
12: return true
13: end ALGORITHM
a. What is the basic operation?
A[i] = A[j]
b. Set up a sum expressing the number of times the algorithms basic operation is executed.
2 1

1
=0 =+1

c. How many times is the basic operation executed?


1
2
2
Cworst(n) = 2
=0 =+1 1 = =0 [( 1) ( + 1) + 1 = =0 ( 1 )
2
2
= 2
=0 ( 1) - =0 = (n 1) =0 1

= (n 1)2 -

(2)(1)
2

(1)
2

2 n2

d. What is the efficiency class of this algorithm?


(g(x)) = (n2)

(2)(1)
2

2. Analysis Process
The analysis of the process was performed using sample input files free of duplicates. These files
have between 10000 and 290000 different numbers. The program compute the number of basic
operations and the counting extension of the each algorithm. Below, we can see all information of
the analysis and this report show that the algorithm 2 is better than the algorithm 1. The time and the
operations of the algorithm 2 is half the time and operations used by the algorithm 1.

Unique Element Problem


Analysis
Count
10000
20000
30000
40000
50000
60000
70000
80000
90000
100000
110000
120000
130000
140000
150000
160000
170000
180000
190000
200000
210000
220000
230000
240000
250000
260000
270000
280000
290000

Operations
Time Unique1 Time
Operations
Time
Time
Unique1
(s)
Unique1 (s) Unique2
Unique2 (s) Unique2 (s)
99980001
1233132
1,23
49985001
544388
0,54
399960001
5016629
5,02
199970001
2394412
2,39
899940001
11404900
11,40
449955001
4804751
4,80
1599920001
17697886
17,70
799940001
8602513
8,60
2499900001
30478309
30,48 1249925001
13641945
13,64
3599880001
43423162
43,42 1799910001
20340885
20,34
4899860001
56055417
56,06 2449895001
25450249
25,45
6399840001
76746871
76,75 3199880001
34845369
34,85
8099820001
96098208
96,10 4049865001
45657886
45,66
9999800001
119850355
119,85 4999850001
57388303
57,39
12099780001
133045188
133,05 6049835001
61113871
61,11
14399760001
164003005
164,00 7199820001
76378519
76,38
16899740001
197956471
197,96 8449805001
85962002
85,96
19599720001
223925706
223,93 9799790001
100139279
100,14
22499700001
258623447
258,62 11249775001
111022954
111,02
25599680001
298615091
298,62 12799760001
137296980
137,30
28899660001
333357927
333,36 14449745001
148999264
149,00
32399640001
393769890
393,77 16199730001
161708447
161,71
36099620001
440160849
440,16 18049715001
200493038
200,49
39999600001
467852234
467,85 19999700001
218569364
218,57
44099580001
500355509
500,36 22049685001
234241364
234,24
48399560001
541432082
541,43 24199670001
253551875
253,55
52899540001
618939177
618,94 26449655001
281437437
281,44
57599520001
644170028
644,17 28799640001
308956771
308,96
62499500001
686799906
686,80 31249625001
326438562
326,44
67599480001
779638029
779,64 33799610001
387912384
387,91
72899460001
828910824
828,91 36449595001
401503000
401,50
78399440001
881541881
881,54 39199580001
429210890
429,21
84099420001
963849385
963,85 42049565001
557199413
557,20

The graphs bellow were constructed using the time in seconds. The conversion was made divided the
time in microseconds to 1000000. In addition, the time in second has 2 decimal places. The result is not
changed because of this alteration.
The graph (Figure 2.1) below represent the difference between the time of the algorithm 1 and the
algorithm 2:

Time (s)

Computer per time


1000,00
900,00
800,00
700,00
600,00
500,00
400,00
300,00
200,00
100,00
0,00

Unique1
Unique2

Count

Figure 2.1

The graph (Figure 2.2) below represent the difference between the operations of the algorithm 1
and the algorithm 2:

Operations

Computer per operations


9E+10
8E+10
7E+10
6E+10
5E+10
4E+10
3E+10
2E+10
1E+10
0

Unique1
Unique2

Count

Figure 2.1

3. Computer Settings
The all analysis was processed in a virtual machine with the operation system Debian. Some
alterations was made in the initial virtual machine to obtain a better time (Figure 3.1). Before The
virtual machine had 1 G, now I using 3GB to improve the time of the operations.

Figure 3.1

4. Conclusion
After some analysis, I could conclude that although the order of growth is the same between the
two algorithms ((n2)), they are the difference in the times that the operation basic was executed.
1
2

The algorithm 1 has the double of time (n2) and the operations that the algorithm 2 ( n2). This points
can be observed on the table of results and in the graphs.
Both algorithms has the same goal, but the algorithm 2 is more efficient than the algorithm 1. This
is proved mathematically and by tests. After all, I learn that the growth order can be the same, but
the number of the basic operations computed is extremely important to analyze an algorithm.

Você também pode gostar