[C Programming] Implement code to construct an adjacency list representation of a graph

User Generated

phopnyyre

Programming

Description

***Attached is base code to help you get started. You must use this base project/code and any functions outlined in this document***

Introduction:

You are a member of a team developing a mars rover. Your job is to develop the path-finding system and it is crucially important that you minimise energy consumption. Given a digital elevation map of the terrain, you must identify the path between two points of least energy cost.

Specification:

You will be given a digital elevation map (DEM) and an equation for calculating energy use in moving from one elevation to another adjacent elevation. To simplify the task you consider only the discrete positions and elevations represented by the DEM and will consider only moves in the four cardinal directions (NSEW).

You will be provided with code that contains some code to get you started.

int** make_dem(int size, int roughness);

This code generates a 2D array of ints representing the heights of a square area of the terrain. The size should be one more than a power of 2 (e.g. 9, 17, 33, 65) and roughness values around 4xsize work well. You will also notice that the code sets a random seed – this can be set to a fixed value for testing purposes (to ensure that you always get the same map).

int cost_funcA(int diff);

int cost_funcB(int diff);

These are two different cost functions for calculating the energy use from moving between two adjacent points in the DEM with the given height difference. The first will be used for part A and will always produce a positive number. The second is used for part B and includes a term for regenerative breaking – hence it can generate a negative value when going downhill.

There are also two functions for printing DEMs and markers:

void print_2D(int** array2D, int size);

void print_2D_ascii(int** array2D, int size);

These functions take a DEM where heights are expected to be in the range 0-99. Any negative value will be printed as a marker. You should use these functions to visualise your solution (setting the height of positions that are on your path to -1). You can use either print function. The first function gives more detail, but the second is a bit easier to interpret. (NOTE: these function print with the y axis going across the page and the x axis going down).

Your first task is to use these functions to generate an adjacency list graph representation of the terrain. Start by generating a DEM, then use the cost functions to build the weighted graph. The vertex number of a given x,y position in the map should be calculated as x*size+y.

So, for example, the following DEM:

49 51 51 56 57

46 47 50 52 54

45 47 48 50 49

46 46 47 47 47

45 45 45 45 47

Would result in an edge from vertex 0 to vertex 5 (in bold) with weight calculate using cost_funcA(46-49).

You must use the following data structures to represent the graph as an adjacency list:

typedef struct edge{

int to_vertex;

int weight;

} Edge;


typedef struct edgeNode{

Edge edge;

struct edgeNode *next;

} *EdgeNodePtr;


typedef struct edgeList{

EdgeNodePtr head;

} EdgeList;


typedef struct graph{

int V;

EdgeList *edges;

} Graph;


***To help you get started, follow these steps outlined below to help you progress in your assignment***

Part A:

For part A you will use cost_funcA which always produces positive edge weights to generate the adjacency list.

You should then implement Dijkstra’s algorithm based on the pseudocode on Wikipedia (https://en.wikipedia.org/wiki/Dijkstra%27s_algorit...). While a priority-queue implementation would be best for our scenario, you should instead use a simple search to find the nearest vertex.

Your submission must contain a main function that:

  • generates a DEM
  • converts it to an adjacency list using const_funcA
  • calculates the shortest paths from vertex 0 (top left in the DEM)
  • reconstructs the path to the last vertex (bottom right in the DEM)
  • creates a copy of the DEM with the heights changed to -1 for any vertex on the path
  • prints the DEM to show the path

Part B:

For part B you will use cost_funcB, which may produce negative edge weights (but no negatively weighted cycles, to build a new adjacency list.

You should then implement the Floyd-Warshall algorithm based on the pseudocode on Wikipedia (https://en.wikipedia.org/wiki/Floyd%E2%80%93Warsha...). Your solution must follow this pseudocode as closely as possible (note however, that in our graph specification the vertex labels start at 0).

Your submission must contain a main function that:

  • includes all of the code from part A
  • calculates a new adjacency list using const_funcB
  • calculates all shortest paths using Floyd-Warshall
  • reconstructs the path from the first to last vertex (top-left to bottom-right)
  • creates a copy of the DEM with the heights changed to -1 for any vertex on the path
  • prints the DEM to show the path

Part B does NOT have to be in a separate project.

I have not specified file names and code structure. You should endeavour to make your code as general, and hence reusable, as possible. Code should be organised into functions and files that make reuse easy. Functions and data structures should be properly commented so that another programmer will know how to use them.

The assessment steps for this task are:

1. Implement code to construct an adjacency list representation of a graph

2. Implement Dijkstra’s algorithm to find shortest paths

3. Implement Floyd-Warshall algorithm to find shortest paths

User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

Well, I have a working program. Please b...


Anonymous
Just what I needed…Fantastic!

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Related Tags