Пример #1
0
        public static Dictionary <int, NodeWeighted> GetTailHeadWeightSpaceGraph(IEnumerable <string> parsed)
        {
            var nodes = new Dictionary <int, NodeWeighted>();

            foreach (var line in parsed)
            {
                var curArray = line.Split(' ');
                var tailNode = int.Parse(curArray[0]);
                var headNode = int.Parse(curArray[1]);
                var weight   = int.Parse(curArray[2]);
                if (!nodes.ContainsKey(tailNode))
                {
                    nodes[tailNode] = new NodeWeighted(tailNode);
                }

                if (!nodes.ContainsKey(headNode))
                {
                    nodes[headNode] = new NodeWeighted(headNode);
                }

                var neighbourToAdd = new Tuple <NodeWeighted, int>(nodes[headNode], weight);
                var parentToAdd    = new Tuple <NodeWeighted, int>(nodes[tailNode], weight);
                nodes[tailNode].Neighbours.Add(neighbourToAdd);
                nodes[headNode].Parents.Add(parentToAdd);
            }
            return(nodes);
        }
Пример #2
0
        public Dictionary <int, NodeWeighted> ReweightEdges(Dictionary <int, NodeWeighted> graph)
        {
            var lastVertex = graph.Keys.Max();
            var newNode    = new NodeWeighted(lastVertex + 1);

            graph[lastVertex + 1] = newNode;
            var parent = new Tuple <NodeWeighted, int>(newNode, 0);

            foreach (var vert in graph.Keys)
            {
                if (vert == lastVertex + 1)
                {
                    continue;
                }
                graph[vert].Parents.Add(parent);
            }

            var bellmanResult = new BellmanFord().GetShortestPaths(graph, lastVertex + 1);

            if (bellmanResult == null)
            {
                return(null);
            }
            var arr = bellmanResult.ToArray();

            for (int i = 1; i < lastVertex + 1; i++)
            {
                if (!arr[i].HasValue)
                {
                    throw new ArgumentException("Some of the shortest paths are null");
                }
                graph[i].JohnsonsValue = arr[i].Value;
            }

            graph.Remove(lastVertex + 1);

            foreach (var vert in graph.Keys)
            {
                var vertScore = graph[vert].JohnsonsValue;
                for (var i = 0; i < graph[vert].Neighbours.Count; i++)
                {
                    var(neighbour, weight) = graph[vert].Neighbours[i];
                    var reweighted = weight + vertScore - neighbour.JohnsonsValue;
                    graph[vert].Neighbours[i] = new Tuple <NodeWeighted, int>(neighbour, reweighted);
                }
            }

            return(graph);
        }
Пример #3
0
        // public string Solve()
        // {
        //     var graph = ParseWeightedGraphFromWeb();
        //
        //     var t = new Stopwatch();
        //     t.Start();
        //     var pathsArray = new DijkstraShortestPath().GetShortestPaths(graph, 1);
        //
        //     var answerArray = _requiredPaths.Select(x => pathsArray[x]).ToArray();
        //
        //     return string.Join(", ", answerArray);
        // }

        private Dictionary <int, NodeWeighted> ParseWeightedGraphFromWeb()
        {
            const string link =
                "https://lagunita.stanford.edu/assets/courseware/v1/c8748131579ef6bd10b2d46f616988e9/asset-v1:Engineering+Algorithms1+SelfPaced+type@asset+block/dijkstraData.txt";
            var nodes = new Dictionary <int, NodeWeighted>();

            var parsed = UtilityMethods.GetParsedStringArrayFromWeb(link, '\n');

            var graphArray = new string[parsed.Length][];

            for (int i = 0; i < parsed.Length; i++)
            {
                parsed[i]     = parsed[i].Replace("\r", "");
                graphArray[i] = parsed[i].Split(new[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
            }

            foreach (var stringArray in graphArray)
            {
                var tailNode = int.Parse(stringArray[0]);
                if (!nodes.ContainsKey(tailNode))
                {
                    nodes[tailNode] = new NodeWeighted(tailNode);
                }

                for (int i = 1; i < stringArray.Length; i++)
                {
                    var temp     = stringArray[i].Split(',').Select(int.Parse).ToArray();
                    var headNode = temp[0];
                    var weight   = temp[1];

                    if (!nodes.ContainsKey(headNode))
                    {
                        nodes[headNode] = new NodeWeighted(headNode);
                    }

                    var neighbourToAdd = new Tuple <NodeWeighted, int>(nodes[headNode], weight);
                    nodes[tailNode].Neighbours.Add(neighbourToAdd);
                    var parentToAdd = new Tuple <NodeWeighted, int>(nodes[tailNode], weight);
                    nodes[headNode].Parents.Add(parentToAdd);
                }
            }

            return(nodes);
        }
Пример #4
0
        private static Dictionary <int, NodeWeighted> ParseMstGraphFromWeb()
        {
            const string link =
                @"https://lagunita.stanford.edu/assets/courseware/v1/1f93e6cee93cbcf26ee59e2f801646cd/asset-v1:Engineering+Algorithms2+SelfPaced+type@asset+block/edges.txt";

            var graphStringsArray = UtilityMethods.GetParsedStringArrayFromWeb(link, '\n');
            var nodeCount         = int.Parse(graphStringsArray[0].Split(' ')[0]);
            var edgeCount         = int.Parse(graphStringsArray[0].Split(' ')[1]);

            var graph = new Dictionary <int, NodeWeighted>(nodeCount);

            for (int i = 1; i <= edgeCount; i++)
            {
                var currentEdge = graphStringsArray[i].Split(' ').Select(int.Parse).ToArray();
                var firstNode   = currentEdge[0];
                var secondNode  = currentEdge[1];
                var weight      = currentEdge[2];

                if (!graph.ContainsKey(firstNode))
                {
                    graph[firstNode] = new NodeWeighted(firstNode);
                }

                if (!graph.ContainsKey(secondNode))
                {
                    graph[secondNode] = new NodeWeighted(secondNode);
                }

                var first  = graph[firstNode];
                var second = graph[secondNode];

                first.Neighbours.Add(new Tuple <NodeWeighted, int>(second, weight));
                second.Neighbours.Add(new Tuple <NodeWeighted, int>(first, weight));
            }

            return(graph);
        }