예제 #1
0
        public static UndirectedGraph ReadFile(string path)
        {
            UndirectedGraph graph = new UndirectedGraph();

            var s = System.IO.File.ReadAllLines(path);

            foreach (var c in s)
            {
                if (!c.Contains('\t'))
                {
                    throw new ArgumentException("Invalid format");
                }

                var nodes = c.Split('\t');

                if (nodes.Length > 2)
                {
                    throw new ArgumentException("Invalid format");
                }
                if (nodes[0].Contains('\t') || nodes[1].Contains('\t'))
                {
                    throw new ArgumentException("Invalid format");
                }

                if (!graph.ContainsVertex(nodes[0]))
                {
                    graph.AddVertex(nodes[0]);
                }
                if (!graph.ContainsVertex(nodes[1]))
                {
                    graph.AddVertex(nodes[1]);
                }

                if (!graph.ContainsEdge(nodes[0], nodes[1]))
                {
                    graph.AddEdge(nodes[0], nodes[1]);
                }
            }

            return(graph);
        }
예제 #2
0
        public static UndirectedGraph ReadFile(string path)
        {
            UndirectedGraph ug = new UndirectedGraph();

            string[] edges = File.ReadAllLines(path);

            string[] temp;

            for (int x = 0; x < edges.Length; x++)
            {
                temp = edges[x].Split(new char[] { ' ' });

                if (temp.Length != 2 || temp[0] == null || temp[1] == null)
                {
                    throw new ArgumentException("Invalid Format");
                }

                if (temp[0] == temp[1])
                {
                    ug.AddVertex(temp[0]);
                }
                else
                {
                    if (!ug.HasVertex(temp[0]))
                    {
                        ug.AddVertex(temp[0]);
                    }
                    if (!ug.HasVertex(temp[1]))
                    {
                        ug.AddVertex(temp[1]);
                    }

                    ug.AddEdge(temp[0], temp[1]);
                }
            }

            return(ug);
        }