C# 宽度优先搜索(BFS)-将每条路径打印成一行,并从文本文件中读取图形
我有一个BFS,它将来自定义节点的所有路径打印为一个序列(在一行中)。我想把每条路径打印成一行。例如:如果我有一个像C# 宽度优先搜索(BFS)-将每条路径打印成一行,并从文本文件中读取图形,c#,C#,我有一个BFS,它将来自定义节点的所有路径打印为一个序列(在一行中)。我想把每条路径打印成一行。例如:如果我有一个像 A B A F B C B D 我想要一个节点A的路径 BC BD F 我当前的脚本输出是BCDF,它将所有路径作为一个序列放在一起 还有,如何将其更改为从文本文件读取图形。因为我不想在scipt中复制所有图形的边 我的BFS脚本 using System; using System.Collections.Generic; using System.Linq; using
A B
A F
B C
B D
我想要一个节点A
的路径
BC
BD
F
我当前的脚本输出是BCDF
,它将所有路径作为一个序列放在一起
还有,如何将其更改为从文本文件读取图形。因为我不想在scipt中复制所有图形的边
我的BFS脚本
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BFS
{
class Graph
{
private int _V; //It shows the number of nodes
private bool _directed; //If _directed is true, it means that the graph is a directed one
LinkedList<int>[] _adj;
public Graph(int V, bool directed)
{
_adj = new LinkedList<int>[V];
for (int i = 0; i < _adj.Length; i++)
{
_adj[i] = new LinkedList<int>();
}
_V = V;
_directed = directed;
}
public void AddEdge(int v, int w)
{
_adj[v].AddLast(w);
if (!_directed)
{
_adj[w].AddLast(v);
}
}
public void BreadthFirstSearch(int s) //s is the checked node
{
bool[] visited = new bool[_V];
for (int i = 0; i < _V; i++)
visited[i] = false;
// Create a queue for BFS
LinkedList<int> queue = new LinkedList<int>();
visited[s] = true; //if a node is visited we swith its value to true
queue.AddLast(s);
while (queue.Any())
{
// Dequeue a vertex from queue and print it
s = queue.First();
Console.Write(s + " ");
queue.RemoveFirst();
LinkedList<int> list = _adj[s];
foreach (var val in list)
{
if (!visited[val])
{
visited[val] = true;
queue.AddLast(val);
}
}
}
}
}
class Program
{
static void Main(string[] args)
{
Graph g = new Graph(1400, true); //the number should be the same or more than the number of nodes
g.AddEdge(1, 2);
g.AddEdge(2, 4);
g.AddEdge(2, 5);
g.AddEdge(2, 3);
Console.Write("Breadth First Traversal from vertex 2:\n");
g.BreadthFirstSearch(2); //The number here shows the starting node
Console.Read();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间BFS
{
类图
{
private int _V;//它显示节点数
private bool _directed;//如果_directed为true,则表示该图是有向图
LinkedList[]_adj;
公共图(整数V,布尔定向)
{
_adj=新链接列表[V];
对于(int i=0;i<_adj.Length;i++)
{
_adj[i]=新链接列表();
}
_V=V;
_定向的=定向的;
}
公共无效补遗(整数v,整数w)
{
_adj[v].AddLast(w);
如果(!\u指示)
{
_adj[w].AddLast(v);
}
}
public void BreadthFirstSearch(int s)//s是选中的节点
{
bool[]已访问=新bool[\u V];
对于(int i=0;i<\u V;i++)
访问[i]=错误;
//为BFS创建一个队列
LinkedList队列=新建LinkedList();
visited[s]=true;//如果访问了节点,则将其值切换为true
queue.AddLast(s);
while(queue.Any())
{
//将顶点从队列中取出并打印它
s=queue.First();
控制台。写入(s+“”);
queue.RemoveFirst();
链接列表=_adj[s];
foreach(列表中的var val)
{
如果(!已访问[val])
{
访问[val]=真;
queue.AddLast(val);
}
}
}
}
}
班级计划
{
静态void Main(字符串[]参数)
{
图g=新图(1400,true);//数量应等于或大于节点数量
g、 增编(1,2);
g、 增编(2,4);
g、 增编(2,5);
g、 增编(2,3);
Write(“从顶点2开始的宽度优先遍历:\n”);
g、 BreadthFirstSearch(2);//此处的数字显示起始节点
Console.Read();
}
}
}
一点。。。为什么要使用LinkedList而不仅仅是一个列表?(我认为列表通常更快…请参阅..另一点查看StyleCop并为变量指定有意义的名称。一点…为什么使用LinkedList而不仅仅是列表?(我认为列表通常更快…请参阅..另一点查看StyleCop并为变量指定有意义的名称。)。