GraphGraphicsView::GraphGraphicsView(QWidget *parent) : QGraphicsView(parent) { setScene(&scene); resize(1000,500); setBackgroundBrush(Qt::white); setFocus(); QLinearGradient linearGrad(-500, -500, 500, 500); // átmenetes ecset linearGrad.setColorAt(0, QColor(255, 255, 255)); linearGrad.setColorAt(1, QColor(192, 192, 192)); setBackgroundBrush(linearGrad); setRenderHint(QPainter::Antialiasing); setFrameStyle(QFrame::NoFrame); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); qsrand(QTime::currentTime().msec()); randomGraph(); dDialog = new dijkstraDialog(); source = -1; destination = -1; QObject::connect(dDialog, SIGNAL(runDijkstraFromTo(int,int)), this, SLOT(runDijkstra(int,int))); }
void GraphGraphicsView::keyPressEvent(QKeyEvent *event) { if (event->key() == Qt::Key_R) { reposition(); } if (event->key() == Qt::Key_1) { randomGraph(); } }
std::shared_ptr<Component> Permute(std::vector<int> &idxs, int szGrp1) { // Create a comparison graph to hold our group comparison results std::unique_ptr<Graph> randomGraph(new Graph(_nodeCount, _lu, (double)_threshold->Value)); // Calculate edge group comparison randomGraph->AddEdges(CalcEdgeComparison(idxs, szGrp1)); // Calculate node group comparison randomGraph->AddNodes(CalcNodeComparison(idxs, szGrp1)); // Calculate NBS components //randomGraph->ComputeComponents(); // Calculate global group comparison randomGraph->SetGlobal(CalcGlobalComparison(idxs, szGrp1)); // Update our edge stats with our random values _cmpGraph->UpdateEdgeStats(randomGraph->Edges); // Update our edge stats with our random values _cmpGraph->UpdateNodeStats(randomGraph->Nodes); //_cmpGraph->UpdateComponentStats( randomGraph->GetLargestComponent() ); _cmpGraph->UpdateGlobalStats(randomGraph->Global); return nullptr; // Get the largest components auto lrgstRndmCmp = randomGraph->GetLargestComponent(); auto lrgstRealCmp = _cmpGraph->GetLargestComponent(); if (lrgstRealCmp != nullptr) { if (lrgstRndmCmp != nullptr) { lrgstRealCmp->AddRandomExtentValue(lrgstRndmCmp->Edges.size()); } else lrgstRealCmp->AddRandomExtentValue(0); } return lrgstRndmCmp; }
int main(int argc, char **argv) { edmondsKarpMaxFlowScratch<Context::internalDirectedGraph, double> scratch; //construct random graph boost::mt19937 randomSource; int edgeIndex = 0; const int nVertices = 20; boost::random::bernoulli_distribution<> bern; Context::internalDirectedGraph randomGraph(nVertices); for(int i = 0; i < nVertices; i++) { for(int j = 0; j < i; j++) { if(bern(randomSource)) { Context::internalDirectedGraph::edge_descriptor newEdge = boost::add_edge(i, j, randomGraph).first; Context::internalDirectedGraph::edge_descriptor newReverseEdge = boost::add_edge(j, i, randomGraph).first; boost::put(boost::edge_index, randomGraph, newEdge, edgeIndex); edgeIndex++; boost::put(boost::edge_index, randomGraph, newReverseEdge, edgeIndex); edgeIndex++; boost::put(boost::edge_reverse, randomGraph, newEdge, newReverseEdge); boost::put(boost::edge_reverse, randomGraph, newReverseEdge, newEdge); } } } std::vector<int> componentMap; std::size_t nDirectedEdges = boost::num_edges(randomGraph); componentMap.resize(nDirectedEdges); int components = boost::connected_components(randomGraph, &(componentMap[0])); if(components != 1) throw std::runtime_error("Random graph did not have a single component"); //The edmonds karp variables std::vector<double> capacity(nDirectedEdges, 0), flow(nDirectedEdges, 0), residual(nDirectedEdges, 0); //The incremental flow variables std::vector<double> incCapacity(nDirectedEdges, 0), incFlow(nDirectedEdges, 0), incResidual(nDirectedEdges, 0); //distribution for the edges boost::random::exponential_distribution<double> expDist(3); //incremental arguments edmondsKarpMaxFlowScratch<Context::internalDirectedGraph, double> incrementalScratch; updateFlowIncrementalArgs<Context::internalDirectedGraph, double> incrementalFlowArgs(randomGraph, incrementalScratch); incrementalFlowArgs.capacity = &(incCapacity[0]); incrementalFlowArgs.flow = &(incFlow[0]); incrementalFlowArgs.residual = &(incResidual[0]); incrementalFlowArgs.source = 0; incrementalFlowArgs.sink = nVertices - 1; incrementalFlowArgs.nDirectedEdges = nDirectedEdges; updateMaxFlowIncrementalArgs<Context::internalDirectedGraph, double> incrementalMaxFlowArgs(randomGraph, incrementalScratch); incrementalMaxFlowArgs.capacity = &(incCapacity[0]); incrementalMaxFlowArgs.flow = &(incFlow[0]); incrementalMaxFlowArgs.residual = &(incResidual[0]); incrementalMaxFlowArgs.source = 0; incrementalMaxFlowArgs.sink = nVertices - 1; incrementalMaxFlowArgs.nDirectedEdges = nDirectedEdges; double incrementalMaxFlow = 0; //Change all the edges 10000 times for(int i = 0; i < 10000; i++) { Context::internalDirectedGraph::edge_iterator current, end; boost::tie(current, end) = boost::edges(randomGraph); for(; current != end; current++) { int edgeIndex = boost::get(boost::edge_index, randomGraph, *current); Context::internalDirectedGraph::edge_descriptor reverseEdge = boost::get(boost::edge_reverse, randomGraph, *current); int reverseEdgeIndex = boost::get(boost::edge_index, randomGraph, reverseEdge); double newCapacity = expDist(randomSource); capacity[edgeIndex] = capacity[reverseEdgeIndex] = newCapacity; std::fill(flow.begin(), flow.end(), 0); std::copy(capacity.begin(), capacity.end(), residual.begin()); double edmondsKarpMaxFlowResult = 0; edmondsKarpMaxFlow<Context::internalDirectedGraph, double>(&(capacity[0]), &(flow[0]), &(residual[0]), randomGraph, 0, nVertices - 1, std::numeric_limits<double>::infinity(), scratch, edmondsKarpMaxFlowResult); //Test the code that only updates the max flow incrementalMaxFlowArgs.edge = *current; incrementalMaxFlowArgs.newCapacity = newCapacity; double previousMaxFlow = incrementalMaxFlow; double onlyMaxFlow; updateMaxFlowIncremental<Context::internalDirectedGraph, double>(incrementalMaxFlowArgs, previousMaxFlow, onlyMaxFlow); if(fabs(onlyMaxFlow - edmondsKarpMaxFlowResult) > 1e-8) { throw std::runtime_error("Max flow mismatch"); } //Test the code that updates everything incrementalFlowArgs.edge = *current; incrementalFlowArgs.newCapacity = newCapacity; updateFlowIncremental(incrementalFlowArgs, previousMaxFlow, incrementalMaxFlow); if(fabs(incrementalMaxFlow - edmondsKarpMaxFlowResult) > 1e-8) { throw std::runtime_error("Max flow mismatch"); } //Check that the incremental code has worked. double tmp = 0; edmondsKarpMaxFlow<Context::internalDirectedGraph, double>(&(incCapacity[0]), &(incFlow[0]), &(incResidual[0]), randomGraph, 0, nVertices - 1, std::numeric_limits<double>::infinity(), scratch, tmp); if(fabs(tmp) > 1e-8) { throw std::runtime_error("Max flow mismatch"); } //The flows and residuals don't have to be the same. /*if(memcmp(&(flow[0]), &(incFlow[0]), sizeof(double)*nDirectedEdges) != 0) { throw std::runtime_error("Flow mismatch"); } if(memcmp(&(residual[0]), &(incResidual[0]), sizeof(double)*nDirectedEdges) != 0) { throw std::runtime_error("Residual mismatch"); }*/ } } std::cout << "Success!" << std::endl; return 0; }
Maingraphv2::Maingraphv2(QWidget *parent) : QMainWindow(parent), ui(new Ui::Maingraphv2) { ui->setupUi(this); this->resize(1000,550); view = new GraphGraphicsView(this); view->show(); view->setGeometry(view->pos().x(), view->pos().y()+20,view->size().width(), view->size().height() ); dialog = new graphDrawingDialog(); Graph = menuBar()->addMenu("Graph"); /* Clear = menuBar()->addMenu("Clear"); Algorithms = menuBar()->addMenu("Run Algorithms..."); */ Clear = ui->menuBar->addMenu("Clear"); Algorithms = ui->menuBar->addMenu("Run Algorithms..."); clearScene = new QAction("Clear scene", this); readFromFile = new QAction("Read graph from file", this); randomize = new QAction("Randomize graph", this); reposition = new QAction("Reposition graph", this); runDijkstra = new QAction("Run algorithm", this); //runDFS = new QAction("Run depth first search", this); specifiedGraph = new QAction("Draw specified graph...", this); Graph->addAction(readFromFile); Graph->addAction(randomize); Graph->addAction(reposition); Graph->addAction(specifiedGraph); Clear->addAction(clearScene); Algorithms->addAction(runDijkstra); //Algorithms->addAction(runDFS); menuBar()->show(); //qDebug() << menuBar()->isVisible(); QObject::connect(clearScene, SIGNAL(triggered()), view, SLOT(clear())); QObject::connect(randomize, SIGNAL(triggered()), view, SLOT(randomGraph())); QObject::connect(reposition, SIGNAL(triggered()), view, SLOT(reposition())); //QObject::connect(runDFS, SIGNAL(triggered()), // view, SLOT(depthFirstSearch())); QObject::connect(specifiedGraph, SIGNAL(triggered()), this, SLOT(specifiedGraph_Triggered())); QObject::connect(dialog, SIGNAL(drawThis(int,int)), view, SLOT(drawSpecifiedGraph(int,int))); QObject::connect(runDijkstra, SIGNAL(triggered()), view, SLOT(preDijkstra())); QObject::connect(readFromFile, SIGNAL(triggered()), view, SLOT(readFromFile())); /* ui->menuBar->addMenu(Graph); ui->menuBar->addMenu(Algorithms); ui->menuBar->addMenu(Clear); */ }