-
Notifications
You must be signed in to change notification settings - Fork 0
/
onePunch.c
173 lines (131 loc) · 4.54 KB
/
onePunch.c
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
#include <stdio.h>
#include <sys/time.h>
#include <stdint.h>
#include <stdlib.h>
#include <limits.h>
#define MAX 10000
#define NOT_CONNECTED (INT_MAX)
int diameter(int distance[MAX][MAX], int nodesCount);
int distance[MAX][MAX];
/* initialize all distances to */
void Initialize() {
for (int i = 0; i < MAX; ++i) {
for (int j = 0; j < MAX; ++j) {
distance[i][j] = NOT_CONNECTED;
}
distance[i][i] = 0;
}
}
uint64_t GetTimeStamp() {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * (uint64_t) 1000000 + tv.tv_usec;
}
int main() {
/* number of nodes */
int nodeCount;
/* Number of edges */
int m;
Initialize();
/* get the node count */
if (scanf("%d", &nodeCount) < 1) {
fprintf(stderr, "Error reading node count\n");
exit(1);
}
if (nodeCount < 1 || nodeCount > MAX) {
fprintf(stderr, "Invalid number of nodes, %d. Must be 1 to %d\n",
nodeCount, MAX);
exit(1);
}
/* edge count */
if (scanf("%d", &m) < 1) {
fprintf(stderr, "Error reading edge count\n");
exit(1);
}
if (m < nodeCount - 1 || m > nodeCount * (nodeCount - 1)) {
fprintf(stderr, "Invalid number of edges, %d. Must be %d to %d\n",
m, nodeCount - 1, nodeCount * (nodeCount - 1));
exit(1);
}
while (m--) {
/* nodes - let the indexation begin from 0 */
int a, b;
/* edge weight */
int c;
if (scanf("%d %d %d", &a, &b, &c) < 3) {
fprintf(stderr, "Error reading edge\n");
exit(1);
}
if (a < 0 || a >= nodeCount || b < 0 || b >= nodeCount || c <= 0) {
fprintf(stderr, "Invalid edge: from %d to %d weight %d\n", a, b, c);
exit(1);
}
distance[a][b] = c;
}
uint64_t start = GetTimeStamp();
printf("Diameter %d\n", diameter(distance, nodeCount));
printf("Time: %ld us\n", (uint64_t) (GetTimeStamp() - start));
//
return 0;
}
/******************************************************************************/
/* Your changes here */
#include "omp.h"
int localVertexCount;
//int localDistance[MAX][MAX];
//#pragma omp threadprivate(localVertexCount,localDistance)
#pragma omp threadprivate(localVertexCount)
int *Dijkstra(int fromVertex, int vertexCount, int graph[MAX][MAX]);
int diameter(int givenDistance[MAX][MAX], int vertexCount) {
int *distancesTable[vertexCount];
localVertexCount = vertexCount;
#pragma omp parallel for copyin(localVertexCount)
for (int fromVertex = 0; fromVertex < vertexCount; ++fromVertex) {
distancesTable[fromVertex] = Dijkstra(fromVertex, localVertexCount, givenDistance);
}
int diameter = -1;
for (int i = 0; i < vertexCount; ++i) {
int maxDistance = 0;
for (int j = 0; j < localVertexCount; ++j) {
if (*(distancesTable[i] + j) > maxDistance && *(distancesTable[i] + j) != NOT_CONNECTED)
maxDistance = *(distancesTable[i] + j);
}
if (maxDistance > diameter) {
diameter = maxDistance;
}
}
return diameter;
}
int *Dijkstra(int fromVertex, int vertexCount, int graph[MAX][MAX]) {
int visitedVertex[vertexCount];
// int distancesOfThisVertex[vertexCount];
int *distancesOfThisVertex = malloc(vertexCount * sizeof(int));
int minEdge, vertex = 0, searchedEdgesCount = 0;
visitedVertex[fromVertex] = 1;
for (int i = 0; i < vertexCount; ++i) {
visitedVertex[i] = 0;
distancesOfThisVertex[i] = graph[fromVertex][i];
}
distancesOfThisVertex[fromVertex] = 0;
while (searchedEdgesCount < vertexCount - 1) {
searchedEdgesCount++;
minEdge = NOT_CONNECTED;
for (int i = 0; i < vertexCount; ++i) {
if (visitedVertex[i] == 0 && minEdge > distancesOfThisVertex[i]) {
vertex = i;
minEdge = distancesOfThisVertex[i];
}
}
visitedVertex[vertex] = 1;
for (int i = 0; i < vertexCount; ++i) {
if (visitedVertex[i] == 0 && graph[vertex][i] != NOT_CONNECTED &&
distancesOfThisVertex[vertex] != NOT_CONNECTED &&
distancesOfThisVertex[vertex] + graph[vertex][i] < distancesOfThisVertex[i]) {
distancesOfThisVertex[i] = distancesOfThisVertex[vertex] + graph[vertex][i];
}
}
}
return distancesOfThisVertex;
}
/* The following is the exact command used to compile this code */
/* g++ -O2 graph-diameter.cpp -o graph-diameter */