I have to find shortest path using adjacncy list matrix represantaion.
I can find shortest path,but i can not find how many shortest path
exist in graph.please help me.Here i have program for graph.cpp for
function delclaration.Search.cpp main file and graph.h is header file.
pls help me.i have to submit by tonight.
//graph.cpp
#include "Graph.h"
#define inf 9999
using namespace std;
class queue
{
private:
int q[100];
int front, rear;
protected:
queue()
{
front=rear=-1;
}
int isempty()
{
if((front==-1 && rear==-1) || (front>rear))
{
front=rear=-1;
return 1;
}
return 0;
}
void push(int a)
{
if(isempty())
front++;
q[++rear]=a;
};
int pop()
{
return q[front++];
}
};
class findpath :public queue
{
private:
int **matr , *dis, *path;
public:
int n;
void initialize(int** matrix, int size) {
n = size - 1;
dis = new int[size];
path = new int[size];
matr = matrix;
}
void init(int s)
{
push(s);
dis[s]=0;
for(int i=1;i<=n;i++)
{
if(i!=s)
{
push(i);
dis[i]=inf;
}
path[i]=0;
}
}
void min_dist(int s)
{
unsigned int v, w;
init(s);
while(!(isempty())) //while queue is not empty
{
v=pop(); //pop the node
for(int i=1;i<=n;i++)
{ //if edge from one node to other node exist
if(matr[v][i]!=0)
{
w=i;
if((dis[v]+matr[v][w])<(dis[w]))
{
dis[w]=dis[v]+matr[v][w];
path[w]=v;
}//endif
}//endif
}//endfor
}//endwhile
// cout << "Min Distance" <<endl;
}
void disp_path(int s)
{
int p=path[s];
if(p==0)
return;
disp_path(p);
// cout<<" "<<s;
}
void disp_dist(int m)
{
cout<<endl<<dis[m]<<endl<<endl;
}
};
void createMatrix(int k ,int** matrix, int size, int* ca)
{
int i,j;
for(int index = 0; index < k; index++)
{
i=ca[index]/size;
j=ca[index]%size;
// cout<<"("<<i<<","<<j<<")"<<" ";
matrix[i][j]=1;
}
for( index = 0; index < size; index++)
{
for(int j = 0; j < size; j++)
{
if(matrix[index][j]==1)
{
matrix[index][j]=1;
}
else
matrix[index][j]=0;
//cout<<matrix[index][j]<<" ";
}
//cout<<endl;
}
}
==============================
//search.cpp
#include "Graph.cpp"
#include "Graph.h"
using namespace std;
int main()
{
cout<<"Please enter starting and ending nodes and a
graph: "<<endl;
int ca[100];
int index = 0;
int iSourceNode = 0;
cin>>iSourceNode;
//cout<<"sourcenode:"<<iSourceNode<<endl;
int iDestinationNode = 0;
cin>>iDestinationNode;
int iSize = 0;
cin>>iSize;
int** matrix = new int*[iSize];
int ind=0;
index=0;
int b[20]={0},aa[20]={0},i=0;
while(ind<(iSize*iSize))
{
cin>> b[index];
ca[i]=ind+ b[index];
if(ind!=0)
ind= b[index]+ind+1;
else
ind=b[index]+1;
i++;index++;
}
for( index=0; index < iSize; index++)
{
*(matrix + index) = new int[iSize];
}
//
cout<<index<<" "<<iSourceNode<<" "<<iDestinationNode<<" "<<iSi
ze<<" "<<endl;
createMatrix(i,matrix, iSize, ca);
findpath fpath;
fpath.initialize(matrix, iSize);
fpath.min_dist(iSourceNode);
// fpath.disp_path(iDestinationNode);
// cout<<endl<<"From "<<iSourceNode<<"
to "<<iDestinationNode<<":"<<endl;
// cout<<"------------"<<endl;
// cout<<"Minimum distance route: ("<<iSourceNode;
fpath.disp_path(iDestinationNode);
//cout<<")"<<endl;
fpath.disp_dist(iDestinationNode);
//createGraph(matrix, iSize);
for(index=0; index < iSize; index++) {
delete[] *(matrix + index);
}
delete[] matrix;
return EXIT_SUCCESS;
}
=======================================
//graph.h
#include<iostream>
class queue;
class findpath;
void createMatrix(int,int**,int,int*);