public void testAddstringMultiple() { AdjacencyListGraph <string, string> g = graph(); Assert.IsTrue(g.AddNode(n1)); Assert.IsTrue(g.AddNode(n2)); }
public void testSetEdgeDataBothNull() { AdjacencyListGraph <string, string> g = graph(n1, n2); g.AddEdge(e1); g.ReplaceEdge <string, string, string>(null, null); }
public void testParentsMultiGraph() { AdjacencyListGraph <string, string> g = graph(n1, n2, n3, n4); Edge <string, string> e5 = new Edge <string, string>(n2, n1, "lorem"); g.AddEdge(e1); g.AddEdge(e2); g.AddEdge(e3); g.AddEdge(e4); g.AddEdge(e5); ISet <string> Parents1 = g.Parents(n1); Assert.IsTrue(Parents1.Contains(n2)); Assert.AreEqual(1, Parents1.Count); ISet <string> Parents2 = g.Parents(n2); Assert.IsTrue(Parents2.Contains(n1)); Assert.AreEqual(1, Parents2.Count); ISet <string> Parents3 = g.Parents(n3); Assert.IsTrue(Parents3.Contains(n2)); Assert.AreEqual(1, Parents3.Count); ISet <string> Parents4 = g.Parents(n4); Assert.IsTrue(Parents4.Contains(n1)); Assert.IsTrue(Parents4.Contains(n3)); Assert.AreEqual(2, Parents4.Count); }
public void testRemovestringNoParents() { AdjacencyListGraph <string, string> g = graph(n1); Assert.IsTrue(g.RemoveNode(n1)); Assert.IsFalse(g.ContainsNode(n1)); }
public void testContainsNoEdge() { AdjacencyListGraph <string, string> g = graph(n1, n2); Edge <string, string> e = new Edge <string, string>(n1, n3, "label"); Assert.IsFalse(g.ContainsEdge(e)); }
public void testChildrenMultiGraph() { AdjacencyListGraph <string, string> g = graph(n1, n2, n3, n4); Edge <string, string> e5 = new Edge <string, string>(n2, n1, "lorem"); g.AddEdge(e1); g.AddEdge(e2); g.AddEdge(e3); g.AddEdge(e4); g.AddEdge(e5); ISet <string> Children1 = g.Children(n1); Assert.IsTrue(Children1.Contains(n2)); Assert.IsTrue(Children1.Contains(n4)); Assert.AreEqual(2, Children1.Count); ISet <string> Children2 = g.Children(n2); Assert.IsTrue(Children2.Contains(n3)); Assert.IsTrue(Children2.Contains(n1)); Assert.AreEqual(2, Children2.Count); ISet <string> Children3 = g.Children(n3); Assert.IsTrue(Children3.Contains(n4)); Assert.AreEqual(1, Children3.Count); ISet <string> Children4 = g.Children(n4); Assert.AreEqual(0, Children4.Count); }
public void testContainsstringMultiple() { AdjacencyListGraph <string, string> g = graph(n1, n2); Assert.IsTrue(g.ContainsNode(n2)); Assert.IsTrue(g.ContainsNode(n1)); }
public void testAddBadEdge() { AdjacencyListGraph <string, string> g = graph(n1, n2); Edge <string, string> e = new Edge <string, string>(n1, n3, "label"); Assert.IsFalse(g.AddEdge(e)); }
public static AdjacencyListGraph <int> CreateShortestPathGraph1() { var vertexKeys = new List <int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; //需要无向的图啊 var graph = new AdjacencyListGraph <int>(false); var vertexs = graph.CreateVertexs(vertexKeys); var edges = new List <AdjacencyEdge <int> > { graph.CreateEdge(vertexs[0], vertexs[1], 3), graph.CreateEdge(vertexs[1], vertexs[2], 4), graph.CreateEdge(vertexs[2], vertexs[3], 1), graph.CreateEdge(vertexs[2], vertexs[0], 2), graph.CreateEdge(vertexs[3], vertexs[4], 3), graph.CreateEdge(vertexs[4], vertexs[0], 1), graph.CreateEdge(vertexs[2], vertexs[5], 1), graph.CreateEdge(vertexs[5], vertexs[6], 1), graph.CreateEdge(vertexs[6], vertexs[7], 2), graph.CreateEdge(vertexs[7], vertexs[5], 2), graph.CreateEdge(vertexs[8], vertexs[7], 9), }; graph.CreatGraph(vertexs, edges); return(graph); }
public static AdjacencyListGraph <int> CreateStronglyConnectedGraph1() { var vertexKeys = new List <int> { 1, 2, 3, 4, 5 }; var graph = new AdjacencyListGraph <int>(true); var vertexs = graph.CreateVertexs(vertexKeys); var edges = new List <AdjacencyEdge <int> > { graph.CreateEdge(vertexs[0], vertexs[1]), graph.CreateEdge(vertexs[1], vertexs[2]), graph.CreateEdge(vertexs[2], vertexs[3]), graph.CreateEdge(vertexs[2], vertexs[0]), graph.CreateEdge(vertexs[3], vertexs[4]), graph.CreateEdge(vertexs[4], vertexs[0]), }; graph.CreatGraph(vertexs, edges); return(graph); }
public static AdjacencyListGraph <int> CreateDirectionNoCircuitGraph1(bool hasDirection = false) { var vertexKeys = new List <int> { 1, 2, 3, 4, 5 }; var graph = new AdjacencyListGraph <int>(hasDirection); var vertexs = graph.CreateVertexs(vertexKeys); var edges = new List <AdjacencyEdge <int> > { graph.CreateEdge(vertexs[0], vertexs[1]), graph.CreateEdge(vertexs[0], vertexs[4]), graph.CreateEdge(vertexs[1], vertexs[2]), graph.CreateEdge(vertexs[1], vertexs[3]), }; graph.CreatGraph(vertexs, edges); return(graph); }
public void testAddstringDuplicate() { AdjacencyListGraph <string, string> g = graph(); Assert.IsTrue(g.AddNode(n1)); Assert.IsFalse(g.AddNode(n1)); }
private IGraph <Vector2> GetMazeGraph() { IGraph <Vector2> graph = new AdjacencyListGraph <Vector2>(); IGraphNode <Vector2>[,] nodes = new IGraphNode <Vector2> [size, size]; for (int y = 0; y < size; ++y) { for (int x = 0; x < size; ++x) { nodes[y, x] = graph.AddNode(new Vector2(x, y)); } } for (int i = 1; i < size; ++i) { graph.AddUndirectedEdge(nodes[i - 1, 0], nodes[i, 0], Random.Range(0f, 1f)); graph.AddUndirectedEdge(nodes[0, i - 1], nodes[0, i], Random.Range(0f, 1f)); } for (int y = 1; y < size; ++y) { for (int x = 1; x < size; ++x) { graph.AddUndirectedEdge(nodes[y - 1, x], nodes[y, x], Random.Range(0f, 1f)); graph.AddUndirectedEdge(nodes[y, x - 1], nodes[y, x], Random.Range(0f, 1f)); } } IMSTStrategy <Vector2> strategy = new KruskalsAlgorithm <Vector2>(); return(new MSTResultGraph <Vector2>(graph, strategy)); }
public void VeryHardGraphWithFifteenCyclesTest() { var graph = new AdjacencyListGraph(15); graph.AddArrow(8, 4); graph.AddArrow(3, 4); graph.AddArrow(5, 10); graph.AddArrow(0, 1); graph.AddArrow(11, 9); graph.AddArrow(9, 11); graph.AddArrow(14, 7); graph.AddArrow(1, 3); graph.AddArrow(10, 6); graph.AddArrow(11, 5); graph.AddArrow(5, 11); graph.AddArrow(8, 13); graph.AddArrow(13, 8); graph.AddArrow(4, 2); graph.AddArrow(2, 0); graph.AddArrow(9, 12); graph.AddArrow(12, 9); graph.AddArrow(12, 8); graph.AddArrow(8, 12); graph.AddArrow(7, 13); graph.AddArrow(1, 6); graph.AddArrow(6, 14); graph.AddArrow(0, 5); graph.AddArrow(9, 2); graph.AddArrow(7, 3); var result = searcher.FindCycles(graph); Assert.That(result.Count, Is.EqualTo(10)); }
public void GraphWithVertexEdgePropertiesTest() { var graph = new AdjacencyListGraph <int, int>(); var v0 = new Vertex(0, 0); var v1 = graph.AddVertex(new Vertex(1, 1)); graph.AddVertex(v0); Assert.AreEqual(default(int), graph.GetVertexProp(v0)); graph.SetVertexProp(v0, 1); Assert.AreEqual(1, graph.GetVertexProp(v0)); Assert.AreEqual(default(int), graph.GetVertexProp(v1)); graph.SetVertexProp(v1, -5); Assert.AreEqual(-5, graph.GetVertexProp(v1)); graph.Clear(); Assert.Throws <GeomException>(() => graph.GetVertexProp(v1)); graph.AddVertex(v0); graph.AddVertex(v1); var e = new Edge(v0, v1); graph.AddEdge(e); Assert.AreEqual(default(int), graph.GetEdgeProp(e)); graph.SetEdgeProp(e, int.MaxValue); Assert.AreEqual(int.MaxValue, graph.GetEdgeProp(e)); graph.Clear(); Assert.Throws <GeomException>(() => graph.GetEdgeProp(e)); }
private Tuple <IEnumerable <string>, string> GetDecompositionWithGraph(AdjacencyListGraph <string> graph, string k, List <Vertex> parcoured, List <string> result, StringBuilder vs) { if (parcoured == null) { parcoured = new List <Vertex>(); } if (result == null) { result = new List <string>(); } Vertex v = graph.GetVertex(k); if (v != null && !parcoured.Contains(v)) { parcoured.Add(v); foreach (Vertex link in v.LinkedVertices) { result.Add(link.Value); vs.Append(String.Format("{0} -> {1};\r\n", v.Value, link.Value)); GetDecompositionWithGraph(graph, link.Value, parcoured, result, vs); } } return(new Tuple <IEnumerable <string>, string>(result, vs.ToString())); }
public BreathFirstSearchTest() { _graph = new AdjacencyListGraph <int>(); _graph.AddVertex(0); _graph.AddVertex(1); _graph.AddVertex(2); _graph.AddVertex(3); _graph.AddVertex(4); _graph.AddVertex(5); _graph.AddVertex(6); _graph.AddVertex(7); _graph.AddVertex(8); _graph.AddVertex(9); _graph.AddVertex(10); _graph.AddVertex(11); _graph.AddVertex(12); _graph.AddEdge(0, 1); _graph.AddEdge(0, 2); _graph.AddEdge(0, 5); _graph.AddEdge(0, 6); _graph.AddEdge(3, 4); _graph.AddEdge(3, 5); _graph.AddEdge(4, 5); _graph.AddEdge(4, 6); _graph.AddEdge(7, 8); _graph.AddEdge(9, 10); _graph.AddEdge(9, 11); _graph.AddEdge(9, 12); _graph.AddEdge(11, 12); Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); }
public void DijkstraShortestPath_EmptyGraphTest() { var graph = new AdjacencyListGraph <int>(); var path = graph.DijkstraShortestPath(1, 10); Assert.AreEqual(0, path.Count()); }
private AdjacencyListGraph <int> CreateNotConnectedComponenetsGraph1() { var vertexKeys = new List <int> { 1, 2, 3, 4, 5, 6, 7, 8 }; var graph = new AdjacencyListGraph <int>(true); var vertexs = graph.CreateVertexs(vertexKeys); var edges = new List <AdjacencyEdge <int> > { graph.CreateEdge(vertexs[0], vertexs[1]), graph.CreateEdge(vertexs[1], vertexs[2]), graph.CreateEdge(vertexs[2], vertexs[3]), graph.CreateEdge(vertexs[2], vertexs[0]), graph.CreateEdge(vertexs[3], vertexs[4]), graph.CreateEdge(vertexs[4], vertexs[0]), graph.CreateEdge(vertexs[5], vertexs[6]), graph.CreateEdge(vertexs[6], vertexs[7]), graph.CreateEdge(vertexs[7], vertexs[5]), }; graph.CreatGraph(vertexs, edges); return(graph); }
private IEnumerable <string> GetDecomposition(AdjacencyListGraph <string> graph, string k, List <Vertex> parcoured, List <string> result) { if (parcoured == null) { parcoured = new List <Vertex>(); } if (result == null) { result = new List <string>(); } Vertex v = graph.GetVertex(k); if (v != null && !parcoured.Contains(v)) { parcoured.Add(v); foreach (Vertex link in v.LinkedVertices) { result.Add(link.Value); GetDecomposition(graph, link.Value, parcoured, result); } } return(result); }
/// <summary> /// Verifies if the given graph is a t-spanner for the given ratio a_t. /// </summary> /// <param name="a_graph"></param> /// <param name="a_t"></param> /// <returns>A vertification struct which holds the ratio and possibly a falsification pair. </returns> public static SpannerVerification VerifySpanner(IGraph a_graph, float a_t) { //first determine the possible edges var completeGraph = new AdjacencyListGraph(new List <Vertex>(a_graph.Vertices)); completeGraph.MakeComplete(); var edges = completeGraph.Edges.ToList(); edges.Sort(); Vertex Start = null; Vertex End = null; var ratio = 1f; // best possible ratio foreach (var edge in edges) { // find distance in given graph // TODO all-pair shortest path var dist = ShortestPath.ShorthestDistance(a_graph, edge.Start, edge.End); // compare ratios float edgeratio = dist / edge.Weight; if (edgeratio > a_t) { Start = edge.Start; End = edge.End; } if (ratio <= edgeratio) { ratio = edgeratio; } } return(new SpannerVerification(Start, End, ratio)); }
public void SmallGraph_ReverseTest() { var graph = new AdjacencyListGraph <int>(); graph.AddEdge(1, 2); graph.AddEdge(1, 3); graph.AddEdge(1, 4); graph.AddEdge(2, 3); graph.AddEdge(3, 4); var reversedGraph = graph.Reverse(); Assert.AreEqual(graph.VertexCount, reversedGraph.VertexCount); Assert.AreEqual(graph.EdgeCount, reversedGraph.EdgeCount); var pathIdx = 0; var paths = new[] { new int[0], new[] { 1 }, new[] { 1, 2 }, new[] { 1, 3 } }; for (int vertex = 1; vertex <= 4; vertex++) { var targetIdx = 0; var path = paths[pathIdx++]; foreach (var edge in reversedGraph.EdgesOf(vertex)) { Assert.AreEqual(path[targetIdx++], edge.Target); } Assert.AreEqual(path.Length, targetIdx); } }
public static AdjacencyListGraph <int> CreateDAGShortestPathGraph1() { var vertexKeys = new List <int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var graph = new AdjacencyListGraph <int>(true); var vertexs = graph.CreateVertexs(vertexKeys); var edges = new List <AdjacencyEdge <int> >(); edges.Add(graph.CreateEdge(vertexs[0], vertexs[1], 3)); edges.Add(graph.CreateEdge(vertexs[1], vertexs[2], 4)); edges.Add(graph.CreateEdge(vertexs[2], vertexs[3], 1)); edges.Add(graph.CreateEdge(vertexs[3], vertexs[4], 3)); edges.Add(graph.CreateEdge(vertexs[2], vertexs[5], 1)); edges.Add(graph.CreateEdge(vertexs[5], vertexs[6], 1)); edges.Add(graph.CreateEdge(vertexs[6], vertexs[7], 2)); edges.Add(graph.CreateEdge(vertexs[8], vertexs[7], 9)); graph.CreatGraph(vertexs, edges); return(graph); }
public void SetUp() { _graph = new AdjacencyListGraph <int>(); _graph.AddVertex(0); _graph.AddVertex(1); _graph.AddVertex(2); _graph.AddVertex(3); _graph.AddVertex(4); _graph.AddVertex(5); _graph.AddVertex(6); _graph.AddVertex(7); _graph.AddVertex(8); _graph.AddVertex(9); _graph.AddVertex(10); _graph.AddVertex(11); _graph.AddVertex(12); _graph.AddEdge(0, 1); _graph.AddEdge(0, 2); _graph.AddEdge(0, 5); _graph.AddEdge(0, 6); _graph.AddEdge(3, 4); _graph.AddEdge(3, 5); _graph.AddEdge(4, 5); _graph.AddEdge(4, 6); _graph.AddEdge(7, 8); _graph.AddEdge(9, 10); _graph.AddEdge(9, 11); _graph.AddEdge(9, 12); _graph.AddEdge(11, 12); }
public void DFSWithAction_NoVertices_SmallGraphTest() { var graph = new AdjacencyListGraph <int>(); foreach (var vertex in graph.DFS(1, v => { })) { } }
public void testSizeEdgesMultigraphEdges() { AdjacencyListGraph <string, string> g = graph(n1, n2, n3, n4); g.AddEdge(e1); g.AddEdge(e2); g.AddEdge(e3); g.AddEdge(e4); Assert.AreEqual(4, g.CountEdges); }
public void TestOneElementIncidenceMatrix() { var graph = new AdjacencyListGraph(1); var incidenceMatrix = graph.GetIncidenceMatrix(); Assert.That(incidenceMatrix.GetLength(0), Is.EqualTo(1)); Assert.That(incidenceMatrix.GetLength(1), Is.EqualTo(0)); }
public void BFS_NoVertices_SmallGraphTest() { var graph = new AdjacencyListGraph <int>(); foreach (var vertex in graph.BFS(1)) { } }
// Use this for initialization void Start() { Cursor.lockState = CursorLockMode.Locked; cc = GetComponent <CharacterController>(); floorGraph = new AdjacencyListGraph <string, Edge <string> >(); floorGraph.AddVertex("Start"); }
public void testReplaceNodeNoDuplicate() { AdjacencyListGraph <string, string> g = graph(n1); g.ReplaceNode(n1, "new_label"); Assert.IsTrue(g.ContainsNode("new_label")); Assert.IsFalse(g.ContainsNode(n1)); }
private IEnumerable<string> GetDecomposition(AdjacencyListGraph<string> graph, string k, List<Vertex> parcoured, List<string> result) { if (parcoured == null) { parcoured = new List<Vertex>(); } if (result == null) { result = new List<string>(); } Vertex v = graph.GetVertex(k); if (v != null && !parcoured.Contains(v)) { parcoured.Add(v); foreach (Vertex link in v.LinkedVertices) { result.Add(link.Value); GetDecomposition(graph, link.Value, parcoured, result); } } return result; }
private Tuple<IEnumerable<string>, string> GetDecompositionWithGraph(AdjacencyListGraph<string> graph, string k, List<Vertex> parcoured, List<string> result, StringBuilder vs) { if (parcoured == null) { parcoured = new List<Vertex>(); } if (result == null) { result = new List<string>(); } Vertex v = graph.GetVertex(k); if (v != null && !parcoured.Contains(v)) { parcoured.Add(v); foreach (Vertex link in v.LinkedVertices) { result.Add(link.Value); vs.Append(String.Format("{0} -> {1};\r\n", v.Value, link.Value)); GetDecompositionWithGraph(graph, link.Value, parcoured, result, vs); } } return new Tuple<IEnumerable<string>, string>(result, vs.ToString()); }