本文章主要包括一下内容:
1、创建图类。二叉树的存储结构使用邻接矩阵或链表。
2、提供操作:遍历、BFS、DFS
3、对建立好的图,执行上述各操作。
4、输出最小生成树。
#include <stdio.h>
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <stack>
using namespace std;
#define MAXSIZE 10
#define MAX 10000000
class Node //队列类中用链表存,这是链表的节点
{
public:
int data;
Node *next;
Node() { next = NULL; }
Node(int d, Node* link = NULL) { data = d; next = link; };
};
//队列类,作为bfs辅助数据结构用
class LinkQueue
{
private:
Node *front, *rear;
public:
LinkQueue() { rear = front = new Node; }
bool isEmpty() { return front == rear; }
void add(int d)//入队列
{
Node *t = new Node(d);
rear->next = t;
rear = t;
}
int remove()//出队列
{
Node *t = front->next;
int d = t->data;
front->next = t->next;
if (rear == t) rear = front;
delete t;
return d;
}
};
class Graph
{
.
public:
Graph(int vertices = 10)
{
numOfVertices = vertices;
numOfEdges = 0;
for (int i = 1; i <= numOfVertices; i++)
for (int j = 1; j <= numOfVertices; j++)
a[i][j] = 0;
for (int i = 1; i <= numOfVertices; i++)
visited[i] = 0;
}
void add(int i, int j, int weight)//增加一条边,包括入点,出点以及权值
{
if (i < 1 || j < 1 || i > numOfVertices || j > numOfVertices || i == j || a[i][j] != 0) { cout << "bad input"; return; }
a[i][j] = weight;//双向tu
a[j][i] = weight;
numOfEdges++;
}
void remove(int i, int j)//删除一条边
{
if (i < 1 || j < 1 || i > numOfVertices || j > numOfVertices || i == j || a[i][j] == 0) { cout << "bad input"; return; }
a[i][j] = 0;
numOfEdges--;
}
int outDegree(int v)//计算某一点的出度
{
if (v < 1 || v > numOfVertices) { cout << "bad input"; return -1; }
int sum = 0;
for (int i = 1; i <= numOfVertices; i++)
{
if (a[v][i] != 0) sum++;
}
return sum;
}
int inDegree(int v)//计算某一点的入度
{
if (v < 1 || v > numOfVertices) { cout << "bad input"; return -1; }
int sum = 0;
for (int i = 1; i <= numOfVertices; i++)
{
if (a[i][v] != 0) sum++;
}
return sum;
}
void bfs(int v) //宽度优先搜索
{
LinkQueue q;
visited[v] = 1;
q.add(v);
int t = 0;
while (!q.isEmpty())
{
t = q.remove();
cout << t << endl;
for (int i = 1; i <= numOfVertices; i++)
{
if (a[t][i] != 0 && visited[i] == 0)
{
q.add(i);
visited[i] = 1;
}
}
}
}
void dfs(int v)//深度优先搜索
{
cout << v << endl;
visited[v]=1;//这个地方要注意否则会一直递归下去
for (int i = 1; i <= numOfVertices; i++)
if (a[v][i] != 0 && visited[i] == 0) dfs(i);
}
void clearVisit()
{
for (int i = 1; i <= numOfVertices; i++)
visited[i] = 0;
}
void prim()//最小生成树
{
int min = MAX, minv = 1;//minv表示即将加入的点
int e = 0, count = 0;
int g[MAXSIZE + 1] = { 0 };//储存已经加入的点集
while (e < numOfVertices)
{
visited[minv] = 1;
g[e++] = minv;
cout << minv << endl;
min = MAX;
for (int i = 0; i < e; i++)
{
for (int j = 1; j <= numOfVertices; j++)
{
if (a[g[i]][j] != 0 && a[g[i]][j] < min && visited[j] == 0)
{
//cout << "ok" << j << endl;
min = a[g[i]][j];
minv = j;
}
}
}
}
}
private:
int numOfVertices;//点的个数
int numOfEdges;//边的个数
int a[MAXSIZE + 1][MAXSIZE + 1];//存储权重
int visited[MAXSIZE + 1];//是否到达这一点
};
int main()
{
Graph g(6);
// g.add(1, 2, 4);//结点1 到 结点2 创建一条权值为4的边
g.add(1, 4, 3);
g.add(1, 3, 2);
// g.add(2, 3, 5);
// g.add(2, 4, 4);
// g.add(2, 5, 3);
// g.add(3, 4, 1);
g.add(3, 6, 2);
// g.add(4, 5, 6);
g.add(4, 6, 2);
// g.add(5, 6, 4);
cout << "bfs:" << endl;
g.bfs(1);
g.clearVisit();
cout << "dfs:" << endl;
g.dfs(1);
g.clearVisit();
cout << "minimum spanning tree:" << endl;
g.prim();
system("pause");
return 0;
}