public override void Open(TextReader reader, IGraph graph) { base.Open(reader, graph); var lines = ReadAllLines(reader); var list = new List <IEnumerable <int> >(); foreach (var line in lines) { var verticesIndexes = line.Split(' ').Select(x => ParseStringTo <int>(x)); list.Add(verticesIndexes); } foreach (var column in list) { var parentVertexIndex = column.First(); var parentVertex = graph.AddVertex(parentVertexIndex); var vertices = column.Skip(1); foreach (var vertex in vertices) { var targetVertex = graph.AddVertex(vertex); graph.AddEdge(parentVertex, targetVertex, true); } } }
private IGraph <T> BuildGraph(Vertex <T> vEnd, VertexData[] vTable) { //Instantiate an instance of the child type graph using reflection thing IGraph <T> result = (IGraph <T>)GetType().Assembly. CreateInstance(this.GetType().FullName); /* * Add the end vertex to result * dataLast <-- vTable (location of vEnd) * previous <-- previous of dataLast * while previous is not null * add prevous to result * add the edge from last and previous * dataLast <-- vTable(location of previous) * previous <-- dataLast previous * return result */ result.AddVertex(vEnd.Data); VertexData dataLast = vTable[vEnd.Index]; Vertex <T> previous = dataLast.vPrevious; while (previous != null) { result.AddVertex(previous.Data); result.AddEdge(dataLast.vVertex.Data, previous.Data); dataLast = vTable[previous.Index]; previous = dataLast.vPrevious; } return(result); }
public static E AddEdgeWithVertices( IGraph <V, E> graph, V sourceVertex, V targetVertex) { graph.AddVertex(sourceVertex); graph.AddVertex(targetVertex); return(graph.AddEdge(sourceVertex, targetVertex)); }
public void AddEdgeThrowsExceptionIfIndicesGreaterOrEqualThanSize(Type generic_type_def) { Type constructed_graph_type = ConstructGraphType <int>(generic_type_def); IGraph <int> graph = InstantiateGraph <int>(constructed_graph_type); graph.AddVertex(5); graph.AddVertex(-10); Assert.Throws <ArgumentException>(() => AddEdgeHelper(constructed_graph_type, graph, 0, 2)); Assert.Throws <ArgumentException>(() => AddEdgeHelper(constructed_graph_type, graph, 3, 0)); }
public void TestGetVertex(Type generic_type_def) { Type constructed_graph_type = ConstructGraphType <int>(generic_type_def); IGraph <int> graph = InstantiateGraph <int>(constructed_graph_type); graph.AddVertex(78); graph.AddVertex(-2); graph.AddVertex(12); graph.AddVertex(100); }
public void GetVertexReturnsExpectedVertex(Type generic_type_def) { Type constructed_graph_type = ConstructGraphType <int>(generic_type_def); IGraph <int> graph = InstantiateGraph <int>(constructed_graph_type); graph.AddVertex(5); graph.AddVertex(-10); Assert.AreEqual(-10, graph.GetVertex(1)); Assert.AreEqual(5, graph.GetVertex(0)); }
public void AddVertexReturnsOk() { Assert.That(_abstractGraph.AddVertex("A"), Is.True); Assert.That(_abstractGraph.AddVertex("A"), Is.False); Assert.That(_abstractGraph.GetVertexSet(), Contains.Item("A")); Assert.That(_abstractGraph.GetVertexSet().Count(), Is.EqualTo(1)); }
public void AddEdgeThrowsExceptionIfIndicesAreNegative(Type generic_type_def) { Type constructed_graph_type = ConstructGraphType <int>(generic_type_def); IGraph <int> graph = InstantiateGraph <int>(constructed_graph_type); graph.AddVertex(5); graph.AddVertex(-10); Assert.Throws <ArgumentException>(() => AddEdgeHelper(constructed_graph_type, graph, -1, 0)); Assert.Throws <ArgumentException>(() => AddEdgeHelper(constructed_graph_type, graph, 0, -1)); Assert.Throws <ArgumentException>(() => AddEdgeHelper(constructed_graph_type, graph, -1, -1)); }
public void EdgeExistsThrowsExceptionOnNegativeIndices(Type generic_type_def) { Type constructed_graph_type = ConstructGraphType <int>(generic_type_def); IGraph <int> graph = InstantiateGraph <int>(constructed_graph_type); graph.AddVertex(5); graph.AddVertex(-10); Assert.Throws <ArgumentException>(() => graph.EdgeExists(-1, 0)); Assert.Throws <ArgumentException>(() => graph.EdgeExists(0, -1)); Assert.Throws <ArgumentException>(() => graph.EdgeExists(-1, -1)); }
public void RemoveEdgeThrowsExceptionIfIndicesGreaterOrEqualToSize(Type generic_type_def) { Type constructed_graph_type = ConstructGraphType <int>(generic_type_def); IGraph <int> graph = InstantiateGraph <int>(constructed_graph_type); graph.AddVertex(5); graph.AddVertex(8); AddEdgeHelper(constructed_graph_type, graph, 0, 1); Assert.Throws <ArgumentException>(() => graph.RemoveEdge(0, 2)); Assert.Throws <ArgumentException>(() => graph.RemoveEdge(2, 1)); Assert.Throws <ArgumentException>(() => graph.RemoveEdge(2, 4)); }
public static bool AddEdgeWithVertices( IGraph <V, E> targetGraph, IGraph <V, E> sourceGraph, E edge) { V sourceVertex = edge.Source; V targetVertex = edge.Target; targetGraph.AddVertex(sourceVertex); targetGraph.AddVertex(targetVertex); return(targetGraph.AddEdge(sourceVertex, targetVertex, edge)); }
public void TestGetIndexOf(Type generic_type_def) { Type constructed_graph_type = ConstructGraphType <int>(generic_type_def); IGraph <int> graph = InstantiateGraph <int>(constructed_graph_type); graph.AddVertex(78); graph.AddVertex(-2); graph.AddVertex(12); Assert.AreEqual(0, graph.GetIndexOf(78)); Assert.AreEqual(2, graph.GetIndexOf(12)); Assert.AreEqual(-1, graph.GetIndexOf(0)); Assert.AreEqual(-1, graph.GetIndexOf(2)); Assert.AreEqual(1, graph.GetIndexOf(-2)); }
public void AddVertexThrowsExceptionOnNull(Type generic_type_def) { Type constructed_graph_type = ConstructGraphType <int?>(generic_type_def); IGraph <int?> graph = InstantiateGraph <int?>(constructed_graph_type); Assert.Throws <ArgumentNullException>(() => graph.AddVertex(null)); }
private void DoMapping() { var graph = levelDescription.GetGraph(); var stageOneGraph = levelDescription.GetGraphWithoutCorridors(); foreach (var vertex in graph.Vertices) { var roomNode = CreateRoomNode(vertex); // Create vertices mapping mappedGraph.AddVertex(roomNode); // Store room description roomDescriptions[roomNode.Id] = levelDescription.GetRoomDescription(vertex); } // Handle main graph edges foreach (var edge in graph.Edges) { mappedGraph.AddEdge(GetRoomNode(edge.From), GetRoomNode(edge.To)); } // Handle stage one graph vertices foreach (var vertex in stageOneGraph.Vertices) { mappedStageOneGraph.AddVertex(GetRoomNode(vertex)); } // Handle stage one graph edges foreach (var edge in stageOneGraph.Edges) { mappedStageOneGraph.AddEdge(GetRoomNode(edge.From), GetRoomNode(edge.To)); } }
static void MakeGraph(IGraph <ComparableTuple> gra) { for (int i = 0; i < numClusters; i++) { for (int j = 0; j < vertexPerCluster; j++) { gra.AddVertex(new ComparableTuple(i, j)); } } for (int i = 0; i < numClusters; i++) { MakeCluster(gra, i); System.Diagnostics.Debug.WriteLine(string.Format("Cluster {0} finished.", i)); } for (int i = 0; i < numClusters; i++) { for (int j = 0; j < numClusters; j++) { gra.AddEdge(new ComparableTuple(i, 0), new ComparableTuple(j, 0)); } } System.Diagnostics.Debug.WriteLine(string.Format("Graph connected")); }
public void Graph_05_ToString_02_OnUnsortedVertices() { // Arrange IGraph graph = DSBuilder.CreateGraphEmpty(); graph.AddVertex("g"); graph.AddVertex("a"); graph.AddVertex("z"); string expected = "a[]g[]z[]"; // Act string actual = TestUtils.TrimmedStringWithoutSpaces(graph.ToString()); // Assert Assert.AreEqual(expected, actual); }
private void DoMapping() { var graph = mapDescription.GetGraph(); var stageOneGraph = mapDescription.GetStageOneGraph(); foreach (var vertex in graph.Vertices) { // Create vertices mapping nodeToIntMapping.Add(vertex, nodeToIntMapping.Count); mappedGraph.AddVertex(nodeToIntMapping[vertex]); // Store room description roomDescriptions[nodeToIntMapping[vertex]] = mapDescription.GetRoomDescription(vertex); } // Handle main graph edges foreach (var edge in graph.Edges) { mappedGraph.AddEdge(nodeToIntMapping[edge.From], nodeToIntMapping[edge.To]); } // Handle stage one graph vertices foreach (var vertex in stageOneGraph.Vertices) { mappedStageOneGraph.AddVertex(nodeToIntMapping[vertex]); } // Handle stage one graph edges foreach (var edge in stageOneGraph.Edges) { mappedStageOneGraph.AddEdge(nodeToIntMapping[edge.From], nodeToIntMapping[edge.To]); } }
private string MakeGraph(IGraph <ComparableTuple> gra, string result) { for (int i = 0; i < numClusters; i++) { for (int j = 0; j < vertexPerCluster; j++) { gra.AddVertex(new ComparableTuple(i, j)); } } for (int i = 0; i < numClusters; i++) { MakeCluster(gra, i); result = result + string.Format("Cluster {0} finished.", i) + "\n"; } for (int i = 0; i < numClusters; i++) { for (int j = 0; j < numClusters; j++) { gra.AddEdge(new ComparableTuple(i, 0), new ComparableTuple(j, 0)); } } result = result + "\n"; result = result + string.Format(string.Format("Graph connected")) + "\n"; return(result); }
public IVertex AddVertex(object id) { var vertex = new PartitionVertex(BaseGraph.AddVertex(id), this); vertex.SetPartition(_writePartition); return(vertex); }
/// <summary> /// Copy the vertex/edges of one graph over to another graph. /// The id of the elements in the from graph are attempted to be used in the to graph. /// This method only works for graphs where the user can control the element ids. /// </summary> /// <param name="from">the graph to copy from</param> /// <param name="to">the graph to copy to</param> public static void CopyGraph(this IGraph from, IGraph to) { if (@from == null) { throw new ArgumentNullException(nameof(@from)); } if (to == null) { throw new ArgumentNullException(nameof(to)); } foreach (var fromVertex in @from.GetVertices()) { var toVertex = to.AddVertex(fromVertex.Id); fromVertex.CopyProperties(toVertex); } foreach (var fromEdge in from.GetEdges()) { var outVertex = to.GetVertex(fromEdge.GetVertex(Direction.Out).Id); var inVertex = to.GetVertex(fromEdge.GetVertex(Direction.In).Id); var toEdge = to.AddEdge(fromEdge.Id, outVertex, inVertex, fromEdge.Label); fromEdge.CopyProperties(toEdge); } }
private void ReadVertex(XmlReader myReader) { #region vertex info var vertexID = myReader.GetAttribute(GraphMLTokens.ID); var vertex = _Graph.AddVertex(vertexID); #endregion #region vertex attributes if (vertex != null) // if vertex is null, it has been added before { using (var vertexDataReader = myReader.ReadSubtree()) { // read attributes while (vertexDataReader.Read()) { if (vertexDataReader.Name == GraphMLTokens.DATA) { ReadAttribute(vertex, vertexDataReader); } } } _Vertices.Add(vertexID, vertex); } #endregion }
/// <summary> /// Add a vertex to the graph with specified id and provided properties. /// </summary> /// <param name="graph">the graph to create a vertex in</param> /// <param name="id">the id of the vertex to create</param> /// <param name="properties">the properties of the vertex to add (must be string,object,string,object,...)</param> /// <returns>the vertex created in the graph with the provided properties set</returns> public static IVertex AddVertex(this IGraph graph, object id, params object[] properties) { if (graph == null) { throw new ArgumentNullException(nameof(graph)); } if (properties == null) { throw new ArgumentNullException(nameof(properties)); } if (properties.Length % 2 != 0) { throw new ArgumentException("properties length must be even"); } var vertex = graph.AddVertex(id); for (var i = 0; i < properties.Length; i = i + 2) { vertex.SetProperty((string)properties[i], properties[i + 1]); } return(vertex); }
/// <summary> /// Adds a range of vertices to a graph of ints. /// </summary> /// <param name="graph"></param> /// <param name="start"></param> /// <param name="count"></param> public static void AddVerticesRange(this IGraph <int> graph, int start, int count) { for (var i = start; i < start + count; i++) { graph.AddVertex(i); } }
public void GetVertexThrowsExceptionIfIndexIsNegative(Type generic_type_def) { Type constructed_graph_type = ConstructGraphType <int>(generic_type_def); IGraph <int> graph = InstantiateGraph <int>(constructed_graph_type); graph.AddVertex(56); Assert.Throws <ArgumentException>(() => graph.GetVertex(-1)); }
public void AddEdgeReturnsFalseIfIndicesAreEqual(Type generic_type_def) { Type constructed_graph_type = ConstructGraphType <int>(generic_type_def); IGraph <int> graph = InstantiateGraph <int>(constructed_graph_type); graph.AddVertex(5); Assert.IsFalse(AddEdgeHelper(constructed_graph_type, graph, 0, 0)); }
public void RemoveVertexThrowsExceptionIfIndexIsGreaterOrEqualToSize(Type generic_type_def) { Type constructed_graph_type = ConstructGraphType <int>(generic_type_def); IGraph <int> graph = InstantiateGraph <int>(constructed_graph_type); graph.AddVertex(5); Assert.Throws <ArgumentException>(() => graph.RemoveVertex(1)); }
public void RegioGraaf_b_AddVertex_2_TwoAdded() { // Arrange IGraph graph = DSBuilder.CreateGraphEmpty(); var expected1 = "Q"; var expected2 = "S"; // Act graph.AddVertex("A", "Q"); graph.AddVertex("F", "S"); string actual1 = graph.GetVertex("A").GetRegio(); string actual2 = graph.GetVertex("F").GetRegio(); // Assert Assert.AreEqual(expected1, actual1); Assert.AreEqual(expected2, actual2); }
public void AssertSizeOneInGraphWithOneVertex(Type generic_type_def) { Type constructed_graph_type = ConstructGraphType <int>(generic_type_def); IGraph <int> graph = InstantiateGraph <int>(constructed_graph_type); graph.AddVertex(5); Assert.AreEqual(1, graph.Size); }
public void AssertEmptyFlagFalseInNonEmptyGraph(Type generic_type_def) { Type constructed_graph_type = ConstructGraphType <int>(generic_type_def); IGraph <int> graph = InstantiateGraph <int>(constructed_graph_type); graph.AddVertex(-3); Assert.IsFalse(graph.Empty); }
public void BreadthFirstSearchThrowsExceptionIfIndexIsGreaterOrEqualToSize(Type generic_type_def) { Type constructed_graph_type = ConstructGraphType <int>(generic_type_def); IGraph <int> graph = InstantiateGraph <int>(constructed_graph_type); graph.AddVertex(5); Assert.Throws <ArgumentException>(() => graph.BreadthFirstSearch(1).GetEnumerator().MoveNext()); }
public bool Read(IGraph myGraph, FileStream myInputStream) { // use a streamreader .. better handling StreamReader reader = null; // stores the added vertices for fast retrieval var addedVertices = new Dictionary<string, IVertex>(); try { reader = new StreamReader(myInputStream); String line; // current line in file String[] edge; // contains source and target vertex id IVertex source; // source vertex IVertex target; // target vertex var count = 0L; // number of processed edges while ((line = reader.ReadLine()) != null) { if (!line.StartsWith(EdgeListTokens.LINE_COMMENT)) // just a comment { // line look like that "0 1", splitting it to get source and target id edge = line.Split(EdgeListTokens.SOURCE_TARGET_SEPARATOR); // got two id's? if (edge.Length == 2) { // check if source has been added before if (!addedVertices.TryGetValue(edge[0], out source)) { // if not, create new vertex source = myGraph.AddVertex(edge[0]); addedVertices.Add(edge[0], source); } // check if target has been added before if (!addedVertices.TryGetValue(edge[1], out target)) { // if not, create new vertex target = myGraph.AddVertex(edge[1]); addedVertices.Add(edge[1], target); } if (myGraph.AddEdge(source, target) != null) { count++; } } } } } catch (IOException ex) { // TODO add logging here return false; } finally { reader.Close(); } return true; }