Example #1
0
 public void RemoveNeighborBiDirection(Vertex vertex)
 {
     RemoveNeighbor(vertex);
     vertex.RemoveNeighbor(this);
 }
Example #2
0
 public void RemoveNeighbor(Vertex vertex)
 {
     Neighbors.Remove(vertex);
 }
Example #3
0
 public Vertex(Vertex vertex)
 {
     Id   = vertex.Id;
     Name = vertex.Name;
 }
Example #4
0
 public bool AddNeighborBiDirection(Vertex vertex)
 {
     return(AddNeighbor(vertex) && vertex.AddNeighbor(this));
 }
Example #5
0
        /// <summary>
        /// Generate graph by Holme & Kim model
        /// </summary>
        /// <param name="n">Number of vertices</param>
        /// <param name="m0">Number of fundamental nodes</param>
        /// <param name="m">Number of edges for new created vertices</param>
        /// <para name="p">Probability of making a triad formation</para>
        /// <returns></returns>
        public Graph GenerateHolmeKimModel(int n, int m0, int m, double p)
        {
            var vertices = Enumerable.Range(0, m0).Select(id => new Vertex(id)).ToList();

            // create base graph (complete graph)
            for (int i = 0; i < m0; i++)
            {
                for (int j = 0; j < m0; j++)
                {
                    if (i == j)
                    {
                        continue;
                    }
                    vertices[i].AddNeighborBiDirection(vertices[j]);
                }
            }

            var vertexDegreeDistribution = new List <int>();

            foreach (var vertex in vertices)
            {
                for (int i = 0; i < vertex.Degree; i++)
                {
                    vertexDegreeDistribution.Add(vertex.Id);
                }
            }

            // add new nodes
            for (int i = m0; i < n; i++)
            {
                var vertex      = new Vertex(m0 + i + 1);
                var usedNodeIds = new List <int>();

                Vertex newNeighbor = null;
                for (int j = 0; j < m; j++)
                {
                    if (j == 0 || p < _random.NextDouble())
                    {
                        int randomIndex = _random.Next(vertexDegreeDistribution.Count - 1);
                        while (usedNodeIds.Contains(vertexDegreeDistribution[randomIndex]))
                        {
                            randomIndex = _random.Next(vertexDegreeDistribution.Count - 1);
                        }

                        newNeighbor = vertices.Single(e => e.Id == vertexDegreeDistribution[randomIndex]);
                        bool isAdded = newNeighbor.AddNeighborBiDirection(vertex);
                        usedNodeIds.Add(vertexDegreeDistribution[randomIndex]);
                    }
                    else
                    {
                        if (newNeighbor == null)
                        {
                            throw new Exception();
                        }
                        var possibleNeighborsToAdd = newNeighbor.Neighbors.Except(new[] { vertex }).ToList();
                        var otherNeighbor          = possibleNeighborsToAdd[_random.Next(possibleNeighborsToAdd.Count)];
                        vertex.AddNeighborBiDirection(otherNeighbor);
                    }
                }

                for (int j = 0; j < m; j++)
                {
                    vertexDegreeDistribution.Add(vertex.Id);
                    vertexDegreeDistribution.Add(vertex.Neighbors[j].Id);
                }

                vertices.Add(vertex);
            }

            return(new Graph(vertices));
        }