コード例 #1
0
        public void ListGraph_BFS_ShouldReturnAllVertex()
        {
            //Arrange
            var graph = new ListGraph <int, int>();
            var v1    = new Vertex <int, int>(1, 1);
            var v2    = new Vertex <int, int>(2, 2);
            var v3    = new Vertex <int, int>(3, 3);
            var v5    = new Vertex <int, int>(5, 5);
            var v6    = new Vertex <int, int>(6, 6);

            graph.AddEdge(v1, v2);
            graph.AddEdge(v1, v3);
            graph.AddEdge(v2, v5);
            graph.AddEdge(v2, v6);
            graph.AddEdge(v3, v6);

            //Act
            var result = graph.BFS(v6.Id).ToList();

            Assert.Equal(5, result.Count);
            Assert.Contains(result, r => r.Id == v6.Id);
            Assert.Contains(result, r => r.Id == v2.Id);
            Assert.Contains(result, r => r.Id == v3.Id);
            Assert.Contains(result, r => r.Id == v1.Id);
            Assert.Contains(result, r => r.Id == v5.Id);
        }
コード例 #2
0
        public string GetResult()
        {
            Graph graph1 = new ListGraph();
            Graph graph2 = new MatrixGraph();

            return($"Graph1: {graph1.GetName()}\nGraph2: {graph2.GetName()}");
        }
コード例 #3
0
        private static ListGraph <int> MakingGraph()
        {
            var graph = new ListGraph <int>();

            graph.AddVertex(1);
            graph.AddVertex(11);
            graph.AddVertex(111);
            graph.AddVertex(1111);
            graph.AddVertex(11111);
            graph.AddVertex(111111);
            graph.AddVertex(111111);
            graph.AddEdge(1, 11);
            graph.AddEdge(11, 111);
            graph.AddEdge(111, 1111);
            graph.AddEdge(1111, 11111);
            graph.AddEdge(11111, 111111);

            graph.AddVertex(2);
            graph.AddVertex(22);
            graph.AddVertex(222);
            graph.AddVertex(2222);
            graph.AddVertex(22222);
            graph.AddVertex(222222);
            graph.AddVertex(2222222);
            graph.AddEdge(1, 2);
            graph.AddEdge(2, 22);
            graph.AddEdge(22, 222);
            graph.AddEdge(222, 2222);
            graph.AddEdge(2222, 22222);
            graph.AddEdge(22222, 222222);
            graph.AddEdge(22222, 2222222);
            return(graph);
        }
コード例 #4
0
        /// <summary>
        /// Recursive function that colors all connections of a node and then colors all the connection's connections
        /// If two connected nodes have the same color, then graph is not bipartite
        /// </summary>
        /// <param name="graph">graph to check if it is bipartite</param>
        /// <param name="colorFlags">nodes that will be colored</param>
        /// <param name="bQueue">keeps track of breadth-first search algorithim</param>
        /// <param name="colorTurn"></param>
        /// <returns>True = Bipartite. False = Not Bipartite</returns>
        private static bool isBipartite(ListGraph graph, color[] colorFlags, Queue <int> bQueue)
        {
            int index = bQueue.Dequeue();

            int[] connections = graph.getConnections(graph[index]);
            /* Colors and checks colors */
            for (int count = 0; count < connections.Length; count++)
            {
                if (colorFlags[connections[count]] == color.NIL)
                {
                    colorFlags[connections[count]] = (colorFlags[index] == color.ORANGE) ? color.BLUE : color.ORANGE;
                    bQueue.Enqueue(connections[count]);
                }
                else if (colorFlags[index] == colorFlags[connections[count]]) //Two nodes are the same color. Not bipartite
                {
                    return(false);
                }
            }
            if (bQueue.Count > 0) //loops until all nodes have been checked
            {
                return(isBipartite(graph, colorFlags, bQueue));
            }

            return(true);
        }
