Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m having trouble figuring out how to code these sections for an assignment...

ID: 3936618 • Letter: I

Question

I'm having trouble figuring out how to code these sections for an assignment... (Sections 4 and 5 are in a .cpp file without a main. Section 6 is in a seperate file that's only purpose is to test using a main method.)

4. Reading the Graph

Document and define a function to read a graph. Use the adjacency list representation. Only store the graph once.

5. Printing a Graph

Document and define a function to print a graph. .

6. Testing

Test reading and echoing the graph. Do not move on until you are satisfied that this much works.

(Testing is done in the main method, not the .cpp file).

How would I code this using the following code?

#include;
#include;
#include ;
using namespace std;

struct Edge
{
int vertexNumber;
int weight;
struct Edge* next;

Edge( int vn, int wgt, struct Edge* nxt )
{
vertexNumber = vn;
weight = wgt;
next = nxt;
};
};

struct Vertex
{
struct Edge* adjacencyList;
double shortestDistance;
int vertexNumber;

Vertex()
{
vertexNumber = -1;
shortestDistance = -1;
adjacencyList = NULL;
}
};

struct Graph{
int nVertices;
int nEdges;

struct Vertex* vertices;
Graph( int nVert ){
vertices = new struct Vertex[nVert];
nEdges = 0;
}
};

4. Reading the Graph

Document and define a function to read a graph. Use the adjacency list representation. Only store the graph once.

5. Printing a Graph

Document and define a function to print a graph. .

6. Testing

Test reading and echoing the graph. Do not move on until you are satisfied that this much works.

(Testing is done in the main method, not the .cpp file).

Explanation / Answer

For 4. Reading the Graph :

Store the graph in text file as following, that each line represents a vertex along with its neighbours i.e.

v1 2 v2 v3

v2 1 v1

v3 1 v1

where v1 is first vertex and ( v2 and v3 ) are two vertices that are v1s neighbours and so on. Then number in bold represents number of neighbours of vertex.

Then read this file, and create vertices and edges from the given structure

You can store nVert and nEdges in the input file too, for initializing these variables of struct graph

For 5. Printing a Graph

Print it in format somewhat similar to the input file described in 4.

v1: n1 n2

,etc i.e. v1 has neighbour n1 and n2. Note that we need not print number of neighbours here

6. For 6, create several input files, and read them and print them and see if printed graph matches that of input file one

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote