예제 #1
0
        public static string BFS(int nS)
        {
            Coada c = new Coada();

            dist = new int[n];
            bool[] visited = new bool[n];
            for (int i = 0; i < n; i++)
            {
                visited[i] = false;
                dist[i]    = int.MaxValue;
            }

            visited[nS] = true;
            c.Adaugare(nS);
            dist[nS] = 0;

            while (c.n != 0)
            {
                int t = c.Stergere();
                for (int i = 0; i < n; i++)
                {
                    if (Grafica.matrix[t, i] != 0)
                    {
                        if (!visited[i])
                        {
                            c.Adaugare(i);
                            visited[i] = true;
                        }

                        if (dist[t] + Grafica.matrix[t, i] < dist[i])
                        {
                            dist[i] = dist[t] + Grafica.matrix[t, i];
                        }

                        //graful este neorientat, deci trebuie si verificarea inversa
                        if (dist[i] + Grafica.matrix[t, i] < dist[t])
                        {
                            dist[t] = dist[i] + Grafica.matrix[t, i];
                        }
                    }
                }
            }

            string result = "";

            for (int i = 0; i < n; i++)
            {
                result += dist[i] + "  ";
            }
            return(result);
        }
예제 #2
0
        static void BFS(int nS)
        {
            Coada c = new Coada();

            bool[] visited = new bool[n];
            for (int i = 0; i < n; i++)
            {
                visited[i] = false;
            }

            visited[nS] = true;
            c.Adaugare(nS);
            dist[nS] = 0;

            while (c.n != 0)
            {
                int t = c.Stergere();
                for (int i = 0; i < n; i++)
                {
                    if (ma[t, i] != 0)
                    {
                        if (!visited[i])
                        {
                            c.Adaugare(i);
                            visited[i] = true;
                        }

                        if (dist[t] + ma[t, i] < dist[i])
                        {
                            dist[i] = dist[t] + ma[t, i];
                        }

                        //graful este neorientat, deci trebuie si verificarea inversa
                        if (dist[i] + ma[t, i] < dist[t])
                        {
                            dist[t] = dist[i] + ma[t, i];
                        }
                    }
                }
            }
        }