Esempio n. 1
0
        public static IReadOnlyList<int?> GetShortestPaths(DirectedWeightedGraph graph, int source)
        {
            int n = graph.NodesCount;
            var dist = new int?[n];
            dist[source] = 0;
            for (int i = 0; i < dist.Length; i++)
            {
                if (i != source)
                {
                    dist[i] = null;
                }
            }

            var edges = graph.GetEdges().ToArray();
            for (int i = 0; i < n + 1; i++)
            {
                bool changed = false;
                foreach (var edge in edges)
                {
                    if (dist[edge.StartNode] != null && dist[edge.StartNode] + edge.Weight < dist[edge.EndNode].GetValueOrDefault(int.MaxValue))
                    {
                        dist[edge.EndNode] = dist[edge.StartNode] + edge.Weight;
                        changed = true;
                    }
                }

                if (!changed)
                    return dist;
            }

            return null;
        }
Esempio n. 2
0
        public static IEnumerable<int?> Find(DirectedWeightedGraph graph, int source)
        {
            var dist = new int?[graph.NodesCount];
            dist[source] = 0;

            var closestNodes = new PriorityQueue<int, int>(dist.Select((d, i) => new KeyValuePair<int, int>(i, d.GetValueOrDefault(int.MaxValue))));
            var exploredNodes = new HashSet<int>();

            while (closestNodes.Count != 0)
            {
                var node = closestNodes.ExtractHighestPriorityElement();
                exploredNodes.Add(node);
                foreach (var edge in graph.GetEdges(node).Where(e => !exploredNodes.Contains(e.EndNode)))
                {
                    if (dist[node] != null)
                    {
                        var alt = dist[node].Value + edge.Weight;
                        if (alt < dist[edge.EndNode].GetValueOrDefault(int.MaxValue))
                        {
                            dist[edge.EndNode] = alt;
                            closestNodes.ChangePriority(edge.EndNode, alt);
                        }
                    }
                }
            }

            return dist;
        }
Esempio n. 3
0
        public static int GetMinimumSpanningTreeLength(DirectedWeightedGraph graph)
        {
            int length = 0;
            var dist = new int[graph.NodesCount];
            const int StartNode = 0;
            dist[StartNode] = 0;
            for (int i = 0; i < dist.Length; i++)
            {
                if (i != StartNode)
                {
                    dist[i] = int.MaxValue;
                }
            }

            var closestNodes = new PriorityQueue<int, int>(dist.Select((d, i) => new KeyValuePair<int, int>(i, d)));
            var exploredNodes = new HashSet<int>();

            while (exploredNodes.Count != graph.NodesCount)
            {
                var node = closestNodes.ExtractHighestPriorityElement();
                length += dist[node];
                exploredNodes.Add(node);
                foreach (var edge in graph.GetEdges(node).Where(e => !exploredNodes.Contains(e.EndNode)))
                {
                    var alt = edge.Weight;
                    if (alt < dist[edge.EndNode])
                    {
                        dist[edge.EndNode] = alt;
                        closestNodes.ChangePriority(edge.EndNode, alt);
                    }
                }
            }

            return length;
        }
Esempio n. 4
0
        public static int?[,] GetShortestPaths(DirectedWeightedGraph graph)
        {
            int n = graph.NodesCount;
            var distances = new int?[n, n];

            for (int i = 0; i < n; i++)
            {
                distances[i, i] = 0;
            }

            foreach (var edge in graph.GetEdges())
            {
                distances[edge.StartNode, edge.EndNode] = edge.Weight;
            }

            for (int k = 0; k < n; k++)
                for (int i = 0; i < n; i++)
                    for (int j = 0; j < n; j++)
                    {
                        int? prevDistance = distances[i, j];
                        int? newDistance = distances[i, k] + distances[k, j];
                        if (prevDistance == null)
                        {
                            distances[i, j] = newDistance;
                        }
                        else if (newDistance == null)
                        {
                            distances[i, j] = prevDistance;
                        }
                        else
                        {
                            distances[i, j] = Math.Min(prevDistance.GetValueOrDefault(), newDistance.GetValueOrDefault());
                        }
                    }

            for (int i = 0; i < n; i++)
            {
                if (distances[i, i] < 0) return null;
            }

            return distances;
        }
Esempio n. 5
0
        public static int GetMaxSpacing(DirectedWeightedGraph graph, int clusterCount)
        {
            int currentClusterCount = graph.NodesCount;
            var unionFind = new UnionFind(currentClusterCount);
            var edges = new PriorityQueue<WeightedEdge, int>(graph.GetEdges().Select(e => new KeyValuePair<WeightedEdge, int>(e, e.Weight)));
            while (currentClusterCount >= clusterCount)
            {
                var edge = edges.ExtractHighestPriorityElement();
                var cluster1 = unionFind.Find(edge.StartNode);
                var cluster2 = unionFind.Find(edge.EndNode);
                if (cluster1 != cluster2)
                {
                    if (currentClusterCount == clusterCount)
                    {
                        return edge.Weight;
                    }

                    unionFind.Union(cluster1, cluster2);
                    currentClusterCount--;
                }
            }

            return 0;
        }