void complete2Tournament(IGraph &comp, IGraph &tour, bool unweighted = true){ //if(comp.isComplete()){ // cout << "complete2Tournament" << endl; for (IGraph::vertex_iterator v = comp.begin(); v != comp.end(); v++){ IGraph::vertex::iterator end = v->end(); for (IGraph::vertex::iterator u = v->begin(); u != end; u++){ if(!(tour.edgeExists(v->id,u->second->id) || tour.edgeExists(u->second->id, v->id))){ double w1 = comp.getEdge(v->id, u->second->id); double w2 = comp.getEdge(u->second->id, v->id); double wVal = (unweighted)? 1 : abs(w1 - w2); if(w1 > w2) tour.addEdge(v->id, u->second->id, (unweighted)? 1 : w1); else if(w1 < w2) tour.addEdge(u->second->id, v->id, (unweighted)? 1 : w2); else{ tour.addEdge(u->second->id, v->id, (unweighted)? 1 : w2); tour.addEdge(v->id, u->second->id, (unweighted)? 1 : w1); } } } } //} }
/** * complexity : O(V + E) * @param G Graph * @param s source vertex */ void DFS(IGraph &G, IGraph::vertex &s){ int n = G.numVertices(); int *color = new int[n]; int *d = new int[n]; int *f = new int[n]; IGraph::vertex **pi = new IGraph::vertex*[n]; for (IGraph::vertex_iterator v = G.begin(); v != G.end(); ++v) { color[v->id] = WHITE; pi[v->id] = NULL; d[v->id] = 0; f[v->id] = 0; } int dtime = 0; DFS_visit(G, s, color, d, f, pi, dtime); for (IGraph::vertex_iterator v = G.begin(); v != G.end(); ++v) { if(color[v->id] == WHITE){ DFS_visit(G, *v, color, d, f, pi, dtime); } } cout << "S : " << s.id << endl; for (int i = 0; i < n; ++i) { if(pi[i] != NULL) cout << pi[i]->id << "(" << d[i] << "/" << f[i] << ") "; else cout << "nil" << "(" << d[i] << "/" << f[i] << ") "; } cout << endl; delete [] color; delete [] d; delete [] f; delete [] pi; }
/** * O(V + E) * @param G Graph * @param s source vertex */ void BFS(IGraph &G, IGraph::vertex &s){ int n = G.numVertices(); int *color = new int[n]; int *d = new int[n]; IGraph::vertex **pi = new IGraph::vertex*[n]; for (IGraph::vertex_iterator v = G.begin(); v != G.end(); ++v) { color[v->id] = WHITE; pi[v->id] = NULL; d[v->id] = 1000000; } color[s.id] = GRAY; d[s.id] = 0; queue<IGraph::vertex * > Q; Q.push(&s); while(!Q.empty()){ IGraph::vertex * u = Q.front(); for (IGraph::vertex::iterator e = u->begin(); e != u->end(); e++) { IGraph::vertex * v = e->second; if(color[v->id] == WHITE){ color[v->id] = GRAY; d[v->id] = d[u->id] + 1; pi[v->id] = u; Q.push(&(*v)); } } color[u->id] = BLACK; Q.pop(); } cout << "S : " << s.id << endl; for (int i = 0; i < n; ++i) { if(pi[i] != NULL) cout << pi[i]->id << " "; else cout << "nil "; } cout << endl; delete [] color; delete [] d; delete [] pi; }