Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace DijkstrasExample
  5. {
  6. class Graphs
  7. {
  8.  
  9. private Dictionary<string, Dictionary<string, int>> vertexes;
  10.  
  11. public Graphs()
  12. {
  13. vertexes = new Dictionary<string, Dictionary<string, int>>();
  14.  
  15. }
  16.  
  17. public void AddEdge(string v1, string v2, int v3)
  18. {
  19. if (!vertexes.ContainsKey(v1))
  20. {
  21. vertexes.Add(v1, new Dictionary<string, int>());
  22. }
  23. vertexes[v1].Add(v2, v3);
  24. }
  25.  
  26. static Graphs CreateGraph()
  27. {
  28. Graphs g = new Graphs();
  29.  
  30. g.AddEdge("a", "b", 9);
  31. g.AddEdge("a", "x", 11);
  32. g.AddEdge("a", "2", 10);
  33. g.AddEdge("b", "b", 9);
  34. g.AddEdge("b", "x", 11);
  35. g.AddEdge("b", "2", 10);
  36.  
  37. return g;
  38. }
  39.  
  40. public void PrintGraph()
  41. {
  42. foreach (var entry in vertexes)
  43. {
  44. var vertexNeighbors = entry.Value;
  45. Console.Write(entry.Key + ": key : ");
  46. PrintVertices(vertexNeighbors);
  47. Console.WriteLine();
  48. }
  49. }
  50.  
  51. public void PrintVertices(Dictionary<string, int> vertices)
  52. {
  53. foreach (var entry in vertices)
  54. {
  55. Console.Write(entry.Key + " " + entry.Value + " ");
  56. }
  57. }
  58.  
  59. static void SeedTheGraph()
  60. {
  61. Graphs graphExample = CreateGraph();
  62. graphExample.PrintGraph();
  63. }
  64.  
  65. static void Main(string[] args)
  66. {
  67. SeedTheGraph();
  68. }
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement