dijkstra.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // Created by zx on 22-11-29.
  3. //
  4. #ifndef LIO_LIVOX_CPP_DIJKSTRA_DIJKSTRA_H_
  5. #define LIO_LIVOX_CPP_DIJKSTRA_DIJKSTRA_H_
  6. //Purpose: Implementation of Dijkstra's algorithm which finds the shortest
  7. //path from a start node to every other node in a weighted graph.
  8. //Time complexity: O(n^2)
  9. #include <iostream>
  10. #include <limits>
  11. #include <vector>
  12. #include <map>
  13. #define MAXV 1000
  14. #define EXP 0.0001
  15. class EdgeNode{
  16. public:
  17. int key;
  18. float weight;
  19. EdgeNode *next;
  20. EdgeNode(int, float);
  21. };
  22. class Graph{
  23. bool directed;
  24. public:
  25. EdgeNode *edges[MAXV + 1];
  26. Graph(bool);
  27. ~Graph();
  28. void insert_edge(int, int, float, bool);
  29. void print();
  30. };
  31. void init_vars(bool discovered[], int distance[], int parent[]);
  32. void dijkstra_shortest_path(Graph *g, int parent[], float distance[], int start);
  33. void get_shortest_path(int v, int parent[],std::vector<int>& path);
  34. void print_distances(int start, float distance[]);
  35. class PathMap
  36. {
  37. public:
  38. typedef struct
  39. {
  40. float x;
  41. float y;
  42. }MapNode;
  43. typedef struct
  44. {
  45. int id1;
  46. int id2;
  47. bool direct;
  48. }MapEdge;
  49. public:
  50. PathMap();
  51. ~PathMap();
  52. void AddVertex(int id,float x,float y);
  53. bool AddEdge(int id1,int id2,bool directed);
  54. bool GetShortestPath(int id1,int id2,std::vector<int>& path,float& shortest_distance);
  55. bool GetVertexPos(int id,float& x,float& y);
  56. std::map<int,MapNode> GetNodes()const{return node_;};
  57. std::vector<MapEdge> GetEdges()const{return edge_;}
  58. protected:
  59. std::map<int,MapNode> node_;
  60. std::vector<MapEdge> edge_;
  61. Graph *grapher_;
  62. int parent_[MAXV + 1];
  63. float distance_[MAXV + 1];
  64. };
  65. #endif //LIO_LIVOX_CPP_DIJKSTRA_DIJKSTRA_H_