Question

Please write the complete code in C. Write a C function that prints the minimum spanning...

Please write the complete code in C.

Write a C function that prints the minimum spanning tree of a graph. At the end,
print the weight of the spanning tree. A suggested report format is shown in the
following example.
Source Vertex To Vertex Weight
A B 2
A C 4
B D 3
D E 1
Total weight of spanning tree: 10
Your main program will read a graph from DataIn file to an adjacency table
before calling the function.

There is a 'skeleton code' given, so can someone just fill it up:

//Q4
//Find out the minimum spanning tree (MST) of a graph which is stored in **AM1
//The MST will be stored in AM2.
//The output format should follow the specification


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#define INFINITY   0
#define MAXNODE       26

// Pointer to the file you want to read and write.
FILE *fp;

int NumberOfNode = 0;

// Initialize the Adjacency table **AM
// In this function, you should set all elements in the Adjacency table to INFINITY
void InitAM(int **AM)
{
   //*********
   //Add your code here
   //*********
   //*********
}


//Read the Graph from the input file to **AM
//
int ReadGraph(int **AM)
{
   //open file
   fp = fopen ("DataIn.txt", "r");  
  
if (fp == NULL)
   {
       printf("Unable to open file.");
       return 0;
   }
   else
   {
       int i = 0;
       // get the number of nodes of the graph
       fscanf (fp, "%d\n", &NumberOfNode);      
      
       for (;i< NumberOfNode;i++ )
       {
           int NodeOfLine = 0;
           int NumberOfNodesLine = 0;
           int node = 0;
           int weight = INFINITY;

//get the current node
           fscanf (fp, "%d", &NodeOfLine);

           if(NodeOfLine == (i+1))
           {
               int j=0;

               //get the number of nodes connected to the current node
               fscanf (fp, "%d", &NumberOfNodesLine);
          

               for (;j< NumberOfNodesLine;j++ )
               {
                   //get the weight of the link
                   fscanf (fp, "%d", &node);
                   fscanf (fp, "%d", &weight);
                   AM[i][node] = weight;                  
               }

               fscanf (fp, "\n");              

           }
           else
           {
               return 0;
           }
       }
          
   }      
  
   fclose(fp);
   return 1;


}

//find the minimum spanning tree (MST) from **AM1, and store the MSP in **AM2
//
int MPS_tree(int **AM1,int **AM2)
{
   //*********
   //Add your code here
   //*********
   //*********

}


//print out the minimum spanning tree in **AM, as specified in Q4
//
void WriteMPS(int ** AM)
{
   //*********
   //Add your code here
   //*********
   //*********

}

///print out the graph in **AM, for testing
// In this function, all you have to do is to print every element in the Adjacency table **AM
//
void WriteGraph(int ** AM)
{
   //*********
   //Add your code here
   //*********
   //*********
}

