Ejemplo n.º 1
0
            /// <summary>
            /// Constructor
            /// </summary>
            /// <param name="graph"></param>
            internal EdgeEnumerator(DigraphAsMatrix graph)
            {
                v = -1;
                w = -1;

                this.graph = graph;
            }
Ejemplo n.º 2
0
        /// <summary>
        /// An algorithm for finding the shortest path between two graph vertices
        /// </summary>
        /// <param name="g"></param>
        /// <returns></returns>
        public static IDigraph FloydsAlgorithm(IDigraph g)
        {
            int vertexCount = g.NumberOfVertices;

            int[,] distance = new int[vertexCount, vertexCount];
            for (int j1 = 0; j1 < vertexCount; j1++)
            {
                for (int k1 = 0; k1 < vertexCount; k1++)
                {
                    distance[j1, k1] = int.MaxValue;                    //means infinity actually
                }
            }
            IEnumerator iEnumerator = g.Edges.GetEnumerator();

            try
            {
                while (iEnumerator.MoveNext())
                {
                    IEdge edge = (IEdge)iEnumerator.Current;
                    distance[edge.V0.Number, edge.V1.Number] = (int)edge.Weight;
                }
            }
            finally
            {
                IDisposable iDisposable = iEnumerator as IDisposable;
                if (iDisposable != null)
                {
                    iDisposable.Dispose();
                }
            }


            for (int j2 = 0; j2 < vertexCount; j2++)
            {
                for (int k2 = 0; k2 < vertexCount; k2++)
                {
                    Console.WriteLine(j2 + "->" + k2);
                    for (int i2 = 0; i2 < vertexCount; i2++)
                    {
                        if (distance[j2, i2] != int.MaxValue && distance[i2, k2] != int.MaxValue)
                        {
                            int i3 = distance[j2, i2] + distance[i2, k2];
                            if (distance[j2, k2] > i3)
                            {
                                distance[j2, k2] = i3;
                                Console.WriteLine("  " + i2);
                            }
                        }
                    }
                }
            }
            IDigraph digraph1 = new DigraphAsMatrix(vertexCount);

            for (int j3 = 0; j3 < vertexCount; j3++)
            {
                digraph1.AddVertex(j3);
            }
            for (int k3 = 0; k3 < vertexCount; k3++)
            {
                for (int i4 = 0; i4 < vertexCount; i4++)
                {
                    if (distance[k3, i4] != int.MaxValue)
                    {
                        digraph1.AddConnection(k3, i4, distance[k3, i4]);
                    }
                }
            }
            return(digraph1);
        }