idk why these stuffs get stashed for so long and I didn't ever commit them

This commit is contained in:
2025-11-06 09:43:54 +08:00
parent 5dbdfef1a1
commit e01c041259
232 changed files with 22806 additions and 1256 deletions

View File

@@ -0,0 +1,112 @@
template <class DT>
struct QNode //结点
{
DT data; //数据域,存储数据元素值
QNode *next;//指针域,指向下一个结点
};
template<class DT>
struct LinkQueue
{
QNode<DT> * front;
QNode<DT> * rear;
};
//【算法3.19】
template <class DT>
void InitQueue(LinkQueue<DT> &Q)//创建空队列
{
Q.front=new QNode<DT>; //创建头结点
if(!Q.front) exit(1); //创建失败,结束运行
Q.front->next=NULL;
Q.rear=Q.front;
}
//【算法3.20】
template <class DT>
void DestroyQueue(LinkQueue<DT> &Q)//释放链队
{
QNode<DT> *p;
while(Q.front)//从头结点开始,依次释放结点
{
p=Q.front;
Q.front=Q.front->next;
delete p;
}
}
//【算法3.21】 入队
template<class DT>
bool EnQueue(LinkQueue<DT> &Q,DT e)
{
QNode<DT> *p;
p=new QNode<DT>; // 创建新结点
if(!p) return false; // 创建失败,结束运行
p->data=e; // 新结点赋值
p->next=NULL; // 链在队尾
Q.rear->next=p;
Q.rear=p;
return true; // 入队成功返回true
}
//【算法3.22】 出队
template<class DT>
bool DeQueue(LinkQueue<DT> &Q,DT &e)
{
QNode<DT> *p;
if(Q.front==Q.rear) return false; //队空返回false
p=Q.front->next; // 取出队元素
e=p->data;
Q.front->next=p->next; //队首元素出队
if(Q.rear==p) //只有一个元素时出队,
Q.rear=Q.front; // 修改队尾
delete p;
return true; // 出队成功返回true
}
//【算法3.23】 取队头元素
template<class DT>
bool GetHead(LinkQueue<DT> Q,DT &e)
{
if(Q.front==Q.rear) return false; // 队空返回false
e=Q.front->next->data;
return true; // 删除成功返回true
}
//取队尾元素
template<class DT>
bool GetTail(LinkQueue<DT> Q,DT &e)
{
if(Q.front==Q.rear) // 队空
return false; // 返回false
e=Q.rear->data; // 获取队尾元素
return true; // 返回true
}
//测队空
template<class DT>
bool QueueEmpty(LinkQueue<DT> Q)
{
if(Q.front==Q.rear) // 队空
return true; //返回true
else //非空
return false; //返回false
}
//显示队列内容
template<class DT>
void DispQueue(LinkQueue<DT> Q)
{
QNode<DT> *p;
p=Q.front->next;
while(p)
{
cout<<p->data<<"\t";
p=p->next;
}
cout<<endl;
}

View File

@@ -0,0 +1,308 @@
/*--------------------------有向网的邻接矩阵表示-----------------------------*/
#define MAX_VEXNUM 20 // 最大顶点数
#define INF 1000 // 无穷大
template <class DT>
struct MGraph // 图的邻接矩阵表示存储定义
{
DT vexs[MAX_VEXNUM]; // 顶点信息
int arcs[MAX_VEXNUM][MAX_VEXNUM];
int vexnum,arcnum; // 顶点数和弧数
};
template <class DT>
void DispG(MGraph<DT> G) // 显示图信息
{
int i,j;
DT u,v;
cout<<G.vexnum<<"个顶点:"<<endl;
for(i=0;i<G.vexnum;i++)
cout<<G.vexs[i]<<" ";
cout<<endl;
cout<<G.arcnum<<"条弧信息如下:"<<endl;
for(i=0;i<G.vexnum;i++)
{
for(j=0;j<G.vexnum;j++)
if (G.arcs[i][j]!=INF)
{
GetVex(G,i,u);
GetVex(G,j,v);
cout<<'('<<u<<","<<v<<"):"<<G.arcs[i][j]<<" ";
}
}
cout<<endl;
}
//算法6.1 顶点定位
template <class DT>
int LocateVex(MGraph<DT> G,DT v)
{
for(int i = 0;i<G.vexnum;i++)
{
if(G.vexs[i] == v)
{
return i;
}
}
return -1;
}
//6.2 创建无向图
template <class DT>
void CreateDN(MGraph<DT> &G)
{
int i,j,k,weight;
DT v1,v2;
cout<<"请输入有向网的顶点数 "; // 1. 输入顶点数、弧数
cin>>G.vexnum ;
cout<<"请输入有向网的弧数 ";
cin>>G.arcnum ;
cout<<"请输入"<<G.vexnum<<"个顶点的值(单个字符)"<<endl; // 2. 输入顶点值
for(i = 0;i<G.vexnum;i++)
cin>>G.vexs[i];
for(i=0;i<G.vexnum;i++) // 3.邻接矩阵初始化
for(j=0;j<G.vexnum;j++)
G.arcs[i][j]=INF;
cout<<"请输入各条弧的两个邻接点"<<endl; // 4.创建各条弧
for( k=0;k<G.arcnum;k++)
{
cout<<"输入第"<<k<<"条弧的两个顶点和权值:"<<endl;
cin>>v1>>v2; // 4.1 输入弧的两个邻接点和弧权值
cin>>weight;
i = LocateVex(G,v1); // 4.2 定位两个邻接点
j = LocateVex(G,v2);
if(i<0 || j<0 || i==j)
{
cout<<"顶点信息错,重新输入!"<<endl;
k--;
continue;
}
G.arcs[i][j]=weight; // 4.3 修改邻接矩阵
}
}
template <class DT>
bool GetVex(MGraph<DT> G, int k, DT &v) // 获取第 u 个顶点值v
{
if(k<0 || k>=G.vexnum) // u不存在返回false
return false;
else
{
v=G.vexs[k];
return true;
}
}
template <class DT>
bool PutVex(MGraph<DT> &G,DT &u,DT v) // 为第u个顶点赋值
{
int k;
k=LocateVex(G,u);
if(k<0 ) // u不存在返回false
return false;
else // u存在赋值
{
G.vexs[k] = v;
return true;
}
}
//算法6.3 按值查找第一邻接点
template <class DT>
int FirstAdjvex(MGraph<DT> G,int u)
{
if(u<0 || u>=G.vexnum) // 顶点不存在
return -1; // 无邻接点,返回-1
for(int j=0;j<G.vexnum;j++) // 扫描邻接矩阵第u行
if(G.arcs[u][j]!=INF) // 如果有非零元,
return j; // 第一个非零元所在列号,为其邻接点序号
return -1; // 否则,无邻接点,返回-1
}
template <class DT>
int NextAdjvex(MGraph<DT> G,int u,int w) //查找第u个顶点邻接点W的下一个邻接点
{
if(u<0 || u>=G.vexnum || w<0
|| w>=G.vexnum || G.arcs[u][w]==INF ) // 参数不合理
return -1; // 无邻接点
for(int j=w+1;j<G.vexnum;j++) // 扫描邻接矩阵第u行w列后的元素
if(G.arcs[u][j]!=INF) // 如果有非零元,
return j; // 第一个非零元所在列号,为其邻接点序号
return -1; // 否则无邻接点,返回-1
}
template <class DT>
bool InsertVex(MGraph<DT> &G,DT v) // 插入值为v的顶点
{
DT w;
int j,weight;
char ans;
if(G.vexnum>=MAX_VEXNUM) // 无存储空间,不能插入
{
cout<<"无存储空间,不能插入!"<<endl;
return false;
}
G.vexs[G.vexnum++]= v; // 顶点信息加入至G.vexs中顶点数增1
for(j=0;j<G.vexnum;j++) // 初始化邻接矩阵最后一行和最后一列值
{
G.arcs[G.vexnum-1][j]=INF;
G.arcs[j][G.vexnum-1]=INF;
}
cout<<"创建弧吗Y/N)?"<<endl;
cin>>ans;
while(ans=='Y'|| ans=='y')
{
cout<<"输入另一个顶点值和弧的权值"<<endl;
cin>>w>>weight;
j=LocateVex(G,w);
if(j>=0) // 顶点存在
InsertArc(G,v,w,weight);
else
cout<<w<<"顶点不存在!";
cout<<"继续创建弧吗Y/N)?"<<endl;
cin>>ans;
};
return true;
}
template <class DT>
bool InsertArc(MGraph<DT> &G,DT v,DT w,int weight) // 在值为v、w顶点间加弧
{ int i = LocateVex(G,v);
int j = LocateVex(G,w);
if(i<0 || j<0 || i==j) // 顶点不存在或两端点相同
{
cout<<"\n顶点不存在或边已存在,不能插入!"<<endl;
return false; // 不能插入弧返回false
}
if(G.arcs[i][j]!=INF) // 弧已存在,不能插入
{
cout<<"\n弧已存在,不能插入!"<<endl;
return false;
}
G.arcs[i][j]=weight;
G.arcnum++;
return true;
}
template <class DT>
bool DeleteArc(MGraph<DT> &G,DT v,DT w) // 按顶点值删除弧
{
int i = LocateVex(G,v);
int j = LocateVex(G,w);
if(i<0||j<0||i == j) // 弧不存在返回false
{
cout<<"弧不存在!"<<endl;
return false;
}
G.arcs[i][j]=INF; // 置邻接矩阵第 i 行第 j 列为零
G.arcnum--; // 弧数减1
return true;
}
template <class DT>
bool DeleteVex(MGraph<DT> &G,DT v) // 按值删除顶点
{
int i,j;
DT w;
i = LocateVex(G,v); // 顶点定位
if(i<0)
{
cout<<"顶点不存在!"<<endl; // 顶点不存在
return false;
}
for(j=0;j<G.vexnum;j++) // 删除与顶点v相连的弧
{
if(G.arcs[i][j]!=INF)
{
GetVex(G,j,w);
DeleteArc(G,v,w);
}
}
for(j=i+1;j<G.vexnum;j++) // 排在顶点v后面的顶点前移
{
G.vexs[j-1] = G.vexs[j];
}
G.vexnum--;
return true;
}
// 算法6.10
template <class DT>
void DFS2(MGraph<DT> G,int v) // 连通图深度优先遍历
{
int w;
visited[v] = true; // 先访问index
cout<<G.vexs[v];
for(w=0;w<G.vexnum;w++)
{
if(G.arcs[v][w]!=INF && !visited[w] )
DFS2(G,w);
}
//cout<<endl;
}
// 算法6.9
template <class DT>
void DFSTraverse(MGraph<DT> G) // 非连通图深度优先遍历
{
int i;
for(i=0;i<G.vexnum;i++) // 访问标志初始化
visited[i]=0;
for( i=0;i<G.vexnum;i++) // 对未被访问的顶点
{
if(!visited[i])
DFS2(G,i); // 进行深度优先递归
}
cout<<endl;
return ;
}
// 算法 6.11 广度优先遍历
template <class DT>
void BFS(MGraph<DT> G,int v)
{
int w;
LinkQueue<int> Q; // 创建一个队列
InitQueue(Q);
cout<<G.vexs[v]; // 访问顶点v
visited[v]=true; // 做访问标志
EnQueue(Q,v); // 入队
while(!QueueEmpty(Q)) // 队非空
{
DeQueue(Q,v); // 出队
for(w=FirstAdjvex(G,v);w>=0;w=NextAdjvex(G,v,w)) // 遍历v的邻接点
if(!visited[w]) // 未被访问
{
cout<<G.vexs[w]; // 访问
visited[w]=true; // 做访问标志
EnQueue(Q,w); // 入队
}
}
}
// 算法6.12
template <class DT>
void BFSTraverse(MGraph<DT> G) // 广度优先遍历
{
int i;
for(i=0;i<G.vexnum;i++) // 访问标志初始化
visited[i]=0;
for(i=0;i<G.vexnum;i++) // 对未被访问的结点
{
if(!visited[i])
BFS(G,i); // 进行BFS遍历
}
}

