实验报告一、实验目的和内容1. 实验目的掌握图的邻接矩阵的存储结构;实现图的两种遍历:深度优先遍历和广度优先遍历。
2. 实验内容1.图的初始化; 2.图的遍历:深度优先遍历和广度优先遍历。
二、实验方案程序主要代码:/// <summary>/// 邻接矩阵的节点数据/// </summary>public struct ArcCell{public int Type; // 顶点的关系类型,对无权图,用 1或0表示相邻;// 对带权图,则为权值类型。
public object Data; // 该弧相关信息public ArcCell( int type, object data){Type = type;Data = data;}}/// <summary>/// 图的类型/// </summary>public enumGKind {DG,DN,UDG,UDN}; // 有向图,有向网,无向图,无向/// <summary>/// 图类/// </summary>public class Graph{public static int Max_Vertex_Num = 20; // 最大顶点数private object [] Vexs; // 顶点数据数组private ArcCell [,] Arcs; // 邻接矩阵private GKind Kind; // 图的种类private int VexNum,ArcNum; // 当前顶点数和弧数/// <summary>/// 图的初始化方法/// </summary>Ill VParam n ame="vex num">顶点数v∕param>III VParam n ame="arc num">弧数<∕param>Ill VParam name="k">图的类型<∕param> public Graph( int vexnum,int arcnum,GKind k) {VexNum = vexnum;ArcNum = arcnum;Kind = k;Vexs = new object [Max_Vertex_Num];Arcs = newArcCell[Max_Vertex_Num,Max_Vertex_Num];}III Vsummary>III设置v1, v2之间的弧的权值,顶点的关系类型,对无权图,用表示相邻;III 对带权图,则为权值类型。
III VIsummary>III VParam n ame="v1"> 顶点 1 VIParam>III VParam n ame="v2" > 顶点2v∕param>III VParam n ame="adj"> 权v∕param>III Vreturns> 成功返回真,否则返回假 VIreturns>Public bool SetArcInfo( int v1, int v2, int adj, object data) { if (v1VVexNum && v2VVexNum){Arcs[v1,v2].TyPe = adj; Arcs[v1,v2].Data = data;switch (Kind) {case GKind.DG:break ;case GKind.UDG: Arcs[v2,v1].Type = adj;Arcs[v2,v1].Data = data; break ;case GKind.DN: break ;case GKind.UDN: break ;}return true ;}else return false ;}/// <summary>/// 设置指定顶点的信息/// </summary>Ill VParam n ame="v "> 顶点号<∕param>/// <param name="info"> 要设置的信息 </param>lll Vreturns> 成功返回真,否则返回假 Vlreturns>public bool SetVexInfo( int v, object info) {if (vVVexNum){Vexs[v] = info;return true ;}else return false ;}lll Vsummary>III返回V的第一个邻接顶点,若没有则返回-1lll Vlsummary>public int FirstAdjVex( int v){for (int j=0;jV this .VexNum;j++){if (( this .Arcs[v,j].Type>0)&&( this .Arcs[v,j].TypeV int .MaxValue)) {return j;Il指定节点VeX的(相对于FVeX)下一个邻接顶点,若没有则返回-1 publicint NextAdjVex( int vex, int Fvex){for (int j=0;j< this .VeXNum;j++){if (( this .Arcs[VeX,j].Type>0)&&( this .Arcs[VeX,j].Type<int .MaXValue )&&(j>FVeX)){return j;}}return -1;}public static bool [] Visited; II 访问标志数组III <summary>III 深度遍历,递归算法III <Isummary>public string DFSTraVerse(){Visited = new bool [ this .VeXNum]; II 初始化访问标志数组string str ="";for (int V=0;V< this .VeXNum;V++){Visited[V] = false ;}for (int V=0;V< this .VeXNum;V++){if (!Visited[V])str +=DFS(V);}return str;}III <summary>Ill从第V个顶点出发递归地深度优先遍历III <Isummary>public string DFS(int V){string str ="";Visited[V] = true ;str +=" "+ this .Vexs[v];for (int i=FirstAdjVex(v);i>=0;i=NextAdjVex(v,i)) if(!visited[i])str +=DFS(i);/// <summary>/// 深度优先遍历,非递归算法/// </summary> public string DFSTrav(){visited = new bool [ this .VexNum]; // 初始化访问标志数组string str ="";for (int v=0;v< this .VexNum;v++){ visited[v] = false ;}System.Collections.Stack st = newStack(); // 初始化辅助栈for (int v=0;v< this .VexNum;v++) // 可以遍历多个散图{if (!visited[v]){visited[v] = true ;str +=" "+ this .Vexs[v]; st.Push(v); //v 入栈 while(st.Count>0){int u = ( int )st.Pop();for (int w=FirstAdjVex(u);w>=0;w=NextAdjVex(u,w)){if (!visited[w]){ visited[w] = true ; str +=" "+ this .Vexs[w];st.Push(w); break ;}}}}}return str;/// <summary>/// 广度优先遍历,非递归算法/// </summary> public string BFSTraverse() {visited = new bool [ this .VexNum]; // 初始化访问标志数组string str ="";for (int v=0;v< this .VexNum;v++) {visited[v] = false ;}System.Collections.Queue Q = newQueue(); // 初始化辅助队列 for(int v=0;v< this .VexNum;v++) // 可以遍历多个散图 {if {visited[v] = true ; str +=" "+ this .Vexs[v];Q.Enqueue(v); //v 入队列 while (Q.Count>0) {int for {visited[w] = true ; str +=" "+this .Vexs[w]; Q.Enqueue(w);} } return/// <summary>/// 显示邻接矩阵/// </summary>public string Display(){string graph = "";for (int i=0;i< this .VexNum;i++){for (int j=0;j< this .ArcNum;j++){graph +=" "+ this .Arcs[i,j].Type; } graph +="\n";}return graph;}}/// <summary>/// 应用程序的主入口点。