New Member FAQ | Forums | Earn Revenue


Resources Entrance Ask Experts Exam Papers Jobs English Projects Universities Colleges Courses Schools Training My India



My Profile
Active Members
TodayLast 7 Days more...



Awards & Gifts
Online Exams

Fresher Jobs


Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian cities including Bangalore, Chennai, Hyderabad, Pune or Kochi

Resources


Find educational articles, blogs, discussion threads and other resources.

Colleges


Find details about any college in India or search for courses.

website counter



Dijkstra’s Algorithm


Posted Date:     Total Responses: 0    Posted By: Rashina   Member Level: Gold   Points/Cash: 6   



/* Program for Dijkstra's Algorithm */

#include
#include
#include
//#include "f:\user1\ug\cse\6csc06\include\mytime.h"

#define MAX 10000
//#define maxim 50
#define true 1
#define false 0

class dijsk
{ int c[10][10];
int n;
int S[10][2];
public:
void input();
// void random();
void menu();
void dijkstra(int);
void output();
};

void dijsk::input()
{
clrscr();
cout<<"How many nodes are there in the graph : ";
cin>>n;
cout<<"\nEnter the cost matrix of the graph\n";
for(int i=0;i { for(int j=0;j { if(i==j)
c[i][j]=0;
else
{ cout<<"Enter the cost of travelling from "< cin>>c[i][j];
}
}
}
}

/*void dijsk::random()
{ n=rand()%49+1;
int i;
for(i=0;i { for(int j=0;j { if(i==j)
c[i][j]=0;
else
c[i][j]=rand()%5000;
}
}
int from[maxim],min_ver;
int t1=0,s=0;
int visited[maxim],d[maxim],edge_cost;
for(int v=0;v { d[v]=c[s][v];
from[v]=s;
visited[v]=false;
}
visited[s]=true;
i=0;
for(int j=0;j for(int k=0;k<2;k++)
S[j][k]=0;
while(i { min_ver=0;
edge_cost=MAX;
for(v=0;v { if((!visited[v])&&(d[v] { min_ver=v;
edge_cost=d[v];
}
}
i++;
visited[min_ver]=true;
S[t1][0]=from[min_ver];
S[t1][1]=min_ver;
for(v=0;v {
if((!visited[v])&&(d[v] {
d[v]=d[min_ver]+c[min_ver][v];
cout< from[v]=min_ver;
}
}
t1++;
}
}*/

void dijsk::dijkstra(int s)
{
int from[10],i,min_ver,total=0;
int t1=0;
int visited[10],d[10],edge_cost;
for(int v=0;v { d[v]=c[s][v];
from[v]=s;
visited[v]=false;
}
visited[s]=true;
i=0;
for(int j=0;j<10;j++)
for(int k=0;k<2;k++)
S[j][k]=0;
while(i { min_ver=0;
edge_cost=MAX;
for(v=0;v { if((!visited[v])&&(d[v] { min_ver=v;
edge_cost=d[v];
}
}
i++;
visited[min_ver]=true;
S[t1][0]=from[min_ver];
S[t1][1]=min_ver;
for(v=0;v {
if((!visited[v])&&(d[v] {
total=d[min_ver]+c[min_ver][v];
d[v]=d[min_ver]+c[min_ver][v];
// cout< from[v]=min_ver;
}
}
t1++;
} cout<<"\t"<}

void dijsk::menu()
{
int ch;
struct time t1,t2;
do
{
clrscr();
cout<<"\n1. Enter the nodes";
cout<<"\n2. Apply Dijsktra's Algorithm";
// cout<<"\n3. Generate random results : ";
cout<<"\n4. Exit";
cout<<"\nEnter your choice : ";
cin>>ch;
switch(ch)
{ case 1: input();
break;
case 2: dijkstra(0);
cout<<"\n";
output();
break;
/* case 3: gettime(&t1);
time_display(&t1);
random();
gettime(&t2);
time_display(&t2);
cout<<"\nThe result for nodes : "< cal_time(t1,t2);
break;*/
case 4: exit(0);
default:cout<<"\nYou entered a wrong choice !!";
}
cout<<"\nPress enter to continue : ";
ch=getch();
}while(ch!='n');
}

void dijsk::output()
{
cout<<"\nThe minimum cost matrix is : \n";
for(int i=0;i cout<"<}

void main()
{ dijsk newd;
newd.menu();
getch();
}



Project Feedbacks


No feedbacks found. Be the first to respond and make money from revenue sharing program.

Post Feedback
You must Sign In to post a feedback.
Next Project: Stack using double linked list
Previous Project: PROGRAM ON VIRTUAL FUNCTION (C++)

Return to Project Index

Post New Project


Related Projects



Advertise Here





Contact Us   Advertise   Editors    Privacy Policy    Terms Of Use   

ISC Technologies.
2006 - 2009 All Rights Reserved.