图的最短算法
从起点开始访问所有路径,可以到达终点的有多条地址,其中路径权值最小的为最短路径。 最短路径算法有深度优先遍历、广度优先遍历、Bellman-Ford算法、弗洛伊德算法、SPFA(Shortest Path Faster Algorithm)算法和迪杰斯特拉算法等。
本代码使用深度优先遍历
主要实现思路 :
从起点开始,到达终点有多条分支,这些分支中又有多条分支… 选择其实一条分支,走到终点,再选择另一个分支(temp = temp ->next)走到终点,分支的分支……
大致流程 :代码实现 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 #include <iostream> #include <queue> using namespace std ;#define Max_Size 1024 bool visited[Max_Size];typedef struct _EdgeNode { int adjvex; int weight; struct _EdgeNode * next ; }EdgeNode; typedef struct _VertexNode //顶点结点,这个就是邻接桶{ char data; struct _EdgeNode * first ; }VertexNode, AdjList; typedef struct _AdjListGraph { AdjList* adjlist; int vex; int edge; }AdjListGraph; int Location (AdjListGraph& G,char c) { for (int i = 0 ; i < G.vex; i++) { if (G.adjlist[i].data == c) { return i; } } return -1 ; } void initGraph (AdjListGraph& G) { G.adjlist = new AdjList[Max_Size]; G.edge = 0 ; G.vex = 0 ; for (int i = 0 ; i < Max_Size; i++) { visited[i] = false ; } } void createGraph (AdjListGraph& G) { cout << "请输入该图的顶点数以及边数" << endl ; cin >> G.vex >> G.edge; cout << "请输入顶点data" << endl ; for (int i = 0 ; i < G.vex; i++) { cin >> G.adjlist[i].data; G.adjlist[i].first = NULL ; } char v1 = 0 , v2 = 0 ; int i1 = 0 , i2 = 0 ; int weight = 0 ; cout << "请输入想关联边的顶点" << endl ; for (int i = 0 ; i < G.edge; i++) { cin >> v1 >> v2 >> weight; i1 = Location(G, v1); i2 = Location(G, v2); if (i1 != -1 && i2 != -1 ) { EdgeNode* temp = new EdgeNode; temp->adjvex = i2; temp->next = G.adjlist[i1].first; temp->weight = weight; G.adjlist[i1].first = temp; } } } int min_weight = 0x7FFFFFFF ;int steps = 0 ;int path[Max_Size ] = { 0 };int shortest_path[Max_Size] = { 0 };void DFS (AdjListGraph& G,int start ,int end,int weights) { int cur = -1 ; if (start == end) { for (int i = 0 ; i < steps; i++) { cout << G.adjlist[path[i]].data << " " ; } cout << "该路径对应的长度是:" << weights << endl ; if (min_weight > weights) { min_weight = weights; memcpy (shortest_path, path, steps * sizeof (int )); } return ; } visited[start] = 1 ; EdgeNode* temp = G.adjlist[start].first; while (temp) { int weight = temp->weight; cur = temp->adjvex; if (!visited[cur]) { visited[cur] = 1 ; path[steps++] = cur; DFS(G, cur, end, weights+weight); visited[cur] = 0 ; path[--steps] = 0 ; } temp = temp->next; } } int main (void ) { AdjListGraph G; initGraph(G); createGraph(G); DFS(G, Location(G, 'A' ), Location(G, 'D' ), 0 ); cout << "成功得到最短路径为" << endl ; int i = 0 ; cout << "起点" ; while (shortest_path[i] > 0 && i < Max_Size) { cout << "->" << G.adjlist[shortest_path[i]].data ; i++; } cout << endl ; return 0 ; }
输入示例 :