-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUndirectedGraphTests.cs
More file actions
71 lines (59 loc) · 1.96 KB
/
UndirectedGraphTests.cs
File metadata and controls
71 lines (59 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Collections.Generic;
using System.Text;
using CoreConcepts.DataStructures.Graphs;
using Xunit;
namespace CoreConcepts.Tests.DataStructures.Graphs
{
public class UndirectedGraphTests
{
[Fact]
public void Test_Undirected_Graph_BFS()
{
// Define vertices
int[] vertices = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Define edges
(int leftVertex, int rightVertex)[] edges = new[] {
(1,2), (1,3),
(2,4), (3,5),
(3,6), (4,7),
(5,7), (5,8),
(5,6), (8,9),
(9,10)};
UndirectedGraph graph = new UndirectedGraph(vertices, edges);
var (vertexFound, visitedVertices) = graph.BreadthFirstSearch(1, 6);
Assert.True(vertexFound);
Assert.NotEmpty(visitedVertices);
Console.WriteLine("BFT:");
Console.WriteLine(ListToString(visitedVertices));
}
[Fact]
public void Test_Undirected_Graph_DFS()
{
// Define vertices
int[] vertices = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Define edges
(int leftVertex, int rightVertex)[] edges = new[] {
(1,2), (1,3),
(2,4), (3,5),
(3,6), (4,7),
(5,7), (5,8),
(5,6), (8,9),
(9,10)};
UndirectedGraph graph = new UndirectedGraph(vertices, edges);
var (vertexFound, visitedVertices) = graph.DepthFirstSearch(1, 10);
Assert.True(vertexFound);
Assert.NotEmpty(visitedVertices);
}
public static string ListToString(List<int> list)
{
StringBuilder sb = new StringBuilder("{");
foreach (int item in list)
{
sb.Append($" {item} ");
}
sb.Append("}");
return sb.ToString();
}
}
}