Skip to content

Commit 4a95d1a

Browse files
authored
Add Dijkstra in C# (#4875)
1 parent 05d38f9 commit 4a95d1a

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

archive/c/c-sharp/Dijkstra.cs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
public static class Program
6+
{
7+
private const int INF = 0x3F3F3F3F;
8+
9+
private static void ShowUsage()
10+
{
11+
Console.Error.WriteLine("Usage: please provide three inputs: a serialized matrix, a source node and a destination node");
12+
Environment.Exit(1);
13+
}
14+
15+
private static List<int> ParseIntegerList(string input)
16+
{
17+
if (string.IsNullOrWhiteSpace(input))
18+
ShowUsage();
19+
20+
var list = input
21+
.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
22+
.Select(s =>
23+
{
24+
if (!int.TryParse(s, out var val) || val < 0)
25+
ShowUsage();
26+
return val;
27+
})
28+
.ToList();
29+
30+
if (list.Count == 0)
31+
ShowUsage();
32+
33+
return list;
34+
}
35+
36+
private static int GetMatrixDimension(List<int> matrix)
37+
{
38+
var length = matrix.Count;
39+
var dimension = (int)Math.Sqrt(length);
40+
return (dimension * dimension == length && dimension > 0) ? dimension : -1;
41+
}
42+
43+
private static int Dijkstra(List<int> matrix, int dimension, int source, int destination)
44+
{
45+
var dist = Enumerable.Repeat(INF, dimension).ToArray();
46+
var visited = new bool[dimension];
47+
48+
dist[source] = 0;
49+
50+
for (int _ = 0; _ < dimension; _++)
51+
{
52+
int minDist = INF;
53+
int minIndex = -1;
54+
55+
for (int j = 0; j < dimension; j++)
56+
{
57+
if (!visited[j] && dist[j] < minDist)
58+
{
59+
minDist = dist[j];
60+
minIndex = j;
61+
}
62+
}
63+
64+
if (minIndex == -1)
65+
break;
66+
67+
if (minIndex == destination)
68+
return dist[minIndex];
69+
70+
visited[minIndex] = true;
71+
72+
for (int j = 0; j < dimension; j++)
73+
{
74+
int weight = matrix[minIndex * dimension + j];
75+
if (!visited[j] && weight > 0 && dist[minIndex] + weight < dist[j])
76+
{
77+
dist[j] = dist[minIndex] + weight;
78+
}
79+
}
80+
}
81+
82+
return dist[destination] == INF ? -1 : dist[destination];
83+
}
84+
85+
public static int Main(string[] args)
86+
{
87+
if (args.Length != 3)
88+
{
89+
ShowUsage();
90+
}
91+
92+
var matrixStr = args[0].Trim();
93+
var sourceStr = args[1].Trim();
94+
var destinationStr = args[2].Trim();
95+
96+
if (string.IsNullOrEmpty(matrixStr) || string.IsNullOrEmpty(sourceStr) || string.IsNullOrEmpty(destinationStr))
97+
ShowUsage();
98+
99+
var matrix = ParseIntegerList(matrixStr);
100+
int dimension = GetMatrixDimension(matrix);
101+
102+
103+
bool sourceParsed = int.TryParse(sourceStr, out int source);
104+
bool destinationParsed = int.TryParse(destinationStr, out int destination);
105+
106+
if (dimension == -1 || !sourceParsed || !destinationParsed ||
107+
source < 0 || source >= dimension ||
108+
destination < 0 || destination >= dimension)
109+
{
110+
ShowUsage();
111+
}
112+
113+
int shortestDistance = Dijkstra(matrix, dimension, source, destination);
114+
115+
if (shortestDistance == -1)
116+
ShowUsage();
117+
118+
Console.WriteLine(shortestDistance);
119+
return 0;
120+
}
121+
}

0 commit comments

Comments
 (0)