Experiment 6 Minimum Cost generation tree and minimum cost generation tree
Lab name: minimum cost Spanning Tree
Experiment chapter: algorithm design and analysis chapter 2
Objective: To master the ideas and general processes of greedy algorithms for solving problems,
Learn to use the prim algorithm to solve practical problems.
Submission Form: original and executable programs (cpp and exe files) of all jobs)
Paper lab report (see the last page for the format and content)
Lab content
Complete the following procedures and answer questions.
1 # include <iostream. h> 2 3 # define G_NODE_NUM 6 // number of nodes 4 5 # define INFTY 65535 6 7 template <class T> 8 9 struct ENode 10 11 {// edge node with Weight Graph 12 13 int adjVex; 14 15 T w; 16 17 ENode * nextArc; 18 19}; 20 21 template <class T> 22 23 class Graph 24 25 {26 27 public: 28 29 Graph (int mSize) {30 31 n = mSize; 32 33 a = new ENode <T> * [mSize]; 34 35 for (int I = 0; I <n; I ++) {36 37 a [I] = NULL; 38 39} 40 41} 42 43 void Prim (int s); 44 45 void putin (t x [G_NODE_NUM] [G_NODE_NUM]); 46 47 void putout (); 48 49 protected: 50 51 void Prim (int k, int * nearest, T * lowcost); 52 53 ENode <T> ** a; 54 55 int n; 56 57 }; 58 59 60 61 template <class T> 62 63 void Graph <T>: putin (t x [G_NODE_NUM] [G_NODE_NUM]) {64 65 ENode <T> * e; 66 67 for (int I = 0; I <n; I ++) {68 69 for (int j = 0; j <n; j ++) {70 71 if (X [I] [j]> 0) {72 73 e = new ENode <T> (); 74 75 e-> adjVex = j; 76 77 e-> w = X [I] [j]; 78 79 e-> nextArc = a [I]; 80 81 a [I] = e; 82 83} 84 85} 86 87} 88 89} 90 91 template <class T> 92 93 void Graph <T>: putout () {94 95 ENode <T> * e; 96 97 cout <"--- Graph Output ---" <endl; 98 99 for (int I = 0; I <n; I ++) {100 101 e = a [I]; 102 103 cout <endl <"no." <I <"node :"; 104 105 while (e! = NULL) {106 107 cout <"<e-> adjVex <" ("<e-> w <")"; 108 109 e = e-> nextArc; 110 111} 112 113} 114 115 cout <endl; 116 117} 118 119 120 121 template <class T> 122 123 void Graph <T >:: Prim (int s) 124 125 {126 127 128 student writing part 129 }; 130 131 132 133 template <class T> 134 135 void Graph <T>: Prim (int k, int * nearest, T * lowcost) 136 137 {138 139 140 student writing part 141 142} 143 144 145 146 void main () {147 Graph <int> * G; 148 149 int data [G_NODE_NUM] [G_NODE_NUM] = {150, 151, 152, 153 }, 154 155 {156, 157, 158, 159, 160, 161, int n = G_NODE_NUM; 162 163G = new Graph <int> (n); 164 165G-> putin (data); 166 167G-> putout (); 168 169G-> Prim (0); 170 171}
Program Problems
7. (optional) What is the minimum subgraph generated on the 6-10 page of the textbook using the cruise Karl algorithm?
8. (optional) What is the minimum subgraph generated by using the cruise Card Algorithm (figure 1?