コード例 #5
0
        /// <summary>
        /// Checks graph to see if it is Bipartite and creates two seperate groups in an array with the integer
        /// values of 1 = Group 1 and 2 = Group 2. If not bipartite then entire array will be in group 1
        /// </summary>
        /// <param name="graph">Graph to partition</param>
        /// <returns>int[] that is parallel to graph[] which links those nodes into two groups of 1 or 2</returns>
        public static int[] getBipartition(ListGraph graph)
        {
            color[] colorFlags = new color[graph.length];
            int[]   partition  = new int[graph.length];
            /* Sets all nodes to colorless and sets the entire partition to 1 */
            for (int count = 0; count < colorFlags.Length; count++)
            {
                colorFlags[count] = color.NIL;
                partition[count]  = 1;
            }

            if (!isBipartite(graph, colorFlags))  //Not bipartite
            {
                Console.WriteLine("Graph is not bipartite, Grouping all nodes.");
            }
            else  //Bipartite, setting all nodes to their respective groups
            {
                for (int count = 0; count < colorFlags.Length; count++)
                {
                    partition[count] = (colorFlags[count] == color.BLUE) ? 1 : 2;
                }
            }

            return(partition);
        }
コード例 #6
0
    public override void OnDrawGizmos()
    {
        ListGraph graph = target as ListGraph;

        //Debug.Log ("Gizmos "+(graph == null)+" "+target);
        if (graph == null)
        {
            return;
        }

        //Handles.color = new Color (0.161F,0.341F,1F,0.5F);
        Gizmos.color = new Color(0.161F, 0.341F, 1F, 0.5F);
        //for (int i=0;i<graph.nodes.Length;i++) {

        if (graph.root != null)
        {
            DrawChildren(graph, graph.root);
        }
        else
        {
            GameObject[] gos = GameObject.FindGameObjectsWithTag(graph.searchTag);
            for (int i = 0; i < gos.Length; i++)
            {
                Gizmos.DrawCube(gos[i].transform.position, Vector3.one * HandleUtility.GetHandleSize(gos[i].transform.position) * 0.1F);
            }
        }
    }
コード例 #7
0
ファイル: Program.cs プロジェクト: mikikora/University
        static void Main(string[] args)
        {
            DateTime time = DateTime.Now;

            Console.WriteLine(time);

            IGraph g = new ListGraph(100, 1000);

            string[] a = g.Neighbours("w1");
            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] != "")
                {
                    //Console.WriteLine(a[i]);
                }
            }
            IGraph graph    = new MatrixGraph(100, 1000);
            BFS    poszukaj = new BFS();

            string[] wynik = poszukaj.FindWay(graph, "w1", "w2");
            for (int i = 0; i < wynik.Length; i++)
            {
                Console.WriteLine(wynik[i]);
            }

            Console.WriteLine(time - DateTime.Now);
        }
コード例 #8
0
 public void DrawChildren(ListGraph graph, Transform tr)
 {
     foreach (Transform child in tr)
     {
         Gizmos.DrawCube(child.position, Vector3.one * HandleUtility.GetHandleSize(child.position) * 0.1F);
         //Handles.CubeCap (-1,graph.nodes[i].position,Quaternion.identity,HandleUtility.GetHandleSize(graph.nodes[i].position)*0.1F);
         //Gizmos.DrawCube (nodes[i].position,Vector3.one);
         if (graph.recursive)
         {
             DrawChildren(graph, child);
         }
     }
 }
コード例 #9
0
        /// <summary>
        /// Used in getPartition to check to see if graph is bipartite and returns an array with set colors
        /// </summary>
        /// <param name="graph">Graph to check to see if it is bipartite</param>
        /// <param name="colorFlags">nodes to be colored</param>
        /// <returns>colored nodes that prove if graph is bipartite or not</returns>
        private static bool isBipartite(ListGraph graph, color[] colorFlags)
        {
            if (graph.length <= 0) //Edge case of empty graph
            {
                return(true);
            }
            Queue <int> bQueue = new Queue <int>();

            for (int count = 0; count < colorFlags.Length; count++)
            {
                colorFlags[count] = color.NIL;
            }
            bQueue.Enqueue(0);
            colorFlags[0] = color.BLUE;
            return(isBipartite(graph, colorFlags, bQueue));
        }
