void complex_step(struct poodleResult *res, Graph g, int *timeToPoodle,
bool *poodled, int *parent, struct computer computers[],
int numComputers) {
int minTime = INT_MAX;
int current = -1;
code
for (int j = 0; j < numComputers; j++) {
printf("%d", poodled[j]);
if (!poodled[j] && timeToPoodle[j] < minTime) {
minTime = timeToPoodle[j];
current = j;
}
}
if (current == -1) {
return;
}
poodled[current] = true;
res->steps[res->numSteps].computer = current;
res->steps[res->numSteps].time = minTime;
res->steps[res->numSteps].recipients = NULL;
for (AdjNode *adj = g->edges[current]; adj != NULL; adj = adj->next) {
int next = adj->v;
if (!poodled[next] && computers[current].securityLevel >=
computers[next].securityLevel - 1) {
int newTime = minTime + adj->weight + computers[next].poodleTime;
if (newTime < timeToPoodle[next]) {
timeToPoodle[next] = newTime;
parent[next] = current;
}
}
}
res->numSteps++;
}
struct poodleResult advancedPoodle(struct computer computers[],
int numComputers,
struct connection connections[],
int numConnections, int sourceComputer) {
struct poodleResult res;
res.numSteps = 0;
res.steps = malloc(numComputers * sizeof(struct step));
code
Assistant
int *timeToPoodle = malloc(numComputers * sizeof(int));
bool *poodled = malloc(numComputers * sizeof(bool));
int *parent = malloc(numComputers * sizeof(int));
for (int i = 0; i < numComputers; i++) {
timeToPoodle[i] = INT_MAX;
poodled[i] = false;
parent[i] = -1;
}
Graph g = GraphNew(numComputers);
for (int i = 0; i < numConnections; i++) {
GraphInsertEdge(g, CreateEdgeObj(connections[i].computerA,
connections[i].computerB,
connections[i].transmissionTime));
}
timeToPoodle[sourceComputer] = computers[sourceComputer].poodleTime;
for (int i = 0; i < numComputers; i++) {
complex_step(&res, g, timeToPoodle, poodled, parent, computers,
numComputers);
// The majority of the extra complexity is here
for (int j = 0; j < numComputers; j++) {
if (poodled[j]) {
for (AdjNode *adj = g->edges[j]; adj != NULL; adj = adj->next) {
int next = adj->v;
if (!poodled[next] &&
computers[j].securityLevel >=
computers[next].securityLevel - 1) {
// security level++
computers[next].securityLevel =
computers[j].securityLevel;
int newTime = timeToPoodle[j] + adj->weight +
computers[next].poodleTime;
if (newTime < timeToPoodle[next]) {
timeToPoodle[next] = newTime;
parent[next] = j;
}
}
}
}
}
printf("|\n");
}
qsort(res.steps, res.numSteps, sizeof(struct step), compareSteps);
GraphFree(g);
free(timeToPoodle);
free(poodled);
free(parent);
return res;
}
This mostly works, but the output ends early sometimes
For example:
Expected:
Plan:
computer 2 poodled at 20 seconds
computer 3 poodled at 47 seconds
computer 1 poodled at 87 seconds
computer 4 poodled at 168 seconds
computer 0 poodled at 248 seconds
Current:
Plan:
computer 2 poodled at 20 seconds
computer 3 poodled at 47 seconds
This loop seems to be problematic
for (int j = 0; j < numComputers; j++) {
if (poodled[j]) {
for (AdjNode *adj = g->edges[j]; adj != NULL; adj = adj->next) {
int next = adj->v;
if (!poodled[next] &&
computers[j].securityLevel >=
computers[next].securityLevel - 1) {
computers[next].securityLevel =
computers[j].securityLevel;
int newTime = timeToPoodle[j] + adj->weight +
computers[next].poodleTime;
if (newTime < timeToPoodle[next]) {
timeToPoodle[next] = newTime;
parent[next] = j;
}
}
}
}
}
The issue you're experiencing might be due to the early termination of the algorithm. The complex_step function returns when there are no more computers to poodle, but it doesn't consider the security level updates that happen in the loop you mentioned.
Here's a modified version of the advancedPoodle function that should address this issue:
c
struct poodleResult advancedPoodle(struct computer computers[],
int numComputers,
struct connection connections[],
int numConnections, int sourceComputer) {
struct poodleResult res;
res.numSteps = 0;
res.steps = malloc(numComputers * sizeof(struct step));
int *timeToPoodle = malloc(numComputers * sizeof(int));
bool *poodled = malloc(numComputers * sizeof(bool));
int *parent = malloc(numComputers * sizeof(int));
for (int i = 0; i < numComputers; i++) {
timeToPoodle[i] = INT_MAX;
poodled[i] = false;
parent[i] = -1;
}
Graph g = GraphNew(numComputers);
for (int i = 0; i < numConnections; i++) {
GraphInsertEdge(g, CreateEdgeObj(connections[i].computerA,
connections[i].computerB,
connections[i].transmissionTime));
}
timeToPoodle[sourceComputer] = computers[sourceComputer].poodleTime;
bool changed;
do {
changed = false;
complex_step(&res, g, timeToPoodle, poodled, parent, computers,
numComputers);
for (int j = 0; j < numComputers; j++) {
if (poodled[j]) {
for (AdjNode *adj = g->edges[j]; adj != NULL; adj = adj->next) {
int next = adj->v;
if (!poodled[next] &&
computers[j].securityLevel >=
computers[next].securityLevel - 1) {
int oldSecurityLevel = computers[next].securityLevel;
computers[next].securityLevel =
computers[j].securityLevel;
int newTime = timeToPoodle[j] + adj->weight +
computers[next].poodleTime;
if (newTime < timeToPoodle[next]) {
timeToPoodle[next] = newTime;
parent[next] = j;
changed = true;
}
if (oldSecurityLevel != computers[next].securityLevel) {
changed = true;
}
}
}
}
}
printf("|\n");
} while (changed);
qsort(res.steps, res.numSteps, sizeof(struct step), compareSteps);
GraphFree(g);
free(timeToPoodle);
free(poodled);
free(parent);
return res;
}
The main changes are:
We've introduced a do-while loop that continues as long as there are changes in the security levels or poodle times.
We've added a changed flag to keep track of whether any changes occurred in each iteration.
We check for changes in both the poodle times and security levels.
This modification ensures that the algorithm continues until no more changes can be made, either due to security level updates or new poodle times. This should allow the algorithm to consider all possible poodle paths, even those that become available after security level updates.
Remember to adjust the complex_step function to not return early if there are no unpoodled computers with finite time, as the security level updates might make new computers available in subsequent iterations.