69,373
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<malloc.h>
#include"graph.h"
extern void mattolist(mgraph, algraph *);
extern void dispadj(algraph *);
int visited[MAXV];
void pathall1(algraph *G, int u, int v, int path[], int i){
arcnode *p;
int j, n;
visited[u] = 1;
p = G->adjlist[u].firstarc;
while (p != NULL){
n= p->adjvex;
if (n == v){
path[i + 1] = v;
for (j = 0; j <= i + 1; j++)
printf("%3d", path[j]);
printf("\n");
}
else if (visited[n] == 0){
path[i + 1] = n;
pathall1(G, n, v, path, i + 1);
}
p = p->nextarc;
}
visited[u] = 0;
}
void pathall2(algraph *G, int u, int v, int l, int path[], int d){
int m, i;
arcnode*p;
visited[u] = 1;
d++;
path[d] = u;
if (u == v&&d == 1){
for (i = 0; i <= d; i++)
printf("%3d", path[i]);
printf("\n");
}
p = G->adjlist[u].firstarc;
while (p != NULL){
m = p->adjvex;
if (visited[m] == 0)
pathall2(G, m, v, l, path, d);
p = p->nextarc;
}
visited[u] = 0;
}
int shortpath(algraph *G, int u, int v, int path[]){
struct{
int vno;
int level;
int parent;
}qu[MAXV];
int front = -1, rear = -1, k, lev, i, j;
arcnode *p;
visited[u] = 1;
rear++;
qu[rear].vno = u;
qu[rear].level = 0;
qu[rear].parent = -1;
while (front<rear){
front++;
k = qu[front].vno;
lev = qu[front].level;
if (k == v){
i = 0;
j = front;
while (j != -1){
path[lev - i] = qu[j].vno;
j = qu[j].parent;
i++;
}
return lev;
}
p = G->adjlist[k].firstarc;
while (p != NULL){
if (visited[p->adjvex] == 0){
visited[p->adjvex] = 1;
rear++;
qu[rear].vno = p->adjvex;
qu[rear].level = lev + 1;
qu[rear].parent = front;
}
p = p->nextarc;
}
}
return -1;
}
void main(){
int i, j;
int u = 5, v = 2, d = 3;
int path[MAXV];
mgraph g;
algraph *G;
int A[MAXV][6] = { { 0, 1, 0, 1, 0,0 }, { 0, 0, 1, 0, 0,0 }, { 1, 0, 0, 0, 0,1 }, { 0, 0, 1, 0, 0,1 }, { 0, 0, 0, 1, 0,0 }, {1,1,0,1,1,0} };
g.n = 6; g.e = 10;
for (i = 0; i<g.n; i++)
for (j = 0; j<g.n; j++)
g.edges[i][j] = A[j][j];
G = (algraph *)malloc(sizeof (algraph));
mattolist(g, G);
printf("邻接表\n");
dispadj(G);
for (i = 0; i<g.n; i++)
visited[i] = 0;
printf("从顶点%d到%d的所有路径:\n", u, v);
path[0] = u;
visited[u] = 1;
pathall1(G, u, v, path, 0);
printf("从顶点%d到%d的所有长度为%d的路径:\n", u, v, d);
pathall2(G, u, v, d, path, -1);
printf("\n");
for (i = 0; i<g.n; i++)
visited[i] = 0;
printf("从顶点%d到%d的最短路径:\n", u, v);
for (i = 0; i<g.n; i++)
visited[i] = 0;
j = shortpath(G, u, v, path);
for (i = 0; i <= j; i++)
printf("%3d", path[i]);
printf("\n");
}
#include<stdio.h>
#include<malloc.h>
#include"graph.h"
//不带权图的算法
void mattolist(mgraph g, algraph *G){
int i, j;
arcnode *p;
G = (algraph *)malloc(sizeof (algraph));
for (i = 0; i<g.n; i++)
G->adjlist[i].firstarc = NULL;
for (i = 0; i<g.n; i++)
for (j = g.n - 1; j >= 0; j--)
if (g.edges[i][j] != 0){
p = (arcnode *)malloc(sizeof(arcnode));
p->adjvex = j;
p->nextarc = G->adjlist[i].firstarc;
G->adjlist[i].firstarc = p;
}
G->n = g.n; G->e = g.e;
}
void listtomat(algraph *G, mgraph g){
int i,j;
arcnode *p;
for (i = 0; i<G->n; i++)
for (j = 0; j<G->n; j++)
g.edges[i][j] = 0;
for (i = 0; i<G->n; i++){
p = G->adjlist[i].firstarc;
while (p != NULL){
g.edges[i][p->adjvex] = 1;
p = p->nextarc;
}
}
g.n = G->n; g.e = G->e;
}
void dispmat(mgraph g){
int i, j;
for (i = 0; i<g.n; i++){
for (j = 0; j<g.n; j++)
printf("%3d", g.edges[i][j]);
printf("\n");
}
}
void dispadj(algraph *G){
int i;
arcnode *p;
for (i = 0; i<G->n; i++){
p = G->adjlist[i].firstarc;
printf("%3d", i);
while (p != NULL){
printf("%3d", p->adjvex);
p = p->nextarc;
}
printf("\n");
}
}
//graph.h头文件
typedef int InfoType;
#define MAXV 100//最大顶点个数
#define INF 32767
//定义邻接矩阵类型
typedef struct
{
int no;//顶点标号
InfoType info;//顶点其他信息,这里用于存放权值
}VertexType;//顶点类型
typedef struct//图的定义
{
int edges[MAXV][MAXV];//邻接矩阵
int n, e;//顶点数,弧数
VertexType vexs[MAXV];//存放顶点信息
}mgraph;//图的邻接矩阵类型
//以下定义邻接表类型
typedef struct ANode//弧的节点结构类型
{
int adjvex;//该弧的终点位置
struct ANode *nextarc;//指向下一条弧的指针
InfoType info;//该弧的相关信息,这里用于存放权值
}arcnode;
typedef int Vertex;
typedef struct Vnode//邻接表节点的类型
{
Vertex data;//顶点信息
arcnode * firstarc;//指向第一条弧
}VNode;
typedef VNode AdjList[MAXV];//AdjList 是邻接表类型
typedef struct
{
AdjList adjlist;//邻接表
int n, e;//图的顶点数n和边数e
}algraph;//图的邻接表类型
void mattolist(mgraph g, algraph *G)
在里面给G分配内存,根本带不回去。应该这样:
void mattolist(mgraph g, algraph **G)
另外,一般为了保险,malloc之后,先用memset把内存清空。防止忘记给里面的变量赋值。
memset(p, sizeof (algraph),0);
我没仔细读你代码,不知道有没有这样问题。