/// <summary> /// These positions are zero-indexed! This sets the undirected adjacency values to true for the matrix. /// Note: If the values are not yet part of the matrix, AddVertexToMatrixIfMissing allows you to specify if they should be /// added or if an exception should be thrown. /// </summary> /// <param name="vertexPosition1"></param> /// <param name="vertexPosition2"></param> /// <param name="addVertexToMatrixIfMissing">This parameter specifies if the vertices should be added to the matrix if their identities do not exist in the matrix yet.</param> /// <returns></returns> public void SetAdjacentVertices(int vertexPosition1, int vertexPosition2, AddVertexToMatrixIfMissing addVertexToMatrixIfMissing) { lock (Values) { var vertex1InMatrix = this.IsVertexInMatrix(vertexPosition1); var vertex2InMatrix = this.IsVertexInMatrix(vertexPosition2); #region checkIfMissing if (addVertexToMatrixIfMissing == AddVertexToMatrixIfMissing.No) // i.e. If we are not to add the vertex if it doesn't exist in the matrix: { // then if vertices are not in the matrix yet, throw an exception. if (!vertex1InMatrix) { throw new ArgumentOutOfRangeException("vertexPosition1", "Vertex does not exist in matrix yet!"); } if (!vertex2InMatrix) { throw new ArgumentOutOfRangeException("vertexPosition2", "Vertex does not exist in matrix yet!"); } } if (addVertexToMatrixIfMissing == AddVertexToMatrixIfMissing.Yes) { if (!vertex1InMatrix) // then if vertices are not in the matrix yet, they should be added. { this.Add(vertexPosition1); } if (!vertex2InMatrix) { this.Add(vertexPosition2); } } #endregion // Now that both vertices are included in the matrix, we can set their adjacencies. // This means vertexPosition1 should be true at vertexPosition2 // and vertexPosition2 should be true at vertexPosition1. Values[vertexPosition1][vertexPosition2] = true; Values[vertexPosition2][vertexPosition1] = true; } }
/// <summary> /// This method sets the adjacency indications on the adjacency matrix for the source vertex to each of the target vertices. /// </summary> /// <param name="vertexPosition1">Source vertex</param> /// <param name="vertexPositions">Target list.</param> /// <param name="addVertexToMatrixIfMissing"></param> public void SetAdjacentVertices(int vertexPosition1, IEnumerable <int> vertexPositions, AddVertexToMatrixIfMissing addVertexToMatrixIfMissing) { var vertexPositionsList = vertexPositions.ToList(); lock (vertexPositionsList) { lock (Values) { var vertex1InMatrix = this.IsVertexInMatrix(vertexPosition1); var verticesNotInMatrix = vertexPositionsList.Where(t => !IsVertexInMatrix(t)).ToList(); #region checkIfMissing if (addVertexToMatrixIfMissing == AddVertexToMatrixIfMissing.No) // i.e. If we are not to add the vertex if it doesn't exist in the matrix: { // then if vertices are not in the matrix yet, throw an exception. if (!vertex1InMatrix) { throw new ArgumentOutOfRangeException("vertexPosition1", "Vertex does not exist in matrix yet!"); } if (verticesNotInMatrix.Any()) { throw new ArgumentOutOfRangeException("vertexPositions", "One or more vertices does not exist in matrix yet!"); } } if (addVertexToMatrixIfMissing == AddVertexToMatrixIfMissing.Yes) { if (!vertex1InMatrix) // then if vertices are not in the matrix yet, they should be added. { this.AddVertex(vertexPosition1); } verticesNotInMatrix.ForEach(t => this.AddVertex(t)); // AddVertex vertices to matrix if missing. } #endregion vertexPositionsList.ForEach(t => SetVertices(vertexPosition1, t)); // Now that both vertices are included in the matrix, we can set their adjacencies. } } }