Beispiel #1
0
        private static WeightedDiAdjacencyMatrix GetSlidesGraph()
        {
            /* dijkrstra.png
             * 's' = 1
             * 'a' = 2
             * 'b' = 3
             * 'c' = 4
             * 'd' = 5
             * 'e' = 6
             */
            var graph = new WeightedDiAdjacencyMatrix(6);

            graph.AddDuplexEdge(1, 3, 2f);
            graph.AddDuplexEdge(1, 5, 1f);
            graph.AddDuplexEdge(1, 4, 2f);

            graph.AddDuplexEdge(3, 4, 1f);
            graph.AddDuplexEdge(3, 5, 1f);
            graph.AddDuplexEdge(3, 2, 1f);

            graph.AddDuplexEdge(2, 5, 3f);
            graph.AddDuplexEdge(2, 6, 1f);

            graph.AddDuplexEdge(4, 5, 2f);

            graph.AddDuplexEdge(5, 6, 4f);

            return(graph);
        }
Beispiel #2
0
        private static WeightedDiAdjacencyMatrix SimpleMatrix()
        {
            var matrix = new WeightedDiAdjacencyMatrix(3);

            matrix.AddEdge(1, 2, 7f);

            matrix.AddEdge(2, 3, 100f);

            return(matrix);
        }
Beispiel #3
0
        private float Weight(int vertexA, int vertexB, WeightedDiAdjacencyMatrix graph)
        {
            var edge = graph.GetEdge(vertexA, vertexB);

            if (edge == null)
            {
                return(float.PositiveInfinity);
            }
            else
            {
                return(edge.Weight);
            }
        }
Beispiel #4
0
        private static WeightedDiAdjacencyMatrix NoPathMatrix()
        {
            var matrix = new WeightedDiAdjacencyMatrix(5);

            matrix.AddEdge(1, 2, 5f);

            matrix.AddEdge(2, 3, 7f);

            matrix.AddEdge(3, 2, 2f);

            matrix.AddEdge(4, 5, 15f);

            return(matrix);
        }
Beispiel #5
0
        public float[] RunDijkstraAlg(WeightedDiAdjacencyMatrix graph, int vertex)
        {
            vertex -= 1;
            var shortestPathsMatrix = new float[graph.Order];

            shortestPathsMatrix[vertex] = 0f;
            var neighbours      = graph.Neighbours(vertex + 1);
            var verticleToCheck = new List <int>();

            for (int i = 1; i <= graph.Order; i++)
            {
                if (i == vertex + 1)
                {
                    continue;
                }

                verticleToCheck.Add(i - 1);

                if (neighbours.Contains(i))
                {
                    shortestPathsMatrix[i - 1] = graph.GetEdge(vertex, i - 1).Weight;
                }
                else
                {
                    shortestPathsMatrix[i - 1] = float.PositiveInfinity;
                }
            }

            Console.WriteLine("Dijkstra:");
            Console.Write("iter 0:\t");
            PrintValues(shortestPathsMatrix);
            int iter = 1;

            while (verticleToCheck.Count > 0)
            {
                var minIndex = IndexOfMinElement(shortestPathsMatrix, verticleToCheck);
                verticleToCheck.Remove(minIndex);

                foreach (var item in verticleToCheck)
                {
                    shortestPathsMatrix[item] = Math.Min(shortestPathsMatrix[item], shortestPathsMatrix[minIndex] + Weight(minIndex, item, graph));
                }
                Console.Write("iter " + iter + ":\t");
                iter++;
                PrintValues(shortestPathsMatrix);
            }

            return(shortestPathsMatrix);
        }
        private static WeightedDiAdjacencyMatrix Cycled5Matrix()
        {
            var matrix = new WeightedDiAdjacencyMatrix(5);

            matrix.AddDuplexEdge(1, 2, 1f);

            matrix.AddDuplexEdge(2, 3, 1f);

            matrix.AddDuplexEdge(3, 4, 1f);

            matrix.AddDuplexEdge(4, 5, 1f);

            matrix.AddDuplexEdge(5, 1, 1f);

            return(matrix);
        }
        private static WeightedDiAdjacencyMatrix CormenMatrix()
        {
            var matrix = new WeightedDiAdjacencyMatrix(6);

            matrix.AddDuplexEdge(1, 4, 1f);

            matrix.AddDuplexEdge(2, 4, 1f);

            matrix.AddDuplexEdge(3, 5, 1f);

            matrix.AddDuplexEdge(5, 2, 1f);

            matrix.AddDuplexEdge(6, 2, 1f);


            return(matrix);
        }
