public static int TestGraph(DirectedGraph graph, Node start, Node target) { Debug.LogFormat("Graph = {0}", graph); Dictionary <Node, Edge> path = GraphOperation.getShortestPathDistances(graph, start, target); if (graph == null || !graph.Contains(start) || !graph.Contains(target)) { Debug.LogFormat("Error param"); return(-1); } if (path == null) { Debug.LogFormat("No solution"); return(-1); } Node currentNode = target; string displayPath = currentNode.ToString(); while (currentNode != start) { Edge edge = path[currentNode]; currentNode = edge.src; displayPath = currentNode + "->" + displayPath; } Debug.LogFormat(displayPath + " : " + path[target].weight); return(path[target].weight); }
public void AddVertex() { DirectedGraph <string, int> graph = new DirectedGraph <string, int>(); graph.AddVertex("A"); graph.AddVertex("B"); Assert.AreEqual(2, graph.Count); Assert.IsTrue(graph.Contains("A")); Assert.IsTrue(graph.Contains("B")); }
/// <summary> /// Creates and returns the dependency graph. The result will be cached. /// </summary> /// <param name="forceRecompute">Whether to force the recomputation of the dependency graph. If false, the cached graph will be used, if it has been previously computed.</param> public DirectedGraph <Type, DirectedEdge <Type> > CreateGraph(bool forceRecompute = false) { if (!forceRecompute && cachedGraph != null) { return(cachedGraph); } var graphNodes = container.Kernel.GraphNodes; var graph = new DirectedGraph <Type, DirectedEdge <Type> >(); //Build the nodes foreach (var graphNode in graphNodes) { if (graphNode is ComponentModel componentModel) { foreach (var service in componentModel.Services) { if (!graph.Contains(service)) { graph.Add(service); } } if (!graph.Contains(componentModel.Implementation)) { graph.Add(componentModel.Implementation); } } } //Build the edges foreach (var graphNode in graphNodes) { if (graphNode is ComponentModel componentModel) { foreach (var dep in componentModel.Dependencies) { foreach (var service in componentModel.Services) { graph.Add(new DirectedEdge <Type>(service, dep.TargetItemType)); } graph.Add(new DirectedEdge <Type>(componentModel.Implementation, dep.TargetItemType)); } } } cachedGraph = graph; return(graph); }
public void TestAddEdge() { IDirectedGraph <INode> directedGraph = new DirectedGraph(); INode[] nodes = EntityFactory.CreateDummyNodes(3); INode a = nodes[0]; INode b = nodes[1]; INode c = nodes[2]; // create a -> b -> c IEdge ab = new Edge(a, b); IEdge bc = new Edge(b, c); directedGraph.AddEdge(ab); directedGraph.AddEdge(bc); Assert.True(directedGraph.Contains(a) && directedGraph.Contains(b) && directedGraph.Contains(c), $"Not every node was added.."); }
public DirectedGraph <T> RemoveAllNodes(DirectedGraph <T> graph) { var inv = this.Inverse(); foreach (var item in this.Where(a => graph.Contains(a)).ToList()) { this.RemoveFullNode(item, inv.RelatedTo(item)); } return(this); }
public void RemoveVertex() { DirectedGraph <string, int> graph = new DirectedGraph <string, int>(); graph.AddVertex("A"); graph.AddVertex("B"); Assert.IsTrue(graph.RemoveVertex("B")); Assert.AreEqual(1, graph.Count); Assert.IsTrue(graph.Contains("A")); Assert.IsFalse(graph.RemoveVertex("bla")); }
public void RemoveOrphanedVerticesWhenEdgeIsRemovedTest() { DirectedGraph <string, DirectedEdge <string> > graph = new DirectedGraph <string, DirectedEdge <string> >(); graph.Add(new DirectedEdge <string>("A", "B")); // A->B graph.Add(new DirectedEdge <string>("A", "C")); // A->C graph.Add(new DirectedEdge <string>("B", "D")); // B->D graph.Add(new DirectedEdge <string>("C", "D")); // C->D graph.Add(new DirectedEdge <string>("D", "E")); // D->E graph.Add(new DirectedEdge <string>("H", "G")); // H->G DirectedEdge <string> toAdd = new DirectedEdge <string>("G", "F"); graph.Add(toAdd); // G->F graph.RemoveOrphanedVerticesOnEdgeRemoval = true; graph.Remove(toAdd); HashSet <string> orphanedVertices = graph.GetOrphanedVertices(); Assert.AreEqual(0, orphanedVertices.Count); Assert.IsFalse(graph.Contains("F")); }
public void GenerateDotTextFromDistanceGraph() { var text = new DirectedGraph() .AddNode("Start") .AddNode("Middle1") .AddNode("Middle2") .AddNode("End") .AddEdge("Start", "End", 2d, true) .AddEdge("Start", "Middle1", 1d, true) .AddEdge("Middle1", "Middle2", 1d, true) .AddEdge("Middle2", "End", 1, true) .ToDotText(); Assert.IsTrue(text.Contains("Start [")); Assert.IsTrue(text.Contains("Middle1 [")); Assert.IsTrue(text.Contains("Middle2 [")); Assert.IsTrue(text.Contains("End [")); Assert.IsTrue(text.Contains("Start -> End")); Assert.IsTrue(text.Contains("Start -> Middle1")); Assert.IsTrue(text.Contains("Middle1 -> Middle2")); Assert.IsTrue(text.Contains("Middle2 -> End")); }
public void TransitiveClosureBasicTest() { // create an integer graph and set the edge producer func DirectedGraph <int, DirectedEdge <int> > g = new DirectedGraph <int, DirectedEdge <int> >((a, b) => new DirectedEdge <int>(a, b)); DirectedEdge <int> toAdd = new DirectedEdge <int>(1, 2); g.Add(toAdd); Assert.IsTrue(g.Contains(toAdd)); g.Add(new DirectedEdge <int>(2, 3)); g.Add(new DirectedEdge <int>(3, 4)); DirectedGraph <int, DirectedEdge <int> > h = g.TransitiveClosure(); Assert.IsTrue(h.VertexCount == 4); Assert.IsTrue(h.EdgeCount == 6); Assert.IsTrue(h.ContainsEdge(1, 2)); Assert.IsTrue(h.ContainsEdge(2, 3)); Assert.IsTrue(h.ContainsEdge(3, 4)); Assert.IsTrue(h.ContainsEdge(1, 3)); Assert.IsTrue(h.ContainsEdge(1, 4)); Assert.IsTrue(h.ContainsEdge(2, 4)); }
public void CommandifiedGraphAddRemoveVertexWithUndoTest() { Guid sessionId = Guid.NewGuid(); CQManager.ActivateCommandQueueStack(sessionId); DirectedGraph <int, DirectedEdge <int> > graph = new DirectedGraph <int, DirectedEdge <int> >(true); graph.Add(42); Assert.IsTrue(graph.Contains(42)); CQManager.UndoLastCommand(); Assert.IsFalse(graph.Contains(42)); Assert.AreEqual(0, graph.VertexCount); graph.Add(42); graph.Add(13); graph.Add(10); Assert.IsTrue(graph.Contains(42)); Assert.IsTrue(graph.Contains(13)); Assert.IsTrue(graph.Contains(10)); graph.Add(new DirectedEdge <int>(42, 13)); // 42 -> 13 graph.Add(new DirectedEdge <int>(42, 10)); // 42 -> 10 Assert.AreEqual(2, graph.EdgeCount); Assert.IsTrue(graph.ContainsEdge(42, 13)); Assert.IsTrue(graph.ContainsEdge(42, 10)); graph.Remove(42); Assert.IsFalse(graph.Contains(42)); Assert.AreEqual(0, graph.EdgeCount); Assert.AreEqual(2, graph.VertexCount); // undo removal of 42. This should re-add 42, but also re-add the edges CQManager.UndoLastCommand(); Assert.AreEqual(2, graph.EdgeCount); Assert.IsTrue(graph.ContainsEdge(42, 13)); Assert.IsTrue(graph.ContainsEdge(42, 10)); Assert.IsTrue(graph.Contains(42)); CQManager.ActivateCommandQueueStack(Guid.Empty); }
public void CommandifiedGraphAddRemoveGraphAndEdgesWithUndoTest() { Guid sessionId = Guid.NewGuid(); CQManager.ActivateCommandQueueStack(sessionId); DirectedGraph <int, DirectedEdge <int> > graph = new DirectedGraph <int, DirectedEdge <int> >(true); graph.Add(42); graph.Add(13); graph.Add(10); graph.Add(new DirectedEdge <int>(42, 13)); // 42 -> 13 graph.Add(new DirectedEdge <int>(42, 10)); // 42 -> 10 Assert.IsTrue(graph.Contains(42)); Assert.IsTrue(graph.Contains(13)); Assert.IsTrue(graph.Contains(10)); Assert.AreEqual(2, graph.EdgeCount); Assert.IsTrue(graph.ContainsEdge(42, 13)); Assert.IsTrue(graph.ContainsEdge(42, 10)); // create graph to add to this graph. Doesn't have to be a commandified graph, as we're not rolling back the actions on that graph. DirectedGraph <int, DirectedEdge <int> > graphToAdd = new DirectedGraph <int, DirectedEdge <int> >(); graphToAdd.Add(1); graphToAdd.Add(2); graphToAdd.Add(3); graphToAdd.Add(new DirectedEdge <int>(1, 2)); // 1 -> 2 graphToAdd.Add(new DirectedEdge <int>(1, 3)); // 1 -> 3 graphToAdd.Add(new DirectedEdge <int>(2, 3)); // 2 -> 3 Assert.AreEqual(3, graphToAdd.VertexCount); Assert.AreEqual(3, graphToAdd.EdgeCount); // add this graph to the main graph. This is an undoable action. graph.Add(graphToAdd); Assert.AreEqual(6, graph.VertexCount); Assert.AreEqual(5, graph.EdgeCount); Assert.IsTrue(graph.Contains(1)); Assert.IsTrue(graph.Contains(2)); Assert.IsTrue(graph.Contains(3)); Assert.IsTrue(graph.ContainsEdge(1, 2)); Assert.IsTrue(graph.ContainsEdge(1, 3)); Assert.IsTrue(graph.ContainsEdge(2, 3)); // undo add CQManager.UndoLastCommand(); Assert.AreEqual(3, graph.VertexCount); Assert.AreEqual(2, graph.EdgeCount); Assert.IsFalse(graph.Contains(1)); Assert.IsFalse(graph.Contains(2)); Assert.IsFalse(graph.Contains(3)); Assert.IsFalse(graph.ContainsEdge(1, 2)); Assert.IsFalse(graph.ContainsEdge(1, 3)); Assert.IsFalse(graph.ContainsEdge(2, 3)); // redo CQManager.RedoLastCommand(); Assert.AreEqual(6, graph.VertexCount); Assert.AreEqual(5, graph.EdgeCount); Assert.IsTrue(graph.Contains(1)); Assert.IsTrue(graph.Contains(2)); Assert.IsTrue(graph.Contains(3)); Assert.IsTrue(graph.ContainsEdge(1, 2)); Assert.IsTrue(graph.ContainsEdge(1, 3)); Assert.IsTrue(graph.ContainsEdge(2, 3)); // remove the graph we added graph.Remove(graphToAdd); Assert.AreEqual(3, graph.VertexCount); Assert.AreEqual(2, graph.EdgeCount); Assert.IsFalse(graph.Contains(1)); Assert.IsFalse(graph.Contains(2)); Assert.IsFalse(graph.Contains(3)); Assert.IsFalse(graph.ContainsEdge(1, 2)); Assert.IsFalse(graph.ContainsEdge(1, 3)); Assert.IsFalse(graph.ContainsEdge(2, 3)); CQManager.UndoLastCommand(); Assert.AreEqual(6, graph.VertexCount); Assert.AreEqual(5, graph.EdgeCount); Assert.IsTrue(graph.Contains(1)); Assert.IsTrue(graph.Contains(2)); Assert.IsTrue(graph.Contains(3)); Assert.IsTrue(graph.ContainsEdge(1, 2)); Assert.IsTrue(graph.ContainsEdge(1, 3)); Assert.IsTrue(graph.ContainsEdge(2, 3)); DirectedEdge <int> newEdge = new DirectedEdge <int>(42, 1); // 42 -> 1 graph.Add(newEdge); Assert.AreEqual(6, graph.EdgeCount); Assert.IsTrue(graph.ContainsEdge(42, 1)); Assert.IsFalse(graph.ContainsEdge(1, 42)); CQManager.UndoLastCommand(); Assert.AreEqual(5, graph.EdgeCount); Assert.IsFalse(graph.ContainsEdge(42, 1)); CQManager.RedoLastCommand(); Assert.AreEqual(6, graph.EdgeCount); Assert.IsTrue(graph.ContainsEdge(42, 1)); graph.Remove(newEdge); Assert.AreEqual(5, graph.EdgeCount); Assert.IsFalse(graph.ContainsEdge(42, 1)); CQManager.UndoLastCommand(); Assert.AreEqual(6, graph.EdgeCount); Assert.IsTrue(graph.ContainsEdge(42, 1)); graph.Disconnect(42, 1, false); Assert.AreEqual(5, graph.EdgeCount); Assert.IsFalse(graph.ContainsEdge(42, 1)); CQManager.UndoLastCommand(); Assert.AreEqual(6, graph.EdgeCount); Assert.IsTrue(graph.ContainsEdge(42, 1)); CQManager.ActivateCommandQueueStack(Guid.Empty); }
public void CommandifiedGraphAddRemoveVertexWithUndoTest() { Guid sessionId = Guid.NewGuid(); CQManager.ActivateCommandQueueStack(sessionId); DirectedGraph<int, DirectedEdge<int>> graph = new DirectedGraph<int, DirectedEdge<int>>(true); graph.Add(42); Assert.IsTrue(graph.Contains(42)); CQManager.UndoLastCommand(); Assert.IsFalse(graph.Contains(42)); Assert.AreEqual(0, graph.VertexCount); graph.Add(42); graph.Add(13); graph.Add(10); Assert.IsTrue(graph.Contains(42)); Assert.IsTrue(graph.Contains(13)); Assert.IsTrue(graph.Contains(10)); graph.Add(new DirectedEdge<int>(42, 13)); // 42 -> 13 graph.Add(new DirectedEdge<int>(42, 10)); // 42 -> 10 Assert.AreEqual(2, graph.EdgeCount); Assert.IsTrue(graph.ContainsEdge(42, 13)); Assert.IsTrue(graph.ContainsEdge(42, 10)); graph.Remove(42); Assert.IsFalse(graph.Contains(42)); Assert.AreEqual(0, graph.EdgeCount); Assert.AreEqual(2, graph.VertexCount); // undo removal of 42. This should re-add 42, but also re-add the edges CQManager.UndoLastCommand(); Assert.AreEqual(2, graph.EdgeCount); Assert.IsTrue(graph.ContainsEdge(42, 13)); Assert.IsTrue(graph.ContainsEdge(42, 10)); Assert.IsTrue(graph.Contains(42)); CQManager.ActivateCommandQueueStack(Guid.Empty); }
public void CommandifiedGraphAddRemoveGraphAndEdgesWithUndoTest() { Guid sessionId = Guid.NewGuid(); CQManager.ActivateCommandQueueStack(sessionId); DirectedGraph<int, DirectedEdge<int>> graph = new DirectedGraph<int, DirectedEdge<int>>(true); graph.Add(42); graph.Add(13); graph.Add(10); graph.Add(new DirectedEdge<int>(42, 13)); // 42 -> 13 graph.Add(new DirectedEdge<int>(42, 10)); // 42 -> 10 Assert.IsTrue(graph.Contains(42)); Assert.IsTrue(graph.Contains(13)); Assert.IsTrue(graph.Contains(10)); Assert.AreEqual(2, graph.EdgeCount); Assert.IsTrue(graph.ContainsEdge(42, 13)); Assert.IsTrue(graph.ContainsEdge(42, 10)); // create graph to add to this graph. Doesn't have to be a commandified graph, as we're not rolling back the actions on that graph. DirectedGraph<int, DirectedEdge<int>> graphToAdd = new DirectedGraph<int, DirectedEdge<int>>(); graphToAdd.Add(1); graphToAdd.Add(2); graphToAdd.Add(3); graphToAdd.Add(new DirectedEdge<int>(1, 2)); // 1 -> 2 graphToAdd.Add(new DirectedEdge<int>(1, 3)); // 1 -> 3 graphToAdd.Add(new DirectedEdge<int>(2, 3)); // 2 -> 3 Assert.AreEqual(3, graphToAdd.VertexCount); Assert.AreEqual(3, graphToAdd.EdgeCount); // add this graph to the main graph. This is an undoable action. graph.Add(graphToAdd); Assert.AreEqual(6, graph.VertexCount); Assert.AreEqual(5, graph.EdgeCount); Assert.IsTrue(graph.Contains(1)); Assert.IsTrue(graph.Contains(2)); Assert.IsTrue(graph.Contains(3)); Assert.IsTrue(graph.ContainsEdge(1, 2)); Assert.IsTrue(graph.ContainsEdge(1, 3)); Assert.IsTrue(graph.ContainsEdge(2, 3)); // undo add CQManager.UndoLastCommand(); Assert.AreEqual(3, graph.VertexCount); Assert.AreEqual(2, graph.EdgeCount); Assert.IsFalse(graph.Contains(1)); Assert.IsFalse(graph.Contains(2)); Assert.IsFalse(graph.Contains(3)); Assert.IsFalse(graph.ContainsEdge(1, 2)); Assert.IsFalse(graph.ContainsEdge(1, 3)); Assert.IsFalse(graph.ContainsEdge(2, 3)); // redo CQManager.RedoLastCommand(); Assert.AreEqual(6, graph.VertexCount); Assert.AreEqual(5, graph.EdgeCount); Assert.IsTrue(graph.Contains(1)); Assert.IsTrue(graph.Contains(2)); Assert.IsTrue(graph.Contains(3)); Assert.IsTrue(graph.ContainsEdge(1, 2)); Assert.IsTrue(graph.ContainsEdge(1, 3)); Assert.IsTrue(graph.ContainsEdge(2, 3)); // remove the graph we added graph.Remove(graphToAdd); Assert.AreEqual(3, graph.VertexCount); Assert.AreEqual(2, graph.EdgeCount); Assert.IsFalse(graph.Contains(1)); Assert.IsFalse(graph.Contains(2)); Assert.IsFalse(graph.Contains(3)); Assert.IsFalse(graph.ContainsEdge(1, 2)); Assert.IsFalse(graph.ContainsEdge(1, 3)); Assert.IsFalse(graph.ContainsEdge(2, 3)); CQManager.UndoLastCommand(); Assert.AreEqual(6, graph.VertexCount); Assert.AreEqual(5, graph.EdgeCount); Assert.IsTrue(graph.Contains(1)); Assert.IsTrue(graph.Contains(2)); Assert.IsTrue(graph.Contains(3)); Assert.IsTrue(graph.ContainsEdge(1, 2)); Assert.IsTrue(graph.ContainsEdge(1, 3)); Assert.IsTrue(graph.ContainsEdge(2, 3)); DirectedEdge<int> newEdge = new DirectedEdge<int>(42, 1); // 42 -> 1 graph.Add(newEdge); Assert.AreEqual(6, graph.EdgeCount); Assert.IsTrue(graph.ContainsEdge(42, 1)); Assert.IsFalse(graph.ContainsEdge(1, 42)); CQManager.UndoLastCommand(); Assert.AreEqual(5, graph.EdgeCount); Assert.IsFalse(graph.ContainsEdge(42, 1)); CQManager.RedoLastCommand(); Assert.AreEqual(6, graph.EdgeCount); Assert.IsTrue(graph.ContainsEdge(42, 1)); graph.Remove(newEdge); Assert.AreEqual(5, graph.EdgeCount); Assert.IsFalse(graph.ContainsEdge(42, 1)); CQManager.UndoLastCommand(); Assert.AreEqual(6, graph.EdgeCount); Assert.IsTrue(graph.ContainsEdge(42, 1)); graph.Disconnect(42, 1, false); Assert.AreEqual(5, graph.EdgeCount); Assert.IsFalse(graph.ContainsEdge(42, 1)); CQManager.UndoLastCommand(); Assert.AreEqual(6, graph.EdgeCount); Assert.IsTrue(graph.ContainsEdge(42, 1)); CQManager.ActivateCommandQueueStack(Guid.Empty); }