Create a struct to store the node label and its cost: struct Node { char label; int cost; }; SCS214: Data Structures Assignment-4 2- Implement a class MinHeap that has the following declaration: 3- Create a class WeightedGraph, which stores a graph using an adjacency matrix with the following declaration: class WeightedGraph {  int** g;  int nVertices; public: int getNVertices();//returns the number of vertices  int getWeight(char,char);//returns weight of the edge connecting the given vertices  int* returnNeighbors(int v);// returns the indices of the neighbors of the vertex v as an int array  int numNeighbors(int v);//returns the number of neighbors of the vertex v  void loadGraphFromFile(ifstream&);//allocates the adjacency matrix & initializes edge weights from the specified file  void dijkstra(char startVertex, char* prev, Node distances[] );//find the shortest path from the start vertex to all other vertices, by filling the prev array and the distances array }; class MinHeap {  Node* heap; //an array of nodes  int _size; //size of array public:  Node extractMin(); //returns & removes the node with minimum cost  void buildMinHeap(Node[],int);// allocates array then builds a min-heap from an array of struct Node with the given size  void minHeapify(int i, int n);//restores the min-heap property for the “heap” array using the given index and size n  void decreaseKey(char label,int newCost);//decreases the node that has the given label to newCost  int parent(int i);//returns the index of the parent of i  int getSize();//returns size of the heap  bool inHeap(char);//checks if the node with the given label is in the heap }; SCS214: Data Structures Assignment-4 int main() {  WeightedGraph wg;  ifstream ifile("graph.txt");  wg.loadGraphFromFile(ifile);  char* p;  p = new char[wg.getNVertices()];  Node* n;  n=new Node[wg.getNVertices()];  wg.dijkstra('g',p,n);  cout<

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter10: Classes And Data Abstraction
Section: Chapter Questions
Problem 17PE
icon
Related questions
icon
Concept explainers
Question

1- Create a struct to store the node label and its cost:
struct Node
{
char label;
int cost;
};
SCS214: Data Structures
Assignment-4
2- Implement a class MinHeap that has the following declaration:
3- Create a class WeightedGraph, which stores a graph using an adjacency matrix
with the following declaration:
class WeightedGraph
{
 int** g;
 int nVertices;
public:
int getNVertices();//returns the number of vertices
 int getWeight(char,char);//returns weight of the edge connecting the given
vertices
 int* returnNeighbors(int v);// returns the indices of the neighbors of the vertex
v as an int array
 int numNeighbors(int v);//returns the number of neighbors of the vertex v
 void loadGraphFromFile(ifstream&);//allocates the adjacency matrix & initializes
edge weights from the specified file
 void dijkstra(char startVertex, char* prev, Node distances[] );//find the shortest
path from the start vertex to all other vertices, by filling the prev array and the
distances array
};
class MinHeap
{
 Node* heap; //an array of nodes
 int _size; //size of array
public:
 Node extractMin(); //returns & removes the node with minimum cost
 void buildMinHeap(Node[],int);// allocates array then builds a min-heap from an
array of struct Node with the given size
 void minHeapify(int i, int n);//restores the min-heap property for the “heap”
array using the given index and size n
 void decreaseKey(char label,int newCost);//decreases the node that has the given
label to newCost
 int parent(int i);//returns the index of the parent of i
 int getSize();//returns size of the heap
 bool inHeap(char);//checks if the node with the given label is in the heap
};
SCS214: Data Structures
Assignment-4
int main()
{
 WeightedGraph wg;
 ifstream ifile("graph.txt");
 wg.loadGraphFromFile(ifile);
 char* p;
 p = new char[wg.getNVertices()];
 Node* n;
 n=new Node[wg.getNVertices()];
 wg.dijkstra('g',p,n);
 cout<<endl<<"Node\tCost\tPrevious";
 for(int i=0;i<wg.getNVertices();i++)
 {
 cout<<endl<<n[i].label<<"\t"<<n[i].cost<<"\t"<<p[i];
 }
 ifile.close();
 return 0;
}
4- Your main function might look like this:
5- The file graph.txt will have the following format, where the
first line represents number of vertices and the second line
represents the number of edges. For each of the following
lines, each line has the label of the first vertex, then the label
of the second vertex, then the edge weight. Note that the
sample graph in this file is a directed graph.
6- For the sample graph in graph.txt, the output should be:
8
15
g a 9
g e 14
g f 15
a b 24
b d 2
b h 19
c b 6
c h 6
d c 11
d h 16
e b 18
e d 30
e f 5
f d 20
f h 44 

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Types of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning