Commit 7a997dbf authored by SPARSA ROYCHOWDHURY's avatar SPARSA ROYCHOWDHURY

9/10/17:

1. Ultimately coded the pairwise shortest path and cycle detection algorithm
2. Need to connect these two together to make the code run.
3. Enjoyment at its best.
parent 737de7f2
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: circuitfinder.h
* Author: sparsa
*
* Created on 2 October, 2017, 11:14 AM
*/
#ifndef CIRCUITFINDER_H
#define CIRCUITFINDER_H
#include <algorithm>
#include <iostream>
#include <list>
#include <vector>
using namespace std;
typedef enum {z,les,leq,que} relation;
//extern relation comparison[4][4];
extern relation addition[4][4];
typedef list<int> NodeList;
//void pairWiseTightestRelation (relation **graph, int V); //for tightening the matrix
typedef struct{bool A; bool B;} bool2;
void pairWiseTightestRelation (relation **graph, int V); //for tightening the matrix
template<int N>
class CircuitFinder // defining a circuit finder class
{
vector<NodeList> AK; //this is the matrix representaion using node list
vector<int> Stack; //a vector used a stack
vector<bool> Blocked; // a boolean array to indicate which nodes are blocked and which are not
vector<NodeList> B; // special node.
int S;
void unblock(int U); //function to unblock the vertix U
bool2 circuit(int V); // function to check if there is a circuit from note V
bool cycleCheck(); //function to output the list of circuits in the graph
relation **relmatrix;
public:
CircuitFinder(int Array[N][N]) //convert associate matrix representation to list representation of grph
: AK(N), Blocked(N), B(N) {
for (int I = 0; I < N; ++I) {
for (int J = 0; J < N; ++J) {
if (Array[I][J]) {
AK[I].push_back(Array[I][J]);
}
}
}
}
CircuitFinder(relation **matrix) :AK(N), Blocked(N), B(N)
{
relmatrix = new relation*[N];
for(int i = 0; i < N; i++)
relmatrix[i] = new relation[N];
for (int I = 0; I<N; I ++)
{
for(int J = 0 ; J < N ; J++)
{
//if(matrix[I][J] == leq || matrix[I][J] == les || matrix[I][J] == que)
//{
relmatrix[I][J] = matrix[I][J]; // copying the matrix information in the class
if(matrix[I][J] != z)
AK[I].push_back(J+1); //every one is linked with every one because its a complete graph
//}
}
}
}
~CircuitFinder()
{
for(int i = 0;i < N; i++)
delete[] relmatrix[i];
delete[] relmatrix;
}
bool run(); //start the run
};
template<int N>
void CircuitFinder<N>::unblock(int U)
{
Blocked[U - 1] = false;
while (!B[U - 1].empty()) {
int W = B[U - 1].front();
B[U - 1].pop_front();
if (Blocked[W - 1]) {
unblock(W);
}
}
}
template<int N>
bool2 CircuitFinder<N>::circuit(int V)
{
bool2 F;
F.A= false;
F.B = false;
Stack.push_back(V);
Blocked[V - 1] = true;
//bool check;
for (int W : AK[V - 1]) {
if (W == S) {
F.B = cycleCheck();
F.A = true;
} else if (W > S && !Blocked[W - 1]) {
F = circuit(W);
}
}
if (F.A) {
unblock(V);
} else {
for (int W : AK[V - 1]) {
auto IT = std::find(B[W - 1].begin(), B[W - 1].end(), V);
if (IT == B[W - 1].end()) {
B[W - 1].push_back(V);
}
}
}
Stack.pop_back();
return F;
}
template<int N>
bool CircuitFinder<N>::cycleCheck() //here we need to check if there exists any cycle
{ // with <+<=* regex. return 1 if there is a cycle and return 0 if not.
//cout << "circuit: "<<endl;
int countles=0;
for (auto I = Stack.begin(), E = Stack.end(); I != E; ++I) {
//checking of if the cycle have a < along with other <=
// cout << *I << " -> ";
if(I+1 != E)
{
if(relmatrix[*I][*(I+1)] != leq || relmatrix[*I][*(I+1)] != les)
{
return false;
}
else if(relmatrix[*I][*(I+1)] == les)
countles++;
}
else
{
if(relmatrix[*I][*Stack.begin()] != leq || relmatrix[*I][*Stack.begin()] != les)
return false;
else if (relmatrix[*I][*Stack.begin()] == les)
countles ++;
}
if( countles == 1)
return true;
else
return false;
}
// cout << *Stack.begin() << std::endl;
}
template<int N>
bool CircuitFinder<N>::run()
{
Stack.clear();
S = 1;
bool2 returnval;
while (S < N) {
for (int I = S; I <= N; ++I) {
Blocked[I - 1] = false;
B[I - 1].clear();
}
returnval= circuit(S);
if(returnval.B == true)
return true;
++S;
}
return false;
}
#endif /* CIRCUITFINDER_H */
......@@ -8,7 +8,7 @@
using namespace std;
#define a3215 32768 // 2^15 shortcut
typedef enum {leq=1,les,na} relation; // to implement open guards leq deontes the relation to be less than equal to, les means less than relation and na means notapplicable.
#include"circuitfinder.h" // to implement open guards leq deontes the relation to be less than equal to, les means less than relation and na means notapplicable.
// state for general TPDA with pop ande push happens at the same time
class stateGCPP{
......
......@@ -2,7 +2,11 @@
<configurationDescriptor version="100">
<logicalFolder name="root" displayName="root" projectFiles="true" kind="ROOT">
<df root="." name="0">
<df name="include">
<in>circuitfinder.h</in>
</df>
<df name="src">
<in>allpairshortest.cpp</in>
<in>continuoustpda.cpp</in>
<in>drawsystem.cpp</in>
<in>pds.cpp</in>
......@@ -63,10 +67,14 @@
</incDir>
</ccTool>
</folder>
<item path="include/circuitfinder.h" ex="false" tool="3" flavor2="0">
</item>
<item path="main.cpp" ex="false" tool="1" flavor2="8">
<ccTool flags="0">
</ccTool>
</item>
<item path="src/allpairshortest.cpp" ex="false" tool="1" flavor2="0">
</item>
<item path="src/continuoustpda.cpp" ex="false" tool="1" flavor2="8">
<ccTool flags="0">
</ccTool>
......
......@@ -7,6 +7,7 @@
<df name="images">
</df>
<df name="include">
<in>circuitfinder.h</in>
<in>continuoustpda.h</in>
<in>drawsystem.h</in>
<in>pds.h</in>
......@@ -23,6 +24,7 @@
<df name="output">
</df>
<df name="src">
<in>allpairshortest.cpp</in>
<in>continuoustpda.cpp</in>
<in>drawsystem.cpp</in>
<in>pds.cpp</in>
......
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
// C Program for Floyd Warshall Algorithm
#include<iostream>
using namespace std;
typedef enum {z,les,leq,que,na} relation;
// Number of vertices in the graph
//#define V 4
relation addition[4][4] = {{les,les,que,les},{les,leq,que,leq},{que,que,que,que},{les,leq,que,na}};
string sarr[4] = {"<","<=","?","NA"};
/* Define Infinite as a large enough value. This value will be used
for vertices not connected to each other */
//#define INF na
// A function to print the solution matrix
void printSolution(relation dist[][], int V);
// Solves the all-pairs shortest path problem using Floyd Warshall algorithm
void floydWarshall (relation graph[][], int V)
{
/* dist[][] will be the output matrix that will finally have the shortest
distances between every pair of vertices */
relation dist[V][V];
int i, j, k;
/* Initialize the solution matrix same as input graph matrix. Or
we can say the initial values of shortest distances are based
on shortest paths considering no intermediate vertex. */
for (i = 0; i < V; i++)
for (j = 0; j < V; j++)
dist[i][j] = graph[i][j];
/* Add all vertices one by one to the set of intermediate vertices.
---> Before start of a iteration, we have shortest distances between all
pairs of vertices such that the shortest distances consider only the
vertices in set {0, 1, 2, .. k-1} as intermediate vertices.
----> After the end of a iteration, vertex no. k is added to the set of
intermediate vertices and the set becomes {0, 1, 2, .. k} */
for (k = 0; k < V; k++)
{
// Pick all vertices as source one by one
for (i = 0; i < V; i++)
{
// Pick all vertices as destination for the
// above picked source
for (j = 0; j < V; j++)
{
// If vertex k is on the shortest path from
// i to j, then update the value of dist[i][j]
relation m = addition[dist[i][k]][dist[k][j]];
if (m < dist[i][j])
dist[i][j] = m;
}
}
}
// Print the shortest distance matrix
printSolution(dist);
}
/* A utility function to print solution */
void printSolution(relation dist[][],int V)
{
cout << "Following matrix shows the shortest distances" <<
" between every pair of vertices" << endl;
cout.width(7);
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
cout << sarr[dist[i][j]];
}
cout << endl;
}
}
// driver program to test above function
int main()
{
/* Let us create the following weighted graph
10
(0)------->(3)
| /|\
5 | |
| | 1
\|/ |
(1)------->(2)
3 */
relation graph[V][V] = { {z,que,les,na},
{na,z,na,leq},
{na,les,z,na},
{leq,na,na,z}
};
// Print the solution
floydWarshall(graph);
return 0;
}
// This file is for general TPDA emptiness checking with continuous implementation, pop and push also can happen at the same transition
#include<tpdaCGPP.h>
#include"tpdaCGPP.h"
#include"timePushDown.h"
#include"treeBitOperations.h"
#include<iostream>
#include<iostream>
//relation comparison[4][4] = {{les,les,les,les},{les,leq,leq,leq},{les,leq,que,que},{les,leq,que,na}};
relation addition[4][4] = {{z,les,leq,que},{les,les,les,que},{leq,les,leq,que},{que,que,que,que}};//,{inf,les,leq,que,inf}};
using namespace std;
// 'tpdaGCPPtrie' is used for checking if newly generated state was already generated earlier or not
......@@ -791,7 +793,7 @@ stateGCPP* getZeroState() {
vs->r_matrix = vs->allocate_r_matrix();
for(int i =0 ; i < vs->P;i++) // with 'na'.
for(int j = 0; j < vs->P; j++)
vs->r_matrix[i][j] = na;
vs->r_matrix[i][j] = que;
vs->del = new char[1]; // memory allocation
vs->w = new char[1];
......@@ -1168,3 +1170,57 @@ relation** stateGCPP::allocate_r_matrix(char P)
matrix[i] = new relation[P];
return matrix;
}
void pairWiseTightestRelation (relation **graph,int V) //this function does the pairwise shortest path
{
/* dist[][] will be the output matrix that will finally have the shortest
distances between every pair of vertices */
relation **dist = new relation*[V];
for(int i = 0 ; i < V ; i ++)
dist[i] = new relation[V];
//int i, j, k;
/* Initialize the solution matrix same as input graph matrix. Or
we can say the initial values of shortest distances are based
on shortest paths considering no intermediate vertex. */
for (int i = 0; i < V; i++)
for (int j = 0; j < V; j++)
dist[i][j] = graph[i][j]; // copying value to the local variable
/* Add all vertices one by one to the set of intermediate vertices.
---> Before start of a iteration, we have shortest distances between all
pairs of vertices such that the shortest distances consider only the
vertices in set {0, 1, 2, .. k-1} as intermediate vertices.
----> After the end of a iteration, vertex no. k is added to the set of
intermediate vertices and the set becomes {0, 1, 2, .. k} */
for (int k = 0; k < V; k++)
{
// Pick all vertices as source one by one
for (int i = 0; i < V; i++)
{
// Pick all vertices as destination for the
// above picked source
for (int j = 0; j < V; j++)
{
// If vertex k is on the shortest path from
// i to j, then update the value of dist[i][j]
relation m = addition[dist[i][k]][dist[k][j]];
if (m < dist[i][j])
dist[i][j] = m;
}
}
}
// Print the shortest distance matrix
//printSolution(dist,V);
//printSolution(graph,V);
for(int i = 0 ; i < V; i++)
for(int j = 0; j < V ; j++)
graph[i][j] = dist[i][j]; // change the original matrix
//printSolution(graph,V);
for(int i = 0; i < V ; i++) // delete the space allocated for the sub matrix
delete[] dist[i];
delete[] dist;
}
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment