void Foam::gnuplotGraph::write(const graph& g, Ostream& os) const { os << "#set term postscript color" << endl << "set output \"" << word(g.title()) << ".ps\"" << endl << "set title " << g.title() << " 0,0" << endl << "show title" << endl << "set xlabel " << g.xName() << " 0,0" << endl << "show xlabel" << endl << "set ylabel " << g.yName() << " 0,0" << endl << "show ylabel" << endl << "plot"; bool firstField = true; for (graph::const_iterator iter = g.begin(); iter != g.end(); ++iter) { if (!firstField) { os << ','; } firstField = false; os << "'-' title " << iter()->name() << " with lines"; } os << "; pause -1" << endl; for (graph::const_iterator iter = g.begin(); iter != g.end(); ++iter) { os << endl; writeXY(g.x(), *iter(), os); } }
graph kruskal (graph &rede){ graph result; int max_custo = 0; graph::iterator it; int no1, no2; vertice v; for (it = rede.begin(); it != rede.end(); it++){ v = it->second; no1 = v.first; no2 = v.second; if (findset(no1) != findset(no2)){ v = ordena (v.first, v.second); result.insert (make_pair (0, v)); join (no1, no2); max_custo += it->first; } } cout << max_custo << endl; return result; }
vector<string> vertex_cover(const graph g) { vector<connection> list; copy(g.begin(), g.end(), back_inserter(list)); sort(list.begin(), list.end(), [](connection a, connection b) { return a.second.size() > b.second.size(); }); vector<string> cover; while (list.size()) { connection max = list.at(0); cover.push_back(max.first); list.erase(list.begin()); for (string conc : max.second) { if (!list.size()) break; auto to_remove = find_if(list.begin(), list.end(), [conc](connection c) { return c.first == conc; }); if (to_remove != list.end()) { list.erase(to_remove); } } } return cover; }
map<char *, int> dijkstra(graph g, char* source_node) { int MAX_INT = 63356 ; // build the heap heap nodes_heap; // create a map with not visited nodes map<char *, int> m_not_visited; graph::iterator it; for( it = g.begin(); it != g.end(); it++) { if( strcmp(it->first, source_node) ){ nodes_heap.push(make_pair(MAX_INT, it->first)); m_not_visited.insert(make_pair(it->first, MAX_INT)); } } m_not_visited.insert(make_pair(source_node, 0)); // initialize the map that will store the shortest paths. map<char *, int> m_shortest_path; m_shortest_path.insert( make_pair(source_node , 0) ); while(nodes_heap.size() > 0){ pair<int, char*> node = nodes_heap.top(); if( m_not_visited.find( node.second ) == m_not_visited.end() ) continue; m_shortest_path[node.second] = node.first; update_heap(nodes_heap, g, source_node, m_not_visited, node.first); m_not_visited.erase(node.second); } return m_shortest_path; }
void imprime (graph &g){ vertice v; int cost; graph::iterator it; for (it = g.begin(); it != g.end(); it++){ v = it->second; cout << v.first+1 << ' ' << v.second+1 << ' ' << endl; } }
//Takes a Graph g (EdgeList!!!) with N nodes and computes the MST and Cost of it. Time Complexity: O(M*log(M)) //Requires UnionFind-Datastructure!!! pair<graph,ll> buildMST(int N, graph& g) { UnionFind uf(N); graph mst; ll mst_cost = 0; int M = g.size(); sort(g.begin(),g.end()); for(int i = 0; i < M; i++) { int u = g[i].second.first, v = g[i].second.second; if(uf.findSet(u) != uf.findSet(v)) { mst.push_back(g[i]); mst_cost += g[i].first; uf.unionSets(u,v); } } return make_pair(mst,mst_cost); }
void Foam::jplotGraph::write(const graph& g, Ostream& os) const { os << "# JPlot file" << endl << "# column 1: " << g.xName() << endl; label fieldI = 0; for (graph::const_iterator iter = g.begin(); iter != g.end(); ++iter) { os << "# column " << fieldI + 2 << ": " << (*iter()).name() << endl; fieldI++; } g.writeTable(os); }
void Foam::xmgrGraph::write(const graph& g, Ostream& os) const { os << "@title " << g.title() << endl << "@xaxis label " << g.xName() << endl << "@yaxis label " << g.yName() << endl; label fieldI = 0; for (graph::const_iterator iter = g.begin(); iter != g.end(); ++iter) { os << "@s" << fieldI << " legend " << iter()->name() << endl << "@target G0.S" << fieldI << endl << "@type xy" << endl; writeXY(g.x(), *iter(), os); os << endl; fieldI++; } }
int kruskal(graph& g, int n, graph& mst) { // sort edges by weight sort(g.begin(), g.end(), [](const edge& a, const edge& b){ return a.w < b.w; }); // split vertices into n subtrees vi root(n, 0); for (int i = 0; i < root.size(); i++) { root[i] = i; } int len = 0; for (auto& e : g) { int ru = root_of(e.u, root); int rv = root_of(e.v, root); if (ru != rv) { root[rv] = ru; len += e.w; mst.push_back(e); } } return len; }
int main(){ int w,h; int mazeNo = 1; while (cin >> w >> h && w && h){ g.clear(); d.clear(); longest = -1; qty = 0; for (int i=0; i<w; ++i){ for (int j=0; j<h; ++j){ g[node(2*i, j)].insert(node(2*i, j-1)); g[node(2*i, j-1)].insert(node(2*i, j)); g[node(2*i+1, j)].insert(node(2*i+1, j+1)); g[node(2*i+1, j+1)].insert(node(2*i+1, j)); char c; cin >> c; if (c == '\\'){ g[node(2*i+1, j)].insert(node(2*i, j)); g[node(2*i, j)].insert(node(2*i+1, j)); g[node(2*i, j)].insert(node(2*i+1, j)); g[node(2*i+1, j)].insert(node(2*i, j)); }else if (c == '/'){ g[node(2*i,j)].insert(node(2*i-1, j)); g[node(2*i-1,j)].insert(node(2*i, j)); g[node(2*i+1,j)].insert(node(2*i+2, j)); g[node(2*i+2,j)].insert(node(2*i+1, j)); }else{ cerr << "Unrecognized char in input" << endl; } } } /*for (map<node, set<node> >::iterator i = g.begin(); i != g.end(); ++i){ printf("Vecinos de (%d, %d):\n", i->first.first, i->first.second); set<node> v = i->second; for (set<node>::iterator j = v.begin(); j != v.end(); ++j){ printf("(%d, %d) ", j->first, j->second); } cout << endl; }*/ for (map<node, set<node> >::iterator i = g.begin(); i != g.end(); ++i){ if (d.count(i->first) == 0){ d[i->first] = 0; dfs(i->first); } } printf("Maze #%d:\n", mazeNo++); if (qty == 0){ printf("There are no cycles.\n"); }else{ printf("%d Cycles; the longest has length %d\n", qty, longest); } printf("\n"); } return 0; }