C# Coding Test Course, Finding the Fastest Bus Route

In modern society, public transportation plays an important role, and especially when using buses, many people struggle to find the optimal routes. In this lecture, we will look at the process of solving the problem of finding the fastest bus route. To solve this problem, we will use C# to employ algorithms and data structures.

Problem Definition

The problem is to find the fastest path from a starting point to a destination based on several given bus routes and the time required for each route.
The input consists of the following values:

  • Vertex: Bus stops
  • Edge: Bus routes
  • Weight: Time required

Example Input

        5 6
        1 2 3
        1 3 2
        2 3 1
        1 4 1
        2 4 5
        3 5 1
        

The first line indicates the number of vertices and edges, and each subsequent line shows the relationship between stops and the time required.

Problem Solving Process

Step 1: Graph Modeling

To solve the above problem, we need to construct a graph where the bus stops are vertices and the bus routes and times are edges.
To do this, we will create an adjacency list that contains information about each stop and its edges.

Step 2: Selecting the Shortest Path Algorithm

There are several algorithms to find the shortest path, but when there are weighted edges, it is appropriate to use Dijkstra’s Algorithm.
Dijkstra’s Algorithm is an effective way to calculate the shortest distance to each vertex.

Step 3: Implementing C# Code

Now, let’s write C# code based on the above content. The code below demonstrates how to implement Dijkstra’s Algorithm to find the shortest path in the given graph.

        using System;
        using System.Collections.Generic;

        class Program
        {
            static void Main(string[] args)
            {
                int vertexCount = 5; // Number of vertices
                int[,] graph = new int[vertexCount + 1, vertexCount + 1];
                for (int i = 1; i <= vertexCount; i++)
                    for (int j = 1; j <= vertexCount; j++)
                        graph[i, j] = (i == j) ? 0 : int.MaxValue;

                // Add edges (time required)
                graph[1, 2] = 3;
                graph[1, 3] = 2;
                graph[2, 3] = 1;
                graph[1, 4] = 1;
                graph[2, 4] = 5;
                graph[3, 5] = 1;

                // Call Dijkstra's Algorithm
                int[] shortestPath = Dijkstra(graph, vertexCount, 1);

                // Output results
                Console.WriteLine("Shortest distance from vertex 1 to other vertices:");
                for (int i = 1; i <= vertexCount; i++)
                {
                    Console.WriteLine($"Vertex 1 -> Vertex {i}: {(shortestPath[i] == int.MaxValue ? "Unreachable" : shortestPath[i].ToString())}");
                }
            }

            static int[] Dijkstra(int[,] graph, int vertexCount, int start)
            {
                bool[] visited = new bool[vertexCount + 1];
                int[] distance = new int[vertexCount + 1];

                for (int i = 1; i <= vertexCount; i++)
                {
                    distance[i] = (i == start) ? 0 : int.MaxValue;
                }

                for (int count = 0; count < vertexCount - 1; count++)
                {
                    int u = MinDistance(distance, visited, vertexCount);
                    visited[u] = true;

                    for (int v = 1; v <= vertexCount; v++)
                    {
                        if (!visited[v] && graph[u, v] != int.MaxValue &&
                            distance[u] != int.MaxValue &&
                            distance[u] + graph[u, v] < distance[v])
                        {
                            distance[v] = distance[u] + graph[u, v];
                        }
                    }
                }

                return distance;
            }

            static int MinDistance(int[] distance, bool[] visited, int vertexCount)
            {
                int min = int.MaxValue;
                int minIndex = -1;

                for (int v = 1; v <= vertexCount; v++)
                {
                    if (!visited[v] && distance[v] <= min)
                    {
                        min = distance[v];
                        minIndex = v;
                    }
                }
                return minIndex;
            }
        }
        

Step 4: Time Complexity Analysis

The time complexity of Dijkstra’s Algorithm is O(V^2), where V represents the number of vertices.
Therefore, when there are many vertices, you may consider a more efficient method, which is the improved Dijkstra’s Algorithm using a priority queue.

Conclusion

In this lecture, we implemented Dijkstra’s Algorithm to solve the problem of finding the fastest bus route using C#.
Similar problems may arise in actual coding tests or job interviews, so it is important to understand and practice this algorithm thoroughly.

I hope you continue to study various algorithm problems and solutions in the future. If you have any additional questions or algorithm-related inquiries, feel free to ask!