0% encontró este documento útil (0 votos)
37 vistas5 páginas

Analisis de Diseño y Algortimos Lab-02: Acuña Huisacayna Jhonatan Jesús Cui: 20180555 Octubre 20, 2019

This document contains code for an algorithm that uses bisection method to calculate nth roots. It runs the algorithm for 1000 iterations, recording the iteration count, input values, error, and time taken. A graph is included showing time remains fairly constant between 0.000 and 0.001 ms for most iterations, indicating the algorithm is efficient. The code implements an interface for the algorithm, includes a class to run and time the algorithm, and outputs results to a CSV file.

Cargado por

Jhonatan Acuna
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como PDF, TXT o lee en línea desde Scribd
0% encontró este documento útil (0 votos)
37 vistas5 páginas

Analisis de Diseño y Algortimos Lab-02: Acuña Huisacayna Jhonatan Jesús Cui: 20180555 Octubre 20, 2019

This document contains code for an algorithm that uses bisection method to calculate nth roots. It runs the algorithm for 1000 iterations, recording the iteration count, input values, error, and time taken. A graph is included showing time remains fairly constant between 0.000 and 0.001 ms for most iterations, indicating the algorithm is efficient. The code implements an interface for the algorithm, includes a class to run and time the algorithm, and outputs results to a CSV file.

Cargado por

Jhonatan Acuna
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como PDF, TXT o lee en línea desde Scribd
Está en la página 1/ 5

Universidad Nacional de San Agustín

Escuela Profesional de Ingenieria de Sistemas

Analisis de Diseño y Algortimos Lab-02

Acuña Huisacayna Jhonatan Jesús Cui: 20180555

Octubre 20, 2019

Graph Time vs Iteration

Código

Clase AlgorithmOne
import java . io . ∗ ;
import java . u t i l . Arrays ;
import java . util . List ;
import java . u t i l . Random ;

p u b l i c c l a s s AlgorithmOne implements I A l g o r i t h m {
public String description () {
r e t u r n new S t r i n g ( "NRoot␣ Problem " ) ;
}

p u b l i c b o o l e a n run ( i n t c a n t i d a d ) throws IOException {


S t r i n g name =" E x t r a c c i o n ␣Data . c s v " ;
B u f f e r e d W r i t e r t x t W r i t e r = new B u f f e r e d W r i t e r ( new F i l e W r i t e r ( name ) ) ;
t x t W r i t e r . w r i t e ( " I t e r , Nth , x , e r r o r , time_1 , time_2 , r e s u l t \n" ) ;
Random n= new Random ( ) ;

1
f o r ( i n t i = 1 ; i <= c a n t i d a d ; i ++) {
l o n g s t a r t _ t i m e = System . c u r r e n t T i m e M i l l i s ( ) ;
l o n g nano_time_start = System . nanoTime ( ) ;
i n t nth = n . n e x t I n t ( 1 0 0 0 ) ;
int x = n . nextInt (1000);
d o u b l e e r r o r = n . nextDouble ( ) ∗ 2 ;
int i t e r a t i o n = n . nextInt (1000);
d o u b l e r e s u l t a d o = nthRootByBiSection ( nth , x , i t e r a t i o n , e r r o r ) ;

l o n g end_time = System . c u r r e n t T i m e M i l l i s ( ) ;
l o n g nano_time_end = System . nanoTime ( ) ;

t x t W r i t e r . w r i t e ( i + " , " + nth + " , " + x + " , " + e r r o r + " , " +


( ( end_time − s t a r t _ t i m e ) / 1 0 0 0F) + " , " +
( ( nano_time_end − nano_time_start ) / 1000000000F)
+ " , " + r e s u l t a d o + " \n" ) ;

txtWriter . c l o s e ( ) ;

return true ;
}

p u b l i c s t a t i c d o u b l e nthRootByBiSection ( i n t nth , i n t x , i n t i t e r a t i o n ,
double e r r o r ) {
i n t n=1;
d o u b l e c =0;
d o u b l e a=0,b=x ;

w h i l e ( n<= i t e r a t i o n ) {
c=(a+b ) / 2 ;

i f ( f ( x , c , nth)==0 | | ( b−a)/2< e r r o r ) {
return c ;
}
n++;

i f ( ( f ( x , c , nth )>0 && f ( x , a , nth ) >0) | | f ( x , c , nth )<0 && f ( x , a , nth )<0 ) {
a=c ;
}
else {
b=c ;
}
}

return c ;

2
}
p u b l i c s t a t i c double f ( i n t x , double c , double n) {
r e t u r n x−Math . pow ( c , n ) ;
}

}
Clase IAlgorithm
import j a v a . i o . IOException ;

public i n t e r f a c e IAlgorithm {
p u b l i c b o o l e a n run ( i n t c a n t i d a d ) throws IOException ;
public String description ( ) ;
}
Clase TimeWatch
import j a v a . i o . IOException ;
import j a v a . l a n g . ∗ ;

p u b l i c c l a s s TimeWatch {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) throws IOException {
AlgorithmOne a l g o r i t h m = new AlgorithmOne ( ) ;
System . out . p r i n t ( " T e s t i n g ␣ " + a l g o r i t h m . d e s c r i p t i o n ( ) + " : ␣ " ) ;
i n t cp = 1 0 0 0 ;
l o n g s t a r t _ t i m e = System . c u r r e n t T i m e M i l l i s ( ) ;
l o n g nano_time_start = System . nanoTime ( ) ;

b o o l e a n s t a t e = new AlgorithmOne ( ) . run ( cp ) ;


i f ( state )
System . out . p r i n t l n ( " A l l ␣OK. " ) ;
else
System . out . p r i n t l n ( " Something ␣ went ␣ wrong ! " ) ;
l o n g end_time = System . c u r r e n t T i m e M i l l i s ( ) ;
l o n g nano_time_end = System . nanoTime ( ) ;

System . out . p r i n t l n ( "Time␣ used : ␣ " + ( end_time − s t a r t _ t i m e ) / 1000F + " ␣ s " ) ;


System . out . p r i n t l n ( "Time␣ used : ␣ " +
( nano_time_end − nano_time_start ) / 1000000000F + " ␣ s " ) ;
}
}

Gráfica

3
Iteration vs Time

0.002
Time (ms)

0.001

0.000

0 250 500 750 1000


Iteration
Interpretación

Como se ve expresado en la gráfica las iteraciones casi tienden a estar entre el


rango de 0.000 y 0.0001, a excepción de casos especiales, por ejemplo, en la gráfica
se ve que en alguna iteración dentro de las 250 primeras un valor demoro más
de 0.002 saliendo del rango que se podría considerar normal. Entonces al tener
un tiempo casi constante y bastante reducido demuestra o expresa que este es un
método eficiente y óptimo.

También podría gustarte