예제 #1
0
        //Utility to parse the Tuples to stack and retrieve as per Neighbor path
        public HashSet <T> DFS <T>(GraphTask <T> graph, T start, Action <T> preVisit = null)
        {
            var visited = new HashSet <T>();

            if (!graph.AdjacencyList.ContainsKey(start))
            {
                return(visited);
            }

            var stack = new Stack <T>();

            stack.Push(start);

            while (stack.Count > 0)
            {
                var vertex = stack.Pop();

                if (visited.Contains(vertex))
                {
                    continue;
                }

                if (preVisit != null)
                {
                    preVisit(vertex);
                }

                visited.Add(vertex);
                //Pick from the path provided remove "Reverse" incase of no sequence required
                foreach (var neighbor in graph.AdjacencyList[vertex].Where(node => !visited.Contains(node)).Reverse())
                {
                    if (!visited.Contains(neighbor))
                    {
                        stack.Push(neighbor);
                    }
                }
            }

            return(visited);
        }
        //parse the CSV data and generate vertices & Tuple Lists - INIT Graph
        public EmployeeTask(string[] csvLines)
        {
            employees = new Dictionary <string, Employee>();
            empList   = new List <Employee>();
            var csvData = csvLines.Select(deli => deli.Split('\t'));
            var csv     = from line in csvData select(from dataLn in line select dataLn);

            int top = 0;

            foreach (var n in csv)
            {
                var p = n.GetEnumerator();
                while (p.MoveNext())
                {
                    try
                    {
                        var data = p.Current.ToString().Split(',');
                        if (string.IsNullOrEmpty(data[0]))
                        {
                            Console.WriteLine("ER001 : Employee ID is null or Empty in the Data File !!");
                            continue;
                        }

                        if (string.IsNullOrEmpty(data[1]) && top < 1)
                        {
                            top++;
                        }
                        else if (string.IsNullOrEmpty(data[1]) && top == 1)
                        {
                            Console.WriteLine("ER002 : Multiple CEOs found and Data file incorrect !!");
                            continue;
                        }
                        int empSalary = 0;
                        if (Int32.TryParse(data[2], out empSalary))
                        {
                            var empl = new Employee(data[0], data[1], empSalary);
                            try
                            {
                                empList.Add(empl);
                                if (employees.ContainsKey(empl.EmpName))
                                {
                                    Console.WriteLine("ER004: Multiple Reporting Identified ");
                                }
                                else
                                {
                                    employees.Add(empl.EmpName, empl);
                                }
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine("EX001: While Adding data to the Lists ", e);
                            }
                        }
                        else
                        {
                            Console.WriteLine("ER003 : Employee Salary not an Integer !!");
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("EX002: While parsing Data ", e);
                    }
                }
                p.Dispose();
            }

            var empsObjs  = GetVertices();
            var empTuples = GetTuples();

            graph = new GraphTask <Employee>(empsObjs, empTuples);
        }