Esempio n. 1
0
        public static void ChangeNodesPosition(RectangularGraph graph)
        {
            var numberNodes = NumberOfNodes(graph);

            MakeGraphFalse(graph);
            var matrix = new Matr <bool>(numberNodes, numberNodes);

            GetNetworkMatrix(graph, matrix);
            MakeGraphFalse(graph);
            var levels          = SetLevels(matrix);
            var horizontalOrder = Barycenter(matrix, levels);
            var maxLevel        = Maximum(levels);

            var maxHeights = new int[maxLevel + 1];
            var rectangles = GetRectangles(graph);

            for (int i = 0; i <= maxLevel; i++)
            {
                maxHeights[i] = 0;
                var maxWidth = 0;
                var count    = 0;
                for (int j = 0; j < numberNodes; j++)
                {
                    if (levels[j] == i)
                    {
                        count++;
                        if (maxHeights[i] < rectangles[j].Height)
                        {
                            maxHeights[i] = rectangles[j].Height;
                        }
                        if (maxWidth < rectangles[j].Width)
                        {
                            maxWidth = rectangles[j].Width;
                        }
                    }
                }
                int horizontalShift = (RectangularGraph.FormWidth - count * maxWidth) / (count + 1);
                for (int j = 0; j < numberNodes; j++)
                {
                    if (levels[j] == i)
                    {
                        rectangles[j].Left = horizontalShift * (horizontalOrder[j] + 1) + horizontalOrder[j] * maxWidth;
                    }
                }
            }
            var maxHeight     = Maximum(maxHeights);
            var verticalShift = (RectangularGraph.FormHeigth - (maxLevel + 1) * maxHeight) / (maxLevel + 2);

            for (int i = 0; i <= maxLevel; i++)
            {
                for (int j = 0; j < numberNodes; j++)
                {
                    if (levels[j] == i)
                    {
                        rectangles[j].Top = verticalShift * (i + 1) + i * maxHeight;
                    }
                }
            }
            graph = CreateGraph(matrix, rectangles);
        }
Esempio n. 2
0
        public static int[] Bellman(Matr <bool> networkMatr, int node)
        {
            int[]  paths = new int[networkMatr.Lines];
            bool[] mask  = new bool[networkMatr.Lines];
            bool[] flags = new bool[paths.Length];
            for (int i = 0; i < paths.Length; i++)
            {
                flags[i] = false;
                paths[i] = Int32.MaxValue;
                mask[i]  = false;
            }
            paths[node] = 0;
            Mask(networkMatr, node, mask);
            for (int i = 0; i < paths.Length; i++)
            {
                if (!mask[i])
                {
                    flags[i] = true;
                }
            }
            int nodeIterator;

            do
            {
                nodeIterator = MinPathNode(flags, paths);
                if (nodeIterator != -1)
                {
                    flags[nodeIterator] = true;
                    for (int i = 0; i < paths.Length; i++)
                    {
                        if (networkMatr[nodeIterator, i])
                        {
                            if (paths[nodeIterator] < Int32.MaxValue)
                            {
                                if (paths[i] > paths[nodeIterator] - 1)
                                {
                                    paths[i] = paths[nodeIterator] - 1;
                                }
                            }
                        }
                    }
                }
            } while (nodeIterator != -1);
            for (int i = 0; i < paths.Length; i++)
            {
                if (paths[i] == Int32.MaxValue)
                {
                    paths[i] = -1;
                }
                else
                {
                    paths[i] = paths[i] * (-1);
                }
            }
            return(paths);
        }
Esempio n. 3
0
 public static void Mask(Matr <bool> networkMatr, int node, bool[] mask)
 {
     mask[node] = true;
     for (int i = 0; i < networkMatr.Lines; i++)
     {
         if (networkMatr[node, i])
         {
             Mask(networkMatr, i, mask);
         }
     }
 }
Esempio n. 4
0
 public static void GetNetworkMatrix(RectangularGraph graph, Matr <bool> matrix)
 {
     if (graph.NextElements == null)
     {
         return;
     }
     foreach (var node in graph.NextElements)
     {
         matrix[graph.Content.Identificator, node.Content.Identificator] = true;
         if (node.Flag == false)
         {
             node.Flag = true;
             GetNetworkMatrix(node, matrix);
         }
     }
 }
Esempio n. 5
0
 public static bool[] Sinks(Matr <bool> networkMatr)
 {
     bool[] result = new bool[networkMatr.Lines];
     for (int i = 0; i < networkMatr.Lines; i++)
     {
         result[i] = true;
         for (int j = 0; j < networkMatr.Lines; j++)
         {
             if (networkMatr[i, j])
             {
                 result[i] = false;
                 break;
             }
         }
     }
     return(result);
 }
Esempio n. 6
0
        public static int[] InvDijkstra(Matr <bool> networkMatr, int node)
        {
            int[]  paths = new int[networkMatr.Lines];
            bool[] mask  = new bool[networkMatr.Lines];
            bool[] flags = new bool[paths.Length];
            for (int i = 0; i < paths.Length; i++)
            {
                flags[i] = false;
                paths[i] = -1;
                mask[i]  = false;
            }
            paths[node] = 0;
            Mask(networkMatr, node, mask);
            for (int i = 0; i < paths.Length; i++)
            {
                if (!mask[i])
                {
                    flags[i] = true;
                }
            }
            int nodeIterator;

            do
            {
                nodeIterator = MaxPathNode(flags, paths);
                if (nodeIterator != -1)
                {
                    flags[nodeIterator] = true;
                    for (int i = 0; i < paths.Length; i++)
                    {
                        if (networkMatr[nodeIterator, i])
                        {
                            if (paths[i] < paths[nodeIterator] + 1)
                            {
                                paths[i] = paths[nodeIterator] + 1;
                            }
                        }
                    }
                }
            } while (nodeIterator != -1);
            return(paths);
        }
Esempio n. 7
0
        public static int[] SetLevels(Matr <bool> networkMatr)
        {
            int[] result = new int[networkMatr.Lines];
            var   sinks  = Sinks(networkMatr);

            for (int i = 0; i < result.Length; i++)
            {
                var minDist   = Int32.MaxValue;
                var distanses = Bellman(networkMatr, i);
                for (int j = 0; j < result.Length; j++)
                {
                    if (sinks[j])
                    {
                        if (minDist > distanses[j] && distanses[j] != -1)
                        {
                            minDist = distanses[j];
                        }
                    }
                }
                result[i] = minDist;
            }
            return(result);
        }
Esempio n. 8
0
 public static RectangularGraph CreateGraph(Matr <bool> matrix, Rectangle[] rectangles)
 {
     RectangularGraph[] temp = new RectangularGraph[matrix.Columns];
     for (int i = 0; i < matrix.Lines; i++)
     {
         temp[i] = new RectangularGraph(rectangles[i].Width, rectangles[i].Height);
         temp[i].Content.Left          = 150 + 40 * (i % 3);
         temp[i].Content.Top           = 150 + 40 * (i / 3);
         temp[i].Content.Identificator = i;
     }
     for (int i = 0; i < matrix.Lines; i++)
     {
         temp[i].NextElements = new List <RectangularGraph>();
         for (int j = 0; j < matrix.Lines; j++)
         {
             if (matrix[i, j])
             {
                 temp[i].NextElements.Add(temp[j]);
             }
         }
     }
     return(temp[0]);
 }
Esempio n. 9
0
        public static int[] Barycenter(Matr <bool> networkMatr, int[] levels)
        {
            var result      = new int[networkMatr.Lines];
            var barycenters = new double[networkMatr.Lines];
            int maxLevel    = levels[0];

            bool[] flags = new bool[networkMatr.Lines];
            for (int i = 0; i < networkMatr.Lines; i++)
            {
                flags[i] = false;
                if (maxLevel < levels[i])
                {
                    maxLevel = levels[i];
                }
            }
            for (int i = 0; i <= maxLevel; i++)
            {
                int k = 0;
                if (i == 0)
                {
                    for (int j = 0; j < networkMatr.Lines; j++)
                    {
                        if (levels[j] == i)
                        {
                            barycenters[j] = 1;
                        }
                    }
                }
                else
                {
                    for (int j = 0; j < networkMatr.Lines; j++)
                    {
                        if (levels[j] == i)
                        {
                            int sum   = 0;
                            int count = 0;
                            for (int m = 0; m < networkMatr.Lines; m++)
                            {
                                if (levels[m] == i - 1 && networkMatr[j, m])
                                {
                                    sum += result[m];
                                    count++;
                                }
                            }
                            barycenters[j] = ((double)sum) / count;
                        }
                    }
                }

                for (int j = 0; j < networkMatr.Lines; j++)
                {
                    if (levels[j] == i)
                    {
                        result[j] = k;
                        k++;
                    }
                }

                for (int j1 = 0; j1 < networkMatr.Lines; j1++)
                {
                    if (levels[j1] == i)
                    {
                        for (int j2 = 0; j2 < j1; j2++)
                        {
                            if (levels[j2] == i)
                            {
                                int tempMin, tempMax;
                                if (result[j1] > result[j2])
                                {
                                    tempMin = result[j2];
                                    tempMax = result[j1];
                                }
                                else
                                {
                                    tempMin = result[j1];
                                    tempMax = result[j2];
                                }
                                if (barycenters[j1] > barycenters[j2])
                                {
                                    result[j1] = tempMax;
                                    result[j2] = tempMin;
                                }
                                else
                                {
                                    result[j2] = tempMax;
                                    result[j1] = tempMin;
                                }
                            }
                        }
                    }
                }
            }


            return(result);
        }