Esempio n. 1
0
        static void BreadthFirstSearch(int vertices, List <int>[] lists)
        {
            int rootNode    = 1;
            int currentNode = rootNode;

            bool[] visitedNodes = new bool[vertices + 1];
            SinglyLinkedListQueue <int> queue = new SinglyLinkedListQueue <int>();

            queue.Push(rootNode);
            while (queue.IsEmpty() == false)
            {
                currentNode = queue.Pop();
                if (visitedNodes[currentNode] == false)
                {
                    visitedNodes[currentNode] = true;
                    Console.WriteLine("Odwiedzono wierzchołek " + currentNode);
                    for (int i = 0; i < lists[currentNode].Count; i++)
                    {
                        if (visitedNodes[lists[currentNode][i]] == false)
                        {
                            queue.Push(lists[currentNode][i]);
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        static void ShortestPathInGraphWithoutWeight(int vertices, List <int>[] lists, int rootNode)
        {
            int currentNode = rootNode;

            bool[] visitedNodes = new bool[vertices + 1];
            int[]  distances    = new int[vertices + 1];
            int[]  previous     = new int[vertices + 1];
            for (int i = 1; i < vertices + 1; i++)
            {
                distances[i] = -1;
            }
            SinglyLinkedListQueue <int> queue = new SinglyLinkedListQueue <int>();

            queue.Push(rootNode);
            previous[rootNode] = rootNode;
            while (queue.IsEmpty() == false)
            {
                currentNode = queue.Pop();
                if (visitedNodes[currentNode] == false)
                {
                    visitedNodes[currentNode] = true;
                    distances[currentNode]    = distances[previous[currentNode]] + 1;
                    for (int i = 0; i < lists[currentNode].Count; i++)
                    {
                        if (previous[lists[currentNode][i]] == 0)
                        {
                            queue.Push(lists[currentNode][i]);
                            previous[lists[currentNode][i]] = currentNode;
                        }
                    }
                }
            }
            for (int i = 1; i < vertices + 1; i++)
            {
                Console.Write(distances[i] + " ");
            }
            Console.WriteLine();
            for (int i = 1; i < vertices + 1; i++)
            {
                Console.Write(previous[i] + " ");
            }
        }