1.图的存储结构:第一种为邻接矩阵,矩阵为n*n(n为图的顶点数),其中行代表图的顶点,列代表图的关系,[a][b]代表从a顶点到b顶点的边,其中没右边用0表示,有边用1表示,主对角线恒为0
,如果路径带权则原本1的位置替换为权重
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main()
{
int n;
cout << "输入矩阵顶点数" << endl;
cin >> n;
vector<vector<int>>sove(n,vector<int>(n, 0)) ;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << sove[i][j] << ' ';
}
cout << endl;
}
printf("请输入矩阵边数\n");
int m;
cin >> m;
printf("请输入矩阵边的关系\n");
for (int x = 0; x < m; x++)
{
int row;
int col;
cin >> row >> col;
sove[row][col] = 1;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << sove[i][j] << ' ';
}
cout << endl;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (sove[i][j] == 1)
{
sove[i][j] = 0;
}
else
{
sove[i][j] = 99999;
}
}
}
printf("现在建立带权矩阵,请重新输入顶点及权重\n");
for (int x = 0; x < m; x++)
{
int row;
int col;
int value;
cin >> row >> col >> value;
sove[row][col] = value;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << sove[i][j] << ' ';
}
cout << endl;
}
return 0;
}
第二种存储结构为邻接表,首先它的主体是一个包含着各顶点编号的数组,每个对应位置的节点都包含着一个结构体指针,这个指针指向了起点为该顶点的所有边,其中包含着结束位置,权重等信息
typedef struct cnode
{
int node;
int value;
cnode* next1;
}cnode;
typedef struct fnode
{
cnode* head;
}fnode;
vector<fnode>father;
printf("请输入矩阵的顶点数\n");
int n;
cin >> n;
for (int x = 0; x < n; x++)
{
fnode newnode;
newnode.head = nullptr;
father.push_back(newnode);
}
printf("请输入矩阵的边数\n");
int m;
cin >> m;
printf("请输入矩阵的边和权重\n");
for (int x = 0; x < m; x++)
{
int begin;
int end;
int v;
cin >> begin >> end >> v;
cnode* newnode=new cnode;
newnode->next1 = nullptr;
newnode->node = end;
newnode->value = v;
cnode* point = father[begin].head;
if (point== nullptr)
{
father[begin].head = newnode;
}
else
{
while (point->next1!=nullptr)
{
point = point->next1;
}
point->next1 = newnode;
}
}
再来讲解一下bfs的递归形式,这个函数和平常的bfs完全没有区别,首先由于每个顶点只需访问一次,所以使用check数组(visit),且递归出口为某顶点已经被访问过,这里的问题更像子集形式,所以选择内嵌循环,不断向后遍历边
void dfs(vector<bool>& visit, vector<fnode>& father,int pos)
{
if (visit[pos] == true)
{
return;
}
visit[pos] = true;
cout << pos << "->";
cnode* next = father[pos].head;
while (next!=nullptr)
{
int pos = next->node;
if (visit[pos] == false)
{
dfs(visit, father, pos);
}
next = next->next1;
}
}
最后将它改为非递归形式,首先将第一个顶点入栈,然后进行出栈操作,对顶点进行打印,然后再对边进行进栈操作,等待下一层循环进行依次打印
void dfs1(vector<fnode>& father, vector<bool>& visit, int pos)
{
stack<int>s;
visit[pos] = true;
s.push(pos);
while (!s.empty())
{
int data = s.top();
s.pop();
cout << data<< "->";
cnode* next = father[data].head;
while (next != nullptr)
{
int cur = next->node;
if (visit[cur] == false)
{
s.push(cur);
visit[cur] = true;
}
next = next->next1;
}
}
}
bfs,只需把上面非递归的代码一切有关栈的操作改成队列即可
void bfs(vector<fnode>& father, vector<bool>& visit, int pos)
{
queue<int>s;
visit[pos] = true;
s.push(pos);
while (!s.empty())
{
int data = s.front();
s.pop();
cout << data << "->";
cnode* next = father[data].head;
while (next != nullptr)
{
int cur = next->node;
if (visit[cur] == false)
{
s.push(cur);
visit[cur] = true;
}
next = next->next1;
}
}
}