Advertisement
Guest User

Untitled

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