0% found this document useful (0 votes)
9 views

File KTra LTDO

The document contains code for performing depth-first search (DFS) on graphs represented as adjacency lists to find connected components and paths between nodes. It reads a graph from a file, performs DFS to find connected and reachable nodes, and outputs the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

File KTra LTDO

The document contains code for performing depth-first search (DFS) on graphs represented as adjacency lists to find connected components and paths between nodes. It reads a graph from a file, performs DFS to find connected and reachable nodes, and outputs the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

static void Bai_01()

{
int sodinh;
int start;
List<List<int>> dske;

try
{
using(StreamReader sr = new StreamReader("LienThongDFS.INP"))
{
String[] abc = sr.ReadLine().Split();
sodinh = int.Parse(abc[0]);
start = int.Parse(abc[1]);
dske = new List<List<int>>();
for(int i=0; i < sodinh; i++)
{
abc = sr.ReadLine().Split(new char[] { ' ' },
StringSplitOptions.RemoveEmptyEntries);
List<int> hang = new List<int>();
foreach(string s in abc)
{
hang.Add(int.Parse(s));
}
dske.Add(hang);

//Thuat Toan xu ly' DFS


Stack<int> stack = new Stack<int>();
int[] luuvet = new int[sodinh];

stack.Push(start);
luuvet[start - 1] = -1;
while(stack.Count != 0)
{
int dinhxet = stack.Pop();
foreach(int s in dske[dinhxet - 1])
{
if (luuvet[s - 1] == 0)
{
stack.Push(s);
luuvet[s - 1] =-1;
}
}

//xac dinh cac dinh lien thong voi star

List<int> dinhlienthong = new List<int>();


for (int dinh = 1; dinh <= sodinh; dinh++)
{
if (luuvet[dinh - 1] != 0 && dinh != start)
{
dinhlienthong.Add(dinh);
}
}
Console.WriteLine(dinhlienthong.Count);
//In ra
foreach (int s in dinhlienthong)
{

Console.Write( s+" ");


}

catch (FileNotFoundException)
{
Console.WriteLine("Khong Tim Thay File !");
}

static void Bai_02()


{

int sodinh,start,end;

List<List<int>> dske;
int [] parent= new int[1000];

try
{
using (StreamReader sr = new StreamReader("TimDuongDFS.INP"))
{
String[] abc = sr.ReadLine().Split(new char[] { ' ' },
StringSplitOptions.RemoveEmptyEntries);
sodinh = int.Parse(abc[0]);
start = int.Parse(abc[1]);
end = int.Parse(abc[2]);

dske = new List<List<int>>();


for (int i = 0; i < sodinh; i++)
{
abc = sr.ReadLine().Split(new char[] { ' ' });
List<int> hang = new List<int>();
foreach (string s in abc)
{
hang.Add(int.Parse(s));
}
dske.Add(hang);
}

// Thuật toán xử lý DFS


Stack<int> stack = new Stack<int>();
int[] luuvet = new int[sodinh];

stack.Push(start);
luuvet[start - 1] = -1;
while (stack.Count != 0)
{
int dinhxet = stack.Pop();

foreach (int dinhke in dske[dinhxet - 1])


{
if (luuvet[dinhke - 1] == 0)
{
stack.Push(dinhke);
luuvet[dinhke - 1] = dinhxet;
}
}
}

// Xác định các đỉnh liên thông với start


List<int> duongdi = new List<int>();
if (luuvet[end - 1] != 0)
{
for(int i= end; i != -1; i = luuvet[i - 1])
{
duongdi.Add(i);
}
duongdi.Reverse();
}

// Ghi kết quả ra file output


using (StreamWriter sw = new StreamWriter("TimDuongDFS.OUT"))
{

if (duongdi.Count == 0)
{
Console.WriteLine("Khong co duong di tu dinh"+start+"den
dinh"+end);
sw.WriteLine(0);

}
else
{
Console.WriteLine(" Co duong di tu dinh " + start + " den
dinh " + end);
sw.WriteLine(duongdi.Count);
Console.WriteLine(duongdi.Count);
foreach(int dinh in duongdi)
{
Console.Write(dinh+" ");
sw.Write(dinh + " ");
}
}
}
}
}
catch (FileNotFoundException)
{
Console.WriteLine("Không Tìm Thấy File!");
}
}
}

You might also like