Question: WRITE C + + CODE. NO INSTRUCTIONS. Complete my C + + code and make it run. Do what is necessary even deleting portions of
WRITE C CODE. NO INSTRUCTIONS. Complete my C code and make it run. Do what is necessary even deleting portions of code to ensure it works. Make sure it also adhears to the instructions and provides the sample output.
#include
#include
#include
using namespace std;
#define UNVISITED
#define VISITED
const int defaultSize ;
Big enough for simple testing
#define INFINITE
int minVertexGraph G int D Find min cost vertex
int i v ;
Initialize v to some unvisited vertex
for i ; i GNumVertices; i
if GgetMarki UNVISITED
v i; break;
for i; i GNumVertices; i Now find smallest D value
if GgetMarki UNVISITED && Di Dv
v i;
return v;
void PrimGraph G int D int s
int V new intGNumVertices;
int i w;
for int i ; i GNumVertices; i
Di INFINITE;
Ds;
for i ; i GNumVertices; i
int v minVertexG D;
GsetMarkv VISITED;
if v s
GAddEdgetoMSTVv v;
if Dv INFINITE
return;
for w Gfirstv; w GNumVertices; w Gnextv w
if Dw Gweightv w
Dw Gweightv w;
Vw v;
sets graph matrix
setMatrixG s D V;
delete V;
void setMatrixGraph graph, int start, int weights, int vertices
int less INFINITE;
int index ;
increment through each row
for int row ; row graphNumVertices; row
if current row starting row
if row start
go to each item in the row past the diagonal
for int col row ; col graphNumVertices; col
if stored closest vertex current vertex &&
if verticesrow col && verticesrow
graphsetEdgerow col, weightsrow;
else
graphdeleteEdgerow col;
graphdeleteEdgecol row;
if current row starting row
else
for int col row; col graphNumVertices; col
if least needs to be updated
if less graphweightrow col &&
graphweightrow col
less graphweightrow col;
index col;
if less is less than current item weight
else if less graphweightrow col
if less current item, delete item and it's mirror
graphdeleteEdgerow col;
graphdeleteEdgecol row;
deletes values before less
for int p row; p index; p
if less graphweightp row
graphdeleteEdgerow p;
graphdeleteEdgep row;
set the edge
graphsetEdgerow index, less;
GraphM.h
#include MSTh
#include "Graph.h
#include
#include
using namespace std;
class GraphM : public Graph
private:
int numVertex, numEdge;
int matrix;
int mark;
public:
GraphMint numVert
InitnumVert;
~GraphM
delete mark;
for int i ; i numVertex; i
delete matrixi;
delete matrix;
void Initint n
int i;
numVertex n;
numEdge ;
mark new intn;
for i ; i numVertex; i
marki UNVISITED;
matrix int new intnumVertex;
for i ; i numVertex; i
matrixi new intnumVertex;
for i ; i numVertex; i
for int j ; j numVertex; j
matrixij;
int NumVertices return numVertex;
int NumEdges return numEdge;
Return first neighbor of v
int firstint v
for int i ; i numVertex; i
if matrixvi
return i;
return numVertex;
Return vs next neighbor after w
int nextint v int w
for int i w ; i numVertex; i
if matrixvi
return i;
return numVertex;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
