-
Notifications
You must be signed in to change notification settings - Fork 0
/
图的邻接表.txt
114 lines (108 loc) · 2 KB
/
图的邻接表.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# include <cstdio>
#include <queue>
using namespace std;
#define FFINITY 5000 //代表无穷大
# define M 20
bool visit[M];
queue <int >q;
typedef char datatype;//顶点信息数据
typedef struct node {//边表结点
int adjvex;//邻接点
struct node *next;
}edgenode;
typedef struct vnode {
datatype vertex;//顶点信息
edgenode *firstedge;//邻接链表头指针
}vertexnode;
typedef struct {//邻接表类型
vertexnode adjlist[M];//存放头节点的顺序表
int n, e;//图的定点数与边数
}linkgraph;
void creat(linkgraph *g, int c)
{
int i, j, k;
edgenode *s;
scanf("%d%d", &g->n,&g->e);
for (i = 0; i < g->n; i++)
{
scanf("%1s", &g->adjlist[i].vertex);//读入顶点信息
g->adjlist[i].firstedge = NULL;// 边表置为空表
}
for (k = 0; k < g->e; k++) {
scanf("%d%d", &i, &j);
s = (edgenode *)malloc(sizeof(edgenode));
s->adjvex = j;
s->next = g->adjlist[i].firstedge;
g->adjlist[i].firstedge = s;//将新节点s插入顶点的头部(使用头插法O(1),而不是尾插法O(n))
if (c == 0)
{
s = (edgenode *)malloc(sizeof(edgenode));
s->adjvex = i;
s->next = g->adjlist[j].firstedge;
g->adjlist[j].firstedge = s;//将新节点 s插入顶点 Vj 的头部
}
}
}
void dfs(linkgraph g, int i)
{
edgenode *p;
printf("%c", g.adjlist[i].vertex);
visit[i] = 1;
p = g.adjlist[i].firstedge;
while (p)
{
if (!visit[p->adjvex])
dfs(g, p->adjvex);
p = p->next;
}
}
void dfslinkgraph(linkgraph g)
{
int i;
for (i = 0; i < g.n; i++)
visit[i] = 0;
for (i = 0; i < g.n; i++)
{
if (!visit[i])
dfs(g, i);
}
}
void bfs(linkgraph g, int i)
{
int j;
edgenode *p;
q.push(i);
printf("%c", g.adjlist[i].vertex);
visit[i] = 1;
while(!q.empty())
{
int x = q.front();
q.pop();
p = g.adjlist[x].firstedge;
while (p)
{
if (visit[p->adjvex] == 0)
{
visit[p->adjvex] = 1;
printf("%c", g.adjlist[p->adjvex].vertex);
q.push(p->adjvex);
}
p = p->next;
}
}
}
void bfsgraph(linkgraph g)
{
int i, count = 0;
for (i = 0; i < g.n; i++)
{
visit[i] = 0;
}
for (i = 0; i < g.n; i++)
{
if (!visit[i])
{
bfs(g, i);
}
}
}