Exemplo n.º 1
0
        public static void Run(Vertex s)
        {
            s.Color = Color.Gray;
            s.Depth = 0;

            Queue<Vertex> queue = new Queue<Vertex>();
            queue.Enqueue(s);

            while(queue.Count > 0)
            {
                Vertex current = queue.Dequeue();
                foreach(Vertex next in current.Vertices)
                {
                    if(next.Color == Color.White)
                    {
                        next.Color = Color.Gray;
                        next.Depth = current.Depth + 1;
                        next.Parent = current;
                        queue.Enqueue(next);
                    }
                }

                current.Color = Color.Black;
            }
        }
Exemplo n.º 2
0
        public static bool Run(Vertex[] vertices, int source)
        {
            vertices[source].Depth = 0;

            for (int i = 0; i < vertices.Length; i++)
            {
                foreach(Vertex v in vertices)
                {
                    foreach(Edge e in v.Edges)
                    {
                        if(e.From.Depth != int.MaxValue && e.From.Depth + e.Weight < e.To.Depth)
                        {
                            e.To.Depth = e.From.Depth + e.Weight;
                            e.To.Parent = e.From;
                        }
                    }
                }
            }

            foreach (Vertex v in vertices)
            {
                foreach (Edge e in v.Edges)
                {
                    if(e.From.Depth + e.Weight < e.To.Depth)
                        return false;
                }
            }

            return true;
        }
Exemplo n.º 3
0
        public static Edge[] Run(Vertex[] vertices)
        {
            Dictionary<Vertex, List<Vertex>> sets = new Dictionary<Vertex, List<Vertex>>();
            foreach(Vertex v in vertices)
            {
                sets[v] = new List<Vertex>();
                sets[v].Add(v);
            }

            List<Edge> results = new List<Edge>();

            foreach(Edge edge in vertices.SelectMany(v => v.Edges).OrderBy(e => e.Weight))
            {
                List<Vertex> from = sets[edge.From];
                List<Vertex> to = sets[edge.To];

                if(from != to)
                {
                    results.Add(edge);
                    foreach(Vertex v in to)
                    {
                        from.Add(v);
                        sets[v] = from;
                    }
                }
            }

            return results.ToArray();
        }
Exemplo n.º 4
0
        private static void RemoveRandomEdge(Vertex[] vertices)
        {
            bool found = false;
            int i = 0;
            int j = 0;

            int count = 0;
            Random random = new Random();

            for (int k = 0; k < vertices.Length; k++)
            {
                for (int l = 0; l < vertices.Length; l++)
                {
                    count++;

                    if (random.Next(0, count) == 0)
                    {
                        found = true;
                        i = k;
                        j = l;
                    }
                }
            }

            if (found)
                vertices[i].RemoveDirectedEdge(vertices[j]);
        }
Exemplo n.º 5
0
        private static bool IsDirectedAcyclicGraph(Vertex[] vertices)
        {
            foreach (Vertex v in vertices)
                v.Reset();

            return !vertices.Any(v => TopologicalSortTestClass.HasCycle(v));
        }
Exemplo n.º 6
0
        public static Vertex[] Run(Vertex[] vertices)
        {
            List<Vertex> list = new List<Vertex>();

            foreach(Vertex v in vertices)
                TopologicalSort.Run(v, list);

            return list.ToArray();
        }
Exemplo n.º 7
0
        private static bool HasCycle(Vertex vertex)
        {
            if (vertex.Color == Color.Gray)
                return true;

            if (vertex.Color == Color.Black)
                return false;

            vertex.Color = Color.Gray;

            if (vertex.Vertices.Any(v => TopologicalSortTestClass.HasCycle(v)))
                return true;

            vertex.Color = Color.Black;

            return false;
        }
Exemplo n.º 8
0
        public static void Run(Vertex s)
        {
            if (s.Color != Color.White)
                return;

            s.Color = Color.Gray;

            foreach (Vertex next in s.Vertices)
            {
                if (next.Color == Color.White)
                {
                    next.Parent = s;
                    DepthFirstSearch.Run(next);
                }
            }

            s.Color = Color.Black;
        }
