本文整理汇总了C++中UndirectedGraph类的典型用法代码示例。如果您正苦于以下问题:C++ UndirectedGraph类的具体用法?C++ UndirectedGraph怎么用?C++ UndirectedGraph使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了UndirectedGraph类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: FindMinWeightBridge
int FindMinWeightBridge(const UndirectedGraph& graph) {
vector<Color> colors(graph.Order(), WHITE);
DFSVisitor visitor(graph.Order());
Edge impossible_edge(0, 0, 0, -1);
DepthFirstSearch(graph, 0, impossible_edge, &colors, &visitor);
return visitor.MinWeight();
}
示例2: TEST
TEST(Graph, adjaent) {
UndirectedGraph graph {20};
graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(0, 5);
Iterator<int>* iter = graph.adjacent(0);
EXPECT_EQ(5, iter->next());
EXPECT_EQ(2, iter->next());
EXPECT_EQ(1, iter->next());
}
示例3: main
int main(int argc, char** argv) {
int i;
if (argc != 2) {
cout << "Spatny pocet parametru" << endl;
cout << "binarka nazev-souboru" << endl;
cout << std::endl << "Arguments:" << endl;
exit(EXIT_FAILURE);
}
UndirectedGraph *graph = readGraphFromFile(argv[1]);
cout << "vertex count=" << graph->vertexCount() << endl;
DFSSolver *solver = new DFSSolver(graph);
//time start
//time_t start = time(NULL);
//TimeTool(time(NULL)) start;
TimeTool start(time(NULL));
//hledani reseni
pair<vector<Edge> *, int> *result = solver->findBestSolution();
//time end
//time_t end = time(NULL);
TimeTool end(time(NULL));
vector<Edge> *solution = result->first;
int solutionPrice = result->second;
if (solution != NULL) {
cout << "Best solution:" << endl;
for (i = 0; i < solution->size(); i++) {
cout << (*solution)[i] << endl;
}
cout << "Spanning tree degree: " << solutionPrice << endl;
}
//Celkovy cas vypoctu
//double dif = difftime(end, start);
//int h = dif/3600;
//int m = dif /60;
//int s = dif - (h * 3600) - (m * 60);
//cout << "DIFF:" << dif << "s" << endl;
//cout << "TIME:" << h << ":" << m << ":" << s << " input file " << argv[1] << endl;
TimeTool dif (difftime(end.time, start.time));
//cout << "TIME: " << dif << " input file " << argv[1] << " START:" << start <<" END:" << end << endl;
cout << "TIME: " << dif << " input file " << argv[1] << endl;
delete graph;
delete solution;
delete result;
delete solver; // tady to umre
return 0;
}
示例4: TEST_F
TEST_F(UndirectedGraphFixture, canBeCopied)
{
g_.addEdge(0, 1, 7);
g_.addEdge(3, 2, 123);
UndirectedGraph copy { g_ };
ASSERT_EQ(g_.getWeightOfEdge(0, 1), copy.getWeightOfEdge(0, 1));
ASSERT_EQ(g_.getWeightOfEdge(2, 3), copy.getWeightOfEdge(2, 3));
ASSERT_EQ(g_.getNumberOfEdges(), copy.getNumberOfEdges());
ASSERT_EQ(g_.getNumberOfVertices(), copy.getNumberOfVertices());
ASSERT_EQ(g_.getSumOfWeights(), copy.getSumOfWeights());
}
示例5: UndirectedGraph
UndirectedGraph* GreedyHeuristic::getICTree(UndirectedGraph* t1, UndirectedGraph* t2, list<Edge*>* iset, UndirectedGraph* ug) {
int cardinality = ((*t1).vertices).size() - 1;
UndirectedGraph* greedyTree = new UndirectedGraph();
//cout << "iset size: " << iset->size() << endl;
Edge* minE = getMinEdge(iset);
greedyTree->addVertex(minE->fromVertex());
greedyTree->addVertex(minE->toVertex());
greedyTree->addEdge(minE);
generateUCNeighborhoodFor(ug,minE);
for (int k = 2; k < ((*ug).vertices).size(); k++) {
Edge* newEdge = getICNeighbor(iset);
Vertex* newVertex = NULL;
if (greedyTree->contains(newEdge->fromVertex())) {
newVertex = newEdge->toVertex();
}
else {
newVertex = newEdge->fromVertex();
}
greedyTree->addVertex(newVertex);
greedyTree->addEdge(newEdge);
adaptUCNeighborhoodFor(newEdge,newVertex,greedyTree,ug);
}
if ((greedyTree->vertices).size() > (cardinality + 1)) {
shrinkTree(greedyTree,cardinality);
}
greedyTree->setWeight(weightOfSolution(greedyTree));
return greedyTree;
}
示例6: dfs
bool dfs(int v, int c, UndirectedGraph& g, int* color){
color[v] = c;
for(vector<int>::iterator it=g.edge_list_begin(v); it !=g.edge_list_end(v); it++){
int dst = *it;
if(color[dst] == c)return false;
if(color[dst] == 0)
{
if(!dfs(dst, -c, g, color))return false;
}
}
return true;
}
示例7: main
int main(int argc, char **argv) {
if (argc != 2) {
std::cout << "Usage: " << argv[0] << "||||| Should be: <MAXIMUM_NUMBER_OF_VERTICES>" << std::endl;
return 0;
}
std::cout << "The maximus number of vertices is " << argv[1] << std::endl;
// Try to translate MAXIMUM_NUMBER_OF_VERTICES
// to size_t in order to determine the number of vertices.
try {
long num_of_nodes = std::stoi(argv[1]);
if (num_of_nodes < 0) {
std::cout << "Invalid argument for MAXIMUM_NUMBER_OF_NODES::Must be a positive integer!::TERMINATING PROGRAM\n";
exit(1);
}
const size_t max_val = num_of_nodes;
UndirectedGraph<size_t> testGraph;
DisjointSet<size_t> testDS;
size_t counter = 1;
while (counter <= num_of_nodes) {
testGraph.AddVertex(counter);
testDS.AddNewNode(counter);
++counter;
}
srand(time(0)); //use current time as seed for random generator
while (testDS.Size() > 1) {
int i1 = rand() % max_val + 1;
int i2 = rand() % max_val + 1;
if (testGraph.AddEdge(i1, i2)) {
testDS.Union(i1, i2);
}
}
// testGraph.printGraph();
testGraph.PrintGraphStats();
} catch (std::invalid_argument) {
std::cout << "Invalid argument for MAXIMUM_NUMBER_OF_NODES::Must be a positive integer::TERMINATING PROGRAM\n";
exit(1);
}
return 0;
}
示例8: main
int main(void)
{
using namespace alg;
srand(time(NULL));
int NVERTEX = 10;
UndirectedGraph * g = UndirectedGraph::randgraph(NVERTEX);
g->printdot();
printf("Generating Kruskal's Graph: \n");
Kruskal pg(*g);
pg.print();
printf("Generating Minimal spanning tree: \n");
Graph * mst = pg.run();
mst->printdot();
delete mst;
delete g;
return 0;
}
示例9: UndirectedGraph
UndirectedGraph* DirectedGraph::convertToUndirectedGraph() {
UndirectedGraph* result = new UndirectedGraph(maxNodeId);
for (int i = 0; i != maxNodeId + 1; i++) {
if (!hasNode(i))
continue;
ListType* neighborsList = inNeighborsTable[i];
for (ListType::iterator neighbor = neighborsList->begin();
neighbor != neighborsList->end(); neighbor++) {
result->addEdge(*neighbor, i);
}
neighborsList = outNeighborsTable[i];
for (ListType::iterator neighbor = neighborsList->begin();
neighbor != neighborsList->end(); neighbor++) {
result->addEdge(*neighbor, i);
}
}
result->sort();
return result;
}
示例10: main
int main( int argc, char **argv ) {
if ( argc < 2 ) {
print_help();
exit(1);
}
else {
read_parameters(argc,argv);
}
// initialize random number generator
rnd = new Random((unsigned) time(&t));
// initialize and read instance from file
graph = new UndirectedGraph(i_file);
GreedyHeuristic gho(graph);
for (int i = cardb; i <= carde; i++) {
cardinality = i;
if ((cardinality == cardb) || (cardinality == carde) || ((cardinality % cardmod) == 0)) {
Timer timer;
UndirectedGraph* greedyTree = new UndirectedGraph();
if (type == "edge_based") {
gho.getGreedyHeuristicResult(greedyTree,cardinality,ls);
}
else {
if (type == "vertex_based") {
gho.getVertexBasedGreedyHeuristicResult(greedyTree,cardinality,ls);
}
}
printf("%d\t%f\t%f\n",cardinality,greedyTree->weight(),timer.elapsed_time(Timer::VIRTUAL));
delete(greedyTree);
}
}
delete(graph);
delete rnd;
}
示例11: readGraphFromFile
UndirectedGraph * readGraphFromFile(char * filename) {
char token;
int x=0, y=0, i=0;
int nodes_count = 0;
UndirectedGraph *graph;
FILE *file = fopen(filename, (const char *)"r");
if (file == NULL) {
std::cout << "Neexistujici soubor:" << filename << endl;
exit(EXIT_FAILURE);
}
if (fscanf(file, "%d", &nodes_count) != 1) {
std:cout << "Nelze nacist prvni radek vstupniho souboru" <<endl;
exit(EXIT_FAILURE);
}
graph = new UndirectedGraph(nodes_count);
while (fscanf(file, "%c", &token) == 1) {
if (token == '\r') {
continue;
}
if (token == '\n') {
//printf("\n");
if (i > 0) {
x=0;
y++;
}
continue;
}
if(token == '1') {
graph->addEdge(x,y);
}
x++;
i++;
}
fclose(file);
return graph;
}
示例12: minSpanningTree
/**
* Removes all edges from the graph except those necessary to
* form a minimum cost spanning tree of all vertices using Prim's
* algorithm.
*
* The graph must be in a state where such a spanning tree
* is possible. To call this method when a spanning tree is
* impossible is undefined behavior.
*/
UndirectedGraph UndirectedGraph::minSpanningTree() {
// Define based on the Wikipedia Pseudocode
UndirectedGraph nug;
std::priority_queue<Edge> edges;
for (vertexmap::iterator vi = this->vertices.begin();
vi != this->vertices.end();
vi++)
vi->second->setVisited(false);
Vertex *cur = this->vertices.begin()->second;
cur->setVisited(true);
for (Vertex::edgemap::iterator ei = cur->edges.begin();
ei != cur->edges.end();
ei++)
edges.push(ei->second);
while (!edges.empty() && nug.vertices.size() < this->vertices.size()) {
Edge small = edges.top();
edges.pop();
Vertex *to = small.getTo();
if (to->wasVisited())
continue;
else {
to->setVisited(true);
nug.addEdge(small);
for (Vertex::edgemap::iterator ei = to->edges.begin();
ei != to->edges.end();
ei++)
if (!ei->second.getTo()->wasVisited())
edges.push(ei->second);
}
} // END WHILE
return nug;
}
示例13: DepthFirstSearch
void DepthFirstSearch(const UndirectedGraph& graph, int vertex, const Edge& incoming_edge,
vector<Color>* colors, DFSVisitor* visitor) {
colors->at(vertex) = GREY;
visitor->EnterVertex(vertex);
UndirectedGraph::EdgeIterator edge;
for (edge = graph.EdgesBegin(vertex); edge != graph.EdgesEnd(vertex); ++edge) {
if (edge->id == incoming_edge.id) {
continue;
}
if (colors->at(edge->head) == GREY) {
visitor->BackEdge(*edge);
}
if (colors->at(edge->head) == WHITE) {
DepthFirstSearch(graph, edge->head, *edge, colors, visitor);
visitor->TreeEdge(*edge);
}
}
colors->at(vertex) = BLACK;
}
示例14: ConnectedComponentsDecomposer
explicit ConnectedComponentsDecomposer(const UndirectedGraph<>& g) {
int nVertices = g.num_vertices();
vertexToComponent_.clear();
vertexToComponent_.resize(nVertices);
fill(begin(vertexToComponent_), end(vertexToComponent_), -1);
int iComponent = 0;
for (int i = 0; i < nVertices; ++i) {
if (vertexToComponent_[i] >= 0)
continue; // Lies in processed component
components_.push_back(std::vector<int>());
dfs(g, {i}, [&](const GraphTraversalState& state, int index) {
components_.back().push_back(index);
vertexToComponent_[index] = iComponent;
return IterationControl::Proceed;
});
iComponent++;
}
}
示例15: bigraph
// check whether graph is a bigraph or not with dfs
bool bigraph(UndirectedGraph& g){
int num_vertex = g.get_num_vertex();
int* color = (int *)malloc(sizeof(int)*num_vertex);
memset(color, 0, num_vertex*sizeof(int));
bool result = true;
for(int i=0;i<num_vertex;i++){
if(color[i]==0){
if(!dfs(i, 1, g, color)){
result = false;
break;
}
}
}
free(color);
return result;
}