图的基本运算
[问题描述]
对给定图,实现图的深度优先遍历和广度优先遍历。
[基本要求]
以邻接表为存储结构,实现连通无向图的深度优先和广度优先遍历。以用户指定的结点为起点,分别输出每种遍历下的结点访问序列。
实验内容
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
const int IMAXVF = 32767; //用作表示∞
const int MAXV = 10; //图最多为10个顶点
int[,] a = new int[MAXV, MAXV];
int ns; //图的顶点个数
int es; //图的边数
public TextBox[,] edge = new TextBox[MAXV, MAXV];
public Label[] hlabel = new Label[MAXV];
public Label[] vlabel = new Label[MAXV];
private void Dispgraph() //初始时屏幕上对象布局
{
int ix = 40, iy = 120, i, j, x, y; //(x,y)为每个方块左上角坐标
x = ix + 30; y = iy;
numericUpDown1.Value = ns;
Size s = new Size(30, 30);
for (i = 0; i < MAXV; i++) //显示第一行的标签
{
hlabel[i] = new Label();
hlabel[i].Left = x;
hlabel[i].Top = y;
hlabel[i].Size = s;
hlabel[i].Font = new Font("Arial", 11, FontStyle.Bold);
hlabel[i].Text = i.ToString();
hlabel[i].ForeColor = Color.Blue;
x += 31;
this.Controls.Add(hlabel[i]);
}
x = ix; y = iy + 30;
for (j = 0; j < MAXV; j++) //显示第一列的标签
{
vlabel[j] = new Label();
vlabel[j].Left = x;
vlabel[j].Top = y;
vlabel[j].Size = s;
vlabel[j].Font = new Font("Arial", 11, FontStyle.Bold);
vlabel[j].Text = j.ToString();
vlabel[j].ForeColor = Color.Blue;
y += 31;
this.Controls.Add(vlabel[j]);
}
x = ix + 30; y = iy + 30;
for (i = 0; i < MAXV; i++)
{
for (j = 0; j < MAXV; j++)
{
edge[i, j] = new TextBox(