View File

@@ -0,0 +1,211 @@
#include<string>
#include "LinkQueue.h"
#include "Mgraph.h"
#include <iostream>
using namespace std;
const MAX_ARCNUM=50;
//算法6.20 Dijkstra算法
template<class DT>
void ShortestPath_DIJ(MGraph<DT> G, int v0) // 从顶点v开始计算的最小生成树
{
int v,i,w,min;
bool S[MAX_VEXNUM]={false}; // 记载S集合中的顶点
int D[MAX_VEXNUM]; // 源点到其它各顶点的距离
int P[MAX_VEXNUM]={-1}; // 源点互其它各顶点的路径信息
for(v=0;v<G.vexnum;v++) // 初始化
{
S[v]=false; //
D[v]=G.arcs[v0][v];
if(D[v]<INF) // 源点可直达顶点的距离
P[v]=v0;
else
P[v]=-1;
}
D[v0]=0; // 源点到自身的距离为0
S[v0]=true;
for(i=1;i<G.vexnum;i++) // 求n-1条最短路径
{
min=INF;
for(w=0;w<G.vexnum;++w) // 在源点到未选目的地顶点距离中选路径最小的顶点
if(!S[w] && D[w]<min)
{
v=w; min=D[w];
}
S[v]=true; // 加入S
for(w=0;w<G.vexnum;++w) // 考量经过新求出最短路径有否比原先路径短的
if(!S[w]&&(D[v]+G.arcs[v][w]<D[w])) // 如果有
{
D[w]=D[v]+G.arcs[v][w]; // 更新D[]值
P[w]=v; // 更新P[]值
}
}
cout<<G.vexs[v0]<<"到其余各顶点的距离为:"<<endl; // 输入D[]
for(w=0;w<G.vexnum;w++)
cout<<D[w]<<" ";
cout<<"\n路径距径P为"<<endl; // 输出P[]
for(w=0;w<G.vexnum;w++)
cout<<P[w]<<" ";
cout<<endl;
}
// 算法6.21 Floyd算法
template <class DT>
void ShortestPath_Floyd(MGraph<DT> G) // 返回生成代价
{
int k,i,j;
int D[MAX_VEXNUM][MAX_VEXNUM]; // D[i][j]表示顶点i和顶点j的最短距离
int P[MAX_VEXNUM][MAX_VEXNUM]; // 记载最短距离的路径信息
for(i=0;i<G.vexnum;i++) // 初始化D[][]
for(j=0;j<G.vexnum;j++)
{
// 初始化P[][]
//if(i==j)
//D[i][j]=0;
if(D[i][j]<INF && i!=j) // 顶点 i 和顶点 j 之间存在路径
P[i][j]=i;
else P[i][j]=-1; // 顶点 i 和顶点 j 之间不存在路径
}
cout<<"\nD-1和P-1:"<<endl; // 显示D、P
DispPath_Floyd(D,G.vexnum,P);
for(k=0;k<G.vexnum;k++) //以k为中间点对所有顶点对{i,j}进行检测
{
for(i=0;i<G.vexnum;i++)
{
for(j=0;j<G.vexnum;j++)
{
if(i!=j && D[i][k]+D[k][j]<D[i][j]) // 如果满足修改条件
{
D[i][j]=D[i][k]+D[k][j]; // 修改D[i][j]
P[i][j]=P[k][j]; // 修改路径
}
}
}
cout<<"\n"<<k+1<<"次替代后的D和P"<<endl;
DispPath_Floyd(D,G.vexnum,P);
}
}
void DispPath_Floyd(int D[][MAX_VEXNUM],int n,int P[][MAX_VEXNUM]) // 距离信息和路径信息显示
{
int i,j;
cout<<"\nD:"<<endl;
for(i=0;i<n;i++) // 输出第一趟D[]
{
for(j=0;j<n;j++)
cout<<D[i][j]<<'\t';
cout<<endl;
}
cout<<"\nP:"<<endl;
for(i=0;i<n;i++) // 输出第一趟D[]
{
for(j=0;j<n;j++)
cout<<P[i][j]<<'\t';
cout<<endl;
}
}
void DispMenu()
{
cout<<"\n如果不创建图,可使用测试图!"<<endl;
cout<<"\n 请选择你要的操作"<<endl;
cout<<" 1. " <<endl;
cout<<" 2. 建立有向网Dijkstra算法" <<endl;
cout<<" 3. Floyd算法" << endl;
cout<<" 4. 显示网"<< endl;
cout<<" 0. 退出"<< endl;
}
bool visited[MAX_VEXNUM]={false};
void main()
{
int i,j,v0;
char w;
MGraph<char> G,G1,G2;
G1.vexnum=5; // Dijkstra算法测试图
G1.arcnum=7;
G1.vexs[0]='A';G1.vexs[1]='B';G1.vexs[2]='C';
G1.vexs[3]='D';G1.vexs[4]='E';
for(i=0;i<G1.vexnum;i++) // 邻接矩阵初始化
for(j=0;j<G1.vexnum;j++)
G1.arcs[i][j]=INF;
G1.arcs[0][1]=10;
G1.arcs[0][3]=50;
G1.arcs[0][4]=45;
G1.arcs[1][4]=30;
G1.arcs[3][4]=15;
G1.arcs[1][2]=4;
G1.arcs[2][4]=11;
cout<<"\n测试图为G1"<<endl;
DispG(G1);
G2.vexnum=3; // Floyd算法测试图
G2.arcnum=5;
G2.vexs[0]='A';G2.vexs[1]='B';G2.vexs[2]='C';
for(i=0;i<G2.vexnum;i++) // 邻接矩阵初始化
for(j=0;j<G2.vexnum;j++)
G2.arcs[i][j]=INF;
G2.arcs[0][1]=4;
G2.arcs[1][0]=6;
G2.arcs[0][2]=11;
G2.arcs[2][0]=3;
G2.arcs[1][2]=2;
cout<<"\n测试图为G2"<<endl;
DispG(G2);
bool f=false; // 是否创建网,缺省为未创建
int choice;
do
{
DispMenu();
cin>>choice;
switch(choice)
{
case 1: // 创建无向网
f=true;
CreateDN(G);
cout<<endl;
cout<<"创建的有向网为:"<<endl;
DispG(G);
break;
case 2: // Dijkstra算法
cout<<"请输入起始计算顶点: ";
cin>>w;
v0=LocateVex(G1,w);
if(v0==-1)
cout<<"顶点不存在!"<<endl;
else
{
if(!f) // 使用测试图
ShortestPath_DIJ(G1,v0);
else
ShortestPath_DIJ(G,v0);
}
break;
case 3: // Floyd算法
if(!f) // 使用测试图
ShortestPath_Floyd(G2);
else
ShortestPath_Floyd(G);
cout<<endl;
break;
case 4: // 显示网
DispG(G);
cout<<endl;
break;
case 0:
cout<<"结束运行Bye-Bye!"<<endl;
break;
default:
cout<<"无效选择,请重选!"<<endl;
}//case
}while(choice!=0);
}//main