alexandrheathen

Untitled

Dec 13th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.86 KB | None | 0 0
  1. Лістининг:
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace MathLab5
  9. {
  10.     class Graph
  11.     {
  12.         Dictionary<string, Dictionary<string, int>> vertices = new Dictionary<string, Dictionary<string, int>>();
  13.  
  14.         public void add_vertex(string name, Dictionary<string, int> edges)
  15.         {
  16.             vertices[name] = edges;
  17.         }
  18.  
  19.         public List<string> shortest_path(string start, string finish)
  20.         {
  21.             var previous = new Dictionary<string, string>();
  22.             var distances = new Dictionary<string, int>();
  23.             var nodes = new List<string>();
  24.  
  25.             List<string> path = null;
  26.  
  27.             foreach (var vertex in vertices)
  28.             {
  29.                 if (vertex.Key == start)
  30.                 {
  31.                     distances[vertex.Key] = 0;
  32.                 }
  33.                 else
  34.                 {
  35.                     distances[vertex.Key] = int.MaxValue;
  36.                 }
  37.  
  38.                 nodes.Add(vertex.Key);
  39.             }
  40.  
  41.             while (nodes.Count != 0)
  42.             {
  43.                 nodes.Sort((x, y) => distances[x] - distances[y]);
  44.  
  45.                 var smallest = nodes[0];
  46.                 nodes.Remove(smallest);
  47.  
  48.                 if (smallest == finish)
  49.                 {
  50.                     path = new List<string>();
  51.                     while (previous.ContainsKey(smallest))
  52.                     {
  53.                         path.Add(smallest);
  54.                         smallest = previous[smallest];
  55.                     }
  56.  
  57.                     break;
  58.                 }
  59.  
  60.                 if (distances[smallest] == int.MaxValue)
  61.                 {
  62.                     break;
  63.                 }
  64.  
  65.                 foreach (var neighbor in vertices[smallest])
  66.                 {
  67.                     var alt = distances[smallest] + neighbor.Value;
  68.                     if (alt < distances[neighbor.Key])
  69.                     {
  70.                         distances[neighbor.Key] = alt;
  71.                         previous[neighbor.Key] = smallest;
  72.                     }
  73.                 }
  74.             }
  75.  
  76.             //return path;
  77.             //path = null;
  78.             //GetResult();
  79.             return path;
  80.         }
  81.     }
  82.  
  83.     class MainClass
  84.     {
  85.         public static void Main(string[] args)
  86.         {
  87.             Graph g = new Graph();
  88.            
  89.             g.add_vertex("V0", new Dictionary<string, int>() { { "V1", 4 }, { "V6", 4 } });
  90.             g.add_vertex("V1", new Dictionary<string, int>() { { "V0", 4 }, { "V7", 5 }, { "V2", 4 } });
  91.             g.add_vertex("V2", new Dictionary<string, int>() { { "V1", 4 }, { "V3", 6 }, { "V8", 3 } });
  92.             g.add_vertex("V3", new Dictionary<string, int>() { { "V2", 6 }, { "V4", 7 }, { "V9", 2 } });
  93.             g.add_vertex("V4", new Dictionary<string, int>() { { "V3", 7 }, { "V5", 8 }, { "V10", 7 } });
  94.             g.add_vertex("V5", new Dictionary<string, int>() { { "V4", 8 }, { "V12", 1 } });
  95.  
  96.  
  97.             g.add_vertex("V6", new Dictionary<string, int>() { { "V0", 4 }, { "V13", 3 }, { "V7", 2 } });
  98.             g.add_vertex("V7", new Dictionary<string, int>() { { "V1", 5 }, { "V6", 2 }, { "V14", 3 }, { "V8", 1 } });
  99.             g.add_vertex("V8", new Dictionary<string, int>() { { "V2", 3 }, { "V7", 1 }, { "V15", 4 }, { "V9", 1 } });
  100.             g.add_vertex("V9", new Dictionary<string, int>() { { "V3", 2 }, { "V8", 1 }, { "V16", 6 }, { "V10", 5 } });
  101.             g.add_vertex("V10", new Dictionary<string, int>() { { "V4", 7 }, { "V9", 1 }, { "V17", 7 }, { "V11", 5 } });
  102.             g.add_vertex("V11", new Dictionary<string, int>() { { "V5", 1 }, { "V10", 5 }, { "V17", 2 } });
  103.  
  104.  
  105.             g.add_vertex("V12", new Dictionary<string, int>() { { "V6", 3}, { "V13", 1 }, { "V18", 8 } });
  106.             g.add_vertex("V13", new Dictionary<string, int>() { { "V7", 3 }, { "V14", 1 }, { "V19", 2 }, { "V12", 1} });
  107.             g.add_vertex("V14", new Dictionary<string, int>() { { "V8", 4 }, { "V13", 1 }, { "V20", 3 }, { "V15", 3} });
  108.             g.add_vertex("V15", new Dictionary<string, int>() { { "V9", 6 }, { "V14", 3 }, { "V21", 1 }, { "V16", 7 } });
  109.             g.add_vertex("V16", new Dictionary<string, int>() { { "V10", 7 }, { "V15", 7 }, { "V22", 8 } , { "V17", 1 } });
  110.             g.add_vertex("V17", new Dictionary<string, int>() { { "V11", 2}, { "V16", 1 }, { "V23", 8} });
  111.  
  112.  
  113.             g.add_vertex("V18", new Dictionary<string, int>() { { "V12", 8 }, { "V19", 3 }, { "V24", 4 } });
  114.             g.add_vertex("V19", new Dictionary<string, int>() { { "V13", 2 }, { "V25", 1 }, { "V20", 3 }, { "V18",7} });
  115.             g.add_vertex("V20", new Dictionary<string, int>() { { "V14", 3 }, { "V19", 7 }, { "V26", 2 }, { "V21", 5} });
  116.             g.add_vertex("V21", new Dictionary<string, int>() { { "V15", 8 }, { "V20", 5 }, { "V27", 7 }, { "V22", 3 } });
  117.             g.add_vertex("V22", new Dictionary<string, int>() { { "V16", 8 }, { "V21", 3 }, { "V28", 3 }, { "V23", 5} });
  118.             g.add_vertex("V23", new Dictionary<string, int>() { { "V17", 8 }, { "V22", 5 }, { "V29", 1 } });
  119.  
  120.  
  121.             g.add_vertex("V24", new Dictionary<string, int>() { { "V18", 4 }, { "V25", 3 } });
  122.             g.add_vertex("V25", new Dictionary<string, int>() { { "V19", 1 }, { "V26", 3 }, { "V24", 3 } });
  123.             g.add_vertex("V26", new Dictionary<string, int>() { { "V25", 3 }, { "V20", 2 }, { "V27", 4 } });
  124.             g.add_vertex("V27", new Dictionary<string, int>() { { "V21", 7 }, { "V26", 4 }, { "V28", 7 } });
  125.             g.add_vertex("V28", new Dictionary<string, int>() { { "V22", 3 }, { "V27", 7 }, { "V29", 7 } });
  126.             g.add_vertex("V29", new Dictionary<string, int>() { { "V23", 1 }, { "V28", 7 }});
  127.            
  128.             g.shortest_path("V0", "V29").ForEach(x => Console.WriteLine(x));
  129.             Console.ReadLine();
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment