Advertisement
Guest User

GraphLEFunctions

a guest
Jun 17th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include<string>
  4. #include "GraphADT.h"
  5. using namespace std;
  6.  
  7. Graph::Graph(int x)
  8. {
  9. V = x;
  10. adj = new list <int>[V];
  11. adj2 = new float*[V];
  12. for (int i = 0; i < V; i++)
  13. {
  14. adj2[i] = new float[V];
  15. }
  16. for (int i = 0; i < V; i++)
  17. {
  18. for (int j = 0; j < V; j++)
  19. {
  20. adj2[i][j] = 0;
  21. }
  22. }
  23. places = new vertex[V];
  24. cout << endl << "Input Vertices Information..." << endl;
  25. for (int i = 0; i < V; i++)
  26. {
  27. places[i].v = i;
  28. cout << "V[" << places[i].v << "]...\nLabel: ";
  29. cin >> places[i].label;
  30. }
  31. }
  32.  
  33. void Graph::addEdge(int u, int v)
  34. {
  35. adj[u].push_back(v);
  36. }
  37.  
  38. void Graph::addEdge2(int u, int v, float total)
  39. {
  40. adj2[u][v] = total;
  41. }
  42.  
  43. // A utility function to print the adjacency list
  44. // representation of graph
  45. void Graph::printGraph()
  46. {
  47. cout << "Adjacency List..." << endl;
  48. for (int v = 0; v < V; ++v)
  49. {
  50. cout << "V[" << v << "]";
  51. for (auto x : adj[v])
  52. cout << " -> " << places[x].label;
  53. cout << endl;
  54. }
  55. }
  56.  
  57. void Graph::DFSUtil(int v, bool visited[])
  58. {
  59. // Mark the current node as visited and
  60. // print it
  61. visited[v] = true;
  62. cout << places[v].label << " ";
  63.  
  64. // Recur for all the vertices adjacent
  65. // to this vertex
  66. list<int>::iterator i;
  67. for (i = adj[v].begin(); i != adj[v].end(); ++i)
  68. if (!visited[*i])
  69. DFSUtil(*i, visited);
  70. }
  71.  
  72. // DFS traversal of the vertices reachable from v.
  73. // It uses recursive DFSUtil()
  74. void Graph::DFS(int v)
  75. {
  76. // Mark all the vertices as not visited
  77. bool *visited = new bool[V];
  78. for (int i = 0; i < V; i++)
  79. visited[i] = false;
  80.  
  81. // Call the recursive helper function
  82. // to print DFS traversal
  83. DFSUtil(v, visited);
  84.  
  85. /*for (int i = 0; i < V; i++)
  86. {
  87. if (!visited[i])
  88. {
  89. cout << places[i].label << ' ';
  90. }
  91. }*/
  92. }
  93.  
  94. void Graph::BFS(int s)
  95. {
  96. //Mark all the vertices as not visited
  97. bool *visited = new bool[V];
  98. for (int i = 0; i < V; i++)
  99. {
  100. visited[i] = false;
  101. }
  102.  
  103. //Create a queue for BFS
  104. list<int> queue;
  105.  
  106. //Mark the current node as visited and enqueue it
  107. visited[s] = true;
  108. queue.push_back(s);
  109.  
  110. //'i' will be used to get all adjacent
  111. //vertices of a vertex
  112. list<int>::iterator i;
  113.  
  114. while (!queue.empty())
  115. {
  116. //Dequeue a vertex from queue and print it
  117. s = queue.front();
  118. cout << places[s].label << " ";
  119. queue.pop_front();
  120.  
  121. //Get all adjacent vertices of the dequeued
  122. //verte s. If an adjacent has not been visited, then mark it visited and enqueue it
  123. for (i = adj[s].begin(); i != adj[s].end(); ++i)
  124. {
  125. if (!visited[*i])
  126. {
  127. visited[*i] = true;
  128. queue.push_back(*i);
  129. }
  130. }
  131. }
  132. /*for (int i = 0; i < V; i++)
  133. {
  134. if (!visited[i])
  135. {
  136. cout << places[i].label << ' ';
  137. }
  138. }*/
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement