Example #1
0
 public HeapNode(Vertix vertix)
 {
     if (vertix.Number == 1)
     {
         ShortestDistance = 0;
     }
     else
     {
         ShortestDistance = int.MaxValue;
     }
     MyVertix = vertix;
 }
Example #2
0
        static Dictionary <int, int> GetShortestPaths(Vertix[] vertices)
        {
            Vertix[] extractedVertices = new Vertix[vertices.Length];
            int[]    pathes            = new int[vertices.Length];
            extractedVertices[1] = vertices[1];
            vertices[1].Number   = 0;
            pathes[1]            = 0;
            int count = vertices.Length - 1;

            while (count > 0)
            {
                int shortestPath    = int.MaxValue;
                int vertixToExtract = 0;
                for (int i = 1; i < extractedVertices.Length; i++)
                {
                    if (extractedVertices[i].Number != 0)
                    {
                        for (int j = 0; j < extractedVertices[i].Edges.Length; j++)
                        {
                            int edge = extractedVertices[i].Edges[j];
                            if (vertices[edge].Number != 0)
                            {
                                int tempPath = GetShortestPath(vertices[edge], extractedVertices, pathes);
                                if (tempPath < shortestPath)
                                {
                                    shortestPath    = tempPath;
                                    vertixToExtract = edge;
                                }
                            }
                        }
                    }
                }
                extractedVertices[vertixToExtract] = vertices[vertixToExtract];
                pathes[vertixToExtract]            = shortestPath;
                vertices[vertixToExtract].Number   = 0;

                count--;
            }

            Dictionary <int, int> result = new Dictionary <int, int>(vertices.Length);

            for (int i = 0; i < extractedVertices.Length; i++)
            {
                result.Add(i, pathes[i]);
            }
            return(result);
        }
Example #3
0
        static int GetShortestPath(Vertix vertix, Vertix[] extractedVertices, int[] pathes)
        {
            int shortestPath = int.MaxValue;

            for (int i = 1; i < extractedVertices.Length; i++)
            {
                int temp = 0;
                if (extractedVertices[i].Number != 0)
                {
                    temp = extractedVertices[i].GetDistance(vertix.Number);
                    if (temp != -1)
                    {
                        int newPath = pathes[i] + temp;
                        if (newPath <= shortestPath)
                        {
                            shortestPath = newPath;
                        }
                    }
                }
            }

            return(shortestPath);
        }