Beispiel #8
0
        public void ComputeMaximalMatching(WeightedDiAdjacencyMatrix graph)
        {
            List <int> redVerticles  = null;
            List <int> blueVerticles = null;

            if (!IsGraphBipartite(graph, out redVerticles, out blueVerticles))
            {
                Console.WriteLine("Graph is not Bipartite!");
                return;
            }

            WeightedDiAdjacencyMatrix diGraph = new WeightedDiAdjacencyMatrix(graph.Order + 2);

            int S = 1;
            int T = graph.Order + 2;

            for (int i = 1; i <= graph.Order; i++)
            {
                var neighbours = graph.Neighbours(i);
                foreach (var item in neighbours)
                {
                    if (blueVerticles.Contains(i))
                    {
                        diGraph.AddEdge(i + 1, item + 1, 1f);
                    }
                }
            }

            blueVerticles.ForEach(blue => diGraph.AddEdge(S, blue + 1, 1f));
            redVerticles.ForEach(red => diGraph.AddEdge(red + 1, T, 1f));

            Console.WriteLine("Residual network S=0; T=" + (T - 1));
            for (int i = 0; i < diGraph.Order; i++)
            {
                Console.Write("Neighbours[" + (i) + "]\t");
                diGraph.Neighbours(i + 1).ForEach(_ => Console.Write((_ - 1) + " "));
                Console.WriteLine();
            }

            Console.WriteLine("To read result remove S-> and ->T. Then substract 1 from each middle N->N:");
            var edmondsKarp = new EdmondsKarp();

            edmondsKarp.MaximumFlow(diGraph, S - 1, T - 1);
        }
Beispiel #9
0
        //https://www.youtube.com/watch?v=Tl90tNtKvxs#t=291.549803
        private static WeightedDiAdjacencyMatrix YoutubeMatrix()
        {
            var matrix = new WeightedDiAdjacencyMatrix(6);

            matrix.AddEdge(1, 2, 10f);
            matrix.AddEdge(1, 4, 10f);

            matrix.AddEdge(2, 4, 2f);
            matrix.AddEdge(2, 5, 8f);
            matrix.AddEdge(2, 3, 4f);

            matrix.AddEdge(3, 6, 10f);

            matrix.AddEdge(4, 5, 9f);

            matrix.AddEdge(5, 3, 6f);
            matrix.AddEdge(5, 6, 10f);


            return(matrix);
        }
Beispiel #10
0
        private static WeightedDiAdjacencyMatrix CormenMatrix()
        {
            var matrix = new WeightedDiAdjacencyMatrix(6);

            matrix.AddEdge(1, 2, 16f);
            matrix.AddEdge(1, 3, 13f);

            matrix.AddEdge(2, 3, 10f);
            matrix.AddEdge(2, 4, 12f);

            matrix.AddEdge(3, 2, 4f);
            matrix.AddEdge(3, 5, 14f);

            matrix.AddEdge(4, 3, 9f);
            matrix.AddEdge(4, 6, 20f);

            matrix.AddEdge(5, 4, 7f);
            matrix.AddEdge(5, 6, 4f);

            return(matrix);
        }
Beispiel #11
0
        private static WeightedDiAdjacencyMatrix Testtest()
        {
            var matrix = new WeightedDiAdjacencyMatrix(8);

            matrix.AddEdge(7, 2, 1f);
            matrix.AddEdge(7, 4, 1f);
            matrix.AddEdge(7, 6, 1f);

            matrix.AddEdge(2, 1, 1f);
            matrix.AddEdge(2, 3, 1f);

            matrix.AddEdge(4, 3, 1f);
            matrix.AddEdge(4, 5, 1f);

            matrix.AddEdge(6, 1, 1f);
            matrix.AddEdge(6, 5, 1f);

            matrix.AddEdge(1, 8, 1f);
            matrix.AddEdge(3, 8, 1f);
            matrix.AddEdge(5, 8, 1f);

            return(matrix);
        }
Beispiel #12
0
        public bool IsGraphBipartite(WeightedDiAdjacencyMatrix graph, out List <int> redVerts, out List <int> blueVerts)
        {
            for (int i = 0; i < graph.Order; i++)
            {
                Console.Write("Neighbours[" + (i + 1) + "]\t");
                graph.Neighbours(i + 1).ForEach(_ => Console.Write(_ + " "));
                Console.WriteLine();
            }

            var blueVertices  = new List <int>();
            var redVertices   = new List <int>();
            var verticesQueue = new Queue <int>();

            redVerts  = redVertices;
            blueVerts = blueVertices;

            for (int i = 0; i < graph.Order; i++)
            {
                if (blueVertices.Contains(i) || redVertices.Contains(i))
                {
                    continue;
                }

                redVertices.Add(i + 1);
                verticesQueue.Enqueue(i);

                while (verticesQueue.Count > 0)
                {
                    var vertex      = verticesQueue.Dequeue();
                    var vertexLabel = vertex + 1;
                    var neighbours  = graph.Neighbours(vertexLabel);
                    foreach (var item in neighbours)
                    {
                        if (redVertices.Contains(item) && redVertices.Contains(vertexLabel))
                        {
                            return(false);
                        }

                        if (blueVertices.Contains(item) && blueVertices.Contains(vertexLabel))
                        {
                            return(false);
                        }

                        if (blueVertices.Contains(item) || redVertices.Contains(item))
                        {
                            continue;
                        }

                        if (redVertices.Contains(vertexLabel))
                        {
                            blueVertices.Add(item);
                        }
                        else
                        {
                            redVertices.Add(item);
                        }

                        verticesQueue.Enqueue(item - 1);
                    }
                }
            }
            Console.Write("\nRed:\t");
            redVertices.ForEach(v => Console.Write((v) + " "));
            Console.WriteLine();
            Console.Write("Blue:\t");
            blueVertices.ForEach(v => Console.Write((v) + " "));
            Console.WriteLine();

            return(true);
        }
Beispiel #13
0
        public float?MaximumFlow(WeightedDiAdjacencyMatrix graph, int s, int t)
        {
            bool        noPath      = false;
            float       fMax        = 0f;
            var         nettoMatrix = new WeightedDiAdjacencyMatrix(graph.Order);
            var         predecesors = new int[graph.Order];
            var         cfp         = new float[graph.Order];
            Queue <int> bfsVertex   = new Queue <int>();
            int         iteration   = 0;

            while (true)
            {
                for (int i = 0; i < predecesors.Length; i++)
                {
                    predecesors[i] = -1;
                }
                predecesors[s] = -2;
                cfp[s]         = float.PositiveInfinity;
                while (bfsVertex.Count > 0)
                {
                    bfsVertex.Dequeue();
                }
                bfsVertex.Enqueue(s);
                Console.WriteLine("\n> iteration " + iteration);
                while (bfsVertex.Count > 0)
                {
                    noPath = true;
                    bool breakWhile = false;
                    var  x          = bfsVertex.Dequeue();
                    foreach (var y in graph.Neighbours(x + 1))
                    {
                        var nettoValue = 0f;
                        if (nettoMatrix.IsEdgeByLabels(x + 1, y))
                        {
                            nettoValue = nettoMatrix.GetEdgeByLabels(x + 1, y).Weight;
                        }

                        var residualCapacity = graph.GetEdgeByLabels(x + 1, y).Weight - nettoValue;

                        /*
                         * Jezeli wierzcholek byl odwiedzony lub jego przepustowosc rezydualna wynosi 0
                         * to nie obliczamy dla niego CFP
                         */
                        if (residualCapacity == 0f || predecesors[y - 1] != -1)
                        {
                            continue;
                        }

                        predecesors[y - 1] = x;
                        cfp[y - 1]         = Math.Min(cfp[x], residualCapacity);

                        if ((y - 1) == t)
                        {
                            ShowPath(predecesors, t);
                            noPath     = false;
                            breakWhile = true;
                            break;
                        }
                        bfsVertex.Enqueue(y - 1);
                    }
                    if (breakWhile)
                    {
                        break;
                    }
                }
                if (noPath)
                {
                    if (iteration == 0)
                    {
                        Console.WriteLine("ERROR: NO PATH");
                        return(null);
                    }

                    return(fMax);
                }

                fMax += cfp[t];

                var ny = t;
                Console.WriteLine("Operating with " + cfp[t]);
                while (ny != s)
                {
                    var x = predecesors[ny];

                    if (!nettoMatrix.IsEdgeByLabels(x + 1, ny + 1))
                    {
                        nettoMatrix.AddEdge(x + 1, ny + 1, 0f);
                    }
                    nettoMatrix.GetEdgeByLabels(x + 1, ny + 1).Weight += cfp[t];

                    if (!nettoMatrix.IsEdgeByLabels(ny + 1, x + 1))
                    {
                        nettoMatrix.AddEdge(ny + 1, x + 1, 0f);
                    }

                    nettoMatrix.GetEdgeByLabels(ny + 1, x + 1).Weight -= cfp[t];

                    Console.WriteLine("nettoValue(" + (x + 1) + "," + (ny + 1) + ") == " + nettoMatrix.GetEdgeByLabels(x + 1, ny + 1).Weight);
                    // Console.WriteLine("nettoValue(" + (ny + 1) + "," + (x + 1) + ") == " + nettoMatrix.GetEdgeByLabels(ny + 1, x + 1).Weight);
                    // Console.WriteLine("~~");

                    ny = x;
                }
                iteration++;
            }
            return(fMax);
        }