Repository navigation
Expand file tree
/
Copy pathGraph.cpp
More file actions
180 lines (165 loc) · 4.2 KB
/
Copy pathGraph.cpp
File metadata and controls
180 lines (165 loc) · 4.2 KB
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
/*
THOMAS RULAND
CSCI 3250 ASSIGNMENT 8
COMPILED ON UBUNTU 14.04 USING g++
*/
#include<iostream>
#include<iomanip>
#include"Graph.h"
//The number of vertices in the graph\
int mNumVertices;
// The adjacency matrix for the graph\
int** mMatrix;
// The maximum weight for an edge in the graph (used for non-adjacent vertices)\
static const int MAX_WEIGHT = 9999;
// Constructor
Graph::Graph( int numVertices )
{
mNumVertices=numVertices;//setting number of vertices
mMatrix=new int *[mNumVertices];
for(int i=0; i<mNumVertices; i++)
{
mMatrix[i]=new int[mNumVertices]; //dynamic 2d array
}
for(int i=0; i<mNumVertices; i++)
{
for(int j=0; j<mNumVertices; j++)
{
mMatrix[i][j]=MAX_WEIGHT;//initially everything is infinity
}
}
}
// Copy Constructor
Graph::Graph( const Graph & copy)
{
mNumVertices=copy.mNumVertices;
mMatrix=new int*[mNumVertices];
for(int i=0; i<mNumVertices; i++)
{
mMatrix[i]=new int[mNumVertices];//dynamic 2d array
}
for(int i=0; i<mNumVertices; i++)
{
for(int j=0; j<mNumVertices; j++)
{
mMatrix[i][j]=copy.mMatrix[i][j];//copying over values
}
}
}
// Destructor
Graph::~Graph()
{
deallocate();//deletion
}
// Overloaded Assignment Operator
const Graph & Graph::operator=(const Graph & rhs)
{
deallocate();//deleting old array
mNumVertices=rhs.mNumVertices;
mMatrix=new int*[mNumVertices];
for(int i=0; i<mNumVertices; i++)
{
mMatrix[i]=new int[mNumVertices];//creating new 2d dynamic array
}
for(int i=0; i<mNumVertices; i++)
{
for(int j=0; j<mNumVertices; j++)
{
mMatrix[i][j]=rhs.mMatrix[i][j];//transferring data over
}
}
return *this;
}
// Adds an edge to the graph
// Returns false if vertices are out of range or weight is negative
bool Graph::addEdge( int sourceVertex, int targetVertex, int weight )
{
if(weight<0 || ((sourceVertex < 0) || (sourceVertex > (mNumVertices-1))) || ((targetVertex < 0) || (targetVertex > (mNumVertices-1))))
{
// std::cout << "false" << std::endl;
return false;
}else
{
// std::cout << "not true" << std::endl;
mMatrix[sourceVertex][targetVertex]=weight;//these two things are connected
mMatrix[targetVertex][sourceVertex]=weight;//these are also connected
// std::cout << "true" << std::endl;
return true;
}
}
// Computes the shortest paths from source_vertex to all other vertices
// using Dijkstra's shortest path algorithm
// Distances are stored in currDist
// Predecessors in path are stored in predecessor
void Graph::shortestPaths( int sourceVertex, int currDist[], int predecessor[] )
{
bool checked[mNumVertices];
bool allchecked=true;
int currV;
int minDist;
for(int i=0; i<mNumVertices; i++)
{
currDist[i]=MAX_WEIGHT;
checked[i]=true;
}
currDist[sourceVertex]=0;//source is zero from itself
predecessor[sourceVertex]=sourceVertex;//the source's predecessor is itself
do
{
minDist=MAX_WEIGHT;//settign min to max
for(int i=0; i<mNumVertices; i++)
{
if(checked[i])//if the item has not been checked
{
if(minDist>currDist[i])//if the distance is less than the mindist
{
minDist=currDist[i];//update crap
currV=i;//keep track of the index that i updated crap on
}
}
}
checked[currV]=false;//set the one thing im checking to checked
for(int i=0; i<mNumVertices; i++)//iterating through all the neighbors
{
if((currDist[currV]+mMatrix[i][currV])<currDist[i])//if the distance from the new choice is shorter update
{
currDist[i]=currDist[currV]+mMatrix[i][currV];
predecessor[i]=currV;//update the predecessor
}
}
for(int i=0; i<mNumVertices; i++)//check to see if we went through everything
{
if(checked[i]==true)
{
allchecked=true;
}else{
allchecked=false;
}
}
}while(allchecked);
}
// Displays the contents of the adjacency matrix
void Graph::displayMatrix()//displaying stuff nice and pretty like
{
for(int i=0; i<mNumVertices; i++)
{
for(int j=0; j<mNumVertices; j++)
{
std::cout << mMatrix[i][j] << " ";
}
std::cout << std::endl;
}
}
// Deallocate the matrix
void Graph::deallocate()//deleting dynamic 2d array
{
for(int i=0; i<mNumVertices; i++)
{
delete [] mMatrix[i];
}
delete [] mMatrix;
}
/*
// Copy data from an existing Graph object
void Graph::copyFrom(const Graph &); //I REFUSE TO USE THIS
*/