Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Лістининг:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MathLab5
- {
- class Graph
- {
- Dictionary<string, Dictionary<string, int>> vertices = new Dictionary<string, Dictionary<string, int>>();
- public void add_vertex(string name, Dictionary<string, int> edges)
- {
- vertices[name] = edges;
- }
- public List<string> shortest_path(string start, string finish)
- {
- var previous = new Dictionary<string, string>();
- var distances = new Dictionary<string, int>();
- var nodes = new List<string>();
- List<string> path = null;
- foreach (var vertex in vertices)
- {
- if (vertex.Key == start)
- {
- distances[vertex.Key] = 0;
- }
- else
- {
- distances[vertex.Key] = int.MaxValue;
- }
- nodes.Add(vertex.Key);
- }
- while (nodes.Count != 0)
- {
- nodes.Sort((x, y) => distances[x] - distances[y]);
- var smallest = nodes[0];
- nodes.Remove(smallest);
- if (smallest == finish)
- {
- path = new List<string>();
- while (previous.ContainsKey(smallest))
- {
- path.Add(smallest);
- smallest = previous[smallest];
- }
- break;
- }
- if (distances[smallest] == int.MaxValue)
- {
- break;
- }
- foreach (var neighbor in vertices[smallest])
- {
- var alt = distances[smallest] + neighbor.Value;
- if (alt < distances[neighbor.Key])
- {
- distances[neighbor.Key] = alt;
- previous[neighbor.Key] = smallest;
- }
- }
- }
- //return path;
- //path = null;
- //GetResult();
- return path;
- }
- }
- class MainClass
- {
- public static void Main(string[] args)
- {
- Graph g = new Graph();
- g.add_vertex("V0", new Dictionary<string, int>() { { "V1", 4 }, { "V6", 4 } });
- g.add_vertex("V1", new Dictionary<string, int>() { { "V0", 4 }, { "V7", 5 }, { "V2", 4 } });
- g.add_vertex("V2", new Dictionary<string, int>() { { "V1", 4 }, { "V3", 6 }, { "V8", 3 } });
- g.add_vertex("V3", new Dictionary<string, int>() { { "V2", 6 }, { "V4", 7 }, { "V9", 2 } });
- g.add_vertex("V4", new Dictionary<string, int>() { { "V3", 7 }, { "V5", 8 }, { "V10", 7 } });
- g.add_vertex("V5", new Dictionary<string, int>() { { "V4", 8 }, { "V12", 1 } });
- g.add_vertex("V6", new Dictionary<string, int>() { { "V0", 4 }, { "V13", 3 }, { "V7", 2 } });
- g.add_vertex("V7", new Dictionary<string, int>() { { "V1", 5 }, { "V6", 2 }, { "V14", 3 }, { "V8", 1 } });
- g.add_vertex("V8", new Dictionary<string, int>() { { "V2", 3 }, { "V7", 1 }, { "V15", 4 }, { "V9", 1 } });
- g.add_vertex("V9", new Dictionary<string, int>() { { "V3", 2 }, { "V8", 1 }, { "V16", 6 }, { "V10", 5 } });
- g.add_vertex("V10", new Dictionary<string, int>() { { "V4", 7 }, { "V9", 1 }, { "V17", 7 }, { "V11", 5 } });
- g.add_vertex("V11", new Dictionary<string, int>() { { "V5", 1 }, { "V10", 5 }, { "V17", 2 } });
- g.add_vertex("V12", new Dictionary<string, int>() { { "V6", 3}, { "V13", 1 }, { "V18", 8 } });
- g.add_vertex("V13", new Dictionary<string, int>() { { "V7", 3 }, { "V14", 1 }, { "V19", 2 }, { "V12", 1} });
- g.add_vertex("V14", new Dictionary<string, int>() { { "V8", 4 }, { "V13", 1 }, { "V20", 3 }, { "V15", 3} });
- g.add_vertex("V15", new Dictionary<string, int>() { { "V9", 6 }, { "V14", 3 }, { "V21", 1 }, { "V16", 7 } });
- g.add_vertex("V16", new Dictionary<string, int>() { { "V10", 7 }, { "V15", 7 }, { "V22", 8 } , { "V17", 1 } });
- g.add_vertex("V17", new Dictionary<string, int>() { { "V11", 2}, { "V16", 1 }, { "V23", 8} });
- g.add_vertex("V18", new Dictionary<string, int>() { { "V12", 8 }, { "V19", 3 }, { "V24", 4 } });
- g.add_vertex("V19", new Dictionary<string, int>() { { "V13", 2 }, { "V25", 1 }, { "V20", 3 }, { "V18",7} });
- g.add_vertex("V20", new Dictionary<string, int>() { { "V14", 3 }, { "V19", 7 }, { "V26", 2 }, { "V21", 5} });
- g.add_vertex("V21", new Dictionary<string, int>() { { "V15", 8 }, { "V20", 5 }, { "V27", 7 }, { "V22", 3 } });
- g.add_vertex("V22", new Dictionary<string, int>() { { "V16", 8 }, { "V21", 3 }, { "V28", 3 }, { "V23", 5} });
- g.add_vertex("V23", new Dictionary<string, int>() { { "V17", 8 }, { "V22", 5 }, { "V29", 1 } });
- g.add_vertex("V24", new Dictionary<string, int>() { { "V18", 4 }, { "V25", 3 } });
- g.add_vertex("V25", new Dictionary<string, int>() { { "V19", 1 }, { "V26", 3 }, { "V24", 3 } });
- g.add_vertex("V26", new Dictionary<string, int>() { { "V25", 3 }, { "V20", 2 }, { "V27", 4 } });
- g.add_vertex("V27", new Dictionary<string, int>() { { "V21", 7 }, { "V26", 4 }, { "V28", 7 } });
- g.add_vertex("V28", new Dictionary<string, int>() { { "V22", 3 }, { "V27", 7 }, { "V29", 7 } });
- g.add_vertex("V29", new Dictionary<string, int>() { { "V23", 1 }, { "V28", 7 }});
- g.shortest_path("V0", "V29").ForEach(x => Console.WriteLine(x));
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment