Untitled
unknown
plain_text
9 months ago
2.5 kB
1
Indexable
#include <iostream> #include "BF_Assignment.h" using namespace std; const int V = 20; const int MINUS_ONE = -1; int BFPrevValue[20]; int getIntVertex(char vertex){ return vertex > 'a' ? vertex - 'a' : vertex - 'A'; } int traveling() { } string BF_Path() { return ""; } void initializeDistances(int BFPrev[20], int BFValue[20], char start_vertex) { int src = getIntVertex(start_vertex); for (int i = 0; i < V; i++) { BFPrev[i] = MINUS_ONE; BFValue[i] = MINUS_ONE; } } void copyBFValueToBFPrevValue(int BFValue[V], int BFPrevValue[V]) { for (int i = 0; i < V; i++) { BFPrevValue[i] = BFValue[i]; } } void BF(int G[V][V], int numOfVertices, char start_vertex, int BFValue[V], int BFPrev[V]) { BFValue[getIntVertex(start_vertex)] = 0; copyBFValueToBFPrevValue(BFValue, BFPrevValue); for (int src = 0; src < numOfVertices; src++) { for (int dest = 0; dest < numOfVertices; dest++){ if (G[src][dest] != 0 && BFPrevValue[src] != MINUS_ONE) { if (BFPrevValue[dest] == MINUS_ONE && BFPrev[dest] == MINUS_ONE) { BFValue[dest] = BFPrevValue[src] + G[src][dest]; BFPrev[dest] = src; } else if (BFPrevValue[src] + G[src][dest] < BFValue[dest]) { BFValue[dest] = BFPrevValue[src] + G[src][dest]; BFPrev[dest] = src; } } } } } int main() { int G[20][20] = {{0, 72, 89, 77, 2, 58, 13, 69}, {77, 0, 69, 31, 57, 93, 83, 48}, {52, 21, 0, 62, 8, 77, 32, 14}, {33, 1, 40, 0, 72, 99, 72, 59}, {89, 24, 1, 61, 0, 12, 78, 63}, {36, 91, 98, 79, 26, 0, 28, 1}, {18, 77, 49, 36, 98, 77, 0, 45}, {75, 9, 59, 7, 77, 65, 45, 0}}; int numOfVertices = 8; char start_vertex = 'D'; int BFValue[20]; int BFPrev[20]; initializeDistances(BFPrev, BFValue, start_vertex); for (int i = 0; i < numOfVertices; i++) { printf("step %d:\n", i + 1); BF(G, numOfVertices, start_vertex, BFValue, BFPrev); for (int j = 0; j < 8; j++) { cout << BFValue[j] << " "; } cout << endl; for (int j = 0; j < numOfVertices; j++) { cout << BFPrev[j] << " "; } cout << endl; } return 0; }
Editor is loading...
Leave a Comment