Question: Please use Python as a programming language: You are given the adjacency matrix of a weighted graph. And you are given the labelling array as

Please use Python as a programming language:

You are given the adjacency matrix of a weighted graph. And you are given the labelling array as we used in the in-class implementation of Prim's Algorithm. For testing purposes, you can use the following adjacency matrix and following labelling array L. A = [ [0, 20, 0, 0, 3, 0, 0, 0], [20, 0, 4, 0, 17, 0, 0, 8], [0, 4, 0, 3, 0, 0, 0, 0], [0, 0, 3, 0, 0, 6, 0, 18], [3, 17, 0, 0, 0, 0, 4, 0], [0, 0, 0, 6, 0, 0, 0, 24], [0, 0, 19, 0, 4, 0, 0, 2], [0, 8, 0, 18, 0, 24, 2, 0] ] L = [1, 1, 1, 0, 1, 0, 0, 0] So, A is the adjacency matrix for a 8 vertex graph. For example, vertex 1 and 2 have an edge with weight 20. And currently vertices 1, 2, 3, and 5 are part of the tree, as indicated by the labelling array L. Here is the thing to do: Implement the part of Prim's that will look for the next edge to incorporate into the tree. In other words, implement code that will check for all edges that are between vertices labelled 1 and those labelled 0. First it will print those edges, so you can see it worked correctly. And then it chooses among these edges the one with the minimum weight. And updates the label of the vertex with label 0 in this edge.

Part 2. Write code that converts adjacency matrix to edge list which is a 2D array that records each edge as an array of size 3, by recording the vertices and its weight. So, for example, A given above will become as follows: EL = [ [1, 2, 20], [1, 5, 3], .... ]. Meaning that vertex 1 and 2 have an edge of weight 20, and vertex 1 and 5 have an edge of weight 3 and so on. Keep in mind that in an adjacency matrix each edge is stored twice (cell i and j, and cell j and i). However, in EL, we record it only once. Part 3. Now, write code that does the same thing as in part 1 above, but this time it works for edge list EL rather than work with adjacency matrix A.

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!