Question: . Create a directory to hold assignment 5. Copy equiv.h and equiv.cpp from Assignment 4 into that directory. Put all of the files for this

. Create a directory to hold assignment 5.

Copy equiv.h and equiv.cpp from Assignment 4 into that directory. Put all of the files for this assignment in that directory.

2. Create a file called mst.cpp

Copy and paste the template into it. Edit the file. Add your name and the assignment number. If you will use tabs, say how far apart the tab stops are.

3. Document the program

Write a comment in mst.cpp telling what this program will do when it is finished. Include the input format (below), an example input and an example output. when it is finished.

4. Representing a Weighted Graph

Start by defining two structure types, Edge and Graph, in mst.cpp. Be sure to document each.

An object of type Edge represents one edge in a graph. It has three fields: two vertex numbers and a weight. Include a parameterless constructor that sets all three fields to 0.

An object of type Graph represents a weighted graph. It has four fields:

the number of vertices in the graph,

the number of edges in the graph,

an array of Edge structures that holds the edges,

the physical size of that array of edges.

Include a constructor Graph(nv) that yields a graph with nv vertices and no edges. The constructor must create the array of edges.

You can assume that there are no more than 100 edges, but that number must be easy to change. To increase the maximum number of edges to 200, for example, should only require changing only one line of your program. To achieve that, create a named constant that is the maximum number of edges. For example,

 const int maxEdges = 100; 
defines constant maxEdges with a value of 100. Any time you need to refer to the maximum number of edges, use maxEdges, not 100.

All structure types must be documented by saying (1) what an object of this structure type represents and (2) what property each field represents.

5. Adding an Edge

In mst.cpp, define a function insertEdge(u, v, w, g) that adds an edge between u and v of weight w to graph g. If the edge array is full, insertEdge should refuse to add the edge.

Here is a suitable contract for insertEdge. Note its similarity to the preceding paragraph.

 // insertEdge(u, v, w, g) inserts an edge of weight w between vertices // u and v into graph g. // // If there is not enough room in g to add the edge, then // insertEdge does nothing. 

It is critical that g be passed by reference to insertEdge. Otherwise, insertEdge will modify a copy of your graph, which is not what you want.

Make sure that insertEdge does the whole job of inserting an edge. Don't force its caller to do part of the job.

6. Input and Input Format

In mst.cpp, define a function readGraph(G) that takes a graph G and reads a description of a graph from the standard input, storing it into G.

First, write a contract. Then write the function definition.

The input starts with a line that tells how many vertices the graph has. If there are five vertices, then those vertices have numbers 1, 2, 3, 4 and 5. In general, if there are n vertices, then they are numbered 1, , n.

Following the first line are the edges, one per line. Each edge line has three integers on it. Line

 2 4 50 
indicates that there is an edge of weight 50 between vertices 2 and 4. The weights are integers. The end of the input is signaled by a line that contains just a 0. An input describing graph

looks like this.

5 1 2 9 1 3 12 2 4 18 2 3 8 2 5 20 3 5 15 0 

Function readGraph must use insertEdge to add each edge.

7. Output and Output Format

In mst.cpp, define and document a function writeGraph(G) that writes a description of graph G on the standard output. See the sample output above for a reasonable output format. WriteGraph should not say that the graph is "the input graph". It should just show the graph. Naming the graph is the responsibility of whoever calls writeGraph.

8. Main

In mst.cpp, define a main function that just reads a graph and writes it. Test it. Do not move on until this works. Do not skip testing this!

9. Sorting an Array of Edges

In mst.cpp, define and document a function sortEdges(G) that sorts the array of edges in graph G. Use library function qsort to do this. (Should the contract say that sortEdges uses qsort?)

Function qsort is part of the C standard library. It is a general-purpose sorting function that sorts an array using a variant of the Quicksort algorithm. You should include header file to use qsort.

Qsort needs information about the array and how to sort it. It also needs for you to perform some conversions that, in general, would be questionable, but that work correctly here.

Qsort takes four parameters. Function call

 qsort(base, numElements, elementSize, compare); 
sorts array base, where:

Parameter base is a pointer to the array that you want to sort. You should convert this pointer to type (void*).

Parameter numElements is the number of elements in the base array. It is the logical size of the edge array.

