예제 #1
0
파일: p6b.cpp 프로젝트: mossberg/eece3326
void kruskal(graph &g, graph &sf)
// from weighted graph g, set sf to minimum spanning forest
// uses a priority queue with edges sorted from large to min weight
// since top of queue is the back of underlying vector
// for every edge, add to sf, but if it creates cycle, then
// remove it and move to next edge
{
    g.clearMark();
    pqueue edges = getEdges(g);
    while (!edges.empty())
    {
        edgepair pair = edges.top();
        edges.pop();
        
        // add both edges to create undirected edges
        sf.addEdge(pair.i, pair.j, pair.cost);
        sf.addEdge(pair.j, pair.i, pair.cost);

        if (isCyclic(sf))
        {
            sf.removeEdge(pair.i, pair.j);
            sf.removeEdge(pair.j, pair.i);
        }
    }
}
예제 #2
0
파일: p6b.cpp 프로젝트: tLiMiT/EECE-3326
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
예제 #3
0
파일: p6b.cpp 프로젝트: mossberg/eece3326
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
    }
}
예제 #4
0
파일: p6b.cpp 프로젝트: tLiMiT/EECE-3326
void findMSF(graph &g, graph &sf, int start)
	// finds a minimum spanning tree in graph 'g'
{
	priority_queue<edge, vector<edge>, CompareEdge> pq;
	vector<int> lst = getNeighbors(start, g);

	// build our priority queue
	for (int i = 0; i < lst.size(); i++)
	{
		pq.push(g.getEdge(start, lst[i]));
		g.mark(start, lst[i]);
	}

	// visit the start node
	g.visit(start);

	int src, dst, w;
	edge top;

	while (!pq.empty())
	{
		top = pq.top();
		pq.pop();
		src = top.getSource();
		dst = top.getDest();
		w = top.getWeight();

		// add edges
		if (!sf.isEdge(src, dst))
		{
			sf.addEdge(src, dst, w);
			sf.addEdge(dst, src, w);

			// delete edges if we make a cycle
			if (isCyclic(sf))
			{
				sf.removeEdge(src, dst);
				sf.removeEdge(dst, src);
			}
			else
			{
				g.visit(src);
				lst = getNeighbors(dst, g);

				for (int i = 0; i < lst.size(); i++)
				{
					if (!g.isMarked(dst, lst[i]))
					{
						pq.push(g.getEdge(dst, lst[i]));
						g.mark(dst, lst[i]);
					}
				} // for
			} // else
		} // if
	} // while
} // findMSF
예제 #5
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);
        }
    }
}
예제 #6
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);
}
예제 #7
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);
}
예제 #8
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);
}
예제 #9
0
pair<int,int> set_dummy(graph& G)
{
    int start = -1;
    int end = -1;
    node curr;
    for (int n = 0; n < G.V.size(); ++n) {
        curr = G.V[n];
        if (curr.inDeg == curr.outDeg) {
            continue;
        }
        if (curr.inDeg == (curr.outDeg - 1) && start == -1) start = n;
        else if (curr.inDeg == (curr.outDeg + 1) && end == -1) end = n;
        else return make_pair(-1,-1);
    }
    if (start == -1 && end == -1)
        return make_pair(0,-1);
    else {
        G.addEdge(end,start); // dummy edge to create tour
        // cout << "Adding dummy " << end << "," << start << endl;
    }
    if (start != -1 && end == -1) return make_pair(-1,-1);
    if (end != -1 && start == -1) return make_pair(-1,-1);
    return make_pair(start,end);
}
예제 #10
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);
               }
            }
         }
      }
   }
}
예제 #11
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.
{
	NodeWeight minWeight = 0;
	NodeWeight minR, minP;
	bool edgeFound;

	g.clearMark();

	for(int i=0; i<g.numNodes(); i++)
	{
		if(!g.isMarked(i))
		{
			g.mark(i);
			for(int j=0; j<g.numNodes()-1; j++)
			//start at i and grow a spanning tree untill no more can be added
			{
				edgeFound = false;
				minWeight = MaxEdgeWeight;

				for(int r=0; r<g.numNodes(); r++)
				{
					for(int p=0; p<g.numNodes(); p++)
					{
						if(g.isEdge(r,p) && g.isMarked(r) && !g.isMarked(p))
						{
							if(g.getEdgeWeight(r,p) < minWeight)
							{
								minWeight = g.getEdgeWeight(r,p);
								minR= r;
								minP= p;
								edgeFound = true;
							}
						}
					}
				}
				//if edge was found add it to the tree
				if(edgeFound)
				{
					g.mark(minR,minP);
					g.mark(minP, minR);
					g.mark(minP);
				}
			}
		 }
		}
	//add marked edges to spanning forest graph
	for(int i=0; i<g.numNodes(); i++)
	{
		for(int j=i+1; j<g.numNodes(); j++)
		{
			if(g.isEdge(i,j) && g.isMarked(i,j))
			{
				sf.addEdge(i,j,g.getEdgeWeight(i,j));
				sf.addEdge(j,i,g.getEdgeWeight(j,i));
				cout<<"adding edge "<< i << " "<< j << endl;
				cout<<"num edges: "<<sf.numEdges() << endl;
			}
		}
	}
}
예제 #12
0
void circuit::addComponent(int id, int e1, int e2,electrical_component EC)
{
    if(!circuit_graph.verticeIsHere(e1)) circuit_graph.addVertice(e1);
    if(!circuit_graph.verticeIsHere(e2)) circuit_graph.addVertice(e2);
    circuit_graph.addEdge(id,e1,e2,EC);
}