30 void Timer(
bool state,
float time) {
48 bool canSwitch(
int currentQueue,
int maxOtherQueue)
const {
55 return (currentQueue < maxOtherQueue) || (
starvationCost() > 12.0f);
80 RoadDetails(
float ab,
float bc,
float c,
float aplha = 0.5,
float beta = 4,
92 float avail = capacity_ofnext_road - pop_of_next_road;
93 if (avail > allowed) {
200template <
class t,
int size>
class Graph {
216 void bfs(
int levels[size]) {
220 int visited[size] = {0};
222 int starting =
getIndex(array[0].vertex);
223 adjacent.push(starting);
224 visited[starting] = 1;
226 levels[starting] = 0;
228 while (!adjacent.empty()) {
229 int top = adjacent.front();
230 cout << array[top].vertex <<
"\t";
233 typename list<weighted>::iterator temp = array[top].Neighbors.begin();
234 while (temp != array[top].Neighbors.end()) {
235 int neighborIndex = temp->index;
236 if (visited[neighborIndex] != 1) {
237 visited[neighborIndex] = 1;
238 adjacent.push(neighborIndex);
239 levels[neighborIndex] = levels[top] + 1;
250 for (
int i = 0; i < size; i++) {
256 for (
int i = 0; i <
Vcount; i++) {
257 if (max < levels[i]) {
262 for (
int i = 0; i <= max; i++) {
263 cout <<
"\nLevel = " << i << endl;
264 for (
int j = 0; j <
Vcount; j++) {
265 if (levels[j] == i) {
266 cout <<
"\t" << array[j].vertex <<
"\t";
274 array[
Vcount].vertex = vertex;
281 void makeEdge(
int a,
int b,
float len,
float speed,
float cap) {
284 if (a < size && b < size && a >= 0 && b >= 0) {
287 array[a].Neighbors.push_back(tempA);
288 array[b].Neighbors.push_back(tempB);
291 if (a < size && b < size && a >= 0 && b >= 0) {
293 array[a].Neighbors.push_back(tempA);
297 void makeEdge(
int a,
int b,
float len,
float speed,
float cap,
298 float alpha = 0.5,
float beta = 4.0) {
302 if (a < size && b < size && a >= 0 && b >= 0) {
305 array[a].Neighbors.push_back(tempA);
306 array[b].Neighbors.push_back(tempB);
309 if (a < size && b < size && a >= 0 && b >= 0) {
311 array[a].Neighbors.push_back(tempA);
316 for (
int i = 0; i <
Vcount; i++) {
317 if (array[i].vertex == label)
331 if (a == -1 || b == -1) {
335 typename list<weighted>::iterator temp = array[a].Neighbors.begin();
336 typename list<weighted>::iterator temp1 = array[b].Neighbors.begin();
337 while (temp != array[a].Neighbors.end()) {
338 if (temp->index == b) {
343 while (temp1 != array[b].Neighbors.end()) {
344 if (temp1->index == a) {
358 if (a == -1 || b == -1)
369 array[a].Neighbors.remove(B);
370 array[b].Neighbors.remove(A);
372 array[a].Neighbors.remove(B);
375 cout <<
"\t\t\t\t\t\tDeleted\n";
385 for (
int i = 0; i <
Vcount; i++) {
386 array[i].Neighbors.remove(d);
389 for (
int i = index; i <
Vcount - 1; i++) {
390 array[i] = array[i + 1];
395 for (
int i = 0; i <
Vcount; i++) {
396 typename list<weighted>::iterator it = array[i].Neighbors.begin();
397 while (it != array[i].Neighbors.end()) {
398 if (it->index > index) {
399 it->index = it->index - 1;
408 bool visited[size] = {
false};
409 t data = array[0].vertex;
412 visited[start] =
true;
414 while (!holder.empty()) {
415 int index = holder.top();
416 cout << array[index].vertex <<
"\t";
418 typename list<weighted>::iterator temp = array[index].Neighbors.begin();
419 while (temp != array[index].Neighbors.end()) {
420 if (visited[temp->index] ==
false) {
421 holder.push(temp->index);
422 visited[temp->index] =
true;
432 if (a == -1 || b == -1) {
436 typename list<weighted>::iterator temp = array[a].Neighbors.begin();
437 while (temp != array[a].Neighbors.end()) {
438 if (b == temp->index) {
448 for (
int i = 0; i <
Vcount; i++) {
449 typename list<weighted>::iterator temp = array[i].Neighbors.begin();
450 while (temp != array[i].Neighbors.end()) {
464 for (
int i = 0; i <
Vcount; i++) {
467 bool visited[size] = {
false};
469 for (
int edges = 0; edges <
Vcount - 1; edges++) {
471 float minimum = 100000;
475 for (
int i = 0; i <
Vcount; i++) {
476 if (visited[i] ==
true) {
478 typename list<weighted>::iterator temp = array[i].Neighbors.begin();
479 while (temp != array[i].Neighbors.end()) {
481 if (temp->weight.calculateWeight() < minimum &&
482 visited[temp->index] ==
false) {
483 minimum = temp->weight.calculateWeight();
497 typename list<weighted>::iterator temp = array[start].Neighbors.begin();
498 while (temp != array[start].Neighbors.end()) {
499 if (temp->index == end) {
510 cout <<
"Inserting link btw " << start <<
"\t" << end
511 <<
"\t for a weight of " << minimum << endl;
521 if (a == -1 || b == -1) {
522 cout <<
"Vertex not found";
526 typename list<weighted>::iterator temp = array[a].Neighbors.begin();
530 while (temp != array[a].Neighbors.end()) {
531 if (b == temp->index) {
546 cout <<
"The minimum distance between the vertices " << data <<
"\t"
547 << data1 <<
"\t" <<
"is \t" << min;
549 cout <<
"No direct link";
557 float distance[size];
560 for (
int i = 0; i < size; i++) {
562 distance[i] = 1000000000;
568 for (
int i = 0; i <
Vcount; i++) {
570 float min = 1000000000;
571 for (
int j = 0; j <
Vcount; j++) {
572 if (distance[j] < min && visited[j] ==
false) {
577 if (nextNode == -1) {
582 visited[nextNode] =
true;
585 for (
auto it = array[nextNode].Neighbors.begin();
586 it != array[nextNode].Neighbors.end(); ++it) {
588 float weight = it->weight.NonIdealtime();
590 if (!visited[v] && distance[nextNode] + weight < distance[v]) {
591 distance[v] = distance[nextNode] + weight;
592 indexes[v] = nextNode;
596 if (distance[b] == 1000000000) {
604 while (current != -1) {
605 path[count] = current;
607 current = indexes[current];
610 for (
int i = count - 1; i >= 0; i--) {
611 int nodeIndex = path[i];
612 temp.push_back(array[nodeIndex].vertex);
621 float distance[size];
624 for (
int i = 0; i < size; i++) {
626 distance[i] = 1000000000;
632 for (
int i = 0; i <
Vcount; i++) {
634 float min = 1000000000;
635 for (
int j = 0; j <
Vcount; j++) {
636 if (distance[j] < min && visited[j] ==
false) {
641 if (nextNode == -1) {
642 cout <<
"Path does not exist";
647 visited[nextNode] =
true;
650 for (
auto it = array[nextNode].Neighbors.begin();
651 it != array[nextNode].Neighbors.end(); ++it) {
653 float weight = it->weight.NonIdealtime();
655 if (!visited[v] && distance[nextNode] + weight < distance[v]) {
656 distance[v] = distance[nextNode] + weight;
657 indexes[v] = nextNode;
661 if (distance[b] == 1000000000) {
662 cout <<
"No path exists\n";
669 while (current != -1) {
670 path[count] = current;
672 current = indexes[current];
674 for (
int i = count - 1; i >= 0; i--) {
675 int nodeIndex = path[i];
676 cout << array[nodeIndex].vertex <<
"\t";
678 cout <<
"\nTotal Travel Cost: " << distance[b] <<
" units" << endl;
686 cout <<
"\nVertex does not exist \n";
691 degree = array[a].Neighbors.size();
692 cout <<
"\nThe degree of the vertex " << data <<
" is " << degree << endl;
698 cout <<
"\nThe graph is Directed\n";
700 cout <<
"\nThe graph is Undirected\n";
708 cout <<
"\nCity not found.\n";
711 typename list<weighted>::iterator temp = array[index].Neighbors.begin();
712 x +=
"\nThe vertex ";
713 x += array[index].vertex +
" has links to \n";
714 while (temp != array[index].Neighbors.end()) {
715 x += array[temp->index].vertex +
"\t";
716 x +=
"length = " + to_string(temp->weight.length) +
"\n";
717 x +=
"Current Vehicles = " + to_string(temp->weight.currentVehicles) +
719 x +=
"Signal State = " + to_string(temp->weight.signalState) +
"\n";
720 x +=
"Congestion = " + to_string(temp->weight.Congestion()) +
"\n";
733 cout <<
"Incoming flights to " << target <<
":" << endl;
734 for (
int i = 0; i <
Vcount; i++) {
735 typename list<weighted>::iterator temp = array[i].Neighbors.begin();
736 while (temp != array[i].Neighbors.end()) {
737 if (temp->index == targetIdx) {
738 cout << array[i].vertex <<
"\t";
748 if (a == -1 || b == -1) {
750 throw std::runtime_error(
"Edge not found: invalid vertex");
753 typename list<weighted>::iterator temp = array[a].Neighbors.begin();
754 while (temp != array[a].Neighbors.end()) {
755 if (temp->index == b) {
760 throw std::runtime_error(
"Edge not found");
763 list<RoadDetails *>
getEdges(t data, list<RoadDetails *> edges) {
769 for (
int i = 0; i <
Vcount; i++) {
770 typename list<weighted>::iterator temp = array[i].Neighbors.begin();
771 while (temp != array[i].Neighbors.end()) {
772 if (temp->index == index) {
773 edges.push_back(&(temp->weight));
785 for (
int i = 0; i <
Vcount; i++) {
786 typename list<weighted>::iterator temp = array[i].Neighbors.begin();
787 while (temp != array[i].Neighbors.end()) {
789 sum += temp->weight.Congestion();
804 for (
int i = 0; i <
Vcount; i++) {
811 double totalCost = 0;
812 for (
int i = 0; i <
Vcount;
814 typename list<weighted>::iterator temp = array[i].Neighbors.begin();
815 while (temp != array[i].Neighbors.end()) {
817 totalCost += temp->weight.choosing();
827 for (
int i = 0; i <
Vcount; i++) {
828 typename list<weighted>::iterator temp = array[i].Neighbors.begin();
829 while (temp != array[i].Neighbors.end()) {
830 if (temp->weight.currentVehicles > 0 || temp->weight.queueCount > 0) {
831 result +=
" " + array[i].vertex +
" -> " + array[temp->index].vertex;
832 result +=
" | Vehicles: " + to_string(temp->weight.currentVehicles);
833 result +=
"/" + to_string((
int)temp->weight.capacity);
834 result +=
" | Queue: " + to_string(temp->weight.queueCount);
835 result +=
" | Congestion: " +
836 to_string((
int)(temp->weight.Congestion() * 100)) +
"%";
837 result +=
" | Signal: " +
838 string(temp->weight.signalState ?
"GREEN" :
"RED");
int Vcount
Definition Header1.h:206
string display_edge_of_index(string s)
Definition Header1.h:704
void insertVertex(t vertex)
Definition Header1.h:272
bool isEmpty()
Definition Header1.h:325
float AverageRush()
Definition Header1.h:781
int No_Of_Edges()
Definition Header1.h:446
string printRoadSnapshot()
Definition Header1.h:825
string printGrapgh()
Definition Header1.h:802
void DeleteVertex(t data)
Definition Header1.h:377
void shortest_Path_btw2_vericex(t data, t data2)
Definition Header1.h:617
void dfs()
Definition Header1.h:406
int getVertex()
Definition Header1.h:323
RoadDetails & getEdgeDetails(t data, t data1)
Definition Header1.h:745
void Incoming(t target)
Definition Header1.h:725
void makeEdge(int a, int b, float len, float speed, float cap, float alpha=0.5, float beta=4.0)
Definition Header1.h:297
void Type()
Definition Header1.h:696
Graph minimumSpanningtree()
Definition Header1.h:462
int getIndex(t label)
Definition Header1.h:315
list< RoadDetails * > getEdges(t data, list< RoadDetails * > edges)
Definition Header1.h:763
t getVertexAt(int i)
Definition Header1.h:800
void Shortest_Link_btw_two_vertices(t data, t data1)
Definition Header1.h:518
int getDegree(t data)
Definition Header1.h:683
void PrintLevels()
Definition Header1.h:248
bool edgeExist(t data1, t data2)
Definition Header1.h:429
bool searchVertex(t data)
Definition Header1.h:681
void DeleteEdge(t data1, t data2)
Definition Header1.h:355
double total_System_Cost()
Definition Header1.h:810
int No_of_edges_btw_2_vertices(t data, t data2)
Definition Header1.h:327
list< t > shortest_Path_btw2_vericex_returing_list(t data, t data2)
Definition Header1.h:553
void makeEdge(int a, int b, float len, float speed, float cap)
Definition Header1.h:281
Graph(bool x)
Definition Header1.h:211
Graph()
Definition Header1.h:207
void bfs(int levels[size])
Definition Header1.h:216
t vertex
Definition Header1.h:195
bool operator==(t v)
Definition Header1.h:197
list< weighted > Neighbors
Definition Header1.h:196
RoadDetails(float ab, float bc, float c, float aplha=0.5, float beta=4, int discharge=5)
Definition Header1.h:80
float a
Definition Header1.h:66
float calculateWeight() const
Definition Header1.h:74
float NonIdealWeight
Definition Header1.h:65
void changeState()
Definition Header1.h:169
float Congestion()
Definition Header1.h:160
void decVehicles()
Definition Header1.h:154
float capacity
Definition Header1.h:61
int dischargeCapcity
Definition Header1.h:69
TrafficSignal light
Definition Header1.h:67
float bestTime()
Definition Header1.h:114
int queueCount
Definition Header1.h:63
float max_speed
Definition Header1.h:61
float DischargeAllowed(int capacity_ofnext_road, int pop_of_next_road)
Definition Header1.h:90
void updateVehicles()
Definition Header1.h:138
int currentVehicles
Definition Header1.h:62
float NonIdealtime()
Definition Header1.h:116
bool operator<(const RoadDetails &other) const
Definition Header1.h:70
float b
Definition Header1.h:66
void increment(int time)
Definition Header1.h:180
void vehicleJoinsQueue()
Definition Header1.h:143
void change_to_red()
Definition Header1.h:176
void vehicleExitsRoad()
Definition Header1.h:131
bool signalState
Definition Header1.h:64
float length
Definition Header1.h:61
float choosing()
Definition Header1.h:182
void vehicleEntersRoad()
Definition Header1.h:125
void Time(int time)
Definition Header1.h:167
float TimeCal()
Definition Header1.h:105
void vehicleExitsQueue()
Definition Header1.h:149
bool canSwitch(int currentQueue, int maxOtherQueue) const
Definition Header1.h:48
float maxGreenTime
Definition Header1.h:17
float starvationCost() const
Definition Header1.h:38
float minGreenTime
Definition Header1.h:15
float pb
Definition Header1.h:19
float pa
Definition Header1.h:19
void turnGreen(bool &state)
Definition Header1.h:39
TrafficSignal()
Definition Header1.h:20
float redtimer
Definition Header1.h:18
float greentimer
Definition Header1.h:14
void Timer(bool state, float time)
Definition Header1.h:30
bool mustSwitch() const
Definition Header1.h:57
float queueThreshold
Definition Header1.h:16
void turnRed(bool &state)
Definition Header1.h:43
int index
Definition Header1.h:190
bool operator==(const weighted &other) const
Definition Header1.h:192
RoadDetails weight
Definition Header1.h:191