GIS算法——C#迪杰斯特拉算法

本文详细介绍了如何使用C#编程语言实现GIS领域的迪杰斯特拉算法,探讨了该算法在地理信息系统中的应用,以及其在解决最短路径问题上的关键步骤和优化技巧。
摘要由CSDN通过智能技术生成
using System;
using System.Collections.Generic;
using System.IO;

class DijkstraAlgorithm
{
    private int numVertices;
    private int[,] graph;
    private int[] distance;
    private bool[] shortestPathTreeSet;

    public DijkstraAlgorithm(int numVertices)
    {
        this.numVertices = numVertices;
        graph = new int[numVertices, numVertices];
        distance = new int[numVertices];
        shortestPathTreeSet = new bool[numVertices];
    }

    public void ReadGraphFromFile(string filePath)
    {
        string[] lines = File.ReadAllLines(filePath);

        foreach (string line in lines)
        {
            string[] values = line.Split(' ');
            int source = int.Parse(values[0]);
            int destination = int.Parse(values[1]);
            int weight = int.Parse(values[2]);

            graph[source, destination] = weight;
        }
    }

    public void FindShortestPath(int source)
    {
        for (int i = 0; i < numVertices; i++)
        {
            distance[i] = int.MaxValue;
            shortestPathTreeSet[i] = false;
        }

        distance[source] = 0;

        for (int count = 0; count < numVertices - 1; count++)
        {
            int u = MinimumDistance(distance, shortestPathTreeSet);
            shortestPathTreeSet[u] = true;

            for (int v = 0; v < numVertices; v++)
            {
                if (!shortestPathTreeSet[v] && graph[u, v] != 0 && distance[u] != int.MaxValue &&
                    distance[u] + graph[u, v] < distance[v])
                {
                    distance[v] = distance[u] + graph[u, v];
                }
            }
        }

        PrintSolution(distance, source);
    }

    private int MinimumDistance(int[] distance, bool[] shortestPathTreeSet)
    {
        int min = int.MaxValue, minIndex = -1;

        for (int v = 0; v < numVertices; v++)
        {
            if (!shortestPathTreeSet[v] && distance[v] <= min)
            {
                min = distance[v];
                minIndex = v;
            }
        }

        return minIndex;
    }

    private void PrintSolution(int[] distance, int source)
    {
        Console.WriteLine("Shortest Path from {0}:", source);
        Console.WriteLine("Vertex \t Distance \t Path");

        for (int v = 0; v < numVertices; v++)
        {
            if (distance[v] == int.MaxValue)
            {
                Console.WriteLine(v + " \t\t NULL \t\t NULL");
            }
            else
            {
                Console.WriteLine(v + " \t\t " + distance[v] + " \t\t " + GetPath(v));
            }
        }
    }

    private string GetPath(int vertex)
    {
        string path = vertex.ToString();
        int currentVertex = vertex;

        while (currentVertex != 0)
        {
            for (int v = 0; v < numVertices; v++)
            {
                if (graph[v, currentVertex] != 0 && distance[v] + graph[v, currentVertex] == distance[currentVertex])
                {
                    path = v + " -> " + path;
                    currentVertex = v;
                    break;
                }
            }
        }

        return path;
    }
}

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter the number of vertices:");
        int numVertices = int.Parse(Console.ReadLine());

        DijkstraAlgorithm dijkstra = new DijkstraAlgorithm(numVertices);

        Console.WriteLine("Enter the file path of the graph data:");
        string filePath = Console.ReadLine();

        dijkstra.ReadGraphFromFile(filePath);

        Console.WriteLine("Enter the source vertex:");
        int source = int.Parse(Console.ReadLine());

        dijkstra.FindShortestPath(source);
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

EO_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值