## 图的基本算法实现（邻接矩阵与邻接表两种方法）

1 无向图——邻接矩阵

```#include "stdafx.h"
#include <stdlib.h>
#include <malloc.h>
#define MAX_VEX 20
#define INFINITY 65535
int *visited;
struct _node
{
int vex_num;
struct _node *next;
};
typedef struct _node node, *pnode;
struct _graph
{
char *vexs;
int arcs[MAX_VEX][MAX_VEX];
int vexnum, arcnum;
};
typedef struct _graph graph, *pgraph;
int locate(graph g, char ch)
{
int i;
for(i=1; i<=g.vexnum; i++)
if(g.vexs[i]==ch)
return i;
return -1;
}
graph create_graph()
{
int i, j, w, p1, p2;
char ch1, ch2;
graph g;
printf("Enter vexnum arcnum: ");
scanf("%d %d", &g.vexnum, &g.arcnum);
getchar();
for(i=1; i<=g.vexnum; i++)
for(j=1; j<g.vexnum; j++)
g.arcs[i][j]=INFINITY;
g.vexs=(char *)malloc(sizeof(char));
printf("Enter %d vexnum.../n", g.vexnum);
for(i=1; i<=g.vexnum; i++)
{
printf("vex %d: ", i);
scanf("%c", &g.vexs[i]);
getchar();
}
printf("Enter %d arcnum.../n", g.arcnum);
for(i=1; i<=g.arcnum; i++)
{
printf("arc %d: ", i);
scanf("%c %c %d", &ch1, &ch2, &w);
getchar();
p1=locate(g, ch1);
p2=locate(g, ch2);
g.arcs[p1][p2]=g.arcs[p2][p1]=w;
}
return g;
}
int firstvex_graph(graph g, int i)
{
int k;
if(i>=1 && i<=g.vexnum)
for(k=1; k<=g.vexnum; k++)
if(g.arcs[i][k]!=INFINITY)
return k;
return -1;
}
int nextvex_graph(graph g, int i, int j)
{
int k;
if(i>=1 && i<=g.vexnum && j>=1 && j<=g.vexnum)
for(k=j+1; k<=g.vexnum; k++)
if(g.arcs[i][k]!=INFINITY)
return k;
return -1;
}
void dfs(graph g, int i)
{
int k, j;
if(!visited[i])
{
visited[i]=1;
printf("%3c", g.vexs[i]);
for(j=firstvex_graph(g, i); j>=1; j=nextvex_graph(g, i, j))
if(!visited[j])
dfs(g, j);
}
}
void dfs_graph(graph g)
{
int i;
visited=(int *)malloc((g.vexnum+1)*sizeof(int));
for(i=1; i<=g.vexnum; i++)
visited[i]=0;
for(i=1; i<g.vexnum; i++)
if(!visited[i])
dfs(g, i);
}
int _tmain(int argc, _TCHAR* argv[])
{
graph g;
g=create_graph();
printf("DFS: ");
dfs_graph(g);
printf("/n");
return 0;
}```

==========================================================

2 无向图—— 邻接表

```#include "stdafx.h"
#include <stdlib.h>
#include <malloc.h>
#define MAX_VEX 20
int *visited;
typedef struct _ArcNode
{
int ivex; /* next ivex */
struct _ArcNode *nextarc; /* next node */
int *info; /* arc weight */
};
typedef struct _ArcNode ArcNode, *pArcNode;
struct _VNode
{
char adjvex; /* note vex */
ArcNode *firstarc;
};
typedef struct _VNode VNode;
struct _ALGraph
{
int vexnum, arcnum;
int kink;
};
typedef struct _ALGraph ALGraph;
int locate(ALGraph g, char ch)
{
int i;
for(i=1; i<=g.vexnum; i++)
return i;
return -1;
}
ALGraph create_graph()
{
int i, j, w, p1, p2;
char ch1, ch2;
pArcNode pnode, pnode1, pnode2;
ALGraph g;
printf("Enter vexnum arcnum: ");
scanf("%d %d", &g.vexnum, &g.arcnum);
getchar();

printf("Enter %d vexnum.../n", g.vexnum);
for(i=1; i<=g.vexnum; i++)
{
printf("vex %d: ", i);
getchar();
}
printf("Enter arc.../n");
for(i=1; i<=g.arcnum; i++)
{
printf("arc %d: ", i);
scanf("%c %c", &ch1, &ch2);
getchar();
p1=locate(g, ch1);
p2=locate(g, ch2);
pnode1=(pArcNode)malloc(sizeof(ArcNode));
pnode2=(pArcNode)malloc(sizeof(ArcNode));
pnode1->ivex=p2; /* next ivex */
pnode2->ivex=p1; /* next ivex */
}
return g;
}
int firstvex_graph(ALGraph g, int i)
{
int k;
if(i>=1 && i<=g.vexnum)
return -1;
}
int nextvex_graph(ALGraph g, int i, int k)
{
pArcNode pnode;
if(i>=1 && i<=g.vexnum && k>=1 && k<=g.vexnum)
{
while(pnode->nextarc)
{
k=pnode->nextarc->ivex;
if(!visited[k])
return k;
else
pnode=pnode->nextarc;
}
}
return -1;
}
void dfs(ALGraph g, int i)
{
int k;
if(!visited[i])
{
visited[i]=1;
for(k=firstvex_graph(g, i); k>=1; k=nextvex_graph(g, i, k))
if(!visited[k])
dfs(g, k);
}
}
void dfs_graph(ALGraph g)
{
int i;
visited=(int *)malloc((g.vexnum+1)*sizeof(int));
for(i=1; i<=g.vexnum; i++)
visited[i]=0;
for(i=1; i<=g.vexnum; i++)
if(!visited[i])
dfs(g, i);
}
int _tmain(int argc, _TCHAR* argv[])
{
ALGraph g;
g=create_graph();
dfs_graph(g);
printf("/n");
return 0;
}
```

==========================================================