int main( )
{
   //create two-dimensional array AM1, AM2
   //
   int ** AM1 = (int **) malloc( sizeof(int *) * MAXNODE );
   int ** AM2 = (int **) malloc( sizeof(int *) * MAXNODE );
   int i;
   for ( i = 0 ; i < MAXNODE ; i ++ )
   {
       AM1[i] = (int *) malloc( sizeof(int) * MAXNODE );
       AM2[i] = (int *) malloc( sizeof(int) * MAXNODE );
   }  

   InitAM(AM1);
   InitAM(AM2);

   if (ReadGraph(AM1) == 0)
   {
       return 1;
   }

   if(MPS_tree(AM1,AM2) == 0)
   {
       printf("There is no minimun spanning tree!");
       return 1;
   }

   WriteMPS( AM2 );

   for ( i = 0 ; i < MAXNODE ; i ++ )
   {
       free( AM1[i] );
       free( AM2[i] );
   }
   free(AM1);
   free(AM2);

   return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <vector>
utilizing namespace sexually transmitted disease;
layout <class T>
class Stack{
private:
vector<T> information;
open:
bool Empty();/checks if stack is vacant
void Push(T thing);/adds thing to stack
T Top();/returns thing at top of stack
void Pop();/expels thing at top of stack
companion ostream& operator<<(ostream&out, const Stack &s);/over-burden yield administrator
};
#include <iostream>
#include <vector>
#include "Stack.h"
utilizing namespace sexually transmitted disease;
layout <class T>
bool Stack<T>::Empty()/checks if stack is vacant
{
return data.empty();
}
layout <class T>
void Stack<T>::Push(T thing)/adds thing to stack
{
data.push_back(item);
}
layout <class T>
T Stack<T>::Top()/returns thing at top of stack
{
if(data.back()>-1)
return data.back();
else
cerr<<"Stack is empty\n";
}
layout <class T>
void Stack<T>::Pop()/expels thing at top of stack
{
data.pop_back();
}
format <class T>
ostream and operator<<(ostream&out, const Stack<T> &s)/over-burden yield administrator
{
while(!s.Empty())
{
out<<s.Top();
s.Pop();
}return out;
}
#include <iostream>
#include <vector>
#include <string>
#include "Stack.h"
#include <fstream>
utilizing namespace sexually transmitted disease;
int principle()
{
ifstream inFile;
ofstream outFile;
inFile.open("palindrome.txt");
outFile.open("outfile.txt");
Stack<string> s;
string holdPalindrome="";
/I know the accompanying isn't generally right however I halted here
while(inFile)
{
holdPalindrome.append(s.Top());
outFile<<holdPalindrome<<endl;
}
inFile.close();
outFile.close();
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Please write the complete code in C. Write a C function that prints the minimum spanning...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Help with my code: The code is suppose to read a text file and when u...

    Help with my code: The code is suppose to read a text file and when u enter the winning lotto number it suppose to show the winner without the user typing in the other text file please help cause when i enter the number the code just end without displaying the other text file #include <stdio.h> #include <stdlib.h> typedef struct KnightsBallLottoPlayer{ char firstName[20]; char lastName[20]; int numbers[6]; }KBLottoPlayer; int main(){ //Declare Variables FILE *fp; int i,j,n,k; fp = fopen("KnightsBall.in","r"); //...

  • I am having problems with the following assignment. It is done in the c language. The...

    I am having problems with the following assignment. It is done in the c language. The code is not reading the a.txt file. The instructions are in the picture below and so is my code. It should read the a.txt file and print. The red car hit the blue car and name how many times those words appeared. Can i please get some help. Thank you. MY CODE: #include <stdio.h> #include <stdlib.h> #include <string.h> struct node { char *str; int...

  • Modify the below code to fit the above requirements: struct node { char data; struct node...

    Modify the below code to fit the above requirements: struct node { char data; struct node *next; struct node *previous; } *front, *MyNode, *rear, *MyPointer, *anchor *Valuenode ; typedef struct node node; int Push(char input) { if(IsFull()==1) {   printf("The queue is full. Enter the ‘^’ character to stop.\n"); return -1; } else if (IsFull()==-1) { node *MyNode=(node*)malloc(sizeof(node)); MyNode->data=input; rear->next=MyNode; MyNode->previous=rear; MyPointer=rear=MyNode; return 1; } else { node *MyNode=(node*)malloc(sizeof(node)); node *anchor=(node*)malloc(sizeof(node)); MyNode->data=input; MyPointer=rear=front=MyNode; MyNode->previous=NULL; MyNode->next=NULL; anchor->next=MyNode; return 0; } } char...

  • Preferably in python but java is good too Task 1: Minimum Spanning Trees For this warm-up...

    Preferably in python but java is good too Task 1: Minimum Spanning Trees For this warm-up task you are to implement any efficient minimum spanning tree algorithm that takes a sequence of edge-weighted graphs and outputs the minimum cost weight of a spanning tree of each Input Format For this assignment we use adjacency matrices with positive integer weights. Here a zero entry at row i and column J indicates that no edge i] exists in the graph. The first...

  • I am stuck on a data structure problem, I am just going off of Geeks for...

    I am stuck on a data structure problem, I am just going off of Geeks for Geeks for help but I need to print 100 random numbers in the range of [1-200]. I can currently only print 5 numbers. Here is my code: #include <stdio.h> #include <stdlib.h> #include <limits.h> using namespace std; //binary tree has data & left and right child struct node{ int data; struct node *left; struct node *right; }; //create a new node struct node* newNode (int...

  • C programming How to get rid of the below error. I do not know why is...

    C programming How to get rid of the below error. I do not know why is this happening and how can I correct it. Please help. typedef struct circleroute{ char* name; } Circle; Circle* circlemalloc(int nroutes); //error free Circle* readRoute(FILE* fin); //error free int main(int argc, char* argv[]){ if(argc < 2){ if(argv[1] != NULL){ FILE *fin = fopen(argv[1], "r"); if(fin == NULL){ printf("Unable to open file %s!\n", argv[1]); return EXIT_FAILURE; } int routes; fscanf(fin, "%d", &routes); Circle* c1= circlemalloc(routes); for(int...

  • This is a code for linked list, it is about adding a node in the middle...

    This is a code for linked list, it is about adding a node in the middle of a list, I am really confused why int i = 2? can't it be 0? Add to the Middle • Allocate memory and store data for new node Traverse to node just before the required position of new node Change next pointers to include new node in between struct node *newNode; newNode = malloc(sizeof(struct node)); newNode->data = 4; struct node *temp head; for(int...

  • LAB 1 2 5 7 6 9 3 8 . Write code to implement an adjacency matrix (2d matrix) which represents th...

    JAVA LAB 1 2 5 7 6 9 3 8 . Write code to implement an adjacency matrix (2d matrix) which represents the graph. Your code should contain a function called addEdgelint i, int j). Use this function to add the appropriate edges in your matrix. Write code to implement Depth-First-Search (DFS) and Breadth-First-Search (BFS) of your graph. Let 0 be the source Node . Traverse the graph using DFS, print the Nodes as they are visited. . Traverse the...

  • How would I traverse through this graph? Provide example code, please! class Edge {    int src,...

    How would I traverse through this graph? Provide example code, please! class Edge {    int src, dest;    Edge(int src, int dest)    {        this.src = src;        this.dest = dest;    } }; // class to represent a graph object class Graph {    // A list of lists to represent adjacency list    List<List<Integer>> adj = new ArrayList<>();    // Constructor to construct graph    public Graph(List<Edge> edges)    {        // allocate memory for adjacency list        for (int i = 0; i < edges.size(); i++) {            adj.add(i,...

  • In c, please implement the following 3 functions to the code below is_reachable(graph_t * g, int...

    In c, please implement the following 3 functions to the code below is_reachable(graph_t * g, int source, int dest) returns 0 if I can reach the destination from source, -1 otherwise ( using BFS) has_cycle(graph_t * g) returns 0 if there is a cycle in the graph, -1 otherwise (using BFS or DFS) print_path(graph_t * g, int source, int dest) prints any path from source to destination if there exists one (Choose either BFS or DFS, typically DFS is much...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT