Ejemplo n.º 1
0
        public void Parse()
        {
            this.ReadFromFile();

            this.House_Count = int.Parse(this.ResultString[0]);

            this.Map = new GraphKingdomMap();

            // generate all vertex
            for (int i = 1; i <= this.House_Count; i++)
            {
                DataVertex newVertex = new DataVertex(i);
                this.Map.AddVertex(newVertex);
            }

            // get a list of all vertex for easy access
            var vlist = this.Map.Vertices.ToList();

            // insert edges from input
            for (int i = 1; i < this.House_Count; i++)
            {
                // split input into two strings
                string[] temp = ResultString[i].Split();

                // parse from string to int to get vertex id
                int source      = int.Parse(temp[0]) - 1;
                int destination = int.Parse(temp[1]) - 1;

                // insert bidirectional edges
                var newEdge = new DataEdge(vlist[source], vlist[destination]);
                this.Map.AddVerticesAndEdge(newEdge);
                newEdge = new DataEdge(vlist[destination], vlist[source]);
                this.Map.AddVerticesAndEdge(newEdge);
            }
        }
Ejemplo n.º 2
0
        private void GraphKingdomArea_Setup(GraphKingdomMap graph)
        {
            //create logic core and fill Graph
            var logicCore = new GraphXLogicCore()
            {
                Graph = graph
            };

            //This property sets layout algorithm that will be used to calculate vertices positions
            //Different algorithms uses different values and some of them uses edge Weight property.
            logicCore.DefaultLayoutAlgorithm = LayoutAlgorithmTypeEnum.KK;
            //Now we can set parameters for selected algorithm using AlgorithmFactory property. This property provides methods for
            //creating all available algorithms and algo parameters.
            logicCore.DefaultLayoutAlgorithmParams = logicCore.AlgorithmFactory.CreateLayoutParameters(LayoutAlgorithmTypeEnum.KK);
            //Unfortunately to change algo parameters you need to specify params type which is different for every algorithm.
            ((KKLayoutParameters)logicCore.DefaultLayoutAlgorithmParams).MaxIterations = 100;

            //This property sets vertex overlap removal algorithm.
            //Such algorithms help to arrange vertices in the layout so no one overlaps each other.
            logicCore.DefaultOverlapRemovalAlgorithm = OverlapRemovalAlgorithmTypeEnum.FSA;
            //Default parameters are created automaticaly when new default algorithm is set and previous params were NULL
            logicCore.DefaultOverlapRemovalAlgorithmParams.HorizontalGap = 50;
            logicCore.DefaultOverlapRemovalAlgorithmParams.VerticalGap   = 50;

            //This property sets edge routing algorithm that is used to build route paths according to algorithm logic.
            //For ex., SimpleER algorithm will try to set edge paths around vertices so no edge will intersect any vertex.
            //Bundling algorithm will try to tie different edges that follows same direction to a single channel making complex graphs more appealing.
            logicCore.DefaultEdgeRoutingAlgorithm = EdgeRoutingAlgorithmTypeEnum.SimpleER;

            //This property sets async algorithms computation so methods like: Area.RelayoutGraph() and Area.GenerateGraph()
            //will run async with the UI thread. Completion of the specified methods can be catched by corresponding events:
            //Area.RelayoutFinished and Area.GenerateGraphFinished.
            logicCore.AsyncAlgorithmCompute = false;

            //Finally assign logic core to GraphArea object
            Area.LogicCore = logicCore;
        }
Ejemplo n.º 3
0
        /* Melakukan DFS dari start hingga destination ditemukan. */
        public static void DFS(int startLevel, int start, int destination, List <int> route, GraphKingdomMap graph)
        {
            foreach (var vert in graph.Vertices)
            {
                // mencari NomorRumah dari daftar rumah
                if (vert.NomorRumah == start)
                {
                    // menambahkan rumah ke rute
                    route.Add(vert.NomorRumah);

                    // jika rumah = destination, method berakhir
                    if (vert.NomorRumah == destination)
                    {
                        break;
                    }

                    // cari tetangga rumah
                    graph.TryGetOutEdges(vert, out IEnumerable <DataEdge> OutEdges);
                    int  i       = 0;
                    bool DeadEnd = true;
                    while ((i < OutEdges.Count()) && (DeadEnd))
                    {
                        // mencari tujuan yang belum pernah dikunjungi dan bukan parent dari start
                        if ((!route.Contains(OutEdges.ElementAt <DataEdge>(i).Target.NomorRumah)) && (OutEdges.ElementAt <DataEdge>(i).Target.Level > startLevel))
                        {
                            DeadEnd = false;
                        }
                        else
                        {
                            i++;
                        }
                    }
                    // jika ditemukan tujuan, pindah ke tujuan
                    if (!DeadEnd)
                    {
                        DFS(startLevel, OutEdges.ElementAt <DataEdge>(i).Target.NomorRumah, destination, route, graph);
                    }
                    else
                    {
                        // jika tidak ditemukan tujuan dan belum kembali ke start
                        if (vert.Level > startLevel)
                        {
                            foreach (var edge in OutEdges)
                            {
                                // backtrack
                                if (edge.Target.Level < vert.Level)
                                {
                                    DFS(startLevel, edge.Target.NomorRumah, destination, route, graph);
                                }
                            }
                        }
                        // jika tidak ditemukan tujuan dan sudah kembali ke start, berhenti
                        else
                        {
                            route.RemoveAt(route.Count() - 1);
                            break;
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
0
        /* Menentukan level (kedalaman) tiap rumah dengan DFS.
         * Dimulai dari NomorRumah = 1 dan level = 0.
         */
        public static void TentukanLevel(int NomorRumah_dicari, int level, List <int> path, GraphKingdomMap graph)
        {
            foreach (var vert in graph.Vertices)
            {
                if (vert.NomorRumah == NomorRumah_dicari)
                {
                    if (!path.Contains <int>(vert.NomorRumah))
                    {
                        path.Add(vert.NomorRumah);
                    }

                    // menentukan level
                    vert.Level = level;

                    // cari tetangga yang belum dikunjungi
                    graph.TryGetOutEdges(vert, out IEnumerable <DataEdge> OutEdges);
                    int  i       = 0;
                    bool DeadEnd = true;
                    while ((i < OutEdges.Count()) && (DeadEnd))
                    {
                        if (!path.Contains(OutEdges.ElementAt <DataEdge>(i).Target.NomorRumah))
                        {
                            DeadEnd = false;
                            TentukanLevel(OutEdges.ElementAt <DataEdge>(i).Target.NomorRumah, level + 1, path, graph);
                        }
                        else
                        {
                            i++;
                        }
                    }
                    // jika tidak ada tentangga yang belum dikunjungi dan belum balik ke istana
                    if (level > 0)
                    {
                        foreach (var edge in OutEdges)
                        {
                            if (edge.Target.Level < level)
                            {
                                level = level - 1;
                                TentukanLevel(edge.Target.NomorRumah, level, path, graph);
                            }
                        }
                    }
                    // jika tidak ada tentangga yang belum dikunjungi dan sudah balik ke istana, berhenti
                    else
                    {
                        break;
                    }
                }
            }
        }