#include<iostream>
#define SIZE 12
#define INFINITY 999
using namespace std;
class Queu
{
public:
Queu();
int isempty();
void insert(int node);
int remove();
void display();
private:
int front;
int rear;
int* queue;
};
Queu :: Queu()
{
queue = new int[SIZE];
front = 0 ;
rear = 0 ;
}
void Queu :: insert(int node)
{
if( (front == 1) && (rear == 11) )
{
cout << "Overflow" <<endl;
}
else if(front == 0)
{
queue[++front] = node;
rear++;
}
else
{
queue[++rear] = node;
}
}
int Queu :: remove()
{
int item = 0;
if(front == 0 )
{
cout << "underflow" <<endl;
}
else if(front == rear )
{
item = queue[rear];
front = 0 ;
rear = 0;
return item;
}
else
{
item = queue[front++];
return item;
}
return 0;
}
int Queu :: isempty()
{
if(front == 0)
{
return 0;
}
else
{
return 1;
}
}
void Queu :: display()
{
cout<<"Queue is ";
for(int i = front ; i <= rear ; i++)
{
cout<<queue[front]<<" ";
}
}
class Djalgo
{
public:
Djalgo();
void insert();
void search();
void initialize();
private:
int vertices;
int* dist;
int** cost;
int edges;
int* visited;
int *pvisit;
int min();
Queu Q;
};
Djalgo :: Djalgo()
{
vertices = 0 ;
dist = new int[SIZE];
cost = new int*[SIZE];
edges = 0 ;
visited = new int[SIZE];
pvisit = new int[SIZE];
}
void Djalgo :: initialize()
{
int n = 0 ;
int i = 0 , j = 0 ;
cout << "Enter number of vertices " << endl;
cin >> vertices ;
cout << " Enter The vertices " << endl;
for(int l = 0 ; l < SIZE ; l ++)
{
cost[l] = new int [SIZE];
visited[l] = 0;
dist[l] = INFINITY;
pvisit[l] = 0;
if(l < vertices)
{
cin >> n ;
Q.insert(n);
}
for(int p = 0 ; p < SIZE ; p++)
{
cost[l][p] = 0;
}
}
cout << " Enter the number of edges" <<endl;
cin>>edges;
for(int k = 0 ; k < edges ; k++)
{
cout <<"Enter the edge" << endl;
cin>>i>>j;
cout<<"Its cost "<< endl;
cin >> cost[i][j];
}
}
int Djalgo :: min()
{
int temp = INFINITY;
int node = 1 ;
int flag = 0;
for( int i = 1 ; i < SIZE ; i ++)
{
if( (visited[i] == 0 ) )
{
if(temp > dist[i])
{
temp = dist[i];
node = i;
}
}
}
return node;
}
void Djalgo :: search()
{
int i = 1 , v = 0 , s = 0 ;
cout << "Enter the source Vertex " <<endl;
cin>>s;
dist[s] = 0;
while(Q.isempty())
{
v = min();
cout<<"Current "<<v<<endl;
Q.remove();
visited[v] = 1;
pvisit[i++] = v;
for(int k = 1 ; k < SIZE ; k++)
{
if( (cost[v][k] != 0) && (visited[k] == 0) )
{
if(dist[k] > dist[v] + cost[v][k])
{
dist[k] = dist[v] + cost[v][k];
}
}
}
}
for(int f = 1 ; f < i ; f++)
{
cout << "source vertex" << s << "Destination" << pvisit[f] << " Cost" << dist[pvisit[f]] << endl;
}
}
int main()
{
Djalgo obj;
obj.initialize();
obj.search();
cin.get();
cin.get();
return 0;
}
BY: SAURABH BHATIA
#define SIZE 12
#define INFINITY 999
using namespace std;
class Queu
{
public:
Queu();
int isempty();
void insert(int node);
int remove();
void display();
private:
int front;
int rear;
int* queue;
};
Queu :: Queu()
{
queue = new int[SIZE];
front = 0 ;
rear = 0 ;
}
void Queu :: insert(int node)
{
if( (front == 1) && (rear == 11) )
{
cout << "Overflow" <<endl;
}
else if(front == 0)
{
queue[++front] = node;
rear++;
}
else
{
queue[++rear] = node;
}
}
int Queu :: remove()
{
int item = 0;
if(front == 0 )
{
cout << "underflow" <<endl;
}
else if(front == rear )
{
item = queue[rear];
front = 0 ;
rear = 0;
return item;
}
else
{
item = queue[front++];
return item;
}
return 0;
}
int Queu :: isempty()
{
if(front == 0)
{
return 0;
}
else
{
return 1;
}
}
void Queu :: display()
{
cout<<"Queue is ";
for(int i = front ; i <= rear ; i++)
{
cout<<queue[front]<<" ";
}
}
class Djalgo
{
public:
Djalgo();
void insert();
void search();
void initialize();
private:
int vertices;
int* dist;
int** cost;
int edges;
int* visited;
int *pvisit;
int min();
Queu Q;
};
Djalgo :: Djalgo()
{
vertices = 0 ;
dist = new int[SIZE];
cost = new int*[SIZE];
edges = 0 ;
visited = new int[SIZE];
pvisit = new int[SIZE];
}
void Djalgo :: initialize()
{
int n = 0 ;
int i = 0 , j = 0 ;
cout << "Enter number of vertices " << endl;
cin >> vertices ;
cout << " Enter The vertices " << endl;
for(int l = 0 ; l < SIZE ; l ++)
{
cost[l] = new int [SIZE];
visited[l] = 0;
dist[l] = INFINITY;
pvisit[l] = 0;
if(l < vertices)
{
cin >> n ;
Q.insert(n);
}
for(int p = 0 ; p < SIZE ; p++)
{
cost[l][p] = 0;
}
}
cout << " Enter the number of edges" <<endl;
cin>>edges;
for(int k = 0 ; k < edges ; k++)
{
cout <<"Enter the edge" << endl;
cin>>i>>j;
cout<<"Its cost "<< endl;
cin >> cost[i][j];
}
}
int Djalgo :: min()
{
int temp = INFINITY;
int node = 1 ;
int flag = 0;
for( int i = 1 ; i < SIZE ; i ++)
{
if( (visited[i] == 0 ) )
{
if(temp > dist[i])
{
temp = dist[i];
node = i;
}
}
}
return node;
}
void Djalgo :: search()
{
int i = 1 , v = 0 , s = 0 ;
cout << "Enter the source Vertex " <<endl;
cin>>s;
dist[s] = 0;
while(Q.isempty())
{
v = min();
cout<<"Current "<<v<<endl;
Q.remove();
visited[v] = 1;
pvisit[i++] = v;
for(int k = 1 ; k < SIZE ; k++)
{
if( (cost[v][k] != 0) && (visited[k] == 0) )
{
if(dist[k] > dist[v] + cost[v][k])
{
dist[k] = dist[v] + cost[v][k];
}
}
}
}
for(int f = 1 ; f < i ; f++)
{
cout << "source vertex" << s << "Destination" << pvisit[f] << " Cost" << dist[pvisit[f]] << endl;
}
}
int main()
{
Djalgo obj;
obj.initialize();
obj.search();
cin.get();
cin.get();
return 0;
}
BY: SAURABH BHATIA