IGraph * Sequencer::buildGraph(std::vector<std::string>& reads){ IGraph * graph = new Graph(); graph->insertVertex(reads.at(0)); for(int i = int(reads.size() - 1);i>0 ; i-- ){ for(int j = i-1; j>=0; j--){ if( reads.at(i).compare(reads.at(j)) == 0){ break; } if(j == 0){ graph->insertVertex(reads.at(i)); } } } std::vector<IVertex *> graph_vertices = graph->getVertices(); int size_graph_vertices = graph_vertices.size(); int max_overlap = 0; for(int i=0; i< size_graph_vertices; i++){ for(int j=0; j< size_graph_vertices; j++){ //find overlap if(i != j){ int overlap = 0; for(int k = 0; k< graph_vertices.at(i)->getName().size(); k++){ int l = 0; int count = 0; int m = k; while( graph_vertices.at(i)->getName()[m] == graph_vertices.at(j)->getName()[l] ){ m++; l++; count++; } if(count > overlap){ overlap = count; } } if(max_overlap < overlap){ max_overlap = overlap; } if(overlap>0){ IEdge *edge = new Edge(); edge->setTarget(graph_vertices.at(j)); edge->setWeight(overlap); graph_vertices.at(i)->addEdge(edge); } } } } for(int i=0; i< size_graph_vertices; i++){ IVertex* vert = graph_vertices.at(i); for(int j =0; j < vert->getEdges().size(); j++){ IEdge * edg = vert->getEdges()[j]; int wt = max_overlap - edg->getWeight(); edg->setWeight(max_overlap - edg->getWeight()); } } return graph; }
/** * O(V + Vlog(V) + Elog(V)) * @param G [description] * @param s [description] */ void MST_prim(IGraph &G, IGraph::vertex &s, double inf = 1e100){ int n = G.numVertices(); bool *isQ = new bool[n]; IGraph::vertex ** pi = new IGraph::vertex *[n]; typedef pair<double, int> vi; vector<vi> vertices; IndexedPriorityQueue<double, less<double> > Q(n); for (int i = 0; i < n; ++i) { vertices.push_back(make_pair(inf, i)); Q.insert(i, inf); pi[i] = NULL; isQ[i] = true; } vertices[s.id].first = 0; Q.changeKey(s.id, 0); int it = 0; while(!Q.isEmpty()){ int id = Q.minIndex(); // O(1) Q.deleteMin(); // O(lg(v)) cout << id << endl; isQ[id] = false; IGraph::vertex *u = G.getVertex(id); for (IGraph::vertex::iterator v = u->begin(); v != u->end(); ++v) { double w = G.getEdge(id, v->second->id); if(isQ[v->second->id] && w < vertices[v->second->id].first){// O(1) pi[v->second->id] = u; Q.changeKey(v->second->id, w); // O(log(v)) vertices[v->second->id].first = w; } } } cout << "PRIMs : "; for (int i = 0; i < n; ++i) { if(pi[i] != NULL) cout << pi[i]->id << " "; else cout << "nil "; } cout << endl; delete [] isQ; }
/** * 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; }
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); } } } } //} }
void assertValidCover(IGraph *graph, vector<Node*> cover) { IGraph *g = new Graph(graph); for (Node *node : cover) { g->removeNode(node); } if (g->nbEdges() == 0) { rlutil::setColor(2); cout << "OK" << endl; rlutil::setColor(7); } else { rlutil::setColor(12); cout << "FAILED : The provided cover is not a valid solution." << endl; rlutil::setColor(7); } delete g; }
int tour_ham_merge_and_count(IGraph &tour, int *A, int *buffer, int ini, int fim, int meio){ memset(buffer, 0, sizeof(int)*(fim-ini+1)); int count = 0; int i = ini; int j = meio + 1; int k = 0; while(i < meio + 1 && j <= fim){ if(tour.edgeExists(A[i], A[j])){ buffer[k] = A[i]; i++; }else{ buffer[k] = A[j]; count += meio - i + 1; j++; } k++; } if(i < meio + 1){ memcpy(buffer + k, A + i, sizeof(int)*(meio-i+1)); }else{ memcpy(buffer + k, A + j, sizeof(int)*(fim-j+1)); } memcpy(A + ini, buffer, sizeof(int)*(fim-ini+1)); return count; }
void DFSImplementation (const IGraph& graph, Visitor& visitor, std::vector<bool>& visited, int vertex_start) { std::stack <int> vertices_stack; vertices_stack.push(vertex_start); while (!vertices_stack.empty()) { int current_vertex = vertices_stack.top(); if (!visited[current_vertex]) { visited[current_vertex] = true; visitor.StartProcessingVertex(current_vertex); std::vector<int> current_vertex_incidences = graph.GetIncidenceList(current_vertex); for (int i = 0; i < current_vertex_incidences.size(); ++i) { int next_incident_vertex = current_vertex_incidences[i]; visitor.ProcessingEdge(current_vertex, next_incident_vertex); if (!visited[next_incident_vertex]) vertices_stack.push(next_incident_vertex); } } else { visitor.LeaveVertex(current_vertex); vertices_stack.pop(); } } } if (!visited[vertex]) DFSImplementation(graph, visitor, visited, vertex);
/** * 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; }
void FAS_pivot(IGraph &graph, std::vector<int> &vertices){ if(vertices.size() <= 1) return; // pick random pivot int i = vertices[rand()%(vertices.size())]; std::vector<int> vL; std::vector<int> vR; for (int j = 0; j < vertices.size(); ++j) { if(i == vertices[j]) continue; if(graph.edgeExists(vertices[j], i)){ vL.push_back(vertices[j]); }else if(graph.edgeExists(i, vertices[j])){ vR.push_back(vertices[j]); } } FAS_pivot(graph, vL); FAS_pivot(graph, vR); vertices.clear(); for (int k = 0; k < vL.size(); ++k) { vertices.push_back(vL[k]); } vertices.push_back(i); for (int k = 0; k < vR.size(); ++k) { vertices.push_back(vR[k]); } }
inline void TestIGraphHasEdge(const IGraph& graph, const std::vector<Edge> &list_of_edges, int vertex_a, int vertex_b) { bool has_edge_between_a_and_b = graph.HasEdge(vertex_a, vertex_b); bool right_has_edge_between_a_and_b = false; for (int edge_index = 0; edge_index < list_of_edges.size(); ++edge_index) { if (list_of_edges[edge_index].begin_ == vertex_a && list_of_edges[edge_index].end_ == vertex_b) { right_has_edge_between_a_and_b = true; break; } } if (right_has_edge_between_a_and_b != has_edge_between_a_and_b) { throw std::logic_error("TestIGraphHasEdge: FAIL"); } }
inline void TestIGraphGetIncidenceList(const IGraph& graph, const std::vector<Edge> &list_of_edges, int vertex) { std::unordered_set<int> right_insidence_vertices_set; std::vector<int> incidence_list = graph.GetIncidenceList(vertex); std::unordered_set<int> incidence_vertices_set(incidence_list.begin(), incidence_list.end()); for (int edge_index = 0; edge_index < list_of_edges.size(); ++edge_index) { if (list_of_edges[edge_index].begin_ == vertex) { right_insidence_vertices_set.insert(list_of_edges[edge_index].end_); } } if (incidence_vertices_set != right_insidence_vertices_set) { throw std::logic_error("IGraph::GetIncidenceList : FAIL"); } }
void DVhamiltonPathForTournament(IGraph &tour, int * path){ int * buffer = new int[tour.numVertices()]; tour_ham_sort_and_count(tour, path, buffer, 0, tour.numVertices() - 1); }
inline void TestIGraphGetNumberOfVertices (const IGraph& graph, int vertices_amount) { if (graph.GetNumberOfVertices() != vertices_amount) { throw std::logic_error("IGraph::GetNumberOfVertices: FAIL"); } }
/*! * \brief Calback function invoking layout algorithms from Dia's menu * \ingroup LayoutPlugin */ static ObjectChange * layout_callback (DiagramData *data, const gchar *filename, guint flags, /* further additions */ void *user_data, GraphCreateFunc func) { ObjectChange *changes = NULL; GList *nodes = NULL, *edges = NULL, *list; const char *algorithm = (const char*)user_data; /* from the selection create two lists */ list = data_get_sorted_selected (data); while (list) { DiaObject *o = (DiaObject *)list->data; if (!maybe_edge (o)) nodes = g_list_append (nodes, o); //FIXME: neither 1 nor num_handles-1 is guaranteed to be the second connection // it entirely depends on the objects implementation else if ( o->num_handles > 1 && o->handles[0]->connected_to && (o->handles[1]->connected_to || o->handles[o->num_handles-1]->connected_to)) edges = g_list_append (edges, o); list = g_list_next(list); } if (g_list_length (edges) < 1 || g_list_length (nodes) < 2) { message_warning (_("Please select edges and nodes to layout.")); } else { IGraph *g = func ? func () : NULL; if (!g) message_error (_("Graph creation failed")); else { std::vector<double> coords; /* transfer nodes and edges */ for (list = nodes; list != NULL; list = g_list_next(list)) { DiaObject *o = (DiaObject *)list->data; const Rectangle *bbox = dia_object_get_bounding_box (o); g->AddNode (bbox->left, bbox->top, bbox->right, bbox->bottom); } for (list = edges; list != NULL; list = g_list_next(list)) { DiaObject *o = (DiaObject *)list->data; DiaObject *src = o->handles[0]->connected_to->object; // see above: there is no guarantee ... DiaObject *dst = o->handles[1]->connected_to ? o->handles[1]->connected_to->object : o->handles[o->num_handles-1]->connected_to->object; if (_obj_get_bends (o, coords)) g->AddEdge (g_list_index (nodes, src), g_list_index (nodes, dst), &coords[0], coords.size()); else g->AddEdge (g_list_index (nodes, src), g_list_index (nodes, dst), NULL, 0); } IGraph::eResult res; if ((res = g->Layout (algorithm)) != IGraph::SUCCESS) { const char *sErr; switch (res) { case IGraph::NO_MODULE : sErr = _("No such module."); break; case IGraph::OUT_OF_MEMORY : sErr = _("Out of memory."); break; case IGraph::NO_TREE: sErr = _("Not a tree."); break; case IGraph::NO_FOREST: sErr = _("Not a forest."); break; case IGraph::FAILED_ALGORITHM: sErr = _("Failed algorithm."); break; case IGraph::FAILED_PRECONDITION: sErr = _("Failed precondition."); break; case IGraph::CRASHED : sErr = _("OGDF crashed."); break; default : sErr = _("Unknown reason"); break; } message_warning (_("Layout '%s' failed.\n%s"), (const char*)user_data, sErr); } else { changes = change_list_create (); /* transfer back information */ int n; for (n = 0, list = nodes; list != NULL; list = g_list_next (list), ++n) { Point pt; if (g->GetNodePosition (n, &pt.x, &pt.y)) { DiaObject *o = (DiaObject *)list->data; GPtrArray *props = g_ptr_array_new (); //FIXME: can't use "obj_pos", it is not read in usual update_data impementations // "elem_corner" will only work for Element derived classes, but that covers most // of the cases here ... prop_list_add_point (props, "elem_corner", &pt); change_list_add (changes, object_apply_props (o, props)); } } // first update to reuse the connected points diagram_update_connections_selection(DIA_DIAGRAM (data)); /* use edge bends, if any */ int e; for (e = 0, list = edges; list != NULL; list = g_list_next (list), ++e) { DiaObject *o = (DiaObject *)list->data; // number of bends / 2 is the number of points int n = g->GetEdgeBends (e, NULL, 0); if (n >= 0) { // with 0 it is just a reset of the exisiting line try { coords.resize (n); } catch (std::bad_alloc& ex) { g_warning ("%s", ex.what()); continue; } g->GetEdgeBends (e, &coords[0], n); change_list_add (changes, _obj_set_bends (o, coords)); } } /* update view */ diagram_update_connections_selection(DIA_DIAGRAM (data)); } g->Release (); } } g_list_free (nodes); g_list_free (edges); return changes; }
void DFS (const IGraph& graph, Visitor& visitor) { std::vector<bool> visited(graph.GetNumberOfVertices(), false); for (int vertex = 0; vertex < graph.GetNumberOfVertices(); ++vertex) if (!visited[vertex]) DFSImplementation(graph, visitor, visited, vertex); }