Вы находитесь на странице: 1из 9

Chinese-Remainder-Theorem

public class CRT { /* * performs the Euclidean algorithm on a and b to find a pair of coefficients * (stored in the output array) that correspond to x and y in the equation * ax by ! gcd(a"b) * constraint# a $ b */ public static int%& euclidean(int a" int b) { if(b $ a) { //re'erse the order of inputs" run through this method" then re'erse outputs int%& coeffs ! euclidean(b" a)( int%& output ! {coeffs%)&" coeffs%*&+( return output( + int q ! a/b( //a ! q*b r ,,$ r ! a , q*b int r ! a ,q*b( //-hen there is no remainder" -e ha'e reached the gcd and are done if(r !! *) { int%& output ! {*" )+( return output( + //call the next iteration do-n (b ! qr int%& next ! euclidean(b" r)( r./)

int%& output ! {next%)&" next%*& , q*next%)&+( return output( + //finds the least positi'e integer equi'alent to a mod m public static int least0osEqui'(int a" int m) { //a eqi'alent to b mod ,m 1!!$ a equi'alent to b mod m if(m 1 *) return least0osEqui'(a" ,)*m)( //if * 1! a 1 m" then a is the least positi'e integer equi'alent to a mod if(a $! * 22 a 1 m) return a( //for negati'e a" find the least negati'e integer equi'alent to a mod m //then add m if(a 1 *) return ,)*least0osEqui'(,)*a" m) m(

//the only case left is that of a"m $ * and a $! m //ta3e the remainder according to the 4i'ision algorithm int q ! a/m( /* * a ! * r ! * and */ return qm r" -ith * 1! r 1 m a , qm is equi'alent to a mod m is the least such non,negati'e number (since r 1 m) a , q*m(

public static 'oid main(5tring%& args) { /* * the current setup finds a number x such that# * x ! / mod 6" x ! 7 mod 8" x ! 9 mod :" and x ! 6 mod )) * note that the 'alues in mods must be mutually prime */ int%& constraints ! {/"7"9"6+( //put modular contraints here int%& mods ! {6"8":"))+( //put moduli here //; is the product of the mods int ; ! )( for(int i ! *( i 1 mods<length( i ; *! mods%i&(

int%& mult=n' ! ne- int%constraints<length&( /* * this loop applies the Euclidean algorithm to each pair of ;/mods%i& and mods%i& * since it is assumed that the 'arious mods%i& are pair-ise coprime" * the end result of applying the Euclidean algorithm -ill be * gcd(;/mods%i&" mods%i&) ! ) ! a(;/mods%i&) b(mods%i&)" or that a(;/mods%i&) is * equi'alent to ) mod (mods%i&)< This a is then the multiplicati'e * in'erse of (;/mods%i&) mod mods%i&" -hich is -hat -e are loo3ing to multiply * by our constraint constraints%i& as per the Chinese Remainder Theorem * euclidean(;/mods%i&" mods%i&)%*& returns the coefficient a * in the equation a(;/mods%i&) b(mods%i&) ! ) */ for(int i ! *( i 1 mult=n'<length( i ) mult=n'%i& ! euclidean(;/mods%i&" mods%i&)%*&( int x ! *( //x ! the sum o'er all gi'en i of (;/mods%i&)(constraints%i&)(mult=n'%i&) for(int i ! *( i 1 mods<length( i ) x ! (;/mods%i&)*constraints%i&*mult=n'%i&( x ! least0osEqui'(x" ;)( 5ystem<out<println(>x is equi'alent to > ++ x > mod > ;)(

Bellman-Ford Algorithm
import ?a'a<util<5canner( public class @ellmanAord { pri'ate int distances%&( pri'ate int numberof'ertices( public static final int ;BC.DBEFE ! :::( public @ellmanAord(int numberof'ertices) { this<numberof'ertices ! numberof'ertices( distances ! ne- int%numberof'ertices )&( + public 'oid @ellmanAordE'aluation(int source" int ad?acencymatrix%& %&) { for (int node ! )( node 1! numberof'ertices( node { distances%node& ! ;BC.DBEFE( + )

distances%source& ! *( for (int node ! )( node 1! numberof'ertices , )( node ) { for (int sourcenode ! )( sourcenode 1! numberof'ertices( sourcenode ) { for (int destinationnode ! )( destinationnode 1! numberof'ertices( destinationnode ) { if (ad?acencymatrix%sourcenode&%destinationnode& G! ;BC.DBEFE) { if (distances%destinationnode& $ distances%sourcenode& ad?acencymatrix%sourcenode& %destinationnode&) distances%destinationnode& ! distances%sourcenode& ad?acencymatrix%sourcenode& %destinationnode&( + + + + for (int sourcenode ! )( sourcenode 1! numberof'ertices( sourcenode ) { for (int destinationnode ! )( destinationnode 1! numberof'ertices( destinationnode ) {

;BC.DBEFE)

if (ad?acencymatrix%sourcenode&%destinationnode& G! {

if (distances%destinationnode& $ distances%sourcenode& ad?acencymatrix%sourcenode& %destinationnode&) 5ystem<out<println(>The Hraph contains negati'e egde cycle>)( + + + for (int 'ertex ! )( 'ertex 1! numberof'ertices( 'ertex { 5ystem<out<println(>distance of source > source 'ertex > is > distances%'ertex&)( + + public static 'oid main(5tring<<< arg) { int numberof'ertices ! *( int source( 5canner scanner ! ne- 5canner(5ystem<in)( 5ystem<out<println(>Enter the number of 'ertices>)( numberof'ertices ! scanner<next=nt()( int ad?acencymatrix%&%& ! ne- int%numberof'ertices )& %numberof'ertices )&( 5ystem<out<println(>Enter the ad?acency matrix>)( for (int sourcenode ! )( sourcenode 1! numberof'ertices( sourcenode ) { for (int destinationnode ! )( destinationnode 1! numberof'ertices( destinationnode ) { ad?acencymatrix%sourcenode&%destinationnode& ! scanner<next=nt()( if (sourcenode !! destinationnode) { ad?acencymatrix%sourcenode&%destinationnode& ! *( continue( + if (ad?acencymatrix%sourcenode&%destinationnode& !! *) { ad?acencymatrix%sourcenode&%destinationnode& ! ;BC.DBEFE( + + + 5ystem<out<println(>Enter the source 'ertex>)( source ! scanner<next=nt()( @ellmanAord bellmanford ! ne- @ellmanAord(numberof'ertices)( ) > to >

bellmanford<@ellmanAordE'aluation(source" ad?acencymatrix)( scanner<close()( ++

Output:
I?a'ac @ellmanAord<?a'a I?a'a @ellmanAord Enter the number of 'ertices J Enter the ad?acency matrix * 9 * * ,) * * * ,) * ,/ * * * * * * * * * * * * * * * * ,6 * 7 * * * * * * Enter the source 'ertex ) distance distance distance distance distance distance of of of of of of source source source source source source ) ) ) ) ) ) to to to to to to ) / 7 9 6 J is is is is is is * 9 7 ,J ,) /

Kapsack Algorithm
import ?a'a<util<5canner( /** Class Knapsac3 **/ public class Knapsac3 { public 'oid sol'e(int%& -t" int%& 'al" int L" int M) { int MEHBT=DE.=MA=M=TN ! =nteger<;=M.DBEFE( int%&%& m ! ne- int%M )&%L )&( int%&%& sol ! ne- int%M )&%L )&( for (int i ! )( i 1! M( i ) { for (int ? ! *( ? 1! L( ? ) { int m) ! m%i , )&%?&( int m/ ! MEHBT=DE.=MA=M=TN( if (? $! -t%i&) m/ ! m%i , )&%? , -t%i&& 'al%i&( /** select max of m)" m/ **/ m%i&%?& ! ;ath<max(m)" m/)( sol%i&%?& ! m/ $ m) O ) # *( + + /** ma3e list of -hat all items to finally select **/ int%& selected ! ne- int%M )&( for (int n ! M" - ! L( n $ *( n,,) { if (sol%n&%-& G! *) { selected%n& ! )( - ! - , -t%n&( + else selected%n& ! *( + /** 0rint finally selected items **/ 5ystem<out<println(>Pn=tems selected # >)( for (int i ! )( i 1 M )( i ) if (selected%i& !! )) 5ystem<out<print(i > >)( 5ystem<out<println()(

+ /** ;ain function **/ public static 'oid main (5tring%& args) { 5canner scan ! ne- 5canner(5ystem<in)( 5ystem<out<println(>Knapsac3 Blgorithm TestPn>)( /** ;a3e an ob?ect of Knapsac3 class **/ Knapsac3 3s ! ne- Knapsac3()( 5ystem<out<println(>Enter number of elements >)( int n ! scan<next=nt()(

int%& -t ! ne- int%n int%& 'al ! ne- int%n

)&( )&(

5ystem<out<println(>PnEnter -eight for > n > elements>)( for (int i ! )( i 1! n( i ) -t%i& ! scan<next=nt()( 5ystem<out<println(>PnEnter 'alue for > n > elements>)( for (int i ! )( i 1! n( i ) 'al%i& ! scan<next=nt()( 5ystem<out<println(>PnEnter 3napsac3 -eight >)( int L ! scan<next=nt()( 3s<sol'e(-t" 'al" L" n)( + +

Output:
Knapsac3 Blgorithm Test Enter number of elements 6 Enter -eight for 6 elements 6* )* /* 9* 7* Enter 'alue for 6 elements 7** J* :* )** /9* Enter 3napsac3 -eight J* =tems selected # / 7 6

Miller Rabin Primality Test Algorithm

import ?a'a<util<5canner( import ?a'a<util<Random( import ?a'a<math<@ig=nteger( /** Class ;illerRabin **/ public class ;illerRabin { /** Aunction to chec3 if prime or not **/ public boolean is0rime(long n" int iteration) { /** base case **/ if (n !! * QQ n !! )) return false( /** base case , / is prime **/ if (n !! /) return true( /** an e'en number other than / is composite **/ if (n R / !! *) return false( long s ! n , )( -hile (s R / !! *) s /! /( Random rand ! ne- Random()( for (int i ! *( i 1 iteration( i ) { long r ! ;ath<abs(rand<nextEong())( long a ! r R (n , )) )" temp ! s( long mod ! mod0o-(a" temp" n)( -hile (temp G! n , ) 22 mod G! ) 22 mod G! n , )) { mod ! mul;od(mod" mod" n)( temp *! /( + if (mod G! n , ) 22 temp R / !! *) return false( + return true(

+ /** Aunction to calculate (a S b) R c **/ public long mod0o-(long a" long b" long c) { long res ! )( for (int i ! *( i 1 b( i ) { res *! a( res R! c( + return res R c( + /** Aunction to calculate (a * b) R c **/ public long mul;od(long a" long b" long mod) { return @ig=nteger<'alueTf(a)<multiply(@ig=nteger<'alueTf(b))<mod(@ig=nteger<'a lueTf(mod))<longDalue()(

+ /** ;ain function **/ public static 'oid main (5tring%& args) { 5canner scan ! ne- 5canner(5ystem<in)( 5ystem<out<println(>;iller Rabin 0rimality Blgorithm TestPn>)( /** ;a3e an ob?ect of ;illerRabin class **/ ;illerRabin mr ! ne- ;illerRabin()( /** Bccept number **/ 5ystem<out<println(>Enter numberPn>)( long num ! scan<nextEong()( /** Bccept number of iterations **/ 5ystem<out<println(>PnEnter number of iterations>)( int 3 ! scan<next=nt()( /** chec3 if prime **/ boolean prime ! mr<is0rime(num" 3)( if (prime) 5ystem<out<println(>Pn> num > is prime>)( else 5ystem<out<println(>Pn> num > is composite>)( + +

Output:
;iller Rabin 0rimality Blgorithm Test Enter number 66)*7U: Enter number of iterations / 66)*7U: is prime

Вам также может понравиться