Question: PLease rewrite this graph.h and graph.c and make sure it works and rewrite the findminturns ( ) and findMinEnergyPath ( ) functions so that it

PLease rewrite this graph.h and graph.c and make sure it works and rewrite the findminturns() and findMinEnergyPath() functions so that it works with graph.h and graph.c
Graph.h:
#ifndef GRAPH_H
#define GRAPH_H
#include "Wall.h"
#include
typedef struct {
int from;
int to;
int weight;
} Edge;
typedef struct {
struct rock rock;
Edge *edges;
int num_edges;
} Node;
typedef struct {
Node *nodes;
int num_nodes;
} Graph;
Graph* createGraph(Wall w, int reach);
void freeGraph(Graph *g);
#endif
graph.c:
#include "graph.h"
#include
/**
* @brief Creates and constructs a graph representation from a given Wall.
*
* This function builds a graph where nodes represent rocks on the wall, and
* edges represent potential connections between rocks within a specified reach.
*
* @param w The Wall structure containing the rock information.
* @param reach The maximum distance (Manhattan distance) between rocks to be considered connected.
* @return A pointer to the newly created Graph structure, or NULL if allocation fails.
*/
Graph* createGraph(Wall w, int reach){
int numRocks = WallNumRocks(w);
// Allocate memory for the graph
Graph *g = malloc(sizeof(Graph));
if (g == NULL){ return NULL; }// Handle allocation failure
// Allocate memory for the nodes and rocks arrays
g->nodes = malloc(numRocks * sizeof(Node));
if (g->nodes == NULL){
free(g);
return NULL;
}// Handle allocation failure
struct rock *rocks = malloc(numRocks * sizeof(struct rock));
if (rocks == NULL){
free(g->nodes);
free(g);
return NULL;
}// Handle allocation failure
g->num_nodes = numRocks;
// Populate the rocks array
WallGetAllRocks(w, rocks);
// Initialize nodes and build edges
for (int i =0; i < numRocks; i++){
g->nodes[i].rock = rocks[i];
g->nodes[i].num_edges =0;
// Allocate space for potential edges (may be optimized later)
g->nodes[i].edges = malloc(numRocks * sizeof(int));
if (g->nodes[i].edges == NULL){
// Handle allocation failure (include a loop to free previously allocated memory)
for (int j =0; j < i; j++){
free(g->nodes[j].edges);
}
free(g->nodes);
free(g);
return NULL;
}
// Add edges based on distance criteria
for (int j =0; j < numRocks; j++){
if (i != j && (abs(rocks[i].row - rocks[j].row)+ abs(rocks[i].col - rocks[j].col)<= reach)){
g->nodes[i].edges[g->nodes[i].num_edges++]= j;
}
}
}
return g;
}
/**
* @brief Deallocates memory associated with a Graph structure.
*
* @param g The Graph structure to be freed.
*/
void freeGraph(Graph *g){
// Free edges for each node
for (int i =0; i < g->num_nodes; i++){
free(g->nodes[i].edges);
}
// Free the nodes array and the graph itself
free(g->nodes);
free(g);
}
climber.h:
// Interface to boulder climbing algorithms
//!!! DO NOT MODIFY THIS FILE !!!
#ifndef CLIMBER_H
#define CLIMBER_H
#include "Wall.h"
struct path {
int numRocks;
struct rock *rocks;
};
struct path findShortestPath(Wall w, int reach, Colour colour);
struct path findMinEnergyPath(Wall w, int reach, int energyCosts[NUM_COLOURS]);
struct path findMinTurnsPath(Wall w, int reach, int energyCosts[NUM_COLOURS],
int maxEnergy);
#endif
climber.c:
// Implementation of boulder climbing algorithms
#include
#include
#include
#include
#include
#include
#include "climber.h"
#include "Wall.h"
#include "graph.h"
#include "dijkstra.h"
struct path findShortestPath(Wall w, int reach, Colour colour){
int height = WallHeight(w);
int width = WallWidth(w);
// Initialise an array to store the shortest distance to reach each rock
int **dist =(int**)malloc(height * sizeof(int*));
for (int i =0; i < height; i++){
dist[i]=(int*)malloc(width * sizeof(int));
for (int j =0; j < width; j++){
dist[i][j]= INT_MAX; // Initialise distances to infinity
}
}
// Initialise a queue for BFS
int queueSize = height * width;
int* queue =(int*)malloc(queueSize * sizeof(int));
int front =0, rear =0;
// Find all starting rocks (at most 'reach' units above the ground)
for (int j =0; j < width; j++){
if (WallGetRockColour(w, height -1, j)== colour){
queue[rear++]=(height -1)* width + j;
dist[height -1][j]=0; // distance from the bottom row is 0
}
}
// Define possible movements
int dx[]={-1,0,1}; // up right left
int dy[]={0,1,-1};
while (front != rear){
int curr = queue[front++];
int row = curr / width;
int col = curr % width;
// check adjacent rocks
for (int i =0; i <3; i++){
for (int j =0; j <3; j++){

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!