Exemple #1
0
 bool canFinish(int numCourses, vector<pair<int, int>>& prerequisites) {
     vector<vector<int>> adjList(numCourses);
     for (auto &edge : prerequisites) {
         adjList[edge.second].push_back(edge.first);
     }
     vector<int> color(numCourses, WHITE);
     for (int u = 0; u < numCourses; ++u) if (color[u]==WHITE
         && !dfsVisit(adjList, color, u)) { return false; }
     return true;
 }
int main(int argc, char *argv[])
{
    int minCutEdges = N_NODES;

    AdjacencyList adjList(N_NODES);

    std::ifstream infile;
    infile.open("../kargerMinCut.txt");
    if (!infile) {
	std::cout << "Opening file failed." << std::endl;
	return -1;
    }
    
    std::string line;
    while (std::getline(infile, line)) {
	std::istringstream iss(line);
        int u, v;
	iss >> u;
	while (iss >> v) {
	    if (iss.fail()) { 
		std::cout << "Reading error or incorrect type." << std::endl;
		return -1;
	    }
	    if (u < 1 || u > N_NODES || v < 1 || v > N_NODES) {
		std::cout << "Vertex ID out of range" << std::endl;
		return -1;
	    }
	    adjList.insertEdge(u-1, v-1);
	}
    }
    
    SteadyClockTimer timer;

    timer.start();
    for (int i = 0; i < ITERATIONS; ++i) {
	srand(i);
	AdjacencyList adjListCopy = adjList;

	while (adjListCopy.nodeCount() > 2) {
	    adjListCopy.contractRandomEdge();
	}

	if (adjListCopy.edgeCount() < minCutEdges) {
	    minCutEdges = adjListCopy.edgeCount();
	}
    }

    std::cout << "After " << ITERATIONS << " iterations," << std::endl;
    
    std::cout << "Possible min cut edge count is: " <<  minCutEdges << std::endl;

    std::cout << "time taken is: " << timer.getMs() << " ms" << std::endl;
    return 0;
}
 vector<vector<neighbor> > generateGraph(vector<string> &strVec) {
     
     auto n = strVec.size();
     // the graph is saved by an adjacency list
     vector<vector<neighbor> > adjList(n, vector<neighbor>());
     if(strVec.size() == 0)
         return adjList;
     
     for(int i=0; i<n-1; i++) {
         for(int j=i+1; j<n; j++) {
             if(strDist(strVec[i], strVec[j]) == 1) {
                 adjList[i].push_back(neighbor(j, 1));
                 adjList[j].push_back(neighbor(i, 1));
             }
         }
     }
     
     return adjList;
 }
Exemple #4
0
// sorts the edges around all nodes of AG corresponding to the
// layout given in AG
// there is no check of the embedding afterwards because this
// method could be used as a first step of a planarization
void UMLGraph::sortEdgesFromLayout()
{
	//we order the edges around each node corresponding to
	//the input embedding in the GraphAttributes layout
	NodeArray<SListPure<adjEntry> > adjList(*m_pG);

	EdgeComparer* ec = new EdgeComparer(*this);

	for(node v : m_pG->nodes)
	{
		for(adjEntry ae : v->adjEntries)
		{
			adjList[v].pushBack(ae);
		}

		//sort the entries
		adjList[v].quicksort(*ec);
		m_pG->sort(v, adjList[v]);

	}

	delete ec;
}//sortedgesfromlayout