Exemplo n.º 1
0
        public BellmanFord(WeightedGraph graph, int start)
        {
            this.graph     = graph;
            booking        = new bool[graph.V()];
            rotation       = new bool[graph.V()];
            booking[start] = true;
            path           = new Edge[graph.V()];

            weight = new double[graph.V()];
            for (int i = 0; i < weight.Length; i++)
            {
                weight[i] = double.PositiveInfinity;
            }
            weight[start] = 0;

            for (int pass = 1; pass < graph.V(); pass++)
            {
                for (int i = 0; i < booking.Length; i++)
                {
                    if (booking[i])
                    {
                        booking[i] = false;
                        foreach (Edge edge in graph.Edges(i))
                        {
                            int to = edge.To();
                            if (weight[to] > weight[i] + edge.Weight())
                            {
                                path[to]     = edge;
                                weight[to]   = weight[i] + edge.Weight();
                                rotation[to] = true;
                            }
                        }
                    }
                }
                bool[] temp = booking;
                booking  = rotation;
                rotation = temp;
            }

            for (int i = 0; i < graph.V(); i++)
            {
                foreach (Edge edge in graph.Edges(i))
                {
                    if (weight[edge.To()] > weight[i] + edge.Weight())
                    {
                        negative = true;
                        break;
                    }
                }
                if (!negative)
                {
                    break;
                }
            }
        }
Exemplo n.º 2
0
        public KruskMST(WeightedGraph graph)
        {
            MinHeap <Edge> queue = new MinHeap <Edge>(graph.E());

            for (int i = 0; i < graph.V(); i++)
            {
                foreach (Edge edge in graph.Edges(i))
                {
                    if (edge.From() < edge.To())
                    {
                        queue.Insert(edge);
                    }
                }
            }
            List <Edge> edges = new List <Edge>();

            UnionFind.UnionFind unionFind = new QuickUnionWithCompression(graph.V());
            while (!queue.isEmpty())
            {
                Edge edge = queue.Popup();
                if (!unionFind.isConnected(edge.From(), edge.To()))
                {
                    edges.Add(edge);
                    wt += edge.Weight();
                    unionFind.union(edge.From(), edge.To());
                }
            }

            this.edges = edges.ToArray <Edge>();
        }
Exemplo n.º 3
0
        private void visit(int node)
        {
            Trace.Assert(!booking[node]);
            booking[node] = true;

            foreach (Edge edge in graph.Edges(node))
            {
                int to = edge.To();
                if (!booking[to])
                {
                    if (path[to] != null)
                    {
                        if (weight[to] > weight[node] + edge.Weight())
                        {
                            weight[to] = weight[node] + edge.Weight();
                            queue.Change(to, weight[to]);
                            path[to] = edge;
                        }
                    }
                    else
                    {
                        weight[to] = weight[node] + edge.Weight();
                        queue.Insert(to, weight[to]);
                        path[to] = edge;
                    }
                }
            }
        }
Exemplo n.º 4
0
 private void visit(int k)
 {
     Trace.Assert(!visited[k]);
     visited[k] = true;
     foreach (var edge in graph.Edges(k))
     {
         if (!visited[edge.Other(k)])
         {
             heap.Insert(edge);
         }
     }
 }
Exemplo n.º 5
0
        private void visit(int k)
        {
            if (visited[k])
            {
                Trace.Assert(!visited[k]);
            }
            visited[k] = true;
            foreach (Edge edge in graph.Edges(k))
            {
                if (!visited[edge.To()])
                {
                    Edge e = queue.Get(edge.To());

                    if (e == null)
                    {
                        queue.Insert(edge.To(), edge);
                    }
                    else if (e.Weight() > edge.Weight())
                    {
                        queue.Change(edge.To(), edge);
                    }
                }
            }
        }