/// <summary> /// Change a vertex user name /// If the vertex doesn't exist throws GraphVertexDoesntExistException /// </summary> /// <param name="vertex">name</param> /// <param name="newUserName">user name</param> public void RenameVertexUserName(IVertexInterface vertex, string newUserName) { // Variable string oldUserName; VertexExtended vertexExtended = ConvertVertexToVertexExtended(vertex); if (ExistsUserName(newUserName)) { throw new MyException.GraphException.GraphVertexUserNameAlreadyExistsException(); } oldUserName = vertex.GetUserName(); mappingUserName.Remove(vertex.GetUserName()); vertexExtended.SetUserName(newUserName); mappingUserName.Add(newUserName, vertexExtended); // Change the name in components // The graph has more than one component => need to change vertex name in one of them. if (GetGraphProperty().GetIsInitializedComponent() && GetGraphProperty().GetCountComponents() > 1) { foreach (IGraphInterface componentGraph in GetGraphProperty().GetComponents()) { if (componentGraph.ExistsUserName(oldUserName)) { componentGraph.RenameVertexUserName(componentGraph.GetVertexByUserName(oldUserName), newUserName); break; } } } }
/// <summary> /// Add a new vertex /// Deinitialize colored graph /// If the vertex exists in the graph throws GraphVertexAlreadyExistsException /// </summary> /// <param name="vertex">vertex</param> public void VertexAdd(IVertexInterface vertex) { // Vertex exists if (ExistsVertex(vertex) || ExistsUserName(vertex.GetUserName())) { throw new MyException.GraphException.GraphVertexAlreadyExistsException(); } VertexExtended vertexExtended = new VertexExtended(vertex.GetIdentifier()); vertexExtended.SetColor(vertex.GetColor()); vertexExtended.SetUserName(vertex.GetUserName()); SetCanDeIncreaseCountVertices(true); GetGraphProperty().IncrementCountVertices(); SetCanDeIncreaseCountVertices(false); AddVertexToAdjacencyList(vertexExtended); vertex = vertexExtended; // ColoredGraph coloredGraph.AddVertexInHashSets(vertex); if (coloredGraph.GetIsInitializedColoredGraph()) { coloredGraph.DeinitializationColoredGraph(); } // GraphProperty GetGraphProperty().VertexAdd(vertex); }
/// <summary> /// Create new vertices (number of vertices: countVertices - realCountVertex) /// </summary> public void FullGenerateVertices() { if (GetIsInitialized()) { throw new MyException.GraphException.GraphInitializationException(); } while (GetRealCountVertices() != graphProperty.GetCountVertices()) { VertexExtended vertexExtended = new VertexExtended(); AddVertexToAdjacencyList(vertexExtended); } }
/// <summary> /// Return an identifier of vertex which user name is userNameVertex /// If the vertex with the identifier does not exist construct one /// </summary> /// <param name="userNameVertex">user name of vertex</param> /// <returns>identifier of vertex</returns> private int GetIdentifier(string userNameVertex) { if (mapping.TryGetValue(userNameVertex, out int identifierVertex)) { return(identifierVertex); } VertexExtended vertexExtended = new VertexExtended(userNameVertex); AddVertexToAdjacencyList(vertexExtended); mapping.Add(userNameVertex, vertexExtended.GetIdentifier()); return(vertexExtended.GetIdentifier()); }
/// <summary> /// Initialize graph /// </summary> /// <param name="countVertices">Count of vertices = size of an adjacency matrix</param> public GraphAdjacencyMatrix(int countVertices) : base(countVertices) { // Variable VertexExtended vertexExtended; mapping = new Dictionary <int, int>(); // Create vertices for (int i = 0; i < countVertices; i++) { vertexExtended = new VertexExtended(); AddVertexToAdjacencyList(vertexExtended); mapping.Add(i, vertexExtended.GetIdentifier()); } }
/// <summary> /// Add a vertex to AdjacencyList (no neighbors) /// If countVertices is less than realCountVertices throws GraphInvalidCountVertices /// </summary> /// <param name="vertexExtended">new vertex</param> protected void AddVertexToAdjacencyList(VertexExtended vertexExtended) { try { adjacencyList.Add(vertexExtended, new HashSet <VertexExtended>()); mapping.Add(vertexExtended.GetIdentifier(), vertexExtended); mappingUserName.Add(vertexExtended.GetUserName(), vertexExtended); IncrementRealCountVertices(); } catch (ArgumentException) { throw new MyException.GraphException.GraphVertexAlreadyExistsException(); } if (graphProperty.GetCountVertices() < GetRealCountVertices()) { throw new MyException.GraphException.GraphInvalidCountVerticesException(); } }