コード例 #10
0
    public override void OnInspectorGUI(NavGraph target)
    {
        ListGraph graph = target as ListGraph;

/*
 #if UNITY_3_3
 *              graph.root = (Transform)EditorGUILayout.ObjectField (new GUIContent ("Root","All childs of this object will be used as nodes, if it is not set, a tag search will be used instead (see below)"),graph.root,typeof(Transform));
 #else
 *              graph.root = (Transform)EditorGUILayout.ObjectField (new GUIContent ("Root","All childs of this object will be used as nodes, if it is not set, a tag search will be used instead (see below)"),graph.root,typeof(Transform),true);
 #endif
 */
        graph.root = ObjectField(new GUIContent("Root", "All childs of this object will be used as nodes, if it is not set, a tag search will be used instead (see below)"), graph.root, typeof(Transform), true) as Transform;

        graph.recursive = EditorGUILayout.Toggle(new GUIContent("Recursive", "Should childs of the childs in the root GameObject be searched"), graph.recursive);
        graph.searchTag = EditorGUILayout.TagField(new GUIContent("Tag", "If root is not set, all objects with this tag will be used as nodes"), graph.searchTag);

        if (graph.root != null)
        {
            GUILayout.Label("All childs " + (graph.recursive ? "and sub-childs ":"") + "of 'root' will be used as nodes\nSet root to null to use a tag search instead", AstarPathEditor.helpBox);
        }
        else
        {
            GUILayout.Label("All object with the tag '" + graph.searchTag + "' will be used as nodes" + (graph.searchTag == "Untagged" ? "\nNote: the tag 'Untagged' cannot be used" : ""), AstarPathEditor.helpBox);
        }

        graph.maxDistance = EditorGUILayout.FloatField(new GUIContent("Max Distance", "The max distance in world space for a connection to be valid. A zero counts as infinity"), graph.maxDistance);
        graph.limits      = EditorGUILayout.Vector3Field("Max Distance (axis aligned)", graph.limits);
        graph.raycast     = EditorGUILayout.Toggle(new GUIContent("Raycast", "Use raycasting to check if connections are valid between each pair of nodes"), graph.raycast);

        editor.GUILayoutx.BeginFadeArea(graph.raycast, "raycast");

        EditorGUI.indentLevel++;

        graph.thickRaycast = EditorGUILayout.Toggle(new GUIContent("Thick Raycast", "A thick raycast checks along a thick line with radius instead of just along a line"), graph.thickRaycast);

        editor.GUILayoutx.BeginFadeArea(graph.thickRaycast, "thickRaycast");

        graph.thickRaycastRadius = EditorGUILayout.FloatField(new GUIContent("Raycast Radius", "The radius in world units for the thick raycast"), graph.thickRaycastRadius);

        editor.GUILayoutx.EndFadeArea();

        //graph.mask = 1 << EditorGUILayout.LayerField ("Mask",(int)Mathf.Log (graph.mask,2));
        graph.mask = EditorGUILayoutx.LayerMaskField(/*new GUIContent (*/ "Mask" /*,"Used to mask which layers should be checked")*/, graph.mask);
        EditorGUI.indentLevel--;

        editor.GUILayoutx.EndFadeArea();
    }
コード例 #11
0
        public void ListGraph_FindMother_ShouldReturnNull()
        {
            //Arrange
            var graph = new ListGraph <int, int>();
            var v1    = new Vertex <int, int>(1, 1);
            var v2    = new Vertex <int, int>(2, 2);
            var v3    = new Vertex <int, int>(3, 3);
            var v5    = new Vertex <int, int>(5, 5);

            graph.AddEdge(v1, v3);
            graph.AddEdge(v2, v5);

            //Act
            var result = graph.FindMother();

            Assert.Null(result);
        }
コード例 #12
0
        /// <summary>
        /// Checks to see if graph is bipartite. That is, if the graph can be seperated into two groups of connections.
        /// Checks in a breadth-first search style
        /// </summary>
        /// <param name="graph">Graph to check if it is bipartite</param>
        /// <returns>True = is bipartite. False = not bipartite</returns>
        public static bool isBipartite(ListGraph graph)
        {
            if (graph.length <= 0) //Edge case of empty graph
            {
                return(true);
            }
            color[]     colorFlags = new color[graph.length];
            Queue <int> bQueue     = new Queue <int>();

            /* sets all nodes to colorless*/
            for (int count = 0; count < colorFlags.Length; count++)
            {
                colorFlags[count] = color.NIL;
            }
            bQueue.Enqueue(0);
            colorFlags[0] = color.BLUE;
            return(isBipartite(graph, colorFlags, bQueue));
        }