Exemplo n.º 9
0
        private static void Run(Vertex vertex, List<Vertex> list)
        {
            if (vertex.Color != Color.White)
                return;

            vertex.Color = Color.Gray;

            foreach(Vertex next in vertex.Vertices)
            {
                if(next.Color == Color.White)
                {
                    next.Parent = vertex;
                    TopologicalSort.Run(next, list);
                }
            }

            vertex.Color = Color.Black;
            list.Insert(0, vertex);
        }
Exemplo n.º 10
0
        public static void Run(Vertex vertex)
        {
            vertex.Depth = 0;
            PriorityQueue<Vertex> queue = new PriorityQueue<Vertex>((x, y) => y.Depth.CompareTo(x.Depth));

            queue.Insert(vertex);
            while(!queue.IsEmpty)
            {
                Vertex current = queue.Pop();
                foreach(Edge edge in current.Edges)
                {
                    if(edge.From.Depth + edge.Weight < edge.To.Depth)
                    {
                        edge.To.Depth = edge.From.Depth + edge.Weight;
                        edge.To.Parent = edge.From;
                        queue.Insert(edge.To);
                    }
                }
            }
        }
Exemplo n.º 11
0
        private static Vertex[] CreateDirectedAcyclicGraph(int n)
        {
            Vertex[] vertices = new Vertex[n];

            for (int i = 0; i < vertices.Length; i++)
                vertices[i] = new Vertex();

            for(int i = 0; i < vertices.Length; i++)
            {
                for (int j = 0; j < i; j++)
                    vertices[i].AddUnDirectedEdge(vertices[j]);
            }

            while (!TopologicalSortTestClass.IsDirectedAcyclicGraph(vertices))
                TopologicalSortTestClass.RemoveRandomEdge(vertices);

            foreach (Vertex v in vertices)
                v.Reset();

            return vertices;
        }
Exemplo n.º 12
0
        public static int[,] Run(Vertex[] vertices)
        {
            int[,] depths = new int[vertices.Length, vertices.Length];
            for (int i = 0; i < depths.GetLength(0); i++)
            {
                for (int j = 0; j < depths.GetLength(1); j++)
                {
                    if(i != j)
                        depths[i, j] = int.MaxValue;
                }
            }

            for(int i = 0; i < vertices.Length; i++)
            {
                foreach(Edge edge in vertices[i].Edges)
                {
                    int j = 0;
                    while (j < vertices.Length && vertices[j] != edge.To)
                        j++;

                    depths[i, j] = edge.Weight;
                }
            }

            for (int i = 0; i < vertices.Length; i++)
            {
                for (int j = 0; j < vertices.Length; j++)
                {
                    for (int k = 0; k < vertices.Length; k++)
                    {
                        if(depths[j, i] != int.MaxValue && depths[i, k] != int.MaxValue)
                            depths[j, k] = Math.Min(depths[j, k], depths[j, i] + depths[i, k]);
                    }
                }
            }

            return depths;
        }
Exemplo n.º 13
0
 public Edge(Vertex from, Vertex to, int weight)
 {
     this.From = from;
     this.To = to;
     this.Weight = weight;
 }
Exemplo n.º 14
0
 public void RemoveUndirectedEdge(Vertex to)
 {
     this.RemoveDirectedEdge(to);
     to.RemoveDirectedEdge(this);
 }
Exemplo n.º 15
0
 public void RemoveDirectedEdge(Vertex to)
 {
     Edge edge = this.Edges.FirstOrDefault(e => e.To == to);
     if (edge != null)
         edges.Remove(edge);
 }
Exemplo n.º 16
0
 public void AddUnDirectedEdge(Vertex to, int weight = 1)
 {
     this.AddDirectedEdge(to, weight);
     to.AddDirectedEdge(this, weight);
 }
Exemplo n.º 17
0
 public void AddDirectedEdge(Vertex to, int weight = 1)
 {
     this.edges.Add(new Edge(this, to, weight));
 }