Advertisement
splash365

ds 203 online 2

Jan 18th, 2021
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int MAXN = 27;
  5.  
  6. int N, E;
  7. vector<pair<int, int> > adj[MAXN];
  8. int parent[MAXN];
  9. bool vis[MAXN];
  10.  
  11. void init()
  12. {
  13.     for (int i = 0; i < MAXN; i++)
  14.     {
  15.         adj[i].clear();
  16.         vis[i] = false;
  17.         parent[i] = -1;
  18.     }
  19. }
  20.  
  21.  
  22. ///complexity O(V+E)
  23. void bfs(int src)
  24. {
  25.     queue<int> q;
  26.     q.push(src);
  27.     vis[src] = true;
  28.     while(!q.empty())  /// total V nodes will be pushed in the queue..it takes O(V) complexity. Again for each node only their adjecent will be checked. Thus total complexity O(V+E)
  29.     {
  30.         int u = q.front();
  31.         q.pop();
  32.  
  33.         ///printing the char representation of the current node
  34.         char node = (char)(u + 65);
  35.         cout << node << " ";
  36.         for(auto i : adj[u])
  37.         {
  38.             int v = i.second;
  39.             if (!vis[v])
  40.             {
  41.                 vis[v] = true;
  42.                 parent[v] = u;
  43.                 q.push(v);
  44.             }
  45.         }
  46.     }
  47. }
  48.  
  49. void dfs(int src)
  50. {
  51.     cout << (char)(src + 65) << " ";
  52.     vis[src] = true;
  53.     for(auto i : adj[src])
  54.     {
  55.         int v = i.second;
  56.         if(!vis[v])
  57.         {
  58.             parent[v] = src;
  59.             dfs(v);
  60.         }
  61.     }
  62. }
  63.  
  64. void PrintPath(int x)
  65. {
  66.     if(parent[x] == -1) return;
  67.     PrintPath(parent[x]);
  68.     cout << " -> " <<(char) (x+65) ;
  69. }
  70.  
  71. int main()
  72. {
  73.     init();
  74.     cin >> N >> E;
  75.     for (int i = 0; i < E; i++)
  76.     {
  77.         char x, y;
  78.         int w;
  79.         cin >> x >> y >> w;
  80.         int u = (int)x - 65;
  81.         int v = (int)y - 65;
  82.         adj[u].emplace_back(make_pair(w, v));
  83.         adj[v].emplace_back(make_pair(w, u));
  84.     }
  85.  
  86.     /// each node will take O(VlogV) times.. for all V nodes, it takes O(V^2longV) complexity to sort the whole adj list
  87.     for(int i = 0; i < 20; i++) sort(adj[i].begin(), adj[i].end());
  88.  
  89.  
  90.     char nodeX = 'E', nodeY = 'S';
  91.     int src = (int)nodeX - 65, des = (int)nodeY - 65; /// convert nodeX and nodeY to numerical value
  92.  
  93.     /// BFS here
  94.     cout << "**BFS**\n";
  95.     cout << "Exploration Order for bfs: ";
  96.     bfs(src);
  97.     cout << "\n";
  98.     cout << "From " << nodeX << " to " << nodeY << " path: " << nodeX;
  99.     PrintPath(des);
  100.     cout << "\n\n";
  101.  
  102.     /// clear vis and parent array
  103.     memset(vis, 0, sizeof(vis));
  104.     memset(parent, -1, sizeof(parent));
  105.  
  106.     /// DFS here
  107.     cout << "**DFS**\n";
  108.     cout << "Exploration Order for dfs: ";
  109.     dfs(src);
  110.     cout << "\n";
  111.     cout << "From " << nodeX << " to " << nodeY << " path: " << nodeX;
  112.     PrintPath(des);
  113.     cout << "\n\n";
  114. }
  115.  
  116.  
  117. /*
  118. 19 28
  119. A I 5
  120. A E 1
  121. A B 8
  122. B D 8
  123. B G 1
  124. G H 0
  125. G F 9
  126. H D 5
  127. C D 2
  128. E F 8
  129. C F 4
  130. C P 3
  131. F P 7
  132. E R 9
  133. R N 3
  134. R O 4
  135. R T 2
  136. P T 6
  137. T L 0
  138. T Q 5
  139. T K 5
  140. N K 7
  141. L S 7
  142. Q S 6
  143. Q O 4
  144. K O 9
  145. Q M 9
  146. M S 8
  147. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement