Advanced ADA Lab Manual M-Tech VTU
Advanced ADA Lab Manual M-Tech VTU
Experiment No: 1
Title of the Experiment :
Design, develop and run program in any language to implement the Bellman-Ford algorithm and
determine its performance.
Objective of the Experiment :
To solve the single-source shortest-paths problem using Bellman-Ford algorithm.
Theory:
Given a weighted, directed graph G=(V,E) with s and weight function w:ER,the Bellman
Ford algorithm returns a Boolean value indicating whether or not there is negative-weight
cycle that is reachable from the source. If there is such a cycle, the algorithm indicates that
no solution exists. If there is no such cycle, the algorithm produces the shortest paths and
their weights.
Algorithm :
Bellman-Ford (G,w,s)
1 INITIALIZE-SINGLE-SOURCE(G,s)
2 for i=1 to |G.V| - 1
3
for each edge (u,v) G.E
4
RELAX(u,v,w)
5 for each edge (u,v) G.E
6 if v.d > u.d +w(u,v)
7
return FALSE
8 return TRUE
Source Code:
import java.util.Scanner;
public class BellmanFord
{
private int distances[];
private int numberofvertices;
public static final int MAX_VALUE = 999;
public BellmanFord(int numberofvertices)
{
this.numberofvertices = numberofvertices;
distances = new int[numberofvertices + 1];
}
public void BellmanFordEvaluation(int source, int adjacencymatrix[][])
{
1
Dept of Computer Science & Engineering, Basaveshwar Engineering College,
Bagalkot
Results:
1.
Input:
Enter the number of vertices
4
Enter the adjacency matrix
1234
5678
9 10 11 12
13 14 15 16
Enter the source vertex
4
Output:
Program Started AT : Sun May 15 12:53:01 IST 2016
Distance of Source 4 to 1 is 13
Distance of Source 4 to 2 is 14
Distance of Source 4 to 3 is 15
Distance of Source 4 to 4 is 0
Program Ended AT : Sun May 15 12:53:01 IST 2016
Performance Parameters:
Time Taken By the Program to Run : 0 ms
Number of Iterations taken By the Program to Run : 11
2. Input:
For the directed Graph
5
Dept of Computer Science & Engineering, Basaveshwar Engineering College,
Bagalkot
Experiment No : 2
Title of the Experiment:
Design, develop and run a program in any language to implement a Miller Rabin / Monte Carlo
algorithm to test the primality of a given integer and determine its performance.
Objective of the Experiment :
Miller Rabin Algorithms always gives an answer, but there is a probability of being completely wrong.
Theory:
o There are problems for which no efficient algorithm is known, and
Approximate solutions do not make sense.
o A Miller Rabin Primality Test/ Monte Carlo algorithm always gives an answer but
occasionally makes a mistake.
o But it finds a correct solution with high probability whatever the instance is processed, i.e.
there is no instance on which the probability of error is high.
o However, no warning is usually given when the algorithm gives a wrong solution.
Algorithm :
Miller Rabin Primality Test MC Algorithm
function MillerRabinPrimalityTest(n)
Input: n (an odd positive integer)
Output: .true. Or .false. (Always correct when .false. Is returned, and correct at least 75% when .true. is
returned)
a Random(2, n-2)
find positive integers k, m such that n-1 =2k m, and m odd
b am mod n
for j 1 to k do
if b 1 .and. b n-1 then return(.false.)
else
if b = n-1 then return(.true.) endif
endif
b b*b mod n
endfor
If b 1 then return(.false.) else return(.true.) endif
6
Dept of Computer Science & Engineering, Basaveshwar Engineering College,
Bagalkot
Experiment No: 3
Title of the Experiment:
Design, develop and run program in any language to solve the string matching problem using nave
approach and the KMP algorithm and compare their performances.
Objective of the Experiment :
To solve the string matching problem using Nave and Knuth Morris Pratt algorithm and compare
their performances.
Theory:
Components of KMP algorithm
The prefix function,
The prefix function, for a pattern encapsulates knowledge about how the pattern matches
against shifts of itself. This information can be used to avoid useless shifts of the pattern p.
In other words, this enables avoiding backtracking on the string S.
Algorithm :
Nave Algorithm
NAVE-STRING-MATCHER(T,P)
1 n=T.length
2 m=P.length
3 for s=0 to n-m
4 if P[1..m] == T[s+1..s+m]
5 print Pattern occurs with shift s
10
Dept of Computer Science & Engineering, Basaveshwar Engineering College,
Bagalkot
Source Code:
Nave Algorithm
11
Dept of Computer Science & Engineering, Basaveshwar Engineering College,
Bagalkot
16
Dept of Computer Science & Engineering, Basaveshwar Engineering College,
Bagalkot
Experiment No: 4
Title of the Experiment
Design, develop and write program to solve string matching problem using Finite Automata and
determine its performance.
Theory:
In FA based algorithm, we preprocess the pattern and build a 2D array that represents a Finite Automata.
Construction of the FA is the main tricky part of this algorithm. Once the FA is built, the searching is
simple. In search, we simply need to start from the first state of the automata and first character of the
text. At every step, we consider next character of text, look for the next state in the built FA and move to
new state. If we reach final state, then pattern is found in text. Time complexity of the search prcess is
O(n).
Before we discuss FA construction, let us take a look at the following FA for pattern ACACAGA.
The above diagrams represent graphical and tabular representations of pattern ACACAGA.
Number of states in FA will be M+1 where M is length of the pattern. The main thing to construct FA is
to get the next state from the current state for every possible character
17
Dept of Computer Science & Engineering, Basaveshwar Engineering College,
Bagalkot
Source Code:
import java.io.*;
public class FiniteAutomata {
int[][] TF;
public int getNextState(String p, int m, int q, int x)
{
int i;
if ((q < m) && (p.charAt(q)==x))
return q+1;
for (int ns = q; ns > 0; ns--)
{
if (p.charAt(ns-1) == x)
{
for (i = 0; i < ns-1; i++)
{
if (p.charAt(i) != p.charAt(q-ns+i+1))
{
break;
}
}
if (i == ns-1)
return ns;
}
}
return 0;
}
public void computeTF(String p, int m, int[][]TF)
{
for (int q=0; q<=m; q++)
{
for(int x = 0; x<=255;x++)
{
18
Dept of Computer Science & Engineering, Basaveshwar Engineering College,
Bagalkot
Output:
Program Started AT : Sun May 15 15:07:26 IST 2016
Pattern found at 7
Program Ended AT : Sun May 15 15:07:26 IST 2016
Performance Parameters:
Time Taken By the Program to Run : 1 ms
Number of Iterations taken By the Program to Run : 1567
Input:
Enter text:
Finite Automata
Enter pattern:
ataa
Output:
Program Started AT : Sun May 15 15:09:20 IST 2016
Pattern not found
Program Ended AT : Sun May 15 15:09:20 IST 2016
Performance Parameters:
Time Taken By the Program to Run : 1 ms
Number of Iterations taken By the Program to Run : 1301
20
Dept of Computer Science & Engineering, Basaveshwar Engineering College,
Bagalkot
Experiment No: 5
Title of the Experiment
Design, develop and write program to solve string matching problem using Robin Karp algorithm
and determine its performance.
Theory:
Rabin-Karp algorithm slides the pattern one by one. But unlike the Naive algorithm,
Rabin Karp algorithm matches the hash value of the pattern with the hash value of current substring of
text, and if the hash values match then only it starts matching individual characters. So Rabin Karp
algorithm needs to calculate hash values for following strings.
1) Pattern itself.
2) All the substrings of text of length m.
Since we need to efficiently calculate hash values for all the substrings of size m of
text,
we
must
have
a
hash
function
which
has
following
property.
Hash at the next shift must be efficiently computable from the current hash value and next character in
text or we can say hash(txt[s+1 .. s+m]) must be efficiently computable from hash(txt[s .. s+m-1]) and
txt[s+m] i.e., hash(txt[s+1 .. s+m])= rehash(txt[s+m], hash(txt[s .. s+m-1]) and rehash must be O(1)
operation.
The hash function suggested by Rabin and Karp calculates an integer value. The integer
value for a string is numeric value of a string. For example, if all possible characters are from 1 to 10, the
numeric value of 122 will be 122. The number of possible characters is higher than 10 (256 in general)
and pattern length can be large. So the numeric values cannot be practically stored as an integer.
Therefore, the numeric value is calculated using modular arithmetic to make sure that the hash values can
be stored in an integer variable (can fit in memory words). To do rehashing, we need to take off the most
significant digit and add the new least significant digit for in hash value. Rehashing is done using the
following formula.
hash( txt[s+1 .. s+m] ) = d ( hash( txt[s .. s+m-1]) txt[s]*h ) + txt[s + m] ) mod q
hash( txt[s .. s+m-1] ) : Hash value at shift s.
hash( txt[s+1 .. s+m] ) : Hash value at next shift (or shift s+1)
d: Number of characters in the alphabet
q: A prime number
h: d^(m-1)
21
Dept of Computer Science & Engineering, Basaveshwar Engineering College,
Bagalkot
Algorithm:
Source Code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
public class RabinKarp {
public void search(String T, String P, int d, int q)
{
int n, m, h, p, i;
n = T.length();
m = P.length();
int []t = new int[n];
h = 1;
for (i = 1; i <= m-1; i++)
h = (h*d) % q;
p = 0;
t[0] = 0;
boolean found = false;
for (i = 0; i < m; i++)
{
p = (d*p + (P.charAt(i)-48))%q;
t[0] = (d*t[0]+(T.charAt(i)-48))%q;
}
for (int s = 0; s <= n-m; s++)
{
if (p == t[s])
{
for (i = 0; i < m; i++)
22
Dept of Computer Science & Engineering, Basaveshwar Engineering College,
Bagalkot
24
Dept of Computer Science & Engineering, Basaveshwar Engineering College,
Bagalkot
25
Dept of Computer Science & Engineering, Basaveshwar Engineering College,
Bagalkot