Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. #include "Graph.h"
  2. #include "Edge.h"
  3. #include "Vertex.h"
  4.  
  5. #include <string>
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. /**
  10. * @return The number of vertices in the Graph
  11. */
  12. template <class V, class E>
  13. unsigned int Graph<V,E>::numVertices() const {
  14. // TODO: Part 2
  15. return vertexMap.size();
  16. }
  17.  
  18.  
  19. /**
  20. * The degree of the vertex. For directed: Sum of in-degree and out-degree
  21. * @return Returns the degree of a given vertex.
  22. * @param v Given vertex to return degree.
  23. */
  24. template <class V, class E>
  25. unsigned int Graph<V,E>::degree(const V & v) const {
  26. // TODO: Part 2
  27. auto it = adjList.find(v.key());
  28. return it -> second.size();
  29. }
  30.  
  31.  
  32. /**
  33. * Inserts a Vertex into the Graph.
  34. * @param key The key of the Vertex to insert
  35. * @return The inserted Vertex
  36. */
  37. template <class V, class E>
  38. V & Graph<V,E>::insertVertex(std::string key) {
  39. V & v = *(new V(key));
  40. vertexMap.emplace(key, v);
  41. adjList[key] = list<edgeListIter>();
  42. return v;
  43. }
  44.  
  45.  
  46. /**
  47. * Removes a given Vertex
  48. * @param v The Vertex to remove
  49. */
  50. template <class V, class E>
  51. void Graph<V,E>::removeVertex(const std::string & key) {
  52. // TODO: Part 2
  53. vertexMap.erase(key);
  54. typename list<E_byRef>::iterator it;
  55. for (it = edgeList.begin(); it != edgeList.end(); ++it) {
  56. E edge = *it;
  57. if (edge.source().key() == key || edge.dest().key() == key) {
  58. removeEdge(it);
  59. }
  60. }
  61. adjList.erase(key);
  62. }
  63.  
  64.  
  65. /**
  66. * Inserts an Edge into the Graph.
  67. * @param v1 The source Vertex
  68. * @param v2 The destination Vertex
  69. * @return The inserted Edge
  70. */
  71. template <class V, class E>
  72. E & Graph<V,E>::insertEdge(const V & v1, const V & v2) {
  73. // TODO: Part 2
  74. E & e = *(new E(v1, v2));
  75. edgeListIter it = edgeList.insert(edgeList.begin(), e);
  76. adjList[v1.key()].push_back(it);
  77. adjList[v2.key()].push_back(it);
  78. return e;
  79. }
  80.  
  81.  
  82. /**
  83. * Removes an Edge from the Graph. Consider both the undirected and directed cases.
  84. * min(degree(key1), degree(key2))
  85. * @param key1 The key of the source Vertex
  86. * @param key2 The key of the destination Vertex
  87. */
  88. template <class V, class E>
  89. void Graph<V,E>::removeEdge(const std::string key1, const std::string key2) {
  90. // TODO: Part 2
  91. if (vertexMap.find(key1) == vertexMap.end() || vertexMap.find(key2) == vertexMap.end()) {
  92. return;
  93. }
  94. Edge edge(vertexMap.at(key1), vertexMap.at(key2));
  95. edgeListIter it = edgeList.begin();
  96. while (it != edgeList.end() && !(it -> get() == edge)) {
  97. it++;
  98. }
  99. if (it != edgeList.end()) {
  100. removeEdge(it);
  101. }
  102. }
  103.  
  104.  
  105. /**
  106. * Removes an Edge from the Graph given by the location of the given iterator into the edge list.
  107. * @param it An iterator at the location of the Edge that
  108. * you would like to remove
  109. */
  110. template <class V, class E>
  111. void Graph<V,E>::removeEdge(const edgeListIter & it) {
  112. // TODO: Part
  113. Edge e = *it;
  114. Vertex dest1 = e.dest();
  115. Vertex source1 = e.source();
  116. adjList.at(dest1.key()).remove(it);
  117. adjList.at(source1.key()).remove(it);
  118. edgeList.erase(it);
  119. }
  120.  
  121.  
  122. /**
  123. * Return the list of incident edges from a given vertex.
  124. * For the directed case, consider all edges that has the vertex as either a source or destination.
  125. * @param key The key of the given vertex
  126. * @return The list edges (by reference) that are adjacent to the given vertex
  127. */
  128. template <class V, class E>
  129. const std::list<std::reference_wrapper<E>> Graph<V,E>::incidentEdges(const std::string key) const {
  130. // TODO: Part 2
  131. std::list<std::reference_wrapper<E>> edges;
  132. for(edgeListIter it : adjList.at(key)) {
  133. edges.push_back(*it);
  134. }
  135. return edges;
  136. }
  137.  
  138.  
  139. /**
  140. * Return whether the two vertices are adjacent to one another. Consider both the undirected and directed cases.
  141. * When the graph is directed, v1 and v2 are only adjacent if there is an edge from v1 to v2.
  142. * @param key1 The key of the source Vertex
  143. * @param key2 The key of the destination Vertex
  144. * @return True if v1 is adjacent to v2, False otherwise
  145. */
  146. template <class V, class E>
  147. bool Graph<V,E>::isAdjacent(const std::string key1, const std::string key2) const {
  148. // TODO: Part 2
  149. list<reference_wrapper<E>> elist = incidentEdges(key1);
  150. for (Edge e : elist) {
  151. if (e.source().key() == key1 && e.dest().key() == key2) {
  152. return true;
  153. }
  154. if (e.source().key() == key2 && e.dest().key() == key1 && e.directed()) {
  155. return true;
  156. }
  157. }
  158. return false;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement