private void GetShortestPath()
        {
            PriorityQueue <V> minQueue = new PriorityQueue <V>(Graph.Vertices());

            IntializeDistances();
            Vertix.Distance = 0;

            while (minQueue.IsNull() == false)
            {
                Vertix <V>       C           = minQueue.MinimumDistance();
                List <Node <V> > adjVertices = Graph.Adj(C);

                foreach (Node <V> node in adjVertices)
                {
                    int x = Vertix.Distance + node.Weight;
                    if (x < node.Vertix.Distance)
                    {
                        node.Vertix.Distance = x;
                        node.Vertix.Previous = Vertix;
                    }
                }

                minQueue.RemoveVertex(C);
                C.Visited = true;
            }
        }
Beispiel #2
0
 public Vertix(V name)
 {
     Name     = name;
     Visited  = false;
     Distance = 0;
     Previous = null;
 }
        static void Main(string[] args)
        {
            Dictionary <Vertix <String>, List <Node <String> > > graph = new Dictionary <Vertix <String>, List <Node <String> > >();

            Vertix <String> v1 = new Vertix <String>("A");
            Vertix <String> v2 = new Vertix <String>("B");
            Vertix <String> v3 = new Vertix <String>("C");
            Vertix <String> v4 = new Vertix <String>("D");
            Vertix <String> v5 = new Vertix <String>("E");

            //adjacent nodes of v1
            List <Node <String> > arr1 = new List <Node <String> >();

            arr1.Add(new Node <String>(v2, 6));
            //arr1.Add(new Node<String>(v3, 13));
            arr1.Add(new Node <String>(v4, 1));
            //arr1.Add(new Node<String>(v5, 20));
            graph.Add(v1, arr1);

            List <Node <String> > arr2 = new List <Node <String> >();

            arr2.Add(new Node <String>(v1, 6));
            arr2.Add(new Node <String>(v3, 5));
            arr2.Add(new Node <String>(v4, 2));
            arr2.Add(new Node <String>(v5, 2));
            graph.Add(v2, arr2);

            List <Node <String> > arr3 = new List <Node <String> >();

            //arr3.Add(new Node<String>(v1, 13));
            arr3.Add(new Node <String>(v2, 5));
            //arr3.Add(new Node<String>(v4, 7));
            arr3.Add(new Node <String>(v5, 5));
            graph.Add(v3, arr3);

            //
            List <Node <String> > arr4 = new List <Node <String> >();

            arr4.Add(new Node <String>(v1, 1));
            arr4.Add(new Node <String>(v2, 2));
            //arr4.Add(new Node<String>(v3, 7));
            arr4.Add(new Node <String>(v5, 1));
            graph.Add(v4, arr4);

            List <Node <String> > arr5 = new List <Node <String> >();

            //arr5.Add(new Node<String>(v1, 20));
            arr5.Add(new Node <String>(v2, 2));
            arr5.Add(new Node <String>(v3, 5));
            arr5.Add(new Node <String>(v4, 1));
            graph.Add(v5, arr5);

            Graph <String> Graph = new Graph <String>(graph);

            DijkstraAlgorithm <String> dijs = new DijkstraAlgorithm <String>(Graph, v1);

            dijs.PrintShortestPath();
        }
Beispiel #4
0
        public void AddNode(Vertix <V> vertex, Node <V> node)
        {
            List <Node <V> > nodes = Adj(vertex);

            if (nodes == null || (nodes.Count < 1))
            {
                nodes = new List <Node <V> >();
                nodes.Add(node);
            }
            else
            {
                nodes.Add(node);
            }

            adjList[vertex] = nodes;
        }
Beispiel #5
0
        }                          // weight of the edge connecting them

        public Node(Vertix <V> vertix, int weight)
        {
            Vertix = vertix;
            Weight = weight;
        }
Beispiel #6
0
 public void AddVertex(Vertix <V> vertix)
 {
     vertices.Add(vertix);
 }
Beispiel #7
0
 public void RemoveVertex(Vertix <V> vertix)
 {
     vertices.Remove(vertix);
 }
 public DijkstraAlgorithm(Graph <V> G, Vertix <V> vertix)
 {
     Graph  = G;
     Vertix = vertix;
     GetShortestPath();
 }
Beispiel #9
0
        public List <Node <V> > Adj(Vertix <V> vert)
        {
            List <Node <V> > values = adjList[vert];

            return(values);
        }