Parameter elementSize is the number of bytes occupied by each element. Since each thing in the array has type Edge, elementSize should be sizeof(Edge). (In general, sizeof(T) is the number of bytes that something of type T occupies.)

Parameter compare is a function. (C++ allows you to pass a function as a parameter to another function. You do not run the function yourself; qsort will run it for you, many times. You are just lending the tool to qsort, not using the tool yourself.)

Function compare takes two parameters, which will be pointers to particular members of the array when qsort calls it. compare(A, B) should return

A negative number

if *A should come before *B in the sorted array

A positive number

if *A should come after *B in the sorted array

0

if you do not care what order they are in

Your comparison function can look as follows, since you want to sort according to the weights of the edges.

 int compareEdges(const Edge* A, const Edge* B) { return A->weight - B->weight; } 

To run qsort, define a new type, QSORT_COMPARE_TYPE, as follows. It is the type of the compare parameter that qsort expects to receive.

 typedef int (*QSORT_COMPARE_TYPE)(const void*, const void*); 
Now statement
 qsort((void*) Arr, n, sizeof(Edge), (QSORT_COMPARE_TYPE) compareEdges); 
will sort array of n edges Arr according to weights, with smaller weights toward the beginning of the array.

Warning. Function qsort is part of the C standard library, and C is an old language designed for experts. Some things are done in clumsy ways.

Your function, compareEdges, does not have type QSORT_COMPARE_TYPE, since compareEdges takes two parameters of type const Edge*, and qsort says that it should take two parameters of type const void*. But you can tell the compiler to ignore this, and to let you pass your compareEdges function to qsort, by doing a cast, which tells the compiler to treat a value of one type as if it is a value of a different type. In most cases, that is a very dangerous thing to do, but you have to do it to use qsort.

Do not do your own pointer casts until you have a very good understanding of what they mean.

10. Building a Minimal Spanning Tree

In mst.cpp, define and document a function minimalSpanningTree(G) that will take a Graph G as a parameter and return a minimal spanning tree of G (also of type Graph).

Document minimalSpanningTree as it will be when finished. It will return a minimal spanning tree of G.

For now, only make it sort the edges of G and return G. You will write more later.

Modify main so that it computes the minimal spanning tree (by calling minimalSpanningTree) and prints that tree. Test this, keeping in mind that minimalSpanningTree currently just sorts the edges. Are the edges sorted correctly? Do not skip testing this!

11. Performing Kruskal's Algorithm

Now modify your minimalSpanningTree function definition so that it uses Kruskal's algorithm to compute a minimal spanning tree of G. Create a new graph K without any edges.

Look at each edge in G and decide whether to add it to K. If so, then add it to K. You already have a function that adds an edge to a graph. Use it.

See the next step, "determining connections," to see how to decide whether to add an edge to the minimal spanning tree.

The only modification of G that minimalSpanningTree(G) is allowed to do is to sort the array of edges. You are not allowed to destroy the array of edges in G in order to build the array of edges of the minimal spanning tree. Graph G must be intact after running minimalSpanningTree(G). Don't be naive about copies of graphs. You do not need to copy G. Read about shallow copying.

12. Determining Connections

For two vertices u and v, say that u ~ v just when there is a path between u and v. Then ~ is an equivalence relation. (~ is reflexive: the path from u to u has length 0 and is just (u). It should be easy to see that ~ is also symmetric and transitive.)

Use your equivalence relation manager from the previous assignment to keep track of the equivalence classes of ~. Initially, each vertex is in an equivalence class by itself. When Kruskal's algorithm calls for adding an edge between u and v, tell the equivalence relation manager to merge u and v. To ask if there is a path between u and v, just ask if u and v are in the same equivalence class.

Do not copy your equivalence manager into your file that implements Kruskal's algorithm. Keep it in separate files equiv.cpp and equiv.h. Just include "equiv.h" in mst.cpp. Do not include equiv.cpp in mst.cpp!

Now test this. Is the minimal spanning tree correct? Try more than one graph! Many students have submitted programs that they thought were correct but that failed some tests.

13. Getting the Total Weight

In mst.cpp, define and document a function to compute the total weight of a graph by adding up the weights of the edges. It should return the weight, and must not write anything.

Add a call to this function in main. Test it.

14. Submit your work.

6 87 8,0 6 87 8,0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!