Консультация № 185073
06.01.2012, 12:55
62.66 руб.
0 0 0
Здравствуйте, уважаемые эксперты! Прошу вас ответить на следующий вопрос: прошу мне помочь мне сделать подробное описание программы, математическая постановка задачи, входные и выходные данные, и ручная просчёт программы
Код:
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace myGraph
{
public class mygraph
{
private class graphnode
{
private int[,] array;

public int this [int i, int j]
{
get
{
return array[i, j];
}
set
{
array[i, j] = value;
}
}

public int arrSize
{
get
{
return array.GetLength(0);
}
}

public bool[] know;

//флаг, помечающий просмотренные вершины
//дефолтное значение false
public void KnowSet()
{
for (int i = 0; i < arrSize; i++)
{
know[i] = false;
}
}

public graphnode(int[,] a)
{
array = a;
know = new bool[a.GetLength(0)];
}


//обход в ширину
public void widthSearch(int v)
{
myQueue q = new myQueue();
q.Add(v);
know[v] = true;
while (!q.IsEmpty)
{
v = (int)q.Take();
Console.Write("{0} ", v);

for (int u = 0; u < arrSize; u++)
{
if (array[v,u] != 0 && !know[u])
{
q.Add(u);
know[u] = true;
}
}
}
}
}
// конец graphnode

private graphnode graph;
public mygraph(string name)
{
using (StreamReader fileIn = new StreamReader(name))
{
string temp = fileIn.ReadLine();
string[] tempmas = temp.Split(new char[]{ ' ' }, StringSplitOptions.RemoveEmptyEntries);
int n = tempmas.Length;
int[,] a = new int[n, n];
for (int i = 0; i <n; i++)
{
string[] mas = temp.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
for (int j = 0; j <n; j++)
{
a[i, j] = int.Parse(mas[j]);
}
temp = fileIn.ReadLine();
}
graph = new graphnode(a);
}
}

// вывод таблицы смежности вершин
public void ShowArr()
{
for (int i = 0; i<graph.arrSize; i++ )
{
for (int j=0; j<graph.arrSize; j++)
{
Console.Write("{0,4}",graph[i,j]);
}
Console.WriteLine();
}
}


public void widthSearch(int v)
{
graph.KnowSet();
graph.widthSearch(v);
Console.WriteLine();
}

}
}

Обсуждение

Форма ответа