shortest path c# in csharp

Here's an example of how to find the shortest path between nodes in a graph using Dijkstra's algorithm in C#:

main.cs
using System;
using System.Collections.Generic;

class Graph
{
    private int V;                     
    private List<Tuple<int, int>>[] adj; 

    public Graph(int V)
    {
        this.V = V;
        adj = new List<Tuple<int, int>>[V];
        for (int i = 0; i < V; i++)
            adj[i] = new List<Tuple<int, int>>();
    }

    public void AddEdge(int u, int v, int w)
    {
        adj[u].Add(Tuple.Create(v, w));
        adj[v].Add(Tuple.Create(u, w));
    }

    public int[] ShortestPath(int s)
    {
        var dist = new int[V];
        for (int i = 0; i < V; i++)
            dist[i] = int.MaxValue;
        dist[s] = 0;

        var pq = new PriorityQueue<Tuple<int, int>>(V);
        pq.Enqueue(Tuple.Create(s, 0));

        while (pq.Count > 0)
        {
            var u = pq.Dequeue();
            if (u.Item2 > dist[u.Item1])
                continue;

            foreach (var v in adj[u.Item1])
            {
                var alt = dist[u.Item1] + v.Item2;
                if (alt < dist[v.Item1])
                {
                    dist[v.Item1] = alt;
                    pq.Enqueue(Tuple.Create(v.Item1, alt));
                }
            }
        }

        return dist;
    }
}

class PriorityQueue<T> where T : IComparable<T>
{
    private List<T> data;

    public PriorityQueue()
    {
        this.data = new List<T>();
    }

    public PriorityQueue(int capacity)
    {
        this.data = new List<T>(capacity);
    }

    public void Enqueue(T item)
    {
        data.Add(item);
        int ci = data.Count - 1;

        while (ci > 0)
        {
            int pi = (ci - 1) / 2;
            if (data[ci].CompareTo(data[pi]) >= 0)
                break;
            T tmp = data[ci]; data[ci] = data[pi]; data[pi] = tmp;
            ci = pi;
        }
    }

    public T Dequeue()
    {
        int li = data.Count - 1;
        T frontItem = data[0];
        data[0] = data[li];
        data.RemoveAt(li);

        --li;
        int pi = 0;
        while (true)
        {
            int ci = pi * 2 + 1;
            if (ci > li)
                break;
            int rc = ci + 1;
            if (rc <= li && data[rc].CompareTo(data[ci]) < 0)
                ci = rc;
            if (data[pi].CompareTo(data[ci]) <= 0)
                break;
            T tmp = data[pi]; data[pi] = data[ci]; data[ci] = tmp;
            pi = ci;
        }
        return frontItem;
    }

    public T Peek()
    {
        T frontItem = data[0];
        return frontItem;
    }

    public int Count()
    {
        return data.Count;
    }
}

class Program
{
    static void Main(string[] args)
    {
        var g = new Graph(5);
        g.AddEdge(0, 1, 4);
        g.AddEdge(0, 2, 1);
        g.AddEdge(2, 1, 2);
        g.AddEdge(1, 3, 1);
        g.AddEdge(2, 3, 5);
        g.AddEdge(3, 4, 3);

        int[] dist = g.ShortestPath(0);
        for (int i = 0; i < dist.Length; i++)
            Console.WriteLine("Shortest distance from 0 to " + i + " is " + dist[i]);

        Console.ReadLine();
    }
}
3046 chars
139 lines

This code defines a Graph class that holds the graph and a ShortestPath method that returns the shortest distances from a given node to all other nodes in the graph using Dijkstra's algorithm. The PriorityQueue class is used to efficiently select the node with the smallest tentative distance in each iteration of the algorithm.

gistlibby LogSnag