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