コード例 #13
0
    public override void OnInspectorGUI(NavGraph target)
    {
        ListGraph graph = target as ListGraph;

#if UNITY_3_3
        graph.root = (Transform)EditorGUILayout.ObjectField(new GUIContent("Root", "All childs of this object will be used as nodes, if it is not set, a tag search will be used instead (see below)"), graph.root, typeof(Transform));
#else
        graph.root = (Transform)EditorGUILayout.ObjectField(new GUIContent("Root", "All childs of this object will be used as nodes, if it is not set, a tag search will be used instead (see below)"), graph.root, typeof(Transform), true);
#endif
        graph.recursive = EditorGUILayout.Toggle(new GUIContent("Recursive", "Should childs of the childs in the root GameObject be searched"), graph.recursive);
        graph.searchTag = EditorGUILayout.TagField(new GUIContent("Tag", "If root is not set, all objects with this tag will be used as nodes"), graph.searchTag);

        if (graph.root != null)
        {
            GUILayout.Label("All childs " + (graph.recursive ? "and sub-childs ":"") + "of 'root' will be used as nodes\nSet root to null to use a tag search instead", AstarPathEditor.helpBox);
        }
        else
        {
            GUILayout.Label("All object with the tag '" + graph.searchTag + "' will be used as nodes" + (graph.searchTag == "Untagged" ? "\nNote: the tag 'Untagged' cannot be used" : ""), AstarPathEditor.helpBox);
        }

        graph.maxDistance = EditorGUILayout.FloatField("Max Distance", graph.maxDistance);
        graph.limits      = EditorGUILayout.Vector3Field("Max Distance (axis aligned)", graph.limits);
        graph.raycast     = EditorGUILayout.Toggle("Raycast", graph.raycast);

        editor.GUILayoutx.BeginFadeArea(graph.raycast, "raycast");

        EditorGUI.indentLevel++;

        graph.thickRaycast = EditorGUILayout.Toggle("Thick Raycast", graph.thickRaycast);

        editor.GUILayoutx.BeginFadeArea(graph.thickRaycast, "thickRaycast");

        graph.thickRaycastRadius = EditorGUILayout.FloatField("Raycast Radius", graph.thickRaycastRadius);

        editor.GUILayoutx.EndFadeArea();

        //graph.mask = 1 << EditorGUILayout.LayerField ("Mask",(int)Mathf.Log (graph.mask,2));
        graph.mask = EditorGUILayoutx.LayerMaskField("Mask", graph.mask);
        EditorGUI.indentLevel--;

        editor.GUILayoutx.EndFadeArea();
    }
コード例 #14
0
        public void ListGraph_CountPaths_ShouldReturn3()
        {
            //Arrange
            var graph = new ListGraph <int, int>();
            var v0    = new Vertex <int, int>(0, 0);
            var v1    = new Vertex <int, int>(1, 1);
            var v2    = new Vertex <int, int>(2, 2);
            var v3    = new Vertex <int, int>(3, 3);

            graph.AddEdge(v0, v1);
            graph.AddEdge(v0, v2);
            graph.AddEdge(v0, v3);
            graph.AddEdge(v2, v1);
            graph.AddEdge(v1, v3);

            //Act
            var result = graph.CountPaths(v2.Id, v3.Id);

            Assert.Equal(4, result);
        }
コード例 #15
0
        public void ListGraph_FindMother_ShouldReturnSixVertex()
        {
            //Arrange
            var graph = new ListGraph <int, int>();
            var v1    = new Vertex <int, int>(1, 1);
            var v2    = new Vertex <int, int>(2, 2);
            var v3    = new Vertex <int, int>(3, 3);
            var v5    = new Vertex <int, int>(5, 5);
            var v6    = new Vertex <int, int>(6, 6);

            graph.AddEdge(v1, v2);
            graph.AddEdge(v1, v3);
            graph.AddEdge(v2, v5);
            graph.AddEdge(v2, v6);
            graph.AddEdge(v3, v6);

            //Act
            var result = graph.FindMother();

            Assert.NotNull(result);
            Assert.Equal(2, result.Id);
        }
