void get_Yen_k_shortest_paths(char *filePath, int k, struct job *nj, int &path_id) { Graph my_graph(filePath); YenTopKShortestPathsAlg yenAlg(my_graph, my_graph.get_vertex(nj->source_id), my_graph.get_vertex(nj->dest_id)); int i=0; while(yenAlg.has_next() && i < k) { ++i; BasePath *p = yenAlg.next(); struct path *pa = (struct path *) calloc (1, sizeof(struct path)); pa->job_id = nj->job_id; pa->path_id = path_id; for (int j = 0; j < p->length() - 1; j++) { struct arc a; a.u = p->GetVertex(j)->getID(); a.v = p->GetVertex(j + 1)->getID(); pa->arcs.push_back(a); } nj->paths.push_back(pa); path_id++; } }
void testDijkstraGraph(char *filePath, int source, int dest) { Graph* my_graph_pt = new Graph(filePath); DijkstraShortestPathAlg shortest_path_alg(my_graph_pt); BasePath* result = shortest_path_alg.get_shortest_path( my_graph_pt->get_vertex(source), my_graph_pt->get_vertex(dest)); result->PrintOut(cout); }
void YenTopKShortestPathsAlg::_init() { clear(); if (m_pSourceVertex != NULL && m_pTargetVertex != NULL) { BasePath* pShortestPath = get_shortest_path(m_pSourceVertex, m_pTargetVertex); if (pShortestPath != NULL && pShortestPath->length() > 1) { m_quPathCandidates.insert(pShortestPath); m_mpDerivationVertexIndex[pShortestPath] = m_pSourceVertex; } } }
void optiq_alg_yen_distinct_shortest_paths(char *filePath, struct job *nj, int &path_id, int **load, int maxload) { Graph my_graph(filePath); YenTopKShortestPathsAlg yenAlg(my_graph, my_graph.get_vertex(nj->source_id), my_graph.get_vertex(nj->dest_id)); int trials = 0; while (yenAlg.has_next() && trials <= 200) { BasePath *p = yenAlg.next(); trials++; struct path *pa = (struct path *) calloc (1, sizeof(struct path)); pa->job_id = nj->job_id; pa->path_id = path_id; bool underload = true; for (int j = 0; j < p->length() - 1; j++) { struct arc a; a.u = p->GetVertex(j)->getID(); a.v = p->GetVertex(j + 1)->getID(); if (load[a.u][a.v] >= maxload) { free(pa); underload = false; break; } pa->arcs.push_back(a); } if (underload) { nj->paths.push_back(pa); path_id++; break; } } }
void optiq_alg_yen_k_shortest_paths_2vertices(char *graphfile, int v1, int v2, int num_paths, std::vector<struct path *> &paths) { Graph my_graph (graphfile); YenTopKShortestPathsAlg yenAlg(my_graph, my_graph.get_vertex(v1), my_graph.get_vertex(v2)); int k = 0; while (yenAlg.has_next() && k < num_paths) { BasePath *p = yenAlg.next(); if (p->length() > 0) { struct path *pa = (struct path*) calloc (1, sizeof (struct path)); pa->arcs.clear(); pa->path_id = k; for (int i = 0; i < p->length() - 1; i++) { struct arc a = arc(); a.u = p->GetVertex(i)->getID(); a.v = p->GetVertex(i + 1)->getID(); pa->arcs.push_back(a); } /*for (int i = 0; i < p->m_vtVertexList.size(); i++) { delete p->m_vtVertexList[i]; } p->m_vtVertexList.clear();*/ //delete p; paths.push_back(pa); } k++; } }
void optiq_alg_yen_k_shortest_paths (std::vector<struct path *> &complete_paths, std::vector<struct job> &jobs, int num_paths, char *graphFilePath) { Graph my_graph(graphFilePath); for (int i = 0; i < jobs.size(); i++) { int source_id = jobs[i].source_id; int dest_id = jobs[i].dest_id; YenTopKShortestPathsAlg yenAlg (my_graph, my_graph.get_vertex(source_id), my_graph.get_vertex(dest_id)); int k = 0; while (yenAlg.has_next() && k < num_paths) { BasePath *p = yenAlg.next(); struct path *pa = (struct path *) calloc (1, sizeof(struct path)); for (int j = 0; j < p->length() - 1; j++) { struct arc a; a.u = p->GetVertex(j)->getID(); a.v = p->GetVertex(j + 1)->getID(); pa->arcs.push_back(a); } jobs[i].paths.push_back(pa); complete_paths.push_back(pa); k++; } } for (int i = 0; i < complete_paths.size(); i++) { complete_paths[i]->path_id = i; } }
void get_most_h_hops_k_shortest_paths (char *filePath, int h, int k, struct job *nj, int &path_id) { Graph my_graph(filePath); YenTopKShortestPathsAlg yenAlg(my_graph, my_graph.get_vertex(nj->source_id), my_graph.get_vertex(nj->dest_id)); int i = 0; while (yenAlg.has_next()) { BasePath *p = yenAlg.next(); if (p->Weight() >= h || i > k) { /*If i = 0, but even the shortest path has more than h hops, print at least one.*/ if (i > 0) { break; } } i++; struct path *pa = (struct path *) calloc (1, sizeof(struct path)); pa->job_id = nj->job_id; pa->path_id = path_id; for (int j = 0; j < p->length() - 1; j++) { struct arc a; a.u = p->GetVertex(j)->getID(); a.v = p->GetVertex(j + 1)->getID(); pa->arcs.push_back(a); } nj->paths.push_back(pa); path_id++; } }