#include <iostream>
#include <cstring>
#define MaxVertexNum 10
#define Maximum 9999
using namespace std;
typedef struct {
char Vex[MaxVertexNum];
int Edge[MaxVertexNum][MaxVertexNum];
int Weight[MaxVertexNum][MaxVertexNum];//权值
int vexnum,arcnum;
}MGraph;
typedef struct ArcNode{
char ch;
struct ArcNode *next;
}ArcNode;
typedef struct VNode{
char ch;
struct ArcNode *firstNode;
}VNode,AdjList[MaxVertexNum];
typedef struct {
AdjList vertices;
int vexnum,arcnum;
}ALGraph;
typedef struct StackNode{//栈节点
struct VNode *vNode;
struct StackNode *next;//指向栈中下一个节点
}StackNode,*Stack;
struct QueueNode{//队列节点
struct VNode *vNode;//指向树的节点
struct QueueNode *next;//指向队列中下一个节点
};
typedef struct LQueue{//队列首尾指针
QueueNode *front;
QueueNode *rear;
}*LinkedQueue;
void initQueue(LinkedQueue &Q){
Q=(LQueue*) malloc(sizeof (LQueue));
QueueNode *headNode = (QueueNode*) malloc(sizeof (QueueNode));//队列的头节点
Q->front = Q->rear = headNode;
headNode->next = nullptr;
/* -----------
* | headNode| --->NULL
* -----------
* ^ ^
* | |
* front rear
* ------------
* | Q |
* ------------
*/
}
void Enqueue(LinkedQueue &Q,VNode *vNode){
QueueNode *queueNode = (QueueNode*) malloc(sizeof (QueueNode));
queueNode->vNode = vNode;
queueNode->next = nullptr;
Q->rear->next = queueNode;
Q->rear = queueNode;
}
void DeQueue(LinkedQueue Q,VNode *&vNode){
if (Q->front==Q->rear)//Q 为空
return;
QueueNode *p = Q->front->next;
if (p == Q->rear){
Q->rear = Q->front;
Q->front->next = nullptr;
}else{
Q->front->next = p->next;
}
vNode = p->vNode;
free(p);
}
bool queueIsEmpty(LinkedQueue Q){
if (Q->front == Q->rear)
return true;
return false;
}
void getHeadQueue(LinkedQueue Q,VNode *&vNode){
if (queueIsEmpty(Q))
vNode= nullptr;
vNode = Q->front->next->vNode;
}
void freeQueue(LinkedQueue &Q){
VNode *node;
while (!queueIsEmpty(Q)){
DeQueue(Q,node);
}
free(Q->front);
free(Q);
}
void DFS(ALGraph alGraph, int v, bool visited[]);
void initStack(Stack &S){
if((S = (StackNode*) malloc(sizeof (StackNode))))
S->next = nullptr;
}
void push(Stack &S,VNode *vNode){
StackNode *stackNode = (StackNode*) malloc(sizeof (StackNode));
stackNode->vNode = vNode;
stackNode->next = S->next;
S->next = stackNode;
}
void pop(Stack &S,VNode *&vNode){
if (S->next != nullptr)
vNode = S->next->vNode;
StackNode *p = S->next;
S->next = S->next->next;
free(p);
}
bool stackIsEmpty(Stack S){
if (S->next == nullptr)
return true;
return false;
}
void getTopStack(Stack S,VNode *vNode){
if (stackIsEmpty(S))
vNode = nullptr;
vNode = S->next->vNode;
}
void freeStack(Stack &S){
VNode *vNode;
while (!stackIsEmpty(S)){
pop(S,vNode);
}
free(S);
}
void freeList(ALGraph &alGraph){
for (int i = 0; i < MaxVertexNum; ++i) {
if ([Link][i].firstNode){
ArcNode *arcNode = [Link][i].firstNode;
ArcNode *next = arcNode->next;
while (next){
free(arcNode);
arcNode = next;
next = next->next;
}
free(arcNode);
}
}
free([Link]);
}
void initialMatrix(MGraph &mGraph){
for (int i = 0; i < MaxVertexNum; ++i) {
for (int j = 0; j < MaxVertexNum; ++j) {
[Link][i][j] = 0;
[Link][i][j] = Maximum;
}
}
[Link] = [Link] =0;
memset([Link],'\0',sizeof(char)*MaxVertexNum);
}
void initialALGraph(ALGraph &alGraph){
[Link] = [Link] = 0;
for (int i = 0; i < MaxVertexNum; ++i) {
[Link][i].ch = '\0';
[Link][i].firstNode = nullptr;
}
}
//创建无向图
void createUndirectedGraph(MGraph &mGraph){
char ch,sh;
cout<<"创建无向图"<<endl;
cout<<"输入顶点(以#结束): "<<endl;
cin>>ch;
while (ch!='#'&&[Link]<MaxVertexNum){
[Link][[Link]++] = ch;
cin>>ch;
}
cout<<"输入边"<<endl;
int i =0,j =0;
while (true){
cin>>ch;
if (ch=='#')
break;
cin>>sh;
for (i = 0;; i++){
if ([Link][i]==ch)
break;
}
for (j = 0;; j++){
if ([Link][j]==sh)
break;
}
[Link][i][j] = 1;
[Link][j][i] = 1;
[Link]++;
cin>>ch;
}
}
//判断图 G 是否存在边(ch,sh)或<ch,sh>
bool Adjacent(MGraph mGraph,char ch,char sh){
int i,j;
for (i = 0;[Link][i]!=ch; ++i);
for (j = 0;[Link][j]!=sh; ++j);
if (i>[Link])
return false;
if ([Link][i][j]==1)
return true;
return false;
}
//列出图 G 中与结点 x 邻接的边
void Neighbors(MGraph &mGraph,char ch){
int i;
for (i = 0;[Link][i]!=ch; ++i);
for (int j = 0; j < [Link]; ++j) {
if ([Link][i][j]==1){
cout<<"("<<[Link][i]<<","<<[Link][j]<<"), ";
}
}
cout<<"#"<<endl;
}
void InsertVertex(MGraph &mGraph,char ch){
if ([Link] == MaxVertexNum)
return;
int j = [Link];
for (int i = 0; i < j; ++i) {//已经存在
if ([Link][i]=='N')
j++;
if ([Link][i]==ch)
return;
}
for (int i = 0; i < [Link]; ++i) {
if (ch=='#')
return;
if ([Link][i]=='N'){
[Link][i] = ch;
[Link]++;
return;
}
}
[Link][[Link]++] = ch;
}
void DeleteVertex(MGraph &mGraph,char ch){
int num = 0;
for (int i = 0; i < [Link]; ++i) {
if ([Link][i]==ch){
for (int j = 0; j < [Link]; ++j) {
if ([Link][i][j]==1){
num++;
}
[Link][i][j]=0;
}
for (int k = 0; k < [Link]; ++k) {
[Link][k][i]=0;
}
[Link][i] = 'N';
[Link]--;
[Link] = [Link] - num;
break;
}
}
}
//若图 G 中不存在边(x,y)则添加
void AddEdge(MGraph &mGraph,char x,char y){
int i,j;
for (i = 0;[Link][i]!=x; ++i);
for (j = 0;[Link][j]!=y; ++j);
if (i>[Link])
return;
[Link][i][j] = 1;
[Link][j][i] = 1;
[Link]++;
}
//若图 G 中存在边(x,y)则删除
void RemoveEdge(MGraph &mGraph,char x,char y){
int i,j;
for (i = 0;[Link][i]!=x; ++i);
for (j = 0;[Link][j]!=y; ++j);
if (i>[Link])
return;
if ([Link][i][j]==1){
[Link][i][j] = 0;
[Link][j][i] = 0;
[Link]--;
}
}
//求图 G 中顶点 x 的第一个临接点,若有返回顶点号,没有返回-1
int FirstNeighbor(MGraph mGraph,char x){
int i;
for (i = 0;[Link][i]!=x; ++i);
for (int j = 0; j < [Link]; ++j) {
if ([Link][i][j]==1)
return j;
}
return -1;
}
VNode *(FirstNeighbor)(ALGraph alGraph,VNode *vNode){
int i=0;
for(i;[Link][i].ch!=vNode->ch;i++);
if ([Link][i].firstNode== nullptr)
return nullptr;
int j = 0;
for (j; [Link][j].ch!= [Link][i].firstNode->ch; ++j);
VNode *node = &[Link][j];
return node;
}
VNode *(NextNeighbor)(ALGraph alGraph,VNode *vNode1,VNode *vNode2){
int i = 0;
for(i;[Link][i].ch!=vNode1->ch;i++);
int j = 0;
ArcNode *p = [Link][i].firstNode;
while(p){
if (p->ch==vNode2->ch){
p = p->next;
break;
}
p = p->next;
}
if (!p)
return nullptr;
int k = 0;
for(k;[Link][k].ch!=p->ch;k++);
VNode *node = &[Link][k];
return node;
}
int NextNeighbor(MGraph mGraph,char x,char y){
int i,j;
for (i = 0;[Link][i]!=x; ++i);
for (j = 0;[Link][j]!=y; ++j);
for (j = j+1; j < [Link]; ++j) {
if ([Link][i][j]==1)
return j;
}
return -1;
}
//邻接矩阵转邻接表
void MatrixTransformToList(MGraph mGraph,ALGraph &alGraph){
[Link] = [Link];
[Link] = [Link];
for (int i = 0; i < [Link]; ++i) {
[Link][i].ch = [Link][i];
}
for (int i = [Link]-1; i >= 0 ; --i) {
for (int j = [Link]-1; j >= 0 ; --j) {
if ([Link][i][j]){
ArcNode *arcNode = (ArcNode*) malloc(sizeof (ArcNode));
arcNode->ch = [Link][j];
arcNode->next = [Link][i].firstNode;
[Link][i].firstNode = arcNode;
/*
* VNode &vNode = [Link][i];
* arcNode->next = [Link];
* [Link] = arcNode;
*/
}
}
}
}
//P206T4 邻接表转矩阵
void ListTransformToMatrix(ALGraph alGraph,MGraph &mGraph){
for (int i = 0; i < [Link]; ++i) {
[Link][i] = [Link][i].ch;
}
[Link] = [Link];
[Link] = [Link];
ArcNode *arcNode;
for (int i = 0; i < [Link]; ++i) {
if ((arcNode = [Link][i].firstNode) == nullptr)
continue;
while (arcNode!= nullptr){
char ch = arcNode->ch;
int j = 0;
for(;[Link][j]!=ch;j++);
[Link][i][j] = 1;
arcNode = arcNode->next;
}
}
}
void DFS(ALGraph alGraph,int i,bool visited[]){
cout<<[Link][i].ch<<" ";
visited[i] = true;
VNode *vNode;
for (vNode = FirstNeighbor(alGraph,&[Link][i]); vNode!= nullptr ;
vNode = NextNeighbor(alGraph,&[Link][i],vNode)) {
int j = 0;
for (j;[Link][j].ch!=vNode->ch; j++);
if (!visited[j])
DFS(alGraph,j,visited);
}
}
//邻接表存储深度优先遍历递归
void DFSTraverse(ALGraph alGraph){
bool visited[MaxVertexNum];
memset(visited, false,sizeof (bool )*[Link]);
cout<<"T 的深度优先遍历序列: ";
for(int i=0;i<[Link];i++) {
if (!visited[i])
DFS(alGraph,i,visited);
}
cout<<endl;
}
void printMatrix(MGraph mGraph){
cout<<"T 的邻接矩阵"<<endl;
cout<<" ";
for (int i = 0; i < [Link]; ++i) {
cout<<" "<<[Link][i];
}
cout<<endl;
for (int i = 0; i < [Link]; ++i) {
cout<<[Link][i]<<" ";
for (int j = 0; j < [Link]; ++j) {
cout<<[Link][i][j]<<" ";
}
cout<<endl;
}
}
//邻接表存储深度优先遍历非递归
void DFSFor(ALGraph alGraph,int i,bool visited[]){
Stack S;
initStack(S);
visited[i] = true;
push(S,&[Link][i]);
VNode *vNode,*vNodes;
while(!stackIsEmpty(S)){
pop(S, vNode);
cout<<vNode->ch<<" ";
for (vNodes = FirstNeighbor(alGraph,vNode); vNodes!= nullptr ; vNodes =
NextNeighbor(alGraph,vNode,vNodes)) {
int j = 0;
for (j;[Link][j].ch!=vNodes->ch; j++);
if (!visited[j]){
push(S,&[Link][j]);
visited[j] = true;
}
}
}
freeStack(S);
}
void DFSTraverseFor(ALGraph alGraph){
bool visited[MaxVertexNum];
memset(visited, false,sizeof (bool )*[Link]);
cout<<"T 的深度优先遍历序列: ";
for(int i=0;i<[Link];i++) {
if (!visited[i])
DFSFor(alGraph,i,visited);
}
cout<<endl;
}
void printList(ALGraph alGraph){
cout<<"T 的邻接表: "<<endl;
for (int i = 0; i < [Link]; ++i) {
VNode vNode = [Link][i];
cout<<[Link];
while ([Link]){
ArcNode *arcNode = [Link];
while (arcNode){
cout<<" -> "<<arcNode->ch;
arcNode = arcNode->next;
}
cout<<endl;
break;
}
}
}
//图的广度优先遍历
void BFS(ALGraph alGraph,int i,bool visited[]){
cout<<[Link][i].ch<<" ";
visited[i] = true;
LinkedQueue Q;
initQueue(Q);
Enqueue(Q,&[Link][i]);
VNode *vNode,*vNode1;
while (!queueIsEmpty(Q)){
DeQueue(Q,vNode);
for(vNode1 = FirstNeighbor(alGraph,vNode);vNode1!= nullptr;vNode1 =
NextNeighbor(alGraph,vNode,vNode1)){
int j = 0;
for (j; [Link][j].ch!=vNode1->ch;j++);
if (!visited[j]){
cout<<vNode1->ch<<" ";
visited[j] = true;
Enqueue(Q,vNode1);
}
}
}
freeQueue(Q);
}
void BFSTraverse(ALGraph alGraph){
bool visited[MaxVertexNum];
memset(visited, false,sizeof (bool )*MaxVertexNum);
cout<<"T 的广度优先遍历序列: ";
for(int i=0;i<[Link];i++){
if (!visited[i])
BFS(alGraph,i,visited);
}
cout<<endl;
}
void initialArrs(MGraph mGraph,char start,bool final[],int dist[],int path[]){
memset(final, false,sizeof (bool )*[Link]);
int i = 0;
for(i;[Link][i]!=start;i++);
final[i] = true;
int j = 0;
for(j;j<[Link];j++){
dist[j] = [Link][i][j];//路径 error
}
dist[i] = 0;
memset(path,-1,sizeof (int )*[Link]);
for (int k = 0; k < [Link]; ++k) {
if ([Link][i][k]!=0){
path[k] = i;
}
}
}
void Dijkstra(MGraph mGraph,char start){
bool final[[Link]];
int dist[[Link]];
int path[[Link]];
initialArrs(mGraph,start,final,dist,path);
int certainV= 1;
while (certainV<[Link]){
int min = -1,index = -1;
for (int j = 0; j < [Link]; ++j) {
if (!final[j]){
if (min == -1){
min = dist[j];
index = j;
} else if(dist[j]<min){
min = dist[j];
index = j;
}
}
}
final[index] = true;
certainV++;
for (int k = 0; k < [Link]; ++k) {
if (!final[k] && Adjacent(mGraph, [Link][index], [Link][k])){
if ([Link][index][k] + dist[index] < dist [k]){
dist[k] = [Link][index][k] + dist[index];
path[k] = index;
}
}
}
}
for (int i = 0; i < [Link]; ++i) {
cout<<final[i]<<" ";
}
cout<<endl;
for (int i = 0; i < [Link]; ++i) {
cout<<dist[i]<<" ";
}
cout<<endl;
for (int i = 0; i < [Link]; ++i) {
cout<<path[i]<<" ";
}
cout<<endl;
}
void createByHand(MGraph &mGraph){
[Link] = 5;
[Link] = 6;
[Link][0] = 'a';
[Link][1] = 'b';
[Link][2] = 'c';
[Link][3] = 'd';
[Link][4] = 'e';
//ac,ad,bd,be,ce,de,#
[Link][0][2] = 1;
[Link][0][3] = 1;
[Link][1][3] = 1;
[Link][1][4] = 1;
[Link][2][4] = 1;
[Link][3][4] = 1;
[Link][2][0] = 1;
[Link][3][0] = 1;
[Link][3][1] = 1;
[Link][4][1] = 1;
[Link][4][2] = 1;
[Link][4][3] = 1;
[Link][0][2] = 5;
[Link][0][3] = 4;
[Link][1][3] = 2;
[Link][1][4] = 3;
[Link][2][4] = 7;
[Link][3][4] = 6;
[Link][2][0] = 5;
[Link][3][0] = 4;
[Link][3][1] = 2;
[Link][4][1] = 3;
[Link][4][2] = 7;
[Link][4][3] = 6;
}
int main() {
MGraph mGraph;
initialMatrix(mGraph);
//abcde#
//ab,ac,ae,bc,bd,ce,de,#
// createUndirectedGraph(mGraph);
createByHand(mGraph);
// printMatrix(mGraph);
ALGraph alGraph;
initialALGraph(alGraph);
MatrixTransformToList(mGraph,alGraph);
printList(alGraph);
// cout<<Adjacent(mGraph,'a','c')<<endl;
// cout<<Adjacent(mGraph,'a','b')<<endl;
// Neighbors(mGraph,'a');
// Neighbors(mGraph,'e');
// printMatrix(mGraph);
// cout<<FirstNeighbor(mGraph,'b')<<endl;
// cout<<NextNeighbor(mGraph,'b','d')<<endl;
DFSTraverse(alGraph);
DFSTraverseFor(alGraph);
BFSTraverse(alGraph);
Dijkstra(mGraph,'a');
freeList(alGraph);
return 0;
}