图
说实话,昨天用c++学图的时候,感觉有些云里雾里,一直是跟着老师在打代码。
但是自己用c#写了一遍,整个流程清晰了很多,深度广度遍历也都完成了。
(虽然我的广度遍历其实借用了内置的 队列方法queue,也算是学以致用吧)
最后完成了这个图。还是有点开心的,虽然c++跟c#各自花了我两个多小时。
第一部分,结点类
1.储存A B C D E 等的行列的结点
2.储存布尔值,确认是否已经被遍历过了。
class picNode
{
public char Data;//为什么用char 因为 char 就在那里// A B C
public bool isVisited;
public picNode(char data)
{
Data = data;
isVisited = false;
}
public void ChangeVisited(bool trueorfalse )
{
isVisited = trueorfalse;
}
}
接口,实现了 很多方法
interface iPic
{
void AddNode(picNode node);
void resetNode();//重置搜索
bool SetValueToMatrixForDirectedGraph(int row, int col, int val);
bool SetValueToMatrixForUndiretedGraph(int row, int col, int val);
void printMatrix();
void depthFirstTraverse(int nodeIndex);
void breadthFirstTraverse(int nodeIndex);
void setGDdate();
}
正式实现这个方法
创建两个数组
一个NodeArray 【 容量为行 】 储存结点Node 看起来 就是 A B C D E F G
一个Array 【容量为 行列】储存内部的方向 0 0 0 1 0 1 1
一个Array 【容量为 行列】储存内部的方向 0 0 0 1 0 1 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 图csharp
{
class myPic: iPic
{
private int Capacity;
private int NodeCount;
private int[] array;//邻接矩阵
private picNode[] NodeArray;
Queue<int> GDdata = new Queue<int>();
private int getValueFromArray(int row,int col)
{
if