add csharp examples (#10)
This commit is contained in:
committed by
Aditya Bhargava
parent
c3396b8b0a
commit
62ed616954
2
07_dijkstras_algorithm/csharp/01_dijkstras_algorithm/.gitignore
vendored
Normal file
2
07_dijkstras_algorithm/csharp/01_dijkstras_algorithm/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
**/bin/*
|
||||
**/obj/*
|
||||
23
07_dijkstras_algorithm/csharp/01_dijkstras_algorithm/.vscode/launch.json
vendored
Normal file
23
07_dijkstras_algorithm/csharp/01_dijkstras_algorithm/.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": ".NET Core Launch (console)",
|
||||
"type": "coreclr",
|
||||
"request": "launch",
|
||||
"preLaunchTask": "build",
|
||||
"program": "${workspaceRoot}/bin/Debug/netcoreapp1.0/01_dijkstras_algorithm.dll",
|
||||
"args": [],
|
||||
"cwd": "${workspaceRoot}",
|
||||
"externalConsole": false,
|
||||
"stopAtEntry": false,
|
||||
"internalConsoleOptions": "openOnSessionStart"
|
||||
},
|
||||
{
|
||||
"name": ".NET Core Attach",
|
||||
"type": "coreclr",
|
||||
"request": "attach",
|
||||
"processId": "${command.pickProcess}"
|
||||
}
|
||||
]
|
||||
}
|
||||
16
07_dijkstras_algorithm/csharp/01_dijkstras_algorithm/.vscode/tasks.json
vendored
Normal file
16
07_dijkstras_algorithm/csharp/01_dijkstras_algorithm/.vscode/tasks.json
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"version": "0.1.0",
|
||||
"command": "dotnet",
|
||||
"isShellCommand": true,
|
||||
"args": [],
|
||||
"tasks": [
|
||||
{
|
||||
"taskName": "build",
|
||||
"args": [
|
||||
"${workspaceRoot}/project.json"
|
||||
],
|
||||
"isBuildCommand": true,
|
||||
"problemMatcher": "$msCompile"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ConsoleApplication
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
private const double _infinity = double.PositiveInfinity;
|
||||
private static Dictionary<string, Dictionary<string, double>> _graph = new Dictionary<string, Dictionary<string, double>>();
|
||||
private static List<string> _processed = new List<string>();
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
_graph.Add("start", new Dictionary<string, double>());
|
||||
_graph["start"].Add("a", 6.0);
|
||||
_graph["start"].Add("b", 2.0);
|
||||
|
||||
_graph.Add("a", new Dictionary<string, double>());
|
||||
_graph["a"].Add("fin", 1.0);
|
||||
|
||||
_graph.Add("b", new Dictionary<string, double>());
|
||||
_graph["b"].Add("a", 3.0);
|
||||
_graph["b"].Add("fin", 5.0);
|
||||
|
||||
_graph.Add("fin", new Dictionary<string, double>());
|
||||
|
||||
var costs = new Dictionary<string, double>();
|
||||
costs.Add("a", 6.0);
|
||||
costs.Add("b", 2.0);
|
||||
costs.Add("fin", _infinity);
|
||||
|
||||
var parents = new Dictionary<string, string>();
|
||||
parents.Add("a", "start");
|
||||
parents.Add("b", "start");
|
||||
parents.Add("fin", null);
|
||||
|
||||
var node = FindLowestCostNode(costs);
|
||||
while (node != null)
|
||||
{
|
||||
var cost = costs[node];
|
||||
var neighbors = _graph[node];
|
||||
foreach (var n in neighbors.Keys)
|
||||
{
|
||||
var new_cost = cost + neighbors[n];
|
||||
if (costs[n] > new_cost)
|
||||
{
|
||||
costs[n] = new_cost;
|
||||
parents[n] = node;
|
||||
}
|
||||
}
|
||||
_processed.Add(node);
|
||||
node = FindLowestCostNode(costs);
|
||||
}
|
||||
Console.WriteLine(string.Join(", ", costs));
|
||||
}
|
||||
|
||||
private static string FindLowestCostNode(Dictionary<string, double> costs)
|
||||
{
|
||||
var lowestCost = double.PositiveInfinity;
|
||||
string lowestCostNode = null;
|
||||
foreach (var node in costs)
|
||||
{
|
||||
var cost = node.Value;
|
||||
if (cost < lowestCost && !_processed.Contains(node.Key))
|
||||
{
|
||||
lowestCost = cost;
|
||||
lowestCostNode = node.Key;
|
||||
}
|
||||
}
|
||||
return lowestCostNode;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"version": "1.0.0-*",
|
||||
"buildOptions": {
|
||||
"debugType": "portable",
|
||||
"emitEntryPoint": true
|
||||
},
|
||||
"dependencies": {},
|
||||
"frameworks": {
|
||||
"netcoreapp1.0": {
|
||||
"dependencies": {
|
||||
"Microsoft.NETCore.App": {
|
||||
"type": "platform",
|
||||
"version": "1.0.1"
|
||||
}
|
||||
},
|
||||
"imports": "dnxcore50"
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user