当前位置: 首页>>代码示例>>C++>>正文


C++ graph::numNodes方法代码示例

本文整理汇总了C++中graph::numNodes方法的典型用法代码示例。如果您正苦于以下问题:C++ graph::numNodes方法的具体用法?C++ graph::numNodes怎么用?C++ graph::numNodes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在graph的用法示例。


在下文中一共展示了graph::numNodes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: findSpanningForest

void findSpanningForest(graph &g, graph &sf)
	// Create a graph sf that contains a spanning forest on the graph g.
{
	if (isConnected(g) && !isCyclic(g))
	{
		sf = g;
	}
	else
	{
		// add nodes to sf
		for (int i = 0; i < g.numNodes(); i++)
		{
			sf.addNode(g.getNode(i));
		}

		// build sf
		for (int i = 0; i < g.numNodes(); i++)
		{
			for (int j = 0; j < g.numNodes(); j++)
			{
				if (g.isEdge(i, j) && !sf.isEdge(i, j))
				{
					sf.addEdge(i, j, g.getEdgeWeight(i, j));
					sf.addEdge(j, i, g.getEdgeWeight(j, i));

					if(isCyclic(sf))
					{
						sf.removeEdge(j, i);
						sf.removeEdge(i, j);
					} // if
				} // if
			} // for
		} // for
	} // else
} // findSpanningForest
开发者ID:tLiMiT,项目名称:EECE-3326,代码行数:35,代码来源:p6b.cpp

示例2: 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;
}
开发者ID:AdamEdgett,项目名称:optimization-methods,代码行数:33,代码来源:p6a.cpp

示例3: 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;
}
开发者ID:kalnet,项目名称:TerminalApps,代码行数:31,代码来源:main.cpp

示例4: getMinEdge

edgepair getMinEdge(graph &g)
// iterate through whole graph and finds the edge from 
// marked node to unmarked node with minimum weight
// returns a struct with marked, unmarked, and weight
{
    int minCost = HIGH;
    int marked = NONE;
    int unmarked = NONE;
    
    // find the minimum edge
    for (int i = 0; i < g.numNodes() ; i++)
    {
        if (g.isMarked(i))
        {
            for (int j = 0; j < g.numNodes(); j++)
            {
                if (!g.isMarked(j) && g.isEdge(i, j) && g.getEdgeWeight(i, j) < minCost) 
                {
                    minCost = g.getEdgeWeight(i,j);
                    marked = i;
                    unmarked = j;
                }
            }
        }
    }

    edgepair pair = {marked, unmarked, minCost};
    return pair;
}
开发者ID:mossberg,项目名称:eece3326,代码行数:29,代码来源:p6b.cpp

示例5: eq

bool eq(graph &g1, graph &g2) {
	if (g1.numNodes() != g2.numNodes()) {
		return false;
	}
	for (int n = 0; n < g1.numNodes(); n++) {
		if(g1.getColor(n) != g2.getColor(n)) {
			return false;
		}
	}
	return true;
}
开发者ID:willderivera,项目名称:special-topics,代码行数:11,代码来源:graphColor.cpp

示例6: 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;
}
开发者ID:kalnet,项目名称:TerminalApps,代码行数:52,代码来源:main.cpp

示例7: prim

void prim(graph &g, graph &sf)
// from weighted graph g, set sf to minimum spanning forest
// finds the minimum cost edge from a marked node to an unmarked node and adds it
// loop through all nodes and if a node is not marked,
// start adding edges with it as start
{
    g.clearMark();
    
    for (int n = 0; n < g.numNodes(); n++)  // loop through all nodes
    {
        if (!g.isMarked(n))
        {
            g.mark(n);

            edgepair pair = getMinEdge(g);
    
            while (pair.i != NONE && pair.j != NONE)
            {
                // mark edge
                g.mark(pair.i, pair.j);
                g.mark(pair.j, pair.i);

                // add both edges to create undirected edge
                sf.addEdge(pair.i, pair.j, pair.cost);
                sf.addEdge(pair.j, pair.i, pair.cost);
    
                g.mark(pair.j);       // mark the unmarked node

                pair = getMinEdge(g); // get next edge
            }
        }
        // if node is marked, just continue
    }
}
开发者ID:mossberg,项目名称:eece3326,代码行数:34,代码来源:p6b.cpp

示例8: findSpanningForest

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);
}
开发者ID:kalnet,项目名称:TerminalApps,代码行数:44,代码来源:main.cpp

示例9: prim

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
开发者ID:tLiMiT,项目名称:EECE-3326,代码行数:19,代码来源:p6b.cpp

示例10: getEdges

pqueue getEdges(graph &g)
// iterate through graph and construct a priority queue with minimum cost
// only add an edgepair for edge between marked node and unmarked node
{
    pqueue edges;
    for (int i = 0; i < g.numNodes(); i++)
    {
        g.mark(i);
        
        for (int j = 0; j < g.numNodes(); j++)
        {
            if (g.isMarked(i) && !g.isMarked(j) && g.isEdge(i, j))
            {
                edgepair pair = {i, j, g.getEdgeWeight(i, j)};
                edges.push(pair);
            }
        }
    }
    return edges;
}
开发者ID:mossberg,项目名称:eece3326,代码行数:20,代码来源:p6b.cpp

示例11: getNeighbors

vector<int> getNeighbors(graph &g, int current)
// loops through nodes and check if there is an edge between
// current and node, returns a vector of neighboring nodes
{
    vector<int> neighbors;
    for (int i = 0; i < g.numNodes(); i++)
    {
        if (g.isEdge(current, i))
            neighbors.push_back(i);
    }
    return neighbors;
}
开发者ID:mossberg,项目名称:eece3326,代码行数:12,代码来源:p6b.cpp

示例12: 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);
    }
}
开发者ID:mossberg,项目名称:eece3326,代码行数:13,代码来源:p6b.cpp

示例13: 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);
}
开发者ID:AdamEdgett,项目名称:optimization-methods,代码行数:39,代码来源:p6a.cpp

示例14: 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);
}
开发者ID:AdamEdgett,项目名称:optimization-methods,代码行数:38,代码来源:p6a.cpp

示例15: 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;
}
开发者ID:mossberg,项目名称:eece3326,代码行数:14,代码来源:p6b.cpp


注:本文中的graph::numNodes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。