Exemplo n.º 1
0
        static Node<Vertex> Sort(Vertex[] vertexes)
        {
            Node<Vertex> head = null;

            foreach (Vertex v in vertexes)
            {
                if (v.Status == Color.White)
                {
                    DFS(v, ref head);
                }
            }

            return head;
        }
Exemplo n.º 2
0
        static void DFS(Vertex v, ref Node<Vertex> head)
        {
            foreach (Vertex n in v.Neighbors)
            {
                if (n.Status == Color.White)
                {
                    n.Status = Color.Grey;
                    DFS(n, ref head);
                }
            }

            v.Status = Color.Black;
            Node<Vertex> node = new Node<Vertex>(v);
            node.Next = head;
            head = node;
        }
Exemplo n.º 3
0
        public static int ShortestPath(Vertex start, Vertex end)
        {
            Queue<Vertex> queue = new Queue<Vertex>(10);

            if (start == null || end == null)
                return 0;

            start.Status = Color.Grey;
            queue.Push(start);

            Vertex cur = null;
            while (!queue.IsEmpty())
            {
                cur = queue.Pop();
                if (cur == end)
                {
                    break;
                }

                foreach (Vertex v in cur.Neighbors)
                {
                    if (v.Status == Color.White)
                    {
                        v.Status = Color.Grey;
                        v.Parent = cur;
                        v.Distance = cur.Distance + 1;
                        queue.Push(v);
                    }
                }

                cur.Status = Color.Black;
            }

            if (cur.Parent == null)
                return 0;
            else
                return cur.Parent.Distance + 1;
        }
Exemplo n.º 4
0
        public static Dictionary<char, Vertex> CreateGraph(Dictionary<char, char[]> map)
        {
            Dictionary<char, Vertex> vertexes = new Dictionary<char, Vertex>();
            foreach (char key in map.Keys)
            {
                vertexes.Add(key, new Vertex(key));
            }

            foreach (KeyValuePair<char, char[]> kvp in map)
            {
                Vertex[] neighbors = new Vertex[kvp.Value.Length];

                int idx = 0;
                foreach (char key in kvp.Value)
                {
                    neighbors[idx++] = vertexes[key];
                }

                vertexes[kvp.Key].Neighbors = neighbors;
            }

            return vertexes;
        }
Exemplo n.º 5
0
 public Edge()
 {
     Start = End = null;
     Weight = 1;
 }
Exemplo n.º 6
0
 public Vertex(char key)
 {
     Key = key;
     Parent = null;
     Status = Color.White;
     Distance = 0;
 }