本文整理汇总了C++中graph::isVisited方法的典型用法代码示例。如果您正苦于以下问题:C++ graph::isVisited方法的具体用法?C++ graph::isVisited怎么用?C++ graph::isVisited使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类graph
的用法示例。
在下文中一共展示了graph::isVisited方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getNumComponents
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: recursiveDFS
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: getMap
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: findPathNonRecursive
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: dfsCyclic
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: findCycle
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: isConnected
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: dfs
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: isCyclic
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: findSpanningForest
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: kruskal
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: isConnected
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: dfsAddEdges
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: findSpanningForest
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: isCyclic
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;
}