Advertisement
wafflecone67

Untitled

Apr 5th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. // data structure to store graph edges
  7. struct Edge {
  8. int src, dest;
  9. };
  10.  
  11. // class to represent a graph object
  12. class Graph
  13. {
  14. public:
  15. // construct a vector of vectors to represent an adjacency list
  16. vector<vector<int>> adjList;
  17.  
  18. // Graph Constructor
  19. Graph(vector<Edge> const &edges, int N)
  20. {
  21. // resize the vector to N elements of type vector<int>
  22. adjList.resize(N);
  23.  
  24. // add edges to the directed graph
  25. for (auto &edge : edges)
  26. {
  27. // insert at the end
  28. adjList[edge.src].push_back(edge.dest);
  29.  
  30. // Uncomment below line for undirected graph
  31. // adjList[edge.dest].push_back(edge.src);
  32. }
  33. }
  34. };
  35.  
  36. // print adjacency list representation of graph
  37. void printGraph(Graph const& graph, int N)
  38. { //prints the header
  39. for (char col = 'A'; col <= 'H'; col++)
  40. {
  41. cout << col << " ";
  42. }
  43. cout << endl;
  44. int linearindex=0;
  45. for (int row = 1; row <= 1; row++)
  46. {
  47.  
  48. for (char col = 1; col <= 8; col++)
  49. {
  50. //cout << col << " ";
  51.  
  52. vector<int> v = graph.adjList[linearindex+col];
  53.  
  54. for (int n : v)
  55. {
  56.  
  57. cout << n << " " ;
  58. }
  59.  
  60. }
  61. linearindex += 8;
  62. cout << endl;
  63. }
  64.  
  65. for (int i = 0; i < N; i++)
  66. {
  67. // print current vertex number
  68. //cout << i << " --> ";
  69.  
  70. // print all neighboring vertices of vertex i
  71. //for (int v : graph.adjList[i])
  72. //cout << v << " ";
  73. //cout << endl;
  74. }
  75. }
  76.  
  77. // Graph Implementation using STL
  78. int main()
  79. {
  80. // vector of graph edges as per above diagram.
  81. // Please note that initialization vector in below format will
  82. // work fine in C++11, C++14, C++17 but will fail in C++98.
  83. int row;
  84. int col;
  85. string entry;
  86. for (;;)
  87. {
  88. cout << "Please enter row: ";
  89. cin >> row;
  90. cout << "enter col";
  91. cin >> col;
  92. cout << "";
  93. }
  94. vector<Edge> edges =
  95. {
  96. { 0, 1 }, { 1, 2 }, { 2, 0 }, { 2, 1 },
  97. { 3, 2 }, { 4, 5 }, { 5, 4 }, {6, 7}
  98. };
  99. vector<Edge> x = { { 0, 0 } };
  100. //std::fill(edges.begin(), edges.end(), x);
  101. // Number of nodes in the graph
  102. int N = 160;
  103.  
  104. // construct graph
  105. Graph graph(edges, N);
  106.  
  107. // print adjacency list representation of graph
  108. printGraph(graph, N);
  109. system("pause");
  110. return 0;
  111. }
  112. // Get input A1 10
  113. //See the function.
  114. //Find sum function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement