예제 #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 recursiveDFS(int curId, int dstId, graph &g,
                  stack<int> &path, bool &done)
// depth first search that uses the mem stack to search the graph g
{

    if (curId == dstId)
    {
        done = true;
        path.push(curId);
    }
    else
    {
        g.mark(curId);
        g.visit(curId);

        vector<int> lst = getNeighbors(curId, g);

        while (!lst.empty())
        {
            int current =  lst.back();
            lst.pop_back();

            if (!g.isVisited(current))
            {
                recursiveDFS(current, dstId, g, path, done);
            }
            if (done)
                // if we found our node then construct our path
            {
                path.push(curId);
                break;
            }
        }
    }
}
예제 #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
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);
}
예제 #5
0
파일: p6b.cpp 프로젝트: mossberg/eece3326
bool dfsCyclic(graph &g, int current, int prev)
// depth first search to find cycles in graph
// first removes the preceeding node from vector of neighbors
// then if there is a visited node neighbor, there is a cycle
// returns true for there is a cycle, otherwise false
{
    g.visit(current);
    vector<int> neighbors = getNeighbors(g, current);

    // remove prev from neighbors
    // make sure neighbors is not empty so we dont erase from empty vector
    if (prev != NONE && !neighbors.empty())
    {
        int index = 0;
        for (int k = 0; k < (int) neighbors.size(); k++)
        {
            if (neighbors[k] == prev)
                index = k;
        }

        // at some index, it is the (index + 1)th element
        // so just have to do .begin() + index
        neighbors.erase(neighbors.begin() + index);
    }

    for (int i = 0; i < (int) neighbors.size(); i++)
    {
        if (g.isVisited(neighbors[i]))
            return true;
        else if (dfsCyclic(g, neighbors[i], current))
            return true;
    }
    return false; // ran through all neighbors and no cycles
}
예제 #6
0
파일: p6b.cpp 프로젝트: tLiMiT/EECE-3326
void findCycle(int curr, int start, bool &found, graph &g)
	// checks for cycles in a graph
{
	g.mark(curr);

	vector<int> lst = getNeighbors(curr, g);

	for (int i = 0; i < lst.size(); i++)
	{
		if (start == lst[i])
		{
			continue;
		}
		if (g.isMarked(lst[i]))
		{
			found = true;
		}
		else if (!g.isVisited(lst[i]))
		{
			findCycle(lst[i], curr, found, g);
		}
	} // for

	g.unMark(curr);
	g.visit(curr);

} // findCycle
예제 #7
0
bool isConnected(graph &g)
// Returns true if the graph g is connected.  Otherwise returns false.
{
	queue<int> que;
	int id=0,count=1;

	que.push(id);
	g.visit(id);

	while(count<g.numNodes() && !que.empty())
	{
		id=que.front();
		for(int i=0;i<g.numNodes();i++)
		{
			if (g.isEdge(id,i) && !g.isVisited(i))
			{
				g.visit(i);
				que.push(i);
				count++;
			}
		}
		que.pop();                                                                                                                                                                                                                                                                                                                                                                                                                                                          
	}

	for (int z=0;z<g.numNodes();z++)
		g.unVisit(z);

	if(count==g.numNodes())
		return true;
	else return false;
}
예제 #8
0
파일: p6b.cpp 프로젝트: mossberg/eece3326
void dfs(graph &g, int current)
// generic depth first search traversal
{
    g.visit(current);
    vector<int> neighbors = getNeighbors(g, current);
    for (int i = 0; i < (int) neighbors.size(); i++)
    {
        if (!g.isVisited(neighbors[i]))
            dfs(g, neighbors[i]);
    }
}
예제 #9
0
bool isCyclic(graph &g)
// Returns true if the graph g contains a cycle.  Otherwise, returns false.
{
	queue<int> que;
	int id=0,count=1;
	bool first=true;
	vector<int> parentCount(g.numNodes(),-1);

	que.push(id);
	g.visit(id);

	while(count<g.numNodes() || !que.empty())
	{
		if (que.empty())
		{
			id=count;
			que.push(id);
			g.visit(id);
			count++;
		}
		else
			id=que.front();

		for(int i=0;i<g.numNodes();i++)
		{
			if (g.isEdge(id,i) && i!=que.front())
			{
				if(!g.isVisited(i))
				{
					g.visit(i);
					que.push(i);
					count++;
					parentCount[i]=id;
				}
				else if(parentCount[id]==i)
					continue;
				else
				{
					for (int z=0;z<g.numNodes();z++)
						g.unVisit(z);
					return true;
				}
			}
		}
		que.pop();    
	}

	for (int z=0;z<g.numNodes();z++)
		g.unVisit(z);

	return false;
}
예제 #10
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);
    }
}
예제 #11
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);
}
예제 #12
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;
}
예제 #13
0
파일: p6b.cpp 프로젝트: mossberg/eece3326
void dfsAddEdges(graph &g, int current, graph &sf)
// depth first search to visit all nodes and add edges to unvisited nodes
{
    g.visit(current);
    vector<int> neighbors = getNeighbors(g, current);
    for (int i = 0; i < (int) neighbors.size(); i++)
    {
        if (!g.isVisited(neighbors[i]))
        {
            sf.addEdge(current, neighbors[i], g.getEdgeWeight(current, neighbors[i]));
            sf.addEdge(neighbors[i], current, g.getEdgeWeight(neighbors[i], current));
            dfsAddEdges(g, neighbors[i], 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);
               }
            }
         }
      }
   }
}
예제 #15
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;
}
예제 #16
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;
}
예제 #17
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
예제 #18
0
void findSpanningForest(graph &g, graph &sf)
// Create a graph sf that contains a spanning forest on the graph g.
{
	queue<int> que;
	int id=0,count=1;
	bool first=true;
	vector<int> parentCount(g.numNodes(),-1);

	que.push(id);
	g.visit(id);

	while(count<g.numNodes() || !que.empty())
	{
		if (que.empty())
		{
			id=count;
			que.push(id);
			g.visit(id);
			count++;
		}
		else
			id=que.front();

		for(int i=0;i<g.numNodes();i++)
		{
			if (g.isEdge(id,i) && i!=que.front())
			{
				if(!g.isVisited(i) && parentCount[id]!=i)
				{
					g.visit(i);
					sf.addEdge(id,i,g.getEdgeWeight(i,id));
					sf.addEdge(i,id,g.getEdgeWeight(i,id));
					que.push(i);
					count++;
					parentCount[id]++;
				}
			}
		}
		que.pop();    
	}

	for (int z=0;z<g.numNodes();z++)
		g.unVisit(z);
}
예제 #19
0
bool isCyclic(graph &g,int nodeNumber)
// Returns true if the graph g contains a cycle.  Otherwise, returns false.
{
   if (g.isVisited(nodeNumber))
   {
      return true;
   }
   //Visit the node
   g.visit(nodeNumber);
   for (int i=0;i<g.numNodes();i++)
   {
      if (g.isEdge(nodeNumber,i))
      {
         return isCyclic(g,i);
      }
   }

   return false;
}
예제 #20
0
vector<stack<int> > nonRecursiveDFS(int startId, int dstId, graph &g )
// implement a version of Depth First Search that uses a stack data structure
// and does not use recursion returns all paths
{
    vector< stack<int> > paths;
    stack<int> st;
    stack<edge> edges;
    st.push(startId);
    stack<int> path;

    while (!st.empty())
    {
        int top = st.top();
        //check if before we had gone into a sink and remove from path
        while (!edges.empty() && path.top() != edges.top().getSource())
        {
            path.pop();
        }

        path.push(top);
        if (!edges.empty())
        {
            edges.pop();
        }
        st.pop();
        g.visit(top);
        if (top == dstId)
        {
            paths.push_back(path);
        }
        vector<int> lst = getNeighbors(top, g);
        for (int i = 0; i < lst.size(); i++)
        {
            if (!g.isVisited(lst[i]))
            {
                st.push(lst[i]);
                edges.push(g.getEdge(top, lst[i]));
            }
        }

    }
    return paths;
}
예제 #21
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
예제 #22
0
파일: p6b.cpp 프로젝트: tLiMiT/EECE-3326
void prim(graph &g, graph &msf)
	// Given a weighted graph g, sets msf equal to a minimum spanning
	// forest on g. Uses Prim's algorithm.
{
	// build msf
	for (int i = 0; i < g.numNodes(); i++)
	{
		msf.addNode(g.getNode(i));
	}

	// populate msf using findMSF
	for (int i = 0; i < g.numNodes(); i++)
	{
		if (!g.isVisited(i))
		{
			findMSF(g, msf, i);
		}
	}
} // prim
예제 #23
0
stack<int> nonRecursiveBFS(int startId, int dstId, graph &g )
// implement a version of Breadth First Search that uses a stack data structure
// and does not use recursion and returns the first path found
{
    queue<int> q;
    stack<int> path;
    map<int, int> edges;
    q.push(startId);
    bool found = false;

    while (!q.empty())
    {
        int top = q.front();
        q.pop();

        if (top == dstId)
        {
            found = true;
            path.push(dstId);
            break;
        }

        g.visit(top);
        vector<int> lst = getNeighbors(top, g);
        for (int i = 0; i < lst.size(); i++)
        {
            if (!g.isVisited(lst[i]))
            {
                q.push(lst[i]);
                edges.insert( pair<int, int>(lst[i], top) );
            }
        }
    }

    // start backtracking
    int current = dstId;
    while (found && current != startId)
    {
        current = edges.find(current)->second;
        path.push(current);
    }
    return path;
}
예제 #24
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);
}
예제 #25
0
파일: p6b.cpp 프로젝트: tLiMiT/EECE-3326
// Helper Functions
void visitNodes(int start, graph &g)
	// Visit all nodes reachable from the start node
{
	bool found = false;

	// Mark the start node as visited.
	g.visit(start);

	int v = 0;

	// Keep looking for legal moves as long as there are more neighbors
	// to check.
	while (!found && v < g.numNodes())
	{
		// if v is an unvisited neighbor of the start node, recurse.

		if (g.isEdge(start, v) && !g.isVisited(v))
		{
			visitNodes(v, g);
		}
		v++;
	}
} // visitNodes