The Dijkstra algorithm and BFS and DFS algorithms are implemented based on the adjacent matrix.
// ================================================ ==================================================================== // Name: matrixUDG. cpp // Author: fffff // Version: // Copyright: Your copyright notice // Description: Hello World in C ++, ansi-style // ========================================== ========================================================== = # include <iostream> # include <cstdlib> # include <queue> # define maxsize 100 # define far 10000 using namespace std; clas S MatrixUDG {public: char mVerxs [maxsize]; int mVerNum; int mEdgeNum; public: int mMatrix [maxsize] [maxsize]; MatrixUDG (); MatrixUDG (char vexs [], int vlen, char edge [] [2], int elen );~ MatrixUDG (); void print (); int getPosition (char ch); char getChar (int place); char readChar () ;}; MatrixUDG: MatrixUDG (char vexs [], int vlen, char edge [] [2], int elen) {this-> mVerNum = vlen; this-> mEdgeNum = elen; for (int I = 0; I <mVerNum; I ++) for (int j = 0; j <mVerNum; j ++) mMatrix [I] [j] = far; for (int I = 0; I <mVerNum; I ++) {mVerxs [I] = vexs [I] ;}for (int I = 0; I <mEdgeNum; I ++) {int start = getPosition (edge [I] [0]); Int end = getPosition (edge [I] [1]); int weight; cout <"input weight of (" <edge [I] [0] <", "<edge [I] [1] <"): "; cin> weight; mMatrix [start] [end] = weight; mMatrix [end] [start] = weight ;}; MatrixUDG: MatrixUDG () {cout <"input num of verx and edge :"; cin> mVerNum> mEdgeNum; cout <"input verxs:"; for (int I = 0; I <mVerNum; I ++) {mVerxs [I] = readChar () ;}for (int I = 0; I <mEdgeNum; I ++) {cout <"edge (" <I <"):"; c Har startChar = readChar (); char endChar = readChar (); int start = getPosition (startChar); int end = getPosition (endChar); int weight; cout <"input weight of (" <startChar <"," <endChar <"):"; cin> weight; mMatrix [start] [end] = weight; mMatrix [end] [start] = weight ;}}; MatrixUDG ::~ MatrixUDG () {}; void MatrixUDG: print () {for (int I = 0; I <mVerNum; I ++) {for (int j = 0; j <mVerNum; j ++) {cout <mMatrix [I] [j] <";}cout <endl ;}} int MatrixUDG :: getPosition (char ch) {for (int I = 0; I <mVerNum; I ++) {if (mVerxs [I] = ch) return I ;} return-1 ;}; char MatrixUDG: getChar (int place) {return mVerxs [place] ;}; char MatrixUDG: readChar () {char cha; cin >> cha; while (! (Cha> = 'A' & cha <= 'Z') | (cha> = 'A' & cha <= 'Z '))) cin> cha; return cha ;}; void Dijkstra (int n, int v, int * dist, int * pre, int weight [] [maxsize]) {/** Step 1: Initialize s [n], dist, pre */bool s [n]; for (int I = 0; I <n; I ++) {dist [I] = weight [v] [I]; s [I] = false; if (dist [I] = far) pre [I] =-1; else pre [I] = v;} s [v] = true; dist [v] = 0;/** Step 2: find the smallest vertex of dist and add it to s */for (int I = 1; I <n; I ++) {int temp = far; int u = v; for (int j = 0; j <n; j ++) {if (! S [j] & dist [j] <temp) {temp = dist [j]; u = j ;}} s [u] = true;/** Step 3: update dist */for (int j = 0; j <n; j ++) {if (! S [j] & weight [u] [j] <far) {int newdist = dist [u] + weight [u] [j]; if (newdist <dist [j]) {dist [j] = newdist; pre [j] = u ;}}} void printTrace (MatrixUDG * PG, int * pre, int v, int u) {int start = v; int end = u; char cha; while (end! = Start) {cha = PG-> getChar (end); cout <cha <"<--"; end = pre [end];} cout <PG-> getChar (start) <endl;} bool visited [maxsize]; void Dfsk (MatrixUDG * PG, int k) {visited [k] = true; cout <PG-> getChar (k) <"; for (int I = 0; I <PG-> mVerNum; I ++) if (PG-> mMatrix [k] [I]! = Far & visited [I] = false) Dfsk (PG, I);} void Dfs (MatrixUDG * PG) {for (int I = 0; I <PG-> mVerNum; I ++) if (! Visited [I]) Dfsk (PG, I); cout <endl;} void Bfs (MatrixUDG * PG) {queue <int> que; que. push (0); int place; while (! Que. empty () {place = que. front (); que. pop (); visited [place] = true; cout <PG-> getChar (place) <""; for (int I = 0; I <PG-> mVerNum; I ++) {if (PG-> mMatrix [place] [I]! = Far & visited [I] = false) {que. push (I);/** visited [I] = true was dropped at the beginning, leading to an error * because the vertex in the queue may not come out yet, after entering the queue again * Therefore, you must set it to accessed after entering the queue to prevent repeated access due to re-entry */visited [I] = true ;}} }} int main () {char verx [] = {'A', 'B', 'C', 'D', 'E', 'F', 'G '}; char edge [] [2] = {'A', 'C'}, {'A', 'E'}, {'B', 'D '}, {'B', 'E'}, {'B', 'G'}, {'C', 'F'}, {'D', 'F '}, {'E', 'G'}, {'F', 'G'}; int vlen = sizeof (verx)/sizeof (verx [0]); int elen = sizeof (edge)/sizeof (edge [0]); MatrixUDG * PG = new MatrixUDG (verx, vlen, edge, elen); int dist [vlen]; int pre [vlen]; int startVex = 4; Dijkstra (vlen, startVex, dist, pre, PG-> mMatrix); PG-> print (); printTrace (PG, pre, 4, 5); for (int I = 0; I <maxsize; I ++) {visited [I] = false;} Dfs (PG); for (int I = 0; I <maxsize; I ++) {visited [I] = false;} Bfs (PG); return 0 ;}