Graph theory algorithm: DFS finds all paths between two points in a directed or undirected graph and Dijkstra algorithm finds the shortest path

1. Purpose

  1) Obtain all paths from the specified start and end points according to the directed graph; 2) Directly solve the shortest path between two points.

2. Example effect

2.1 Raw data

insert image description here
The starting and ending points of the route are arranged as follows:

// 共计12个顶点,19条边。 (起点,终点,1)最后的1代表起点终点是连通的。
起点,终点,12 4 1
起点,终点,19 10 1
起点,终点,18 11 1
起点,终点,14 12 1
起点,终点,111 12 1
起点,终点,11 2 1
起点,终点,13 2 1
起点,终点,11 3 1
起点,终点,13 4 1
起点,终点,13 6 1
起点,终点,11 5 1
起点,终点,16 5 1
起点,终点,16 7 1
起点,终点,16 9 1
起点,终点,17 9 1
起点,终点,19 10 1
起点,终点,15 8 1
起点,终点,18 7 1
起点,终点,110 11 1

2.1 Program calculation effect

Enter start and end points: 1 and 12.
insert image description here

3. Source code

// GraphDFS.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。

#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
#include<stdio.h>
#include<math.h>
using namespace std;

int mapMatrix[1000][1000] = {
   
    
     0 };///map[i][j]为0表示i, j两点之间不通,为1表示有一条路
int stack[200], markV[500] = {
   
    
     0 };
int nTop = 0;

const

Guess you like

Origin blog.csdn.net/m0_37251750/article/details/131529880