示例#1
0
        /// <summary>
        /// Attempts to remove an edge entering a specific vertex from the strongly connected
        /// portion of a specific connected subgraph and replace it with another edge towards an
        /// edge from a weakly connected portion of the connected subgraph, thus expanding the
        /// strongly connected portion.
        /// </summary>
        /// <param name="connectedSubgraph">The connected subgraph which contains a weakly
        /// connected portion.</param>
        /// <param name="vertex">The vertex from the strongly connected portion of the connected
        /// subgraph.</param>
        /// <returns><see langword="true"/> if the edge was successfully added.</returns>
        private bool AddStronglyConnectingEdgeEnteringVertex(
            ConnectedSubgraph connectedSubgraph,
            Vertex vertex)
        {
            Vertex otherVertex = null;
            List<Vertex> adjacentVertices = GetAdjacentVertices(vertex);
            while (adjacentVertices.Count > 0)
            {
                int vertexIndex = _random.Next(adjacentVertices.Count);
                Vertex adjacentVertex = adjacentVertices[vertexIndex];
                if (!vertex.IsConnectedToVertex(adjacentVertex)
                    && connectedSubgraph.ContainsVertex(adjacentVertex)
                    && !connectedSubgraph.IsVertexStronglyConnected(adjacentVertex))
                {
                    if (connectedSubgraph.CheckWillConnectStrongly(vertex, adjacentVertex))
                    {
                        if (!CheckCanConnectVertices(vertex, adjacentVertex))
                        {
                            // If the two vertices cannot be connected without causing an
                            // intersection, remove the other edge
                            Vertex crossVertex1 = _levelMatrix[vertex.Row, adjacentVertex.Column];
                            Vertex crossVertex2 = _levelMatrix[adjacentVertex.Row, vertex.Column];
                            RemoveEdgeBetweenVertices(crossVertex1, crossVertex2);
                        }
                        // Check if the other vertex has the maximum number of edges entering it
                        // and if so, remove a random edge entering it
                        if (adjacentVertex.ConnectedVertexCount == _maxEdges)
                            RemoveEdgeEnteringVertex(adjacentVertex);

                        otherVertex = adjacentVertex;
                        break;
                    }
                }
                adjacentVertices.RemoveAt(vertexIndex);
            }

            // If no suitable other vertex was found, give up
            if (otherVertex == null)
                return false;

            // Check if the specified vertex has the maximum number of edges entering it and if so,
            // remove a random edge entering it
            if (vertex.ConnectedVertexCount == _maxEdges)
                RemoveEdgeEnteringVertex(vertex);

            // Add the edge to the level graph
            AddEdgeBetweenVertices(vertex, otherVertex);
            return true;
        }