Phoenix_x

Untitled

Apr 29th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.63 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include  <string>
  4.  
  5. using namespace std;
  6.  
  7. const int n = 10;
  8. int btojElementi = 0;
  9. string connection = "";
  10. bool isCyclic = false;
  11.  
  12. struct link //spisak ot grafa
  13. {
  14.     char key;
  15.     link *next;
  16. }
  17. *gr[n]; //масив от указатели
  18.  
  19.  
  20. void inti(link *gr[n]);
  21. int search_node(link *gr[n], char c);
  22. int search_arc(link *gr[n], char c1, char c2);
  23. void add_node(link *gr[n], char c);
  24. void add_arc(link *gr[n], char c1, char c2);
  25. void combinationUtil(char data[], int start, int end, int index, int r);
  26. bool isCycle();
  27.  
  28.  
  29. void main()
  30. {
  31.     inti(gr);
  32.  
  33.     char elem_1 = 'a';
  34.     char elem_2 = 'a';
  35.  
  36.     cout << "Vavedete vrazka mezdu dva elementa: " << endl;
  37.     cout << "Element 1: ";
  38.     cin >> elem_1;
  39.     cout << "Element 2: ";
  40.     cin >> elem_2;
  41.  
  42.     add_arc(gr, elem_1, elem_2);
  43.  
  44.     string answear = "";
  45.     while (true)
  46.     {
  47.         cout << "Zelaete li da vavedete oshte vrazki (z - ne): ";
  48.         cin >> answear;
  49.  
  50.         if (answear == "z")
  51.         {
  52.             break;
  53.         }
  54.         else
  55.         {
  56.             cout << "Element 1: ";
  57.             cin >> elem_1;
  58.             cout << "Element 2: ";
  59.             cin >> elem_2;
  60.             add_arc(gr, elem_1, elem_2);
  61.         }
  62.     }
  63.  
  64.     int r = 3;
  65.     char data[10];
  66.  
  67.     for (int r = 2; r <= btojElementi; r++)
  68.     {
  69.         combinationUtil(data, 0, btojElementi - 1, 0, r);
  70.     }
  71.  
  72.     if (isCyclic) {
  73.         cout << "Grafa sadarza primka" << endl;
  74.     }
  75.     else {
  76.         cout << "Grafa ne sadarza primka" << endl;
  77.     }
  78. }
  79.  
  80. void inti(link *gr[n])
  81. {
  82.     for (int i = 0; i < n; i++)
  83.         gr[i] = NULL;
  84. }
  85.  
  86. int search_node(link *gr[n], char c)
  87. {
  88.     int flag = 0;
  89.  
  90.     for (int i = 0; i < n; i++)
  91.     {
  92.         if (gr[i])
  93.         {
  94.             if (gr[i]->key == c)
  95.                 flag = 1;
  96.         }
  97.     }
  98.     return flag;
  99. }
  100.  
  101. int search_arc(link *gr[n], char c1, char c2)
  102. {
  103.     int flag = 0;
  104.  
  105.     if (search_node(gr, c1) && search_node(gr, c2))
  106.     {
  107.         int i = 0;
  108.  
  109.         while (gr[i]->key != c1)
  110.             i++;
  111.  
  112.         link *p = gr[i];
  113.  
  114.         while (p->key != c2 && p->next != NULL)
  115.             p = p->next;
  116.  
  117.         if (p->key == c2)
  118.             flag = 1;
  119.     }
  120.  
  121.     return flag;
  122. }
  123.  
  124. void add_node(link *gr[n], char c)
  125. {
  126.     if (search_node(gr, c))
  127.         cout << "Sashtestvuva vruh s tazi stojnost" << endl;
  128.     else
  129.     {
  130.         int i = 0;
  131.         while (gr[i] && i < n)
  132.             i++;
  133.  
  134.         if (gr[i] == NULL)
  135.         {
  136.             gr[i] = new link();
  137.             gr[i]->key = c;
  138.             gr[i]->next = NULL;
  139.  
  140.             btojElementi++;
  141.         }
  142.         else
  143.             cout << "Prepalvane na strukturata" << endl;
  144.     }
  145. }
  146.  
  147. void add_arc(link *gr[n], char c1, char c2)
  148. {
  149.     if (search_arc(gr, c1, c2))
  150.         cout << "Dagata veche sashtestvuca" << endl;
  151.     else
  152.     {
  153.         if (!search_node(gr, c1))
  154.             add_node(gr, c1);
  155.         if (!search_node(gr, c2))
  156.             add_node(gr, c2);
  157.  
  158.         int i = 0;
  159.         while (gr[i]->key != c1)
  160.             i++;
  161.  
  162.         link *p = new link();
  163.         p->key = c2;
  164.         p->next = gr[i]->next;
  165.         gr[i]->next = p;
  166.     }
  167. }
  168.  
  169. void combinationUtil(char data[], int start, int end, int index, int r)
  170. {
  171.     if (index == r)
  172.     {
  173.         for (int j = 0; j < r; j++)
  174.         {
  175.             connection += data[j];
  176.         }
  177.  
  178.         bool hasCycle = isCycle();
  179.  
  180.         if (hasCycle)
  181.         {
  182.             isCyclic = true;
  183.         }
  184.  
  185.         connection = "";
  186.         return;
  187.     }
  188.  
  189.     for (int i = start; i <= end && end - i + 1 >= r - index; i++)
  190.     {
  191.         data[index] = gr[i]->key;
  192.         combinationUtil(data, i + 1, end, index + 1, r);
  193.     }
  194. }
  195.  
  196. bool isCycle()
  197. {
  198.     int firstElementIndex = 0;
  199.     int secondElementIndex = 1;
  200.  
  201.     for (int i = 0; i < connection.length() - 1; i++)
  202.     {
  203.         char firstElement = connection[firstElementIndex];
  204.         char secondElement = connection[secondElementIndex];
  205.  
  206.         if (search_arc(gr, firstElement, secondElement) == 0) {
  207.             return false;
  208.         }
  209.  
  210.         firstElementIndex++;
  211.         secondElementIndex++;
  212.     }
  213.  
  214.     if (search_arc(gr, connection[connection.length() - 1], connection[0]) == 0)
  215.     {
  216.         return false;
  217.     }
  218.  
  219.     return true;
  220. }
Advertisement
Add Comment
Please, Sign In to add comment