コード例 #16
0
        public void GISTest()
        {
            Node vertex0 = new Node();
            Node vertex1 = new Node();
            Node vertex2 = new Node();
            Node vertex3 = new Node();
            Node vertex4 = new Node();

            List <Node> nodes = new List <Node> {
                vertex0, vertex1, vertex2, vertex3, vertex4
            };
            ListGraph graph = new ListGraph(nodes);

            graph.AddDoubleEdge(new List <Node> {
                vertex0, vertex1
            });
            graph.AddDoubleEdge(new List <Node> {
                vertex1, vertex2
            });
            graph.AddDoubleEdge(new List <Node> {
                vertex1, vertex3
            });
            graph.AddDoubleEdge(new List <Node> {
                vertex3, vertex4
            });
            graph.AddDoubleEdge(new List <Node> {
                vertex0, vertex4
            });

            Dictionary <int, int> actual = Coloring.GIS(graph);

            foreach (var color in actual)
            {
                Console.WriteLine(color.Key + ": " + color.Value);
            }
        }
コード例 #17
0
 public GraphDepthFirstSearchEngine(ListGraph <TNode> graph) : base(graph)
 {
 }
コード例 #18
0
 public GraphSearchEngine(ListGraph <TNode> graph) => _graph = graph;
コード例 #19
0
ファイル: Third.cs プロジェクト: ImNtail/Lab10
        //===========================================Main==============================================
        public static void Execute()
        {
            int x = 0, y = 0;

            while (x < 1 || x > 8)
            {
                Console.Write("Enter the first vertex: ");
                x = int.Parse(Console.ReadLine());
            }
            Console.WriteLine();
            while (y < 1 || y > 8)
            {
                Console.Write("Enter the second vertex: ");
                y = int.Parse(Console.ReadLine());
            }
            Console.WriteLine("\nIn the form of an incidence matrix:\n");
            int[,] matrix =
            {
                { 0, 1, 1, 0, 0, 0, 0, 0 },
                { 1, 0, 0, 0, 0, 1, 1, 0 },
                { 1, 0, 0, 1, 0, 1, 0, 1 },
                { 0, 0, 1, 0, 1, 0, 0, 0 },
                { 0, 0, 0, 1, 0, 1, 0, 0 },
                { 0, 1, 1, 0, 1, 0, 0, 0 },
                { 0, 1, 0, 0, 0, 0, 0, 1 },
                { 0, 0, 1, 0, 0, 0, 1, 0 }
            };
            Graph       g   = new Graph(matrix, 8);
            Stack <int> dfs = g.DFS(x - 1, y - 1);

            Console.WriteLine("DFS:");
            ShowPath(dfs);
            Stack <int> bfs = g.BFS(x - 1, y - 1);

            Console.WriteLine("BFS:");
            ShowPath(bfs);


            Console.WriteLine("\n\nIn the form of linked lists:\n");
            Dictionary <int, List <int> > lists = new Dictionary <int, List <int> >();

            lists[1] = new List <int> {
                2, 3
            };
            lists[2] = new List <int> {
                1, 6, 7
            };
            lists[3] = new List <int> {
                1, 4, 6, 8
            };
            lists[4] = new List <int> {
                3, 5
            };
            lists[5] = new List <int> {
                4, 6
            };
            lists[6] = new List <int> {
                2, 3, 5
            };
            lists[7] = new List <int> {
                2, 8
            };
            lists[8] = new List <int> {
                3, 7
            };
            ListGraph   listGraph = new ListGraph(lists, 8);
            Stack <int> listDFS   = listGraph.ListDFS(x - 1, y - 1);

            Console.WriteLine("DFS:");
            ShowPath(listDFS);
            Stack <int> listBFS = listGraph.ListBFS(x - 1, y - 1);

            Console.WriteLine("BFS:");
            ShowPath(listBFS);
        }