예제 #1
0
        static void BellmanFord(DirectedWeightedGraph graph, Vertex source)
        {
            int totalDistance = 0;

            foreach (var edge in graph.Edges)
            {
                totalDistance += Math.Abs(edge.Weight);
            }

            foreach (var vertex in graph.Vertices)
            {
                vertex.Distance = Int32.MaxValue - totalDistance;
                vertex.path     = new List <Vertex> {
                    vertex
                };
            }
            source.Distance = 0;

            for (int i = 0; i < graph.Vertices.Count - 1; i++)
            {
                foreach (var edge in graph.Edges)
                {
                    RelaxEdgeVertices(edge);
                }
            }

            foreach (var edge in graph.Edges)
            {
                CheckNegativeCycles(edge);
            }
        }
예제 #2
0
        static void Main(string[] args)
        {
            var graph = new DirectedWeightedGraph();

            graph.SetIncidenceMatrixWeights(graph.ParseMatrixFile(@"..\..\..\..\Exercitiul_1_c\incidenceWeighted.txt"));
            BellmanFord(graph, graph.Vertices.FirstOrDefault());
            graph.PrintGraphStats();
            graph.PrintPath();
        }