-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathGraphsBreadthFirstPathsTest.cs
70 lines (53 loc) · 2.1 KB
/
GraphsBreadthFirstPathsTest.cs
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
using System;
using System.Collections.Generic;
using DataStructures.Graphs;
using Algorithms.Graphs;
using Xunit;
using System.Diagnostics;
namespace UnitTest.AlgorithmsTests
{
public static class GraphsBreadthFirstPathsTest
{
private static string PrintPath(IEnumerable<string> path)
{
string output = string.Empty;
foreach (var node in path)
output = String.Format("{0}({1}) -> ", output, node);
output += "/TERMINATE/";
return output;
}
[Fact]
public static void DoTest()
{
IGraph<string> graph = new UndirectedSparseGraph<string>();
// Add vertices
var verticesSet1 = new string[] { "a", "z", "s", "x", "d", "c", "f", "v", "w", "m" };
graph.AddVertices(verticesSet1);
// Add edges
graph.AddEdge("a", "s");
graph.AddEdge("a", "z");
graph.AddEdge("s", "x");
graph.AddEdge("x", "d");
graph.AddEdge("x", "c");
graph.AddEdge("x", "w");
graph.AddEdge("x", "m");
graph.AddEdge("d", "f");
graph.AddEdge("d", "c");
graph.AddEdge("c", "f");
graph.AddEdge("c", "v");
graph.AddEdge("v", "f");
graph.AddEdge("w", "m");
var sourceNode = "f";
var bfsPaths = new BreadthFirstShortestPaths<string>(graph, sourceNode);
// TODO:
// - Assert distances
// - Assert ShortestPathTo a node
var distance = bfsPaths.DistanceTo("a");
Trace.WriteLine("Distance from '" + sourceNode + "' to 'a' is: " + bfsPaths.DistanceTo("a"));
Trace.WriteLine("Path from '" + sourceNode + "' to 'a' is : " + PrintPath(bfsPaths.ShortestPathTo("a")));
Trace.WriteLine(string.Empty);
Trace.WriteLine("Distance from '" + sourceNode + "' to 'w' is: " + bfsPaths.DistanceTo("w"));
Trace.WriteLine("Path from '" + sourceNode + "' to 'w' is : " + PrintPath(bfsPaths.ShortestPathTo("w")));
}
}
}