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
|
// File: dijkstra.cpp
#include <cstdio>
#include <iostream>
#include "event.h"
#include "pqueue.h"
using namespace std;
int TraceLevel = 1;
struct AdjacencyList
{
int vertex;
PQPriorityType weight;
AdjacencyList* next;
AdjacencyList(int j, double w, AdjacencyList* n)
{
vertex = j;
weight = w;
next = n;
}
AdjacencyList()
{
vertex = 0;
weight = 0;
next = NULL;
}
};
struct VertexInfo
{
AdjacencyList* list;
int distance;
int previous;
VertexInfo()
{
list = NULL;
distance = -1;
previous = 0;
}
};
struct Graph
{
int numVerticies;
int numEdges;
VertexInfo* vInfo;
Graph(int v)
{
numVerticies = v;
numEdges = 0;
vInfo = new VertexInfo[v];
}
};
//Takes in input of the first vertex, the second vertex, and the weight.
void input(Graph* g)
{
PQPriorityType weight;
int v1, v2;
while (true)
{
cin >> v1;
if (v1 == 0)
{
break;
}
cin >> v2;
cin >> weight;
if (g->vInfo[v1].list == NULL)
{
AdjacencyList* tempList = new AdjacencyList(v2, weight, NULL);
g->vInfo[v1].list = tempList;
}
else
{
AdjacencyList* tempList = new AdjacencyList(v2, weight, g->vInfo[v1].list);
g->vInfo[v1].list = tempList;
}
if (g->vInfo[v2].list == NULL)
{
AdjacencyList* tempList = new AdjacencyList(v1, weight, NULL);
g->vInfo[v2].list = tempList;
}
else
{
AdjacencyList* tempList = new AdjacencyList(v1, weight, g->vInfo[v2].list);
g->vInfo[v2].list = tempList;
}
}
}
//Prints the values of Graph g.
void printGraph(Graph* g)
{
Graph* temp = g;
int i = 0;
cout << "There are " << temp->numVerticies - 1 << " vertices." << endl;
cout << "The edges are as follows." << endl <<endl;
while (i < temp->numVerticies)
{
if (temp->vInfo[i].list == NULL)
{
i++;
}
else
{
if (i < temp->vInfo[i].list->vertex)
{
cout << "(" << i << ",";
cout << temp->vInfo[i].list->vertex << ") ";
cout << "weight " << temp->vInfo[i].list->weight << endl;
}
temp->vInfo[i].list = temp->vInfo[i].list->next;
}
}
}
//Creates new events from an adjacenecy list and then inserts the events into PriorityQueue queue.
void createEvents(PriorityQueue& queue, AdjacencyList* list, PQItemType temp)
{
if (list != NULL)
{
if (TraceLevel == 1)
{
cout << "Signal is being sent from " << temp->to << " to " << list->vertex << " at time " << temp->time << "." << endl << "Arriving at " << list->weight + temp->time << endl;
}
Event* e = new Event(temp->to, list->vertex, list->weight + temp->time);
insert(queue, e, temp->time);
createEvents(queue, list->next, temp);
}
}
//Takes the event queue and finds the shortest path from a start node to an end node
void shortestPath(Graph* g, int f, int l, PriorityQueue queue)
{
Event* firstEvent = new Event(0, f, 0.0);
insert(queue, firstEvent, 0.0);
g->vInfo[0].distance = 0;
while (!isEmpty(queue))
{
PQItemType temp1;
PQPriorityType temp2;
remove(queue, temp1, temp2);
createEvents(queue, g->vInfo[temp1->to].list, temp1);
if (temp1->time > -1)
{
if (TraceLevel == 1)
{
cout << "The signal arrives at " << temp1->to << " from " << temp1->from << " at time " << temp1->time << endl << endl;
}
g->vInfo[temp1->to].distance = temp1->time;
g->vInfo[temp1->to].previous = temp1->from;
}
if (g->vInfo[l].distance != -1)
{
cout << "BREAK" << endl;
break;
}
}
}
//Prints the shortest distance from a start node to and end node in the order it travels.
void printFinalPath(Graph* g, int f, int l)
{
}
int main(int argc, char** argv)
{
int numVertices, x, first, last;
PriorityQueue queue;
x = 0;
cin >> numVertices;
numVertices = numVertices + 1;
Graph* g = new Graph(numVertices);
input(g);
cin >> first;
cin >> last;
shortestPath(g, first, last, queue);
printGraph(g);
cout << "The shortest path from " << first << " to " << last << " is " << g->vInfo[last].distance << endl;
//printFinalPath(g, first, last);
return 0;
}
|