コード例 #1
0
ファイル: Distance.cs プロジェクト: emayuri/Algorithms
        /// <summary>
        /// Function to build graph of edges between the zipcodes.
        /// </summary>
        /// <param name="zipCode1"></param>
        /// <param name="zipCode2"></param>
        /// <param name="distance">distance between zipcode1 and zipcode2</param>
        private void BuildGraphEdges(string zipCode1, string zipCode2, int distance)
        {
            ZipNodes zipNode1 = new ZipNodes(zipCode2, distance);
            ZipNodes zipNode2 = new ZipNodes(zipCode1, distance);

            if (!graph[zipCode1].Contains(zipNode1))
            {
                graph[zipCode1].Add(zipNode1);
            }
            if (!graph[zipCode2].Contains(zipNode2))
            {
                graph[zipCode2].Add(zipNode2);
            }
        }
コード例 #2
0
        /// <summary>
        /// Function that fetches the shortest path between the source and destination
        /// </summary>
        /// <param name="source"></param>
        /// <param name="destination"></param>
        private int ShortestPath(string source, string destination)
        {
            int shortestDistance = int.MaxValue;

            vertices = graph.Count;

            ZipNodes zipCodeObj;

            //To store finalised distances
            Dictionary <string, int> distance = new Dictionary <string, int>();

            //Sorted dictionary used in the form of a min heap
            SortedSet <ZipNodes> sortedSet = new SortedSet <ZipNodes>(new CustomComparer());

            Dictionary <string, ZipNodes> trackingHashMap = new Dictionary <string, ZipNodes>();

            zipCodeObj = new ZipNodes(source, 0);

            trackingHashMap.Add(source, zipCodeObj);
            sortedSet.Add(zipCodeObj);

            while (distance.Count <= vertices)
            {
                string currentZip      = sortedSet.ElementAt(0).zipCode;
                int    currentDistance = sortedSet.ElementAt(0).distance;
                distance.Add(currentZip, currentDistance);

                if (currentZip == destination)
                {
                    if (result.ContainsKey(source) && result[source] > distance[destination])
                    {
                        //result[source] = distance[destination];
                        return(distance[destination]);
                    }
                }

                sortedSet.Remove(trackingHashMap[currentZip]);

                for (int i = 0; i < graph[currentZip].Count; i++)
                {
                    if (!distance.ContainsKey(graph[currentZip][i].zipCode))
                    {
                        if (!trackingHashMap.ContainsKey(graph[currentZip][i].zipCode))
                        {
                            ZipNodes newZipCodeObj = new ZipNodes(graph[currentZip][i].zipCode, graph[currentZip][i].distance + currentDistance);
                            sortedSet.Add(newZipCodeObj);
                            trackingHashMap.Add(graph[currentZip][i].zipCode, newZipCodeObj);
                        }
                        else
                        {
                            if (trackingHashMap[graph[currentZip][i].zipCode].distance > graph[currentZip][i].distance + currentDistance)
                            {
                                sortedSet.Remove(trackingHashMap[graph[currentZip][i].zipCode]);
                                trackingHashMap[graph[currentZip][i].zipCode].distance = graph[currentZip][i].distance + currentDistance;
                                sortedSet.Add(trackingHashMap[graph[currentZip][i].zipCode]);
                            }
                        }
                    }
                }
            }
            return(shortestDistance);
        }