magneto903

non stable graph algo c++

Feb 23rd, 2021 (edited)
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.47 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. int v[1000];
  7. float cache_1[1000];
  8. int cache_2[1000];
  9. int s[5000];
  10. int e[5000];
  11. float l[5000];
  12. int graph_1[1000][20];
  13. float graph_2[1000][20];
  14.  
  15. int get_len(int arr[]) {
  16.   int len = 0;
  17.   //int i=0;
  18.   while (arr[len] > -2) {
  19.     len++;
  20.     //i++;
  21.   }
  22.  
  23.   return len;
  24. }
  25.  
  26. void myfill(int arr[]) {
  27.   for (int i=0; i<1000; i++) {
  28.     arr[i] = -2;
  29.   }
  30. }
  31. void init_arr() {
  32.   for (int i=0; i < 1000; ++i) {
  33.     v[i] = -2;
  34.     cache_1[i] = -2.0;
  35.     cache_2[i] = -2;
  36.   }
  37.   for (int i=0; i < 5000; i++) {
  38.     s[i] = -2;
  39.     e[i] = -2;
  40.     l[i] = -2.0;
  41.   }
  42.   for (int i=0; i < 1000; i++) {
  43.     for (int j=0; j < 20; j++) {
  44.       graph_1[i][j] = -2;
  45.       graph_2[i][j] = -2.0;
  46.     }
  47.   }
  48.  
  49. }
  50.  
  51.  
  52.  
  53. void add_point(int i, int point) {
  54.   v[i] = point;
  55. }
  56.  
  57. void add_edge(int i, int s_p, int e_p, float edge_len) {
  58.   s[i] = s_p;
  59.   e[i] = e_p;
  60.   l[i] = edge_len;
  61. }
  62.  
  63. void fill_graph() {
  64.   for (int i=0; i < 5000; i++) {
  65.     int start = s[i];
  66.     int end = e[i];
  67.     float dist = l[i];
  68.     if (start == -2) {
  69.       break;
  70.     }
  71.     // найдём место, куда вставить
  72.     int j = 0;
  73.     while (graph_1[start][j] > -2) {
  74.       j++;
  75.     }
  76.     graph_1[start][j] = end;
  77.     graph_2[start][j] = dist;
  78.     j=0;
  79.     while (graph_1[end][j] > -2) {
  80.       j++;
  81.     }
  82.     graph_1[end][j] = start;
  83.     graph_2[end][j] = dist;
  84.   }
  85. }
  86.  
  87. void get_dists(int a) {
  88.  
  89.   int L=0;
  90.   for (int i=0; i<1000; i++) {
  91.     if (graph_1[i][0] > -2) {
  92.       L++;
  93.     } else {
  94.       break;
  95.     }
  96.   }
  97.  
  98.   int links_1[1000][20];
  99.   float links_2[1000][20];
  100.  
  101.   for (int i=0; i < 1000; i++) {
  102.     for (int j=0; j < 20; j++) {
  103.       links_1[i][j] = -2;
  104.       links_2[i][j] = -2.0;
  105.     }
  106.   }
  107.  
  108.   int i = L;
  109.  
  110.   while (--i >= 0) {
  111.     int neighbors_verts[20];
  112.     float neighbors_dists[20];
  113.     for (int j=0; j<20; j++) {
  114.       neighbors_verts[j] = graph_1[i][j];
  115.     }
  116.     for (int j=0; j < 20; j++) {
  117.       neighbors_dists[j] = graph_2[i][j];
  118.     }
  119.    
  120.     cache_1[i] = 100000000.0;
  121.     cache_2[i] = i;
  122.     for (int j=0; j < 20; j++) {
  123.      
  124.       links_1[i][j] = neighbors_verts[j];
  125.       links_2[i][j] = neighbors_dists[j];
  126.     }
  127.   }
  128.  
  129.  
  130.   float root_1 = cache_1[a];
  131.   int root_2 = cache_2[a];
  132.  
  133.   cache_1[a] = 0;
  134.   cache_2[a] = -1;
  135.  
  136.  
  137.   root_1 = 0.0;
  138.   root_2 = -1;
  139.  
  140.   float queue_1[1000];
  141.   int queue_2[1000];
  142.   int queue_ids[1000];
  143.  
  144.   for (int j=0; j < 1000; j++) {
  145.     queue_1[j] = -2.0;
  146.     queue_2[j] = -2;
  147.     queue_ids[j] = -2;
  148.   }
  149.  
  150.   queue_1[0] = root_1;
  151.   queue_2[0] = root_2;
  152.   queue_ids[0] = 0;
  153.  
  154.   i=0;
  155.   int len = 1;
  156.  
  157.  
  158.   while (i < len) {
  159.    
  160.    
  161.     float node_1 = queue_1[i];
  162.     int node_2 = queue_2[i];
  163.     int node_id = queue_ids[i];
  164.  
  165.    
  166.     int node_verts[20];
  167.     float node_dists[20];
  168.    
  169.     int node_verts_len = 0;
  170.    
  171.     for (int j=0; j < 20; j++) {
  172.       node_verts[j] = links_1[node_id][j];
  173.       node_dists[j] = links_2[node_id][j];
  174.       if (node_verts[j] > -2) {
  175.         node_verts_len++;
  176.       }
  177.     }
  178.    
  179.     //int j = get_len(node_verts);
  180.     int j = node_verts_len;
  181.     while (j >= 0) {
  182.      
  183.       int link_vert = node_verts[j];
  184.       float link_dist = node_dists[j];
  185.  
  186.       float c1 = cache_1[link_vert];
  187.       int c2 = cache_2[link_vert];
  188.      
  189.       float d = node_1 + link_dist;
  190.      
  191.       if (d < c1) {
  192.        
  193.         cache_1[link_vert] = d;
  194.        
  195.         cache_2[link_vert] = node_id;
  196.        
  197.         queue_1[len] = d;
  198.        
  199.         queue_ids[len] = link_vert;
  200.         queue_2[len] = node_id;
  201.         len+=1;
  202.        
  203.       }
  204.      
  205.       j--;
  206.     }
  207.    
  208.    
  209.     i++;
  210.    
  211.   }
  212.  
  213.  
  214. }
  215.  
  216. int get_graph_1(int i, int j) {
  217.   return graph_1[i][j];
  218. }
  219.  
  220. float get_graph_2(int i, int j) {
  221.   return graph_2[i][j];
  222. }
  223.  
  224. float get_cache_1(int i) {
  225.   return cache_1[i];
  226. }
  227.  
  228. int get_cache_2(int i) {
  229.   return cache_2[i];
  230. }
  231.  
  232. int get_v(int i) {
  233.   return v[i];
  234. }
  235.  
  236. int get_sum() {
  237.   int sum = 0;
  238.   for (int i=0; i < 1000; i++) {
  239.     sum += v[i];
  240.   }
  241.  
  242.   return sum;
  243. }
  244.  
  245.  
  246. int main() {
  247.     /*
  248.     init_arr();
  249.    
  250.     add_point(0, 0);
  251.     add_point(1, 1);
  252.     add_point(2, 2);
  253.     add_point(3, 3);
  254.     add_point(4, 4);
  255.    
  256.     add_edge(0, 0, 2, 1);
  257.     add_edge(1, 0, 1, 1);
  258.     add_edge(2, 1, 2, 1);
  259.     add_edge(3, 2, 3, 1);
  260.     add_edge(4, 2, 4, 1);
  261.     add_edge(5, 3, 4, 1);
  262.    
  263.     fill_graph();
  264.    
  265.     get_dists(0);
  266.     */
  267.    
  268.   return 0;
  269. }
Add Comment
Please, Sign In to add comment