1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<functional>
#include<cstdlib>
#include<list>
#include<queue>
using namespace std;
struct vertex;
struct edge
{
vertex *dest;
double cost;
edge(vertex *a=NULL, double b=0)
{
dest=a;
cost=b;
}
};
struct vertex
{
string name;
vector<edge> adj;
bool mark;
double dist;
vertex *prev;
vertex(string s)
{
name=s;
mark=false;
dist=9999;
}
};
class graph
{
public:
typedef map<string, vertex *,less<string> > vmap;
vmap work;
void addvertex(const string&);
void viewvertex();
void addedge(const string& from, const string& to, double cost);
void viewcostofedge(const string &from, const string &to);
bool is_exist(const string&, const string&);
bool pathexists(vertex *, vertex *);
void markvertex(vertex *);
void unmarkvertex(vertex *);
bool ismarked(vertex *);
void unweighted(const string&);
void printpath(const string&);
void printpath(const vertex&);
void djikstra(const string&);
};
struct path
{
vertex *dest;
double cost;
path( vertex *d = 0, double c = 0.0 ) : dest( d ) , cost( c ) { }
bool operator>(path rhs) const
{return cost>rhs.cost;}
bool operator<(path rhs) const
{return cost<rhs.cost;}
};
void graph::addvertex(const string &name)
{
vmap::iterator itr=work.begin();
itr=work.find(name);
if(itr==work.end())
{
vertex *v;
v= new vertex(name);
work[name]=v;
return;
}
cout<<"\nVertex already exists!";
}
void graph::viewvertex()
{
vmap::iterator itr=work.begin();
for(;itr!=work.end();itr++)
{
cout<<"\n"<<itr->first;
}
}
void graph::addedge(const string& from, const string& to, double cost)
{
vertex *f=(work.find(from)->second);
vertex *t=(work.find(to)->second);
edge added(t,cost);
f->adj.push_back(added);
}
void graph::viewcostofedge(const string &from, const string &to)
{
vmap::iterator itr=work.find(from);
vector<edge> v=(itr->second)->adj;
vector<edge>::iterator itr1=v.begin();
vertex *p;
for(;itr1!=v.end();itr1++)
{
if(((*itr1).dest)->name==to)
cout<<"\nThe cost is:-"<<(*itr1).cost;
}
}
bool graph::is_exist(const string& from, const string& to)
{
vmap::iterator itr=work.find(from);
vertex *v=itr->second;
itr=work.find(to);
vertex *w=itr->second;
if(pathexists(v,w))
cout<<"\nPath exists!";
else
cout<<"\nIt does not exist!";
}
bool graph::pathexists(vertex *v, vertex *w)
{
if(v->name==w->name)
return true;
if(v!=NULL)
{
vector<edge>::iterator itr=(v->adj).begin();
for(;itr!=(v->adj).end();itr++)
{
if(!pathexists((*itr).dest,w))
continue;
else
{
return true;
}
}
}
return false;
}
void graph::markvertex(vertex *v)
{
v->mark=true;
}
void graph::unmarkvertex(vertex *v)
{
v->mark=false;
}
bool graph::ismarked(vertex *v)
{
if(v->mark==true)
return true;
else
return false;
}
void graph::unweighted(const string& from)
{
vmap::iterator itr=work.find(from);
vertex *v=itr->second;
list<vertex *> q;
q.push_back(v);
v->dist=0;
while(!q.empty())
{
vertex * w=q.front();
q.pop_front();
for(int i=0;i<w->adj.size();i++)
{
edge e=w->adj[i];
vertex *x=w->adj[i].dest;
if(x->dist==9999)
{
x->dist=w->dist+1;
x->prev=w;
q.push_back(x);
}
}
}
}
void graph::printpath(const string& name)
{
vmap::iterator itr=work.find(name);
vertex w=*(itr->second);
cout<<"\nThe minimum cost is->"<<w.dist;
cout<<"\nThe path is:-";
printpath(w);
}
void graph::printpath(const vertex& w)
{
if(w.prev!=NULL)
{
printpath(*w.prev);
cout<<" to ";
}
cout<<w.name;
}
void graph::djikstra(const string& from)
{
vmap::iterator itr=work.find(from);
priority_queue<path, vector<path>, greater<path> > q;
q.push(path(itr->second,0));
path vrec;
int nodesseen=0;
for(;nodesseen<work.size();nodesseen++)
{
do
{
if(q.empty())
return;
vrec=q.top();
q.pop();
}while(ismarked((q.top()).dest));
vertex *v=vrec.dest;
markvertex(v);
for(int i=0;i<v->adj.size();i++)
{
edge e=v->adj[i];
vertex *w=e.dest;
if(w->dist>v->dist+e.cost)
{
w->dist=v->dist+e.cost;
w->prev=v;
q.push(path(w,w->dist));
}
}
}
}
int main()
{
int ch;
graph g;
string name;
string string1,string2;
double cost;
do
{
cout<<"\n\n\n1.Add Vertex\n2.View Vertex\n3.Add Edge\n4.View Cost\n5.Path exists?"
"\n6.Minimum length\n7.Djiksta\n8.Exit\nEnter choice:-\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter the name of the vertex:-";
cin>>name;
g.addvertex(name);
break;
case 2:
g.viewvertex();
break;
case 3:
cout<<"\nEnter the spot:-";
cin>>string1;
cout<<"\nEnter the destination!";
cin>>string2;
cout<<"\nEnter cost:-";
cin>>cost;
g.addedge(string1,string2,cost);
break;
case 4:
cout<<"\nEnter the spot:-";
cin>>string1;
cout<<"\nEnter the destination!";
cin>>string2;
g.viewcostofedge(string1,string2);
break;
case 5:
cout<<"\nEnter the spot:-";
cin>>string1;
cout<<"\nEnter the destination!";
cin>>string2;
g.is_exist(string1,string2);
break;
case 6:
cout<<"\nEnter the spot:-";
cin>>string1;
cout<<"\nEnter the destination!";
cin>>string2;
g.unweighted(string1);
g.printpath(string2);
break;
case 7:
cout<<"\nEnter the spot:-";
cin>>string1;
cout<<"\nEnter the destination!";
cin>>string2;
g.djikstra(string1);
g.printpath(string2);
break;
case 8:
exit(0);
default:
cout<<"\nWrong Choice!";
}
}while(1);
}
|