예제 #1
0
int getNumComponents(graph &g)
{
   g.clearMark();
   g.clearVisit();
   int numComponents = 0;
   queue<int> currentMoves;
   for (int n=0;n<g.numNodes();n++)
   {
      if (!g.isVisited(n))
      {  
         numComponents++;
         int nodeNumber=n;
         g.visit(nodeNumber);
         currentMoves.push(nodeNumber);
         while(currentMoves.size() > 0)
         {
            int currentNode = currentMoves.front();
            currentMoves.pop();
            //Populate a list of nodes that can be visited
            for (int i=0;i<g.numNodes();i++)
            {
               if (g.isEdge(currentNode,i) && !g.isVisited(i))
               {
                  g.mark(currentNode,i);
                  g.visit(i);
                  currentMoves.push(i);
               }
            }
         }
      }
   }
   return numComponents;
}
예제 #2
0
void maze::findPathNonRecursive(graph &g)
// method for finding a path in the maze given a graph g representing the maze
// uses a stack based DFS
{
    g.clearVisit();
    g.clearMark();
    int start = getMap(0, 0);
    int end = getMap(numRows() - 1, numCols() - 1);
    vector< stack<int> > rpaths = nonRecursiveDFS(start, end, g);
    stack<int> reverse_path;

    for(int i = 0; i < rpaths.size(); i++)
        if (rpaths[i].size() > reverse_path.size())
            reverse_path = rpaths[i];

    stack<int> path;

    while (!reverse_path.empty())
    {
        int top = reverse_path.top();
        reverse_path.pop();
        if (g.isVisited(top))
        {
            path.push(top);
        }
    }

    printPath(path);
}
예제 #3
0
bool maze::findShortestPath1(graph &g)
//finds the shortest path in the given graph using DFS
{

    g.clearVisit();
    g.clearMark();
    int start = getMap(0, 0);
    int end = getMap(numRows() - 1, numCols() - 1);
    vector< stack<int> > rpaths = nonRecursiveDFS(start, end, g);

    stack<int> reverse_path;

    for(int i = 0; i < rpaths.size(); i++)
        if (rpaths[i].size() > reverse_path.size())
            reverse_path = rpaths[i];

    stack<int> path;

    while (!reverse_path.empty())
    {
        int top = reverse_path.top();
        reverse_path.pop();
        if (g.isVisited(top))
        {
            path.push(top);
        }
    }

    printPath(path);

}
예제 #4
0
파일: p6b.cpp 프로젝트: tLiMiT/EECE-3326
// Project Functions
bool isCyclic(graph &g)
	// Returns true if the graph g contains a cycle.  Otherwise, returns false.
{
	g.clearVisit();
	g.clearMark();

	bool cycle = false;

	for (int i = 0; i < g.numNodes(); i++)
	{
		if (!g.isVisited(i))
			findCycle(i, i, cycle, g);
	}

	g.clearMark();
	g.clearVisit();

	return cycle;
} // isCyclic
예제 #5
0
bool maze::findShortestPath2(graph &g)
// finds the shortest path in the given graph using BFS
{

    g.clearVisit();
    g.clearMark();
    int start = getMap(0, 0);
    int end = getMap(numRows() - 1, numCols() - 1);
    stack<int> path = nonRecursiveBFS(start, end, g);
    printPath(path);
}
예제 #6
0
파일: p6b.cpp 프로젝트: mossberg/eece3326
void findSpanningForest(graph &g, graph &sf)
// Create a graph sf that contains a spanning forest on the graph g.  
{
    g.clearVisit();

    // if a node is not visited, call dfsAddEdges on it
    // to create a tree with the node as the start
    for (int i = 0; i < sf.numNodes(); i++)
    {
        if (!sf.isVisited(i))
            dfsAddEdges(g, i, sf);
    }
}
예제 #7
0
파일: p6b.cpp 프로젝트: mossberg/eece3326
bool isConnected(graph &g)
// Returns true if the graph g is connected.  Otherwise returns false.
{
    g.clearVisit();
    int start = 0;
    dfs(g, start);
    
    for (int i = 0; i < g.numNodes(); i++)
    {
        if (!g.isVisited(i))
            return false;
    }
    return true;
}
예제 #8
0
void maze::findPathRecursive(graph &g)
// method for finding a path in the maze given a graph g representing the maze
// uses recursion based DFS
{
    g.clearVisit();
    g.clearMark();
    stack<int> path;
    int start = getMap(0, 0);
    int end = getMap(numRows() - 1, numCols() - 1);
    bool done = false;
    recursiveDFS(start, end, g, path, done);

    printPath(path);
}
예제 #9
0
파일: p6b.cpp 프로젝트: mossberg/eece3326
int numComponents(graph &sf)
// given a spanning forest, finds the number of trees,
// or connected components in that forest
{
    int components = 0;
    sf.clearVisit();
    for (int i = 0; i < sf.numNodes(); i++)
    {
        if (!sf.isVisited(i))
        {
            dfs(sf, i);
            components++;
        }
    }
    return components;
}
예제 #10
0
파일: p6b.cpp 프로젝트: mossberg/eece3326
bool isCyclic(graph &g)
// Returns true if the graph g contains a cycle.  Otherwise, returns false.
// checks all spanning tree components in the graph
{
    g.clearVisit();
    int prev = NONE;
    
    for (int i = 0; i < g.numNodes(); i++)
    {
        // if node is not visited, call traversal with it as the start
        if (!g.isVisited(i) && dfsCyclic(g, i, prev))
            return true;
    }
    
    return false;
}
예제 #11
0
파일: p6b.cpp 프로젝트: tLiMiT/EECE-3326
bool isConnected(graph &g)
	// Returns true if the graph g is connected.  Otherwise returns false.
{
	g.clearVisit();
	g.clearMark();

	visitNodes(0, g);	// start at '0' the 'first' node

	for (int i = 0; i < g.numNodes(); i++)
	{
		if (!g.isVisited(i))
		{
			return false;
		}
	} // for
	return true;
} // isConnected
예제 #12
0
void kruskal(graph &g, graph &sf)
// Given a weighted graph g, sets sf equal to a minimum spanning
// forest on g. Uses Kruskal's algorithm.
{
   g.clearMark();
   g.clearVisit();
   numComponents=0;
   while(!g.allNodesVisited())
   {
      // find the smallest edge
      int smallestEdgeWeight = -1;
      int smallestEdgeBeg = -1;
      int smallestEdgeEnd = -1;
      for(int i = 0; i < g.numNodes(); i++)
      {
         for(int j = 0; j < g.numNodes(); j++)
         {
            if(g.isEdge(i, j) && !g.isVisited(i, j) && !g.isVisited(j, i)
               && (!g.isVisited(i) || !g.isVisited(j)))
            {
               if(g.getEdgeWeight(i, j) < smallestEdgeWeight 
                  || smallestEdgeWeight == -1)
               {
                  smallestEdgeWeight = g.getEdgeWeight(i, j);
                  smallestEdgeBeg = i;
                  smallestEdgeEnd = j;
               }
            }
         }
      }
      // add the new edge
      g.visit(smallestEdgeBeg);
      g.visit(smallestEdgeEnd);
      g.visit(smallestEdgeBeg, smallestEdgeEnd);
      sf.addEdge(smallestEdgeBeg, smallestEdgeEnd);
      sf.setEdgeWeight(smallestEdgeBeg, smallestEdgeEnd, smallestEdgeWeight);
   }
   numComponents = getNumComponents(sf);
}
예제 #13
0
void prim(graph &g, graph &sf)
// Given a weighted graph g, sets sf equal to a minimum spanning
// forest on g. Uses Prim's algorithm.
{
   g.clearMark();
   g.clearVisit();
   numComponents=0;
   int currentNode = 0;
   while(!g.allNodesVisited())
   {
      // find next currentNode
      while(g.isVisited(currentNode) && currentNode < g.numNodes())
      {
         currentNode++;
      }
      g.visit(currentNode);
      int smallestEdgeWeight = -1;
      int smallestEdgeNode = -1;
      // find shortest new edge from currentNode
      for(int i = 0; i < g.numNodes(); i++)
      {
         if(g.isEdge(currentNode, i))
         {
            if(g.getEdgeWeight(currentNode, i) < smallestEdgeWeight 
               || smallestEdgeWeight == -1)
            {
               smallestEdgeWeight = g.getEdgeWeight(currentNode, i);
               smallestEdgeNode = i;
            }
         }
      }
      // add the new edge
      g.visit(smallestEdgeNode);
      sf.addEdge(currentNode, smallestEdgeNode);
      sf.setEdgeWeight(currentNode, smallestEdgeNode, smallestEdgeWeight);
   }
   numComponents = getNumComponents(sf);
}
예제 #14
0
void findSpanningForest(graph &g, graph &sf)
// Create a graph sf that contains a spanning forest on the graph g.  
{
   g.clearMark();
   g.clearVisit();
   numComponents=0;
   queue<int> currentMoves;
   for (int n=0;n<g.numNodes();n++)
   {
      if (!g.isVisited(n))
      {  
         numComponents++;
         int nodeNumber=n;
         g.visit(nodeNumber);
         currentMoves.push(nodeNumber);
         while(currentMoves.size() > 0)
         {
            int currentNode = currentMoves.front();
            currentMoves.pop();
   
            //Populate a list of nodes that can be visited
            for (int i=0;i<g.numNodes();i++)
            {
               if (g.isEdge(currentNode,i) && !g.isVisited(i))
               {
                  g.mark(currentNode,i);
                  sf.addEdge(currentNode,i);
                  sf.setEdgeWeight(currentNode, i, g.getEdgeWeight(currentNode, i));
                  g.visit(i);
                  currentMoves.push(i);
               }
            }
         }
      }
   }
}