#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define MAXSIZE 100
int cnt = 0, cnt2 = 0;
int visited[MAXSIZE] = { 0 };
int visited2[MAXSIZE] = { 0 };
//表结点
typedef struct ArcNode {
int adjvex; //该结点对应图上顶点在顶点数组中的位置
struct ArcNode* nextarc; //指向这个结点下一个结点的指针
}ArcNode;
typedef struct VNode {
int data; //顶点信息
ArcNode* firstarc; //该顶点连着的第一个结点
}VNode, AdjList[MAXSIZE];
typedef struct ALGraph {
AdjList vertices; //图的顶点数组
int vexnum, arcnum; //图的顶点数、弧数
}ALGraph;
void CreateGraph(ALGraph* G) {
int i, j, k, tmp;
ArcNode* p, * q, * arc;
scanf("%d %d", &(G->vexnum), &(G->arcnum));
for (k = 0; k < G->vexnum; k++) {
G->vertices[k].data = k;
G->vertices[k].firstarc = NULL;
}
for (k = 0; k < G->arcnum; k++) {
scanf("%d %d", &i, &j);
//以i为主,以j为辅
//i是某个顶点序号,j是i后面连着的那一串里的一个
arc = (ArcNode*)malloc(sizeof(ArcNode)); //跟i号顶点所连的某个顶点
arc->adjvex = j;
arc->nextarc = NULL;
p = (ArcNode*)malloc(sizeof(ArcNode));
p = G->vertices[i].firstarc; //顶点序号为i
//下面的if-else语句为构造的关键部分
//主要用到了链表的生成操作
//由于每次输入的顶点不是按照一串一串的顺序
//不能直接用链表的头插尾插
if (p == NULL) { G->vertices[i].firstarc = arc; }
//序号为i的顶点还没连过其他顶点,他的第一个所连是序号为j的顶点
else {
while (p->nextarc) { p = p->nextarc; }
p->nextarc = arc;
}
//序号为i的顶点已经连过其他顶点,那么从该顶点往后数
//一直数到最后(p->nextarc为空),在这条链的末端连上序号为j的顶点
//以j为主,以i为辅
//j是顶点序号,i是j后面连着的那一串里的一个
arc = (ArcNode*)malloc(sizeof(ArcNode)); //跟j号顶点所连的某个顶点
arc->adjvex = i;
arc->nextarc = NULL;
p = G->vertices[j].firstarc; //顶点序号为j
//往邻接表里插结点
if (p == NULL) {
G->vertices[j].firstarc = arc;
}
else {
while (p->nextarc) { p = p->nextarc; }
p->nextarc = arc;
}
}
//根据题意,需对邻接表中每一行的一串结点排序
q = (ArcNode*)malloc(sizeof(ArcNode));
for (k = 0; k < G->vexnum; k++) {
for (p = G->vertices[k].firstarc; p != NULL; p = p->nextarc) {
for (q = p->nextarc; q != NULL; q = q->nextarc) {
if (q->adjvex < p->adjvex) {
tmp = q->adjvex;
q->adjvex = p->adjvex;
p->adjvex = tmp;
}
}
}
}
free(q);
}
/******************深度优先遍历******************/
int FirstAdjVex(ALGraph* G, int v) {
int w = -1;
if (G->vertices[v].firstarc != NULL) {
w = G->vertices[v].firstarc->adjvex;
}
return w;
}
int NextAdjVex(ALGraph* G, int v, int w) {
int u = -1;
ArcNode* p = (ArcNode*)malloc(sizeof(ArcNode));
for (p = G->vertices[v].firstarc; p != NULL; p = p->nextarc) {
if (p->adjvex == w && p->nextarc != NULL) {
u = p->nextarc->adjvex;
}
}
return u;
}
void DFS(ALGraph* G, int v) {
int w;
printf("%d", G->vertices[v].data);
if (cnt < G->vexnum) {
printf(" ");
}
cnt++;
visited[v] = 1;
for (w = FirstAdjVex(G, v); w != -1; w = NextAdjVex(G, v, w)) {
if (!visited[w]) {
DFS(G, w);
}
}
}
/******************深度优先遍历******************/
/******************广度优先遍历******************/
typedef struct QNode {
int data;
struct QNode* next;
}QNode;
typedef struct {
QNode* front;
QNode* rear;
}LinkQueue;
void InitQueue(LinkQueue* Q) {
Q->front = (QNode*)malloc(sizeof(QNode));
Q->front->next = NULL;
Q->rear = Q->front;
}
void EnQueue(LinkQueue* Q, int e) {
QNode* p = (QNode*)malloc(sizeof(QNode));
p->data = e;
p->next = NULL;
Q->rear->next = p;
Q->rear = p;
}
int DeQueue(LinkQueue* Q) {
int e = 0;
QNode* p = (QNode*)malloc(sizeof(QNode));
if (Q->front == Q->rear) { return 0; }
p = Q->front->next;
e = p->data;
Q->front->next = p->next;
if (Q->rear == p) {
Q->rear = Q->front;
//如果p(即被从队列中取出的元素)是最后一个(即和尾指针重合)
//那么删除p时尾指针也会消失,故应给尾指针重新赋值到头指针
}
return e;
}
int Empty(LinkQueue* Q) {
int flag = 0;
if (Q->front == Q->rear) {
flag = 1;
}
return flag;
}
void BFSTraverse(ALGraph G, int v) {
int i = 0, u, w;
LinkQueue* Q = (LinkQueue*)malloc(sizeof(LinkQueue));
InitQueue(Q);
if (!visited2[v]) {
printf("%d", G.vertices[v].data);
if (cnt2 < G.vexnum) {
printf(" ");
}
visited2[v] = 1;
cnt2++;
EnQueue(Q, v);
while (!Empty(Q)) {
u = DeQueue(Q);
for (w = FirstAdjVex(&G, u); w >= 0; w = NextAdjVex(&G, u, w)) {
if (!visited2[w]) {
printf("%d", G.vertices[w].data);
if (cnt2 < G.vexnum) {
printf(" ");
}
visited2[w] = 1;
cnt2++;
EnQueue(Q, w);
}
}
}
}
for (i = 0; i < G.vexnum; i++) {
if (!visited2[i]) {
printf("%d", G.vertices[i].data);
if (cnt2 < G.vexnum) {
printf(" ");
}
visited2[i] = 1;
cnt2++;
EnQueue(Q, i);
while (!Empty(Q)) {
u = DeQueue(Q);
for (w = FirstAdjVex(&G, u); w >= 0; w = NextAdjVex(&G, u, w)) {
if (!visited2[w]) {
printf("%d", G.vertices[w].data);
if (cnt2 < G.vexnum) {
printf(" ");
}
visited2[w] = 1;
cnt2++;
EnQueue(Q, w);
}
}
}
}
}
free(Q);
}
/******************广度优先遍历******************/
int main() {
int w = 0;
ALGraph* G = (ALGraph*)malloc(sizeof(ALGraph));
CreateGraph(G);
scanf("%d",&w);
DFS(G, w);
printf("\n");
BFSTraverse(*G, w);
return 0;
}
Prim法构造最小生成树
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
typedef int AdjMatrix;
#define Infinity 65535
typedef struct MGraph {
int vexs[200]; //顶点值
AdjMatrix arcs[200][200]; //矩阵值
int vexnum, arcnum;
}MGraph;
typedef struct Closedge {
int adjvex;
int lowcost;
}Closedge[500];
void CreateMGraph(MGraph* G) {
int i, j, k, w;
scanf("%d %d", &G->vexnum, &G->arcnum);
for (i = 0; i < G->vexnum; i++) {
G->vexs[i] = i + 1;
}
for (i = 0; i < G->vexnum; i++) {
for (j = 0; j < G->vexnum; j++) {
G->arcs[i][j] = Infinity ;
}
}
for (k = 0; k < G->arcnum; k++) {
scanf("%d %d %d", &i, &j, &w);
G->arcs[i - 1][j - 1] = w;
G->arcs[j - 1][i - 1] = G->arcs[i - 1][j - 1];
}
/*for (i = 0; i < G->vexnum; i++) {
for (j = 0; j < G->vexnum; j++) {
printf("%d ", G->arcs[i][j]);
}
printf("\n");
}*/
}
int LocateVex(MGraph G,int u) {
int i;
for (i = 0; i <G.vexnum ; i++) {
if (G.vexs[i] == u) {
return i;
}
}
if (i = G.vexnum - 1) {
printf("没找到");
return 0;
}
}
int minimum(Closedge closedge,int n) {
int i, min = 666, index = 0;
for (i = 0; i < n; i++) { /*比较的时候要对应奥*/
if (closedge[i].lowcost != 0 && closedge[i].lowcost < min) {
min = closedge[i].lowcost;
index = i;
}
}
return index;
}
void MiniSpanTree(MGraph G, int u) {
int i, j, k;
int cost = 0;
Closedge closedge;
k = LocateVex(G, u);
//把跟u点连着的点的信息权值放在closedge里
for (j = 0; j < G.vexnum; j++)
if (j != k) {
closedge[j].adjvex = u;
closedge[j].lowcost = G.arcs[k][j];
//j肯定不能=k啊,自己跟自己怎么连
//邻接矩阵中,跟k有连接的是权值,没连接的是无穷大
}
closedge[k].lowcost = 0;
for (i = 1; i < G.vexnum; i++) {
k = minimum(closedge, G.vexnum);
printf("%d %d %d\n", closedge[k].adjvex, G.vexs[k], closedge[k].lowcost);
cost += closedge[k].lowcost;
closedge[k].lowcost = 0;
for (j = 0; j < G.vexnum; j++)
if (G.arcs[k][j] < closedge[j].lowcost) {
closedge[j].adjvex = G.vexs[k];
closedge[j].lowcost = G.arcs[k][j];
}
}
printf("%d\n", cost);
}
int main() {
int u;
MGraph* G = (MGraph*)malloc(sizeof(MGraph));
CreateMGraph(G);
scanf("%d", &u);
MiniSpanTree(*G, u);
return 0;
}