forked from aalhour/C-Sharp-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphsDirectedSparseGraphTest.cs
147 lines (121 loc) · 5.83 KB
/
GraphsDirectedSparseGraphTest.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using DataStructures.Graphs;
using System;
using System.Linq;
using Xunit;
namespace UnitTest.DataStructuresTests
{
public static class GraphsDirectedSparseGraphTest
{
[Fact]
public static void DoTest()
{
var graph = new DirectedSparseGraph<string>();
var verticesSet1 = new string[] { "a", "z", "s", "x", "d", "c", "f", "v" };
graph.AddVertices(verticesSet1);
graph.AddEdge("a", "s");
graph.AddEdge("a", "z");
graph.AddEdge("s", "x");
graph.AddEdge("x", "d");
graph.AddEdge("x", "c");
graph.AddEdge("x", "a");
graph.AddEdge("d", "f");
graph.AddEdge("d", "c");
graph.AddEdge("d", "s");
graph.AddEdge("c", "f");
graph.AddEdge("c", "v");
graph.AddEdge("c", "d");
graph.AddEdge("v", "f");
graph.AddEdge("f", "c");
var allEdges = graph.Edges.ToList();
Assert.True(graph.VerticesCount == 8, "Wrong vertices count.");
Assert.True(graph.EdgesCount == 14, "Wrong edges count.");
Assert.True(graph.EdgesCount == allEdges.Count, "Wrong edges count.");
Assert.True(graph.OutgoingEdges("a").ToList().Count == 2, "Wrong outgoing edges from 'a'.");
Assert.True(graph.OutgoingEdges("s").ToList().Count == 1, "Wrong outgoing edges from 's'.");
Assert.True(graph.OutgoingEdges("d").ToList().Count == 3, "Wrong outgoing edges from 'd'.");
Assert.True(graph.OutgoingEdges("x").ToList().Count == 3, "Wrong outgoing edges from 'x'.");
Assert.True(graph.OutgoingEdges("c").ToList().Count == 3, "Wrong outgoing edges from 'c'.");
Assert.True(graph.OutgoingEdges("v").ToList().Count == 1, "Wrong outgoing edges from 'v'.");
Assert.True(graph.OutgoingEdges("f").ToList().Count == 1, "Wrong outgoing edges from 'f'.");
Assert.True(graph.OutgoingEdges("z").ToList().Count == 0, "Wrong outgoing edges from 'z'.");
Assert.True(graph.IncomingEdges("a").ToList().Count == 1, "Wrong incoming edges from 'a'.");
Assert.True(graph.IncomingEdges("s").ToList().Count == 2, "Wrong incoming edges from 's'.");
Assert.True(graph.IncomingEdges("d").ToList().Count == 2, "Wrong incoming edges from 'd'.");
Assert.True(graph.IncomingEdges("x").ToList().Count == 1, "Wrong incoming edges from 'x'.");
Assert.True(graph.IncomingEdges("c").ToList().Count == 3, "Wrong incoming edges from 'c'.");
Assert.True(graph.IncomingEdges("v").ToList().Count == 1, "Wrong incoming edges from 'v'.");
Assert.True(graph.IncomingEdges("f").ToList().Count == 3, "Wrong incoming edges from 'f'.");
Assert.True(graph.IncomingEdges("z").ToList().Count == 1, "Wrong incoming edges from 'z'.");
graph.RemoveEdge("d", "c");
graph.RemoveEdge("c", "v");
graph.RemoveEdge("a", "z");
Assert.True(graph.VerticesCount == 8, "Wrong vertices count.");
Assert.True(graph.EdgesCount == 11, "Wrong edges count.");
graph.RemoveVertex("x");
Assert.True(graph.VerticesCount == 7, "Wrong vertices count.");
Assert.True(graph.EdgesCount == 7, "Wrong edges count.");
graph.AddVertex("x");
graph.AddEdge("s", "x");
graph.AddEdge("x", "d");
graph.AddEdge("x", "c");
graph.AddEdge("x", "a");
graph.AddEdge("d", "c");
graph.AddEdge("c", "v");
graph.AddEdge("a", "z");
// BFS from A
// Walk the graph using BFS from A:
var bfsWalk = graph.BreadthFirstWalk("a");
// output: (s) (a) (x) (z) (d) (c) (f) (v)
foreach (var node in bfsWalk)
{
Console.Write(String.Format("({0})", node));
}
// DFS from A
// Walk the graph using DFS from A:
var dfsWalk = graph.DepthFirstWalk("a");
// output: (s) (a) (x) (z) (d) (c) (f) (v)
foreach (var node in dfsWalk)
{
Console.Write(String.Format("({0})", node));
}
// BFS from F
Console.WriteLine("Walk the graph using BFS from F:");
bfsWalk = graph.BreadthFirstWalk("f");
// output: (s) (a) (x) (z) (d) (c) (f) (v)
foreach (var node in bfsWalk)
{
Console.Write(String.Format("({0})", node));
}
// DFS from F
// Walk the graph using DFS from F:
dfsWalk = graph.DepthFirstWalk("f");
// output: (s) (a) (x) (z) (d) (c) (f) (v)
foreach (var node in dfsWalk)
{
Console.Write(String.Format("({0})", node));
}
/********************************************************************/
graph.Clear();
// Cleared the graph from all vertices and edges
var verticesSet2 = new string[] { "a", "b", "c", "d", "e", "f" };
graph.AddVertices(verticesSet2);
graph.AddEdge("a", "b");
graph.AddEdge("a", "d");
graph.AddEdge("b", "e");
graph.AddEdge("d", "b");
graph.AddEdge("d", "e");
graph.AddEdge("e", "c");
graph.AddEdge("c", "f");
graph.AddEdge("f", "f");
Assert.True(graph.VerticesCount == 6, "Wrong vertices count.");
Assert.True(graph.EdgesCount == 8, "Wrong edges count.");
// Walk the graph using DFS:
dfsWalk = graph.DepthFirstWalk();
// output: (a) (b) (e) (d) (c) (f)
foreach (var node in dfsWalk)
{
Console.Write(String.Format("({0})", node));
}
}
}
}