Route Between Nodes

This problem asks for the shortest route between two nodes in a graph. The input is a list of nodes and their connections. The output should be the shortest route between the two given nodes.

Problem

Given a directed graph, design an algorithm to find out whether there is a route between two nodes.
Example input:
A -> B
A -> C
B -> D
C -> D
Example output:
Yes

Solution

This solution is in C#. Our A.I. can create solutions in multiple languages.
by kayakane
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterviewQuestions.Ch1
{
    public static class Q3RouteBetweenNodes
    {
        public static bool IsRouteBetweenNodes(GraphNode start, GraphNode end)
        {
            if (start == null || end == null)
                return false;

            if (start == end)
                return true;

            Queue<GraphNode> queue = new Queue<GraphNode>();
            queue.Enqueue(start);

            while (queue.Count > 0)
            {
                GraphNode node = queue.Dequeue();
                node.Visited = true;

                foreach (GraphNode child in node.Children)
                {
                    if (child == end)
                        return true;

                    if (!child.Visited)
                        queue.Enqueue(child);
                }
            }

            return false;
        }
    }
}

A.I. Evaluation of the Solution

The candidate's solution correctly implements a breadth-first search to find a route between two nodes in a graph. The solution is complete and correctly handles edge cases such as null input values.

Evaluated at: 2022-12-07 08:15:47