Lab Programs
Lab Programs
1. Implement the Ford Fulkerson algorithm to obtain the maximum possible flow
from source to sink in a given flow network. Analyse the time complexity of
the same by measuring the time taken by the algorithm for different input sizes.
2. Implement the all pair shortest path problem for a directed weighted graph using
Johnson’s algorithm approach in a programming language of your choice.
Analyse the time complexity of the same by measuring the time taken for
different input sizes.
3. Given a graph, write a program to check whether it is a bipartite graph.
4. Implement pattern matching problem using Rabin-karp algorithm and analyse
the time complexity of the same by measuring time taken for different inputs.
5. Implement pattern matching using finite state automata and find the time
taken by it.
6. Implement Boyer Moore algorithm for string matching.
Compare and contrast the algorithms in questions 4,5,6 in terms of practical
space and time complexity(time taken for execution) for different input size.
Analyse the same and comment on the efficiency.
1. Forf Fulkersons Algorithm
cout << "The maximum possible flow is " << fordFulkerson(graph, 0, 5);
return 0;
}
//OUTPUT
2. Johnsons Algorithm
import java.util.InputMismatchException;
import java.util.Scanner;
public class JohnsonsAlgorithm
{
private int SOURCE_NODE;
private int numberOfNodes;
private int augmentedMatrix[][];
private int potential[];
private BellmanFord bellmanFord;
private DijkstraShortesPath dijsktraShortesPath;
private int[][] allPairShortestPath;
bellmanFord.BellmanFordEvaluation(SOURCE_NODE, augmentedMatrix);
potential = bellmanFord.getDistances();
int reweightedGraph[][] = reweightGraph(adjacencyMatrix);
for (int i = 1; i <= numberOfNodes; i++)
{
for (int j = 1; j <= numberOfNodes; j++)
{
System.out.print(reweightedGraph[i][j] + "\t");
}
System.out.println();
}
System.out.println();
for (int i = 1; i<= numberOfNodes; i++)
{
System.out.print("\t"+i);
}
System.out.println();
for (int source = 1; source <= numberOfNodes; source++)
{
System.out.print( source +"\t" );
for (int destination = 1; destination <= numberOfNodes; destination++)
{
System.out.print(allPairShortestPath[source][destination]+ "\t");
}
System.out.println();
}
}
try
{
System.out.println("Enter the number of vertices");
number_of_vertices = scan.nextInt();
adjacency_matrix = new int[number_of_vertices + 1][number_of_vertices + 1];
class BellmanFord
{
private int distances[];
private int numberofvertices;
public static final int MAX_VALUE = 999;
distances[source] = 0;
class DijkstraShortesPath
{
private boolean settled[];
private boolean unsettled[];
private int distances[];
private int adjacencymatrix[][];
private int numberofvertices;
int evaluationnode;
for (int vertex = 1; vertex <= numberofvertices; vertex++)
{
distances[vertex] = MAX_VALUE;
}
unsettled[source] = true;
distances[source] = 0;
while (getUnsettledCount(unsettled) != 0)
{
evaluationnode = getNodeWithMinimumDistanceFromUnsettled(unsettled);
unsettled[evaluationnode] = false;
settled[evaluationnode] = true;
evaluateNeighbours(evaluationnode);
}
}
//OUTPUT
//OUTPUT
return 0;
}
int TF[M+1][NO_OF_CHARS];
computeTF(pat, M, TF);
//Output
# include <limits.h>
# include <string.h>
# include <stdio.h>
int badchar[NO_OF_CHARS])
int i;
badchar[i] = -1;
// of a character
badchar[(int) str[i]] = i;
/* A pattern searching function that uses Bad Character Heuristic of Boyer Moore
Algorithm */
int m = strlen(pat);
int n = strlen(txt);
int badchar[NO_OF_CHARS];
/* Fill the bad character array by calling
badCharHeuristic(pat, m, badchar);
// respect to text
int j = m-1;
j--;
if (j < 0)
occurrence of it in pattern.
of text */
else
character. */
s += max(1, j - badchar[txt[s+j]]);
int main()
search(txt, pat);
return 0;
//Output
1. C Programming
#include <time.h>
start = clock();
... /* Do the work. */
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
2. C++ Programming
#include <chrono>
using namespace std::chrono;
3. Java Programming
import java.io.*;