Exemple #1
0
        public void GetShortestPath(int source)
        {
            vertex[] distance = new vertex[adjacencymatrix.GetLength(0)];
            for (int i = 0; i < distance.Length; i++)
            {
                distance[i] = new vertex(i, Int32.MaxValue);
            }
            distance[source].weight = 0;
            PriorityQueue pq1 = new PriorityQueue();

            foreach (vertex x in distance)
            {
                pq1.Enqueue(x);
            }
            while (pq1.count != 0)
            {
                vertex x          = pq1.extractmin();
                int[]  neighbours = GetNeighbour(x.id);
                foreach (int i in neighbours)
                {
                    if (distance[i].weight > adjacencymatrix[x.id, i] + x.weight)
                    {
                        distance[i].weight = adjacencymatrix[x.id, i] + x.weight;
                        distance[i].parent = x.id;
                    }
                }
            }
            foreach (vertex v in distance)
            {
                Console.WriteLine(v.parent + ">>> " + v.id + "== " + v.weight);
            }
        }
Exemple #2
0
        public void BellemenFord(int source)
        {
            vertex[] distance = new vertex[adjacencymatrix.GetLength(0)];
            for (int i = 0; i < distance.Length; i++)
            {
                distance[i] = new vertex(i, int.MaxValue);
            }
            distance[source].weight = 0;
            for (int i = 0; i < distance.Length - 1; i++)
            {
                for (int j = 0; j < distance.Length; j++)
                {
                    int[] neighbours = GetNeighbour(j);
                    foreach (int x in neighbours)
                    {
                        if (distance[x].weight > distance[j].weight + adjacencymatrix[j, x])
                        {
                            distance[x].weight = distance[j].weight + adjacencymatrix[j, x];
                            distance[x].parent = j;
                        }
                    }
                }
            }
            bool check = false;

            for (int i = 0; i < distance.Length; i++)
            {
                int[] neighbours = GetNeighbour(i);
                foreach (int x in neighbours)
                {
                    if (distance[x].weight > distance[i].weight + adjacencymatrix[i, x])
                    {
                        check = true;
                    }
                }
            }
            if (check)
            {
                Console.WriteLine("Solution not possible");
            }
            else
            {
                foreach (vertex x in distance)
                {
                    Console.WriteLine(x.parent + ">>>> " + x.id + "== " + x.weight);
                }
            }
        }
Exemple #3
0
        public vertex extractmin()
        {
            if (l1.Count == 0)
            {
                throw new Exception("Queue is empty");
            }
            vertex[] v   = l1.ToArray <vertex>();
            vertex   min = v[0];

            foreach (vertex x in v)
            {
                if (min.weight > x.weight)
                {
                    min = x;
                }
            }
            l1.Remove(min);
            return(min);
        }
Exemple #4
0
 public void Enqueue(vertex data)
 {
     l1.AddLast(data);
 }