> 图
: 在计算机科学中,一个图就是一些顶点的集合,这些顶点通过一系列边结对(连接)。顶点用圆圈表示,边就
是这些圆圈之间的连线。顶点之间通过边连接。
结构体:
首先一个顶点有两个数据,一个是数据,一个是边。
然后边里包括定点连接的哪条边,权重,顶点的下一条边。
之后就是定义图了,图里包括指向所有顶点的指针,顶点的个数以及边。
visted用来保存该节点是否被访问过。
bool visted[1024];
typedef struct _EdgeNode {
int adj;
int weight;
struct _EdgeNode* next;
}EdgeNode;
typedef struct _VertexNode {
char c;
struct _EdgeNode* frist;
}VertexNode;
typedef struct _AdjListGraph {
VertexNode* adjlist;
int ver;
int edg;
}AdjListGraph;
初始化:
先将给指针分配内存空间
再将边和节点个数设置为0
void init(AdjListGraph& G) {
G.adjlist = new VertexNode[1024];
G.edg = 0;
G.ver = 0;
for (int i = 0;i < 1024;i++) {
visted[i] = false;
}
}
查询:
用过数据查询是否有该顶点,如果有的话,返回它的下标
这里的意思就是,数据和下标之间的转换。
int location(AdjListGraph& G, int e) {
for (int i = 0;i < G.ver;i++) {
if (G.adjlist[i].c == e)
return i;
}
return -1;
}
建立:
首先输入节点个数和边数
再分别输顶点的数据
再输入边之间的关系(建立两个边之间的关系之前通过查询函数,判断是否有这两个顶点。)建立的方法为,设置一个新的边,adj的值赋为第二个顶点的值,将它放置到第一个顶点的放置边的指针中。
void Create(AdjListGraph& G) {
cout << "请输入图的节点个数和边数:" << endl;
cin >> G.ver >> G.edg;
cout << "请输入节点的数据:" << endl;
for (int i = 0;i < G.ver;i++) {
cin >> G.adjlist[i].c;
G.adjlist[i].frist = NULL;
}
char c1, c2;
int i1, i2;
cout << "请输入边的关系:" << endl;
for (int i = 0;i < G.edg;i++) {
cin >> c1 >> c2;
i1 = location(G, c1);
i2 = location(G, c2);
if (i1 != -1 && i2 != -1) {
EdgeNode* E = new EdgeNode;
E->adj = i2;
E->next = G.adjlist[i1].frist;
G.adjlist[i1].frist = E;
}
}
}
广度遍历:
BFS_main:遍历所有的节点
BFS:类似于树的遍历,利用了队列,先判断顶点,是否已经被遍历过了,如果没有,输出,然后将它的边都放到队列中,这样做保证了,先遍立第一层顶点,在遍历其他层
void BFS_main(AdjListGraph& G) {
for (int i = 0;i < G.ver;i++) {
BFS(G,i);
}
}
void BFS(AdjListGraph& G, int v) {
queue <int> q;
int cur = -1;
int index = -1;
q.push(v);
while (!q.empty()) {
cur = q.front();
if (visted[cur] == false) {
cout << G.adjlist[cur].c << " ";
visted[cur] = true;
}
q.pop();
EdgeNode* tmp = G.adjlist[cur].frist;
while (tmp) {
index = tmp->adj;
tmp = tmp->next;
q.push(index);
}
}
}
全部代码:
#include<iostream>
#include<Windows.h>
#include<queue>
using namespace std;
bool visted[1024];
typedef struct _EdgeNode {
int adj;
int weight;
struct _EdgeNode* next;
}EdgeNode;
typedef struct _VertexNode {
char c;
struct _EdgeNode* frist;
}VertexNode;
typedef struct _AdjListGraph {
VertexNode* adjlist;
int ver;
int edg;
}AdjListGraph;
void init(AdjListGraph& G);
void Create(AdjListGraph& G);
int location(AdjListGraph& G, int e);
void BFS(AdjListGraph& G, int v);
void BFS_main(AdjListGraph& G);
int main() {
AdjListGraph G;
init(G);
Create(G);
BFS_main(G);
system("pause");
return 0;
}
void BFS(AdjListGraph& G, int v) {
queue <int> q;
int cur = -1;
int index = -1;
q.push(v);
while (!q.empty()) {
cur = q.front();
if (visted[cur] == false) {
cout << G.adjlist[cur].c << " ";
visted[cur] = true;
}
q.pop();
EdgeNode* tmp = G.adjlist[cur].frist;
while (tmp) {
index = tmp->adj;
tmp = tmp->next;
q.push(index);
}
}
}
void BFS_main(AdjListGraph& G) {
for (int i = 0;i < G.ver;i++) {
BFS(G,i);
}
}
void init(AdjListGraph& G) {
G.adjlist = new VertexNode[1024];
G.edg = 0;
G.ver = 0;
for (int i = 0;i < 1024;i++) {
visted[i] = false;
}
}
void Create(AdjListGraph& G) {
cout << "请输入图的节点个数和边数:" << endl;
cin >> G.ver >> G.edg;
cout << "请输入节点的数据:" << endl;
for (int i = 0;i < G.ver;i++) {
cin >> G.adjlist[i].c;
G.adjlist[i].frist = NULL;
}
char c1, c2;
int i1, i2;
cout << "请输入边的关系:" << endl;
for (int i = 0;i < G.edg;i++) {
cin >> c1 >> c2;
i1 = location(G, c1);
i2 = location(G, c2);
if (i1 != -1 && i2 != -1) {
EdgeNode* E = new EdgeNode;
E->adj = i2;
E->next = G.adjlist[i1].frist;
G.adjlist[i1].frist = E;
}
}
}
int location(AdjListGraph& G, int e) {
for (int i = 0;i < G.ver;i++) {
if (G.adjlist[i].c == e)
return i;
}
return -1;
}