예제 #1
0
        /// <summary>
        /// The eccentricity of a vertex v is the length of the shortest path
        /// from that vertex to the furthest vertex from v.
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public int Eccentricity(int v)
        {
            var bfs          = new BreadthFirstPaths(_g, v);
            var eccentricity = 0;

            for (int u = 0; u < _g.V; u++)
            {
                eccentricity = Math.Max(eccentricity, bfs.DistanceTo(u));
            }
            return(eccentricity);
        }
예제 #2
0
        /// <summary>
        /// The girth is the length of the shortest cycle in the graph.
        /// </summary>
        /// <returns></returns>
        public int Girth()
        {
            int EndOfPath(IEnumerable <int> path)
            {
                int lastElement = 0;

                foreach (var v in path)
                {
                    lastElement = v;
                }
                return(lastElement);
            }

            if (!Cyclic())
            {
                return(int.MaxValue);
            }
            var girth = int.MaxValue;

            for (int s = 0; s < _g.V; s++)
            {
                var bfs = new BreadthFirstPaths(_g, s);

                for (int v = 0; v < _g.V; v++)
                {
                    if (s == v)
                    {
                        continue;
                    }
                    var path = bfs.PathTo(v);
                    var end  = EndOfPath(path);

                    foreach (var u in _g.Adjacent(end))
                    {
                        if (u == s && v != u)
                        {
                            girth = Math.Min(girth, bfs.DistanceTo(v) + 1);
                        }
                    }
                }
            }

            return(girth);
        }
예제 #3
0
        /// <summary>
        /// The Wiener index of a graph is the sum of the lengths of the shortest possible
        /// paths between all pair of vertices.
        /// </summary>
        /// <returns></returns>
        public int WienerIndex()
        {
            int index = 0;

            for (int v = 0; v < _g.V; v++)
            {
                var bfs = new BreadthFirstPaths(_g, v);

                for (int u = 0; u < _g.V; u++)
                {
                    if (v == u)
                    {
                        continue;
                    }
                    index += bfs.DistanceTo(u);
                }
            }

            // All the paths have been counted twice.
            return(index / 2);
        }