Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.78 KB | None | 0 0
  1. /**
  2.  * @file graph_tools.cpp
  3.  * This is where you will implement several functions that operate on graphs.
  4.  * Be sure to thoroughly read the comments above each function, as they give
  5.  *  hints and instructions on how to solve the problems.
  6.  */
  7.  
  8. #include "graph_tools.h"
  9.  
  10. /**
  11.  * Finds the minimum edge weight in the Graph graph.
  12.  * THIS FUNCTION IS GRADED.
  13.  *
  14.  * @param graph - the graph to search
  15.  * @return the minimum weighted edge
  16.  *
  17.  * @todo Label the minimum edge as "MIN". It will appear blue when
  18.  *  graph.savePNG() is called in minweight_test.
  19.  *
  20.  * @note You must do a traversal.
  21.  * @note You may use the STL stack and queue.
  22.  * @note You may assume the graph is connected.
  23.  *
  24.  * @hint Initially label vertices and edges as unvisited.
  25.  */
  26. int GraphTools::findMinWeight(Graph& graph)
  27. {
  28.     /* Your code here! */
  29.     //return -1;
  30.     vector<Vertex> allVertices=graph.getVertices();
  31.     vector<Edge> allEdges=graph.getEdges();
  32.     //setting everything to unexplored for now
  33.     makeUnexplored(graph, allVertices, allEdges);
  34.     queue<Vertex> tempQueue;
  35.     Vertex start=graph.getStartingVertex();
  36.     int minWeight=0;   
  37.     graph.setVertexLabel(start, "VISITED");
  38.     tempQueue.push(start);
  39.     Vertex min1, min2; //for the two vertices that's going to contain the minimum edge
  40.     while(tempQueue.empty()==false){
  41.         Vertex u=tempQueue.front();
  42.         tempQueue.pop();
  43.         vector<Vertex> neighbors=graph.getAdjacent(u);
  44.         for(unsigned long i=0; i<neighbors.size(); i++){
  45.             Vertex v=neighbors[i]; //v would be the potentially unexplored one
  46.             if(graph.getVertexLabel(v)=="UNEXPLORED"){
  47.                 graph.setVertexLabel(v, "VISITED");
  48.                 graph.setEdgeLabel(u, v, "DISCOVERY");
  49.                 tempQueue.push(v);
  50.             }
  51.             else if(graph.getEdgeLabel(u, v)=="UNEXPLORED"){
  52.                 graph.setEdgeLabel(u, v, "CROSS");
  53.             }
  54.             if(graph.getEdgeWeight(u, v)<minWeight||minWeight==0){
  55.                 min1=u;
  56.                 min2=v;
  57.                 minWeight=graph.getEdgeWeight(u,v);
  58.             }
  59.         }
  60.     }
  61. //by now should have minWeight and which vertices the edge is betweeen
  62.     graph.setEdgeLabel(min1,min2,"MIN");
  63.  
  64.     return minWeight;
  65. }
  66. void GraphTools::makeUnexplored(Graph&graph, vector<Vertex> &allVertices, vector<Edge> &allEdges){
  67.     for(unsigned long i=0; i<allVertices.size(); i++){
  68.         graph.setVertexLabel(allVertices[i],"UNEXPLORED");
  69.         vector<Vertex> neighborVertices=graph.getAdjacent(allVertices[i]);
  70.         for(unsigned long j=0; j<neighborVertices.size(); j++){
  71.             graph.setEdgeLabel(allVertices[i],neighborVertices[j],"UNEXPLORED");
  72.         }
  73.     }
  74. }
  75.  
  76. /**
  77.  * Returns the shortest distance (in edges) between the Vertices
  78.  *  start and end.
  79.  * THIS FUNCTION IS GRADED.
  80.  *
  81.  * @param graph - the graph to search
  82.  * @param start - the vertex to start the search from
  83.  * @param end - the vertex to find a path to
  84.  * @return the minimum number of edges between start and end
  85.  *
  86.  * @todo Label each edge "MINPATH" if it is part of the minimum path
  87.  *
  88.  * @note Remember this is the shortest path in terms of edges,
  89.  *  not edge weights.
  90.  * @note Again, you may use the STL stack and queue.
  91.  * @note You may also use the STL's unordered_map, but it is possible
  92.  *  to solve this problem without it.
  93.  *
  94.  * @hint In order to draw (and correctly count) the edges between two
  95.  *  vertices, you'll have to remember each vertex's parent somehow.
  96.  */
  97. int GraphTools::findShortestPath(Graph& graph, Vertex start, Vertex end)
  98. {
  99.     /* Your code here! */
  100.     //return -1;
  101.     vector<Vertex> allVertices=graph.getVertices();
  102.     vector<Edge> allEdges=graph.getEdges();
  103.     //setting everything to unexplored for now
  104.     makeUnexplored(graph, allVertices, allEdges);
  105.     queue<Vertex> tempQueue;
  106.     tempQueue.push(start);
  107.     unordered_map<Vertex, Vertex> vertexMap;
  108.     while(tempQueue.empty()==false){
  109.         Vertex u=tempQueue.front();
  110.         tempQueue.pop();
  111.         vector<Vertex> neighbors=graph.getAdjacent(u);
  112.         for(unsigned long i=0; i<neighbors.size(); i++){
  113.             Vertex v=neighbors[i]; //v would be the potentially unexplored one
  114.             if(graph.getVertexLabel(v)=="UNEXPLORED"){
  115.                 graph.setVertexLabel(v, "VISITED");
  116.                 graph.setEdgeLabel(u, v, "DISCOVERY");
  117.                 tempQueue.push(v);
  118.                 vertexMap[v]=u;
  119.             }
  120.             else if(graph.getEdgeLabel(u, v)=="UNEXPLORED"){
  121.                 graph.setEdgeLabel(u, v, "CROSS");
  122.             }
  123.         }
  124.     }
  125.     int c=0;
  126.     while(start!=end){     
  127.         graph.setEdgeLabel(end, vertexMap[end], "MINPATH");
  128.         end=vertexMap[end];
  129.         c++;
  130.     }
  131.     return c;
  132. }
  133.  
  134. /**
  135.  * Finds a minimal spanning tree on a graph.
  136.  * THIS FUNCTION IS GRADED.
  137.  *
  138.  * @param graph - the graph to find the MST of
  139.  *
  140.  * @todo Label the edges of a minimal spanning tree as "MST"
  141.  *  in the graph. They will appear blue when graph.savePNG() is called.
  142.  *
  143.  * @note Use your disjoint sets class from MP 7.1 to help you with
  144.  *  Kruskal's algorithm. Copy the files into the libdsets folder.
  145.  * @note You may call std::sort instead of creating a priority queue.
  146.  */
  147. void GraphTools::findMST(Graph& graph)
  148. {
  149.     /* Your code here! */
  150.     //Get a list of all edges in the graph and sort them by increasing weight.
  151.     vector<Edge> allEdges=graph.getEdges();
  152.     sort(allEdges.begin(), allEdges.end());
  153.     //Create a disjoint sets structure where each vertex is represented by a set.
  154.     DisjointSets sets;
  155.     vector<Vertex> allVertices=graph.getVertices();
  156.     sets.addelements(allVertices.size());
  157.     //traversing
  158.     for(unsigned int i = 0; i < allEdges.size(); i++){                 
  159.         Vertex u=allEdges[i].dest;
  160.         Vertex v=allEdges[i].source;
  161.         if(sets.find(u)!=sets.find(v)){
  162.             graph.setEdgeLabel(u, v, "MST");
  163.             sets.setunion(u,v);
  164.         }
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement