public void UndirectedGetNeighborsPointingToReciprocalTest() { MyGraph <string> graph = new MyGraph <string>(); graph.AddVertex("A"); graph.AddVertex("B"); Vertex <string> vertRef = null; Vertex <string> vertRef2 = null; foreach (var pair in graph.AdjacencyList) { if (pair.Key.Data == "A") { vertRef = pair.Key; } if (pair.Key.Data == "B") { vertRef2 = pair.Key; } } graph.AddUndirectedEdge(vertRef, vertRef2, 1); foreach (var pair in graph.AdjacencyList) { if (pair.Key.Data == "B") { List <Tuple <Vertex <string>, int> > neighbors = graph.InDegree(pair.Key); Assert.Equal("A", neighbors[0].Item1.Data); } } }
public void AddUndirectedEdge1to2() { MyGraph <string> graph = new MyGraph <string>(); Vertex <string> vert1 = new Vertex <string>("A"); Vertex <string> vert2 = new Vertex <string>("B"); graph.AdjacencyList.Add(vert1, new List <Edge <string> >()); graph.AdjacencyList.Add(vert2, new List <Edge <string> >()); graph.AddUndirectedEdge(vert1, vert2, 1); Assert.Equal(graph.AdjacencyList[vert1][0].Vertex, vert2); }
public void ClusteringCoefficientUndirectedTwoConnection() { MyGraph <string> graph = new MyGraph <string>(); var aa = graph.AddVertex("aa"); var bb = graph.AddVertex("bb"); var cc = graph.AddVertex("cc"); var dd = graph.AddVertex("dd"); graph.AddUndirectedEdge(aa, bb, 5); graph.AddUndirectedEdge(aa, cc, 5); graph.AddUndirectedEdge(aa, dd, 5); graph.AddUndirectedEdge(bb, cc, 5); graph.AddUndirectedEdge(cc, dd, 5); //graph.AddUndirectedEdge(dd, bb, 5); decimal output = graph.ClusteringCoefficientUndirected(aa); Assert.Equal(0.67.ToString(), Math.Round(output, 2).ToString()); }
private void BuildTheForest(int[,] grid, MyGraph <Coordinates> graph) { for (int i = 0; i < grid.GetLength(0); i++) { for (int j = 0; j < grid.GetLength(1); j++) { if (grid[i, j] == 1) { var tempCoordinates = new Coordinates(i, j); if (!graph.Contains(tempCoordinates)) { graph.AddNode(tempCoordinates); } if (GetValidValue(grid, i + 1, j) == 1) { var otherCoordinates = new Coordinates(i + 1, j); if (!graph.Contains(otherCoordinates)) { graph.AddNode(otherCoordinates); } graph.AddUndirectedEdge(graph.Nodes.FindByData(tempCoordinates), graph.Nodes.FindByData(otherCoordinates), 0); } if (GetValidValue(grid, i, j + 1) == 1) { var otherCoordinates = new Coordinates(i, j + 1); if (!graph.Contains(otherCoordinates)) { graph.AddNode(otherCoordinates); } graph.AddUndirectedEdge(graph.Nodes.FindByData(tempCoordinates), graph.Nodes.FindByData(otherCoordinates), 0); } } } } }
static void Main(string[] args) { MyGraph <string> graph = new MyGraph <string>(); var a = graph.AddVertex("a"); var b = graph.AddVertex("b"); var c = graph.AddVertex("c"); var d = graph.AddVertex("d"); var e = graph.AddVertex("e"); var f = graph.AddVertex("f"); var g = graph.AddVertex("g"); graph.AddUndirectedEdge(a, b, 5); graph.AddUndirectedEdge(b, c, 5); graph.AddUndirectedEdge(c, d, 5); graph.AddUndirectedEdge(d, e, 5); graph.AddUndirectedEdge(e, f, 5); graph.AddUndirectedEdge(f, g, 5); graph.AddUndirectedEdge(g, a, 5); graph.AddDirectedEdge(a, c, 1); graph.Print(); //graph.GetNeighborsPointingTo(a); //graph.GetNeighborsPointingFrom(a); //both run by: graph.GetNeighborsDirected(a); MyGraph <string> graph1 = new MyGraph <string>(); var j = graph1.AddVertex("A"); var k = graph1.AddVertex("B"); var l = graph1.AddVertex("C"); var m = graph1.AddVertex("D"); var n = graph1.AddVertex("E"); var o = graph1.AddVertex("F"); graph1.AddUndirectedEdge(j, k, 5); graph1.AddUndirectedEdge(k, l, 5); graph1.AddUndirectedEdge(k, m, 5); graph1.AddUndirectedEdge(l, n, 5); graph1.AddUndirectedEdge(l, o, 5); graph1.AddUndirectedEdge(m, o, 5); graph1.BreadthFirst(j); MyGraph <string> graph2 = new MyGraph <string>(); var aa = graph2.AddVertex("aa"); var bb = graph2.AddVertex("bb"); var cc = graph2.AddVertex("cc"); var dd = graph2.AddVertex("dd"); var ee = graph2.AddVertex("ee"); graph2.AddUndirectedEdge(aa, bb, 5); graph2.AddUndirectedEdge(aa, cc, 5); graph2.AddUndirectedEdge(aa, dd, 5); graph2.AddUndirectedEdge(bb, cc, 5); graph2.AddUndirectedEdge(cc, dd, 5); graph2.AddUndirectedEdge(dd, bb, 5); Console.WriteLine(graph2.ClusteringCoefficientUndirected(aa)); }