Question
This is the task: Implement theGraphMST()function inGraph.cwhich takes in a graph and returns a minimum spanning tree of the graph as a new graph. If
This is the task:
Implement theGraphMST()function inGraph.cwhich takes in a graph and returns a minimum spanning tree of the graph as a new graph. If the given graph has no minimum spanning tree, the function should return NULL. If the graph has multiple minimum spanning trees, you can return any of them.
You may use any minimum spanning tree algorithm you like. You can (and are encouraged to) make use of the priority queue ADT that we've supplied.
I'm not sure how to start and do what it says. This is Graph.c and I attached the #include .h files :
//ImplementationoftheUndirectedWeightedGraphADT
//Usesanadjacencymatrix
#include
#include
#include
#include
#include"Graph.h"
#include"PQ.h"
structgraph{
intnV;//#vertices
intnE;//#edges
double**edges;//adjacencymatrixstoringpositiveweights
//0ifnodesnotadjacent
};
staticbooldoHasCycle(Graphg,Vertexv,Vertexprev,bool*visited);
staticintvalidVertex(Graphg,Vertexv);
////////////////////////////////////////////////////////////////////////
GraphGraphNew(intnV){
assert(nV>0);
Graphg=malloc(sizeof(*g));
if(g==NULL){
fprintf(stderr,"error:outofmemory ");
exit(EXIT_FAILURE);
}
g->nV=nV;
g->nE=0;
g->edges=malloc(nV*sizeof(double*));
if(g->edges==NULL){
fprintf(stderr,"error:outofmemory ");
exit(EXIT_FAILURE);
}
for(inti=0;i g->edges[i]=calloc(nV,sizeof(double)); if(g->edges[i]==NULL){ fprintf(stderr,"error:outofmemory "); exit(EXIT_FAILURE); } } returng; } voidGraphFree(Graphg){ for(inti=0;i free(g->edges[i]); } free(g->edges); free(g); } //////////////////////////////////////////////////////////////////////// intGraphNumVertices(Graphg){ returng->nV; } boolGraphInsertEdge(Graphg,Edgee){ assert(validVertex(g,e.v)); assert(validVertex(g,e.w)); assert(e.v!=e.w); assert(e.weight>0.0); if(g->edges[e.v][e.w]==0.0){ g->edges[e.v][e.w]=e.weight; g->edges[e.w][e.v]=e.weight; g->nE++; returntrue; }else{ returnfalse; } } boolGraphRemoveEdge(Graphg,Vertexv,Vertexw){ assert(validVertex(g,v)); assert(validVertex(g,w)); if(g->edges[v][w]!=0.0){//edgeeingraph g->edges[v][w]=0.0; g->edges[w][v]=0.0; g->nE--; returntrue; }else{ returnfalse; } } doubleGraphIsAdjacent(Graphg,Vertexv,Vertexw){ assert(validVertex(g,v)); assert(validVertex(g,w)); returng->edges[v][w]; } voidGraphShow(Graphg){ printf("Numberofvertices:%d ",g->nV); printf("Numberofedges:%d ",g->nE); for(intv=0;v for(intw=v+1;w if(g->edges[v][w]!=0.0){ printf("Edge%d-%d:%lf ",v,w,g->edges[v][w]); } } } } boolGraphHasCycle(Graphg){ bool*visited=calloc(g->nV,sizeof(bool)); assert(visited!=NULL);//lazyerrorchecking for(intv=0;v if(!visited[v]&&doHasCycle(g,v,v,visited)){ free(visited); returntrue; } } free(visited); returnfalse; } staticbooldoHasCycle(Graphg,Vertexv,Vertexprev,bool*visited){ visited[v]=true; for(intw=0;w if(g->edges[v][w]!=0.0){ if(!visited[w]){ if(doHasCycle(g,w,v,visited)){ returntrue; } }elseif(w!=prev){ returntrue; } } } returnfalse; } GraphGraphMST(Graphg){ returng; } staticintvalidVertex(Graphg,Vertexv){ returnv>=0&&v } ----------------------------------------------------------------------------------------------------------------------------------------------------------- PQ.h: //Priorityqueueofedges //Edgeswithsmallerweighthavehigherpriority #ifndefPQ_H #definePQ_H #include #include"Graph.h" typedefstructPQRep*PQ; /** *Createsanewpriorityqueue *Complexity:O(1) */ PQPQNew(void); /** *Freesallmemoryassociatedwiththegivenpriorityqueue *Complexity:O(1) */ voidPQFree(PQpq); /** *Addsanedgetothepriorityqueue *Averagecomplexity:O(logn) */ voidPQInsert(PQpq,Edgee); /** *Removesandreturnstheedgewiththesmallestweightfromthe *priorityqueue.Ifmultipleedgeshavethesamesmallestweight,one *ofthemwillberemoved. *Complexity:O(logn) */ EdgePQExtract(PQpq); /** *Returnstrueifthegivenpriorityqueueisempty,orfalseotherwise *Complexity:O(1) */ boolPQIsEmpty(PQpq); /** *Printsthegivenpriorityqueuetostdoutfordebuggingpurposes */ voidPQShow(PQpq); #endif ------------------------------------------------------------------------------------------------------------------------------------------------------------------
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started