This repository was archived by the owner on Jul 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain code
More file actions
192 lines (164 loc) · 4.25 KB
/
main code
File metadata and controls
192 lines (164 loc) · 4.25 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
181
182
183
184
185
186
187
188
189
190
191
192
// Martina Nobile 20710312
// Emily Neil 20701863
// Camille Walters 20716280
#include "affinity_graph.hpp"
#include <iostream>
#include <cstdlib>
using namespace std;
affinityGraph::affinityGraph(string* nameConversion, int** graph, int person0, int numPeople0)
{
matrix = graph;
names = nameConversion;
person = person0;
numPeople = numPeople0;
distances = new int[numPeople0]{INT_MAX};
}
affinityGraph::~affinityGraph()
{
}
void affinityGraph::calcPath1()
{
if(!names)
cout << "no! ";
int* distances = new int[numPeople-1];
bool* visited = new bool[numPeople-1];
for (int i = 0; i < 9; i++)
{
distances[i] = INT_MAX;
visited[i]=false;
}
int minDist = INT_MAX;
int cd = 0;
int nextPerson = 9;
int thisPerson = 9;
int distToNode = INT_MAX;
for (int a = 0; a < 9; a++)
{
for (int i = 0; i < 9; i++)
{
if (matrix[thisPerson][i]!=INT_MAX && !visited[i])
{
distToNode = matrix[thisPerson][i] + cd;
if (!visited[i] && cd+matrix[thisPerson][i] < distances[i])
distances[i] = cd+matrix[thisPerson][i];
}
if ((distToNode < minDist || distances[i] < minDist) && visited[i]==false)
{
minDist = distToNode;
nextPerson = i;
}
}
cd = distances[nextPerson];
thisPerson = nextPerson;
visited[nextPerson] = true;
minDist = INT_MAX;
distToNode = INT_MAX;
}
for (int i = 0; i < 9; i++)
{
cout << names[i] << " : " << distances[i] << endl;
}
}
void affinityGraph::calcPath2()
{
cout <<"How many people are there? \n";
int n=0;
cin>>n;
string* names = new string [n];
for (int i=0; i<n; i++){
cout<<"Please type a name: \n";
string name;
cin>>name;
names[i]=name;
}
int** matrix = new int*[n];
for (int i = 0; i < n; i++)
{
matrix[i]= new int[n];
}
for (int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
matrix[i][j] = INT_MAX;
}
}
for (int i=0; i<n; i++){//filling up the diagonal of 0s
matrix[i][i]=0;
}
for (int i=0; i<n; i++){
for (int j=i+1; j<n; j++){
cout<<"What is the affinity between " << names[i] << " and " << names [j] << "? If there is no affinity, enter -1.\n";
int affinity=0;
cin>>affinity;
if (affinity==-1){
matrix[i][j]=INT_MAX;
matrix[j][i]=INT_MAX;
}else{
matrix[i][j]=affinity;
matrix[j][i]=affinity;
}
}
}
cout<<"Who would you like to start at?\n";
string start;
int startLoc=0;
bool found=0;
do{
cin >> start;
for (int i=0; i<n; i++){
if (names[i]==start){
startLoc=i;
found=1;
}
}
if (!found)
cout<<"Sorry, that person does not exist in the matrix. Please try again\n";
}while (!found);
int* distances = new int[n];
bool* visited = new bool[n];
for (int i = 0; i < n; i++)
{
if (i==startLoc)
distances[i] = 0;
else
distances[i] = INT_MAX;
visited[i]=false;
}
int minDist = INT_MAX;
int cd = 0;
int nextPerson = startLoc;
int thisPerson = startLoc;
int distToNode = INT_MAX;
for (int a = 0; a < n; a++)
{
for (int i = 0; i < n; i++)
{
if (matrix[thisPerson][i]!=INT_MAX && !visited[i])
{
distToNode = matrix[thisPerson][i] + cd;
if (!visited[i] && cd+matrix[thisPerson][i] < distances[i])
distances[i] = cd+matrix[thisPerson][i];
}
if ((distToNode < minDist || distances[i] < minDist) && !visited[i])
{
minDist = distToNode;
nextPerson = i;
}
}
cd = distances[nextPerson];
thisPerson = nextPerson;
visited[nextPerson] = true;
minDist = INT_MAX;
distToNode = INT_MAX;
}
cout<<"Distances: \n";
for (int i = 0; i < n; i++)
{
if (i==startLoc)
{}
else
cout << names[i] << " : " << distances[i] << endl;
}
return;
}