Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. THE HPP FILE
  2.  
  3. #ifndef __GRAPH_HPP
  4. #define __GRAPH_HPP
  5. #include <vector>
  6.  
  7.  
  8.  
  9. class WeightedGraph {
  10. public:
  11. // Constructor. Know the graph size in advance.
  12. WeightedGraph(int num_nodes);
  13.  
  14. // Destructor
  15. ~WeightedGraph();
  16.  
  17. // Add a new edge to the graph
  18. // If it exists already, update the weight.
  19. void addEdge(int u, int v, double w);
  20.  
  21. // Get the number of nodes in the graph
  22. int numNodes() const;
  23.  
  24. // Get the number of edges in the graph
  25. int numEdges() const;
  26.  
  27. // Return a list of neighbors for a given vertex
  28. std::vector<int> neighbors(int u);
  29.  
  30. // Check if two nodes are connected
  31. bool areConnected(int u, int v);
  32.  
  33. // Get the weight of the edge between two nodes.
  34. // If there is no edge, return 0.
  35. double getWeight(int u, int v);
  36.  
  37. // print out the graph’s adjacency matrix
  38. void print();
  39.  
  40. private:
  41. int capacity;
  42. vector<double> parts;
  43. };
  44.  
  45. #endif // header include guard
  46.  
  47.  
  48.  
  49. THE CPP FILE
  50.  
  51. #include "graph.hpp"
  52. #include <iostream>
  53. #include <vector>
  54.  
  55. using namespace std;
  56.  
  57.  
  58. WeightedGraph::WeightedGraph(int num_nodes) { //Cj
  59. int capacity = num_nodes;
  60. }
  61.  
  62. WeightedGraph::~WeightedGraph() {
  63. delete parts;
  64. }
  65.  
  66. void WeightedGraph::addedge(int u, int v, double w) { //Cj
  67. double x = static_cast<double>(u);
  68. double z = static_cast<double>(v);
  69.  
  70. for (int i = 0; i <= (parts.size() - 2); i = i + 3) {
  71. if ((x == i) && (z == (i + 1))) {
  72. parts.at(i + 2) = w;
  73. }
  74. }
  75.  
  76. parts.pusback(x);
  77. parts.pusback(z);
  78. parts.pusback(w);
  79. }
  80.  
  81. int WeightedGraph::numNodes() const { //Cj
  82. return capacity;
  83. }
  84.  
  85. int WeightedGraph::numEdges() const { //Cj
  86. return (parts.size() / 3);
  87. }
  88.  
  89. vector<int> WeightedGraph::neighbors(int u) {
  90. for (int i = 0; i <= parts.size(); i = i + 3) {
  91. if ((i == u) && (((i + 1) % 3) != 0)) {
  92. //HOW DO I ADD TO THE VECTOR (USE ARECONNECTED, if so, add number to list)
  93. //remember that the numbers can be reversed
  94. }
  95. }
  96. //WHAT IS THE NAME TO RETURN IT //i have to create my own vector
  97. }
  98.  
  99. bool WeightedGraph::areConnected(int u, int v) {
  100.  
  101. for (int i = 0; i <= (parts.size() - 2); i = i + 3) { //Dan
  102. if ((u == i) && (v == (i + 1))) {
  103. return true;
  104. }
  105. if (i = (parts.size() - 2)) {
  106. return false;
  107. }
  108. }
  109. }
  110.  
  111. double WeightedGraph::getWeight(int u, int v) {
  112. for (int i = 0; i <= (parts.size() - 2); i = i + 3) {
  113. if ((u == i) && (v == (i + 1))) {
  114. return parts.at(i = i + 2);
  115. }
  116. if (i = (parts.size() - 2)) {
  117. return 0;
  118. }
  119. }
  120. }
  121.  
  122. void WeightedGraph::print() {
  123. int x = 0; //CHECK THIS
  124. int y = 0;
  125. for (int j = 1; j <= (capacity * capacity); j++) {
  126. cout << getWeight(x, y) << " "; //Does this work for reverse? How can i get this to work for reverse?
  127. y++;
  128. if ((j % capacity) != 0) {
  129. cout << endl;
  130. x++;
  131. }
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement