GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — TSP-Solver/Approx.cpp at master · aashusingh/TSP-Solver Expand file tree/ Copy pathApprox.cppMore file actions 95 lines (73 loc) · 2.08 KB / Copy pathApprox.cppFile metadata and controls95 lines (73 loc) · 2.08 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 #include "TSP.h" //Implement 2-approximation algorithm //Using Prim algorithm to find MST stack<int> Path; int findpathLength(int **distance){ int temp1 = 0, temp2 = 0, Pathlength=0; int initial = Path.top(); while(!Path.empty()){ temp1 = Path.top(); Path.pop(); if(!Path.empty()){ temp2 = Path.top(); Pathlength += distance[temp1][temp2]; } } Pathlength += distance[temp2][initial]; output_sol << Pathlength << endl; return Pathlength; } int preorder( node *s){ int temp = 0, i = 0; cout<< (s->keyv + 1) << ","; output_sol << (s->keyv + 1) << ","; Path.push(s->keyv); temp = s->cntr; for(i = 0; i < temp; i++){ preorder( s->child[i]); } } int findminKey(int key[], bool mstSet[],int DIM) { // Initialize min value int min = INT_MAX, min_index; for (int v = 0; v < DIM; v++) if (mstSet[v] == false && key[v] < min) min = key[v], min_index = v; return min_index; } void buildTree(int parent, int ch, int link_weight, node** s){ int temp = 0; temp = ((*(s+parent))->cntr); (*(s+parent))-> child[temp] = (*(s + ch)); (*(s+ch))->edgeval_bychild = link_weight; ((*(s+parent))->cntr)++; } int printMST(int parent[], int n, int **graph, node **s,int DIM) { int tot_wt = 0; for (int i = 1; i < DIM; i++){ buildTree(parent[i], i, graph[i][parent[i]], s); tot_wt += graph[i][parent[i]]; } return 0; } void prim(int **distance, node** s,int DIM) { int parent[DIM]; // Array to store constructed MST int key[DIM]; // Key values used to pick minimum weight edge in cut bool mstSet[DIM]; // To represent set of vertices not yet included in MST for (int i = 0; i < DIM; i++) key[i] = INT_MAX, mstSet[i] = false; key[0] = 0; parent[0] = -1; for (int count = 0; count < DIM-1; count++) { int u = findminKey(key, mstSet,DIM); mstSet[u] = true; for (int v = 0; v < DIM; v++) if (distance[u][v] && mstSet[v] == false && distance[u][v] < key[v]) parent[v] = u, key[v] = distance[u][v]; } printMST(parent, DIM, distance, s,DIM); } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |