Beispiel #1
0
        public List <List <int> > FindStrongConnectedComponents(bool isTopologySort = false)
        {
            List <int> order = TopologySort();

            bool[] used = new bool[_numberOfCells];
            //var used = new List<int>();

            SymbolGraph        tGraph     = TransposeGraph();
            List <List <int> > components = new List <List <int> >();

            Func <int, List <int> > depthFirstSearch = null;



            depthFirstSearch = start =>
            {
                var component = new List <int>();

                Stack <int> stack = new Stack <int>();

                stack.Push(start);

                while (stack.Count != 0)
                {
                    int v = stack.Pop();
                    used[v] = true;
                    component.Add(v);

                    if (tGraph[v] != null)
                    {
                        for (int i = 0; i < tGraph[v].Count; i++)
                        {
                            int g = tGraph[v][i];
                            if (!used[g])
                            {
                                stack.Push(g);
                            }
                        }
                    }
                }
                return(component);
            };

            //Parallel.For(0, _numberOfCells, (i, state) => { used[i] = false; });

            foreach (var v in order)
            {
                if (!used[v] && tGraph.Contains(v))
                {
                    List <int> component = depthFirstSearch(v);

                    if (component.Count > 1 || isTopologySort)
                    {
                        components.Add(component);
                    }
                }
            }

            return(components);
        }
Beispiel #2
0
        public void MakeGraph()
        {
            if (xFunction == null || yFunction == null)
            {
                return;
            }

            //for(int i = 0; i < _numberOfCells; i++)
            Parallel.For(0, _numberOfCells, (i, state) =>
            {
                int row   = (int)i / _cols;
                int col   = (int)i % _cols;
                double x1 = _xMin + col * _delta;
                double y1 = _yMax - row * _delta;



                for (int k = 0; k <= _cast_square - 1; k++)
                {
                    for (int t = 0; t <= _cast_square - 1; t++)
                    {
                        double x = x1 + (double)k * _delta / (double)_cast_square;
                        double y = y1 - (double)t * _delta / (double)_cast_square;

                        int cell = ReturnCell(x, y);

                        if (cell == -1)
                        {
                            continue;
                        }
                        if (cell >= _numberOfCells)
                        {
                            continue;
                        }

                        if (!_graph.Contains(i))
                        {
                            _graph[i] = new List <int>();
                        }

                        if (!_graph[i].Contains(cell))
                        {
                            _graph[i].Add(cell);
                        }
                    }
                }
            });
        }
Beispiel #3
0
        private SymbolGraph TransposeGraph()
        {
            SymbolGraph tGraph = new SymbolGraph(_numberOfCells);

            foreach (KeyValuePair <int, List <int> > i in Graph.keyValuePairs)
            {
                foreach (var v in i.Value)
                {
                    if (!tGraph.Contains(v))
                    {
                        tGraph[v] = new List <int>();
                    }
                    if (!tGraph[v].Contains(i.Key))
                    {
                        tGraph[v].Add(i.Key);
                    }
                }
            }

            return(tGraph);
        }
Beispiel #4
0
        public List <List <int> > MakeNewGraph(int n, List <List <int> > components)
        {
            int oldCols = _cols;


            //List<int> component = FindMaxComponent(components);
            for (int i = 0; i < n; i++)
            {
                _numberOfCells *= 4;
                _cols          *= 2;
                _delta         *= 0.5;
                _rows          *= 2;
                SymbolGraph graph = new SymbolGraph(_numberOfCells);

                foreach (var component in components)
                {
                    Parallel.ForEach(component, (c) =>
                                     //foreach(var c in component)
                    {
                        int[] cells = NewCoords(c, oldCols);

                        for (int m = 0; m < 4; m++)
                        {
                            double x, y;
                            ReturnInterval(cells[m], out x, out y);

                            for (int l = 0; l < _cast_square - 1; l++)
                            //Parallel.For(0, _cast, (l, state) =>
                            {
                                for (int d = 0; d < _cast_square - 1; d++)
                                {
                                    double xn = x + (double)l * _delta / (double)_cast_square;
                                    double yn = y - (double)d * _delta / (double)_cast_square;


                                    int cell = ReturnCell(xn, yn);

                                    if (cell == -1)
                                    {
                                        continue;
                                    }
                                    if (cell >= _numberOfCells)
                                    {
                                        continue;
                                    }

                                    int oldCell = OldCoordSystem(cell, oldCols);

                                    if (!Graph.Contains(oldCell))
                                    {
                                        continue;
                                    }

                                    if (!graph.Contains(cells[m]))
                                    {
                                        graph[cells[m]] = new List <int>();
                                    }

                                    if (graph[cells[m]] != null && !graph[cells[m]].Contains(cell))
                                    {
                                        graph[cells[m]].Add(cell);
                                    }
                                }
                            }
                        }
                    });
                    _graph = graph;
                }
                ;
                components = FindStrongConnectedComponents();
                oldCols    = _cols;
            }

            return(components);
        }