c++ - Create a routing table for Dijkstra's algorithm using min_heap for node selection -
i need implement min_heap node selection routing table that's using dijkstra's algorithm. information obtained input file, 'text1.txt', formatted
10 // number of nodes = 10 (labelled 0, 1, 2, …., 9) 25 //number of directed edges = 25; next 25 lines define edges 1 3 7 //there edge v1 v3 cost of 7 5 2 1 //there edge v5 v2 cost of 1 6 4 5 //there edge v6 v4 cost of 5
here code far:
#include <iostream> #include <vector> #include <fstream> using namespace std; class e_node { //stands edge node public: int nb;//the neighbor of considered node int weight; //weight of edge above neighbor e_node() {}//constructor }; class rt_node { //rt stands routing table public: int cost; //cost node int from; //the node node int checked; int h_pos; //the postion in heap node rt_node() { = -1; cost = 9999; checked = 0; } }; class h_node { //stands head_node public: int id; int cost; h_node() { id = -1; cost = 9999; } h_node(int i, int j) { id = i; cost = j; } bool operator<(h_node h) { return (cost < h.cost); } }; void set_up(vector<rt_node> &rt, vector<h_node> & heap, int &start); //insert souce node heap , update rt , heap accordingly void heap_adjust_up(vector<rt_node> &rt, vector<h_node> & heap, int &n); void heap_adjust_down(vector<rt_node> & rt, vector<h_node> & heap); void heap_op(vector<vector<e_node> > & graph, vector<rt_node> & rt, vector<h_node> &heap); int main() { ifstream in("text1.txt"); int n, e; //n: num of nodes ; e: num of edges in >> n >> e; vector<e_node> ve; vector<vector<e_node> > graph(n, ve); e_node e1; int n1, n2, w; (int = 0; < e; i++) { in >> n1 >> n2 >> w; e1.nb = n2; e1.weight = w; graph[n1].push_back(e1); } in.close(); vector<rt_node> rt(n); //rt stands routing table vector<h_node> heap(n); int start = 0; set_up(rt, heap, start); heap_op(graph, rt, heap); (int = 0; < n; i++) { cout << "cost " << start << " " << << " is: " << rt[i].cost << " node " << rt[i].from << endl; } getchar(); getchar(); return 0; } void set_up(vector<rt_node> &rt, vector<h_node> & heap, int &start) { rt[start].from = start; rt[start].cost = 0; (size_t = 0; < heap.size(); i++) { heap[i].id = i; rt[i].h_pos = i; } heap[start].id = 0; heap[0].id = start; heap[0].cost = 0; rt[0].h_pos = start; rt[start].h_pos = 0; } void heap_adjust_up(vector<rt_node> &rt, vector<h_node> & heap, int &n) { } void heap_adjust_down(vector<rt_node> & rt, vector<h_node> & heap) { } void heap_op(vector<vector<e_node> > & graph, vector<rt_node> & rt, vector<h_node> &heap) { }
Comments
Post a Comment