Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows.Forms;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace AISDE
  10. {
  11.     public class MyGraph
  12.     {
  13.  
  14.         //public List<Node> Nodes = new List<Node>();
  15.         public Dictionary<int, Node> Nodes = new Dictionary<int, Node>();
  16.         public List<Edge> Edges = new List<Edge>();
  17.  
  18.         public MyGraph(string path)
  19.         {
  20.             using (StreamReader reader = new StreamReader(path))
  21.             {
  22.                 string line;
  23.                 while ((line = reader.ReadLine()) != null)
  24.                 {
  25.                     while (line[0] == '#')
  26.                     {
  27.                         line = reader.ReadLine();
  28.                         continue;
  29.                     }
  30.                     Console.WriteLine(line);
  31.                     string[] line1 = line.Split(' ');
  32.  
  33.                     Console.WriteLine(line1);
  34.                     if (line1[0] == "WEZLY")
  35.                     {
  36.                         int nodecnt = int.Parse(line1[2]);
  37.                         for (int i = 0; i < nodecnt; i++)
  38.                         {
  39.                             line = reader.ReadLine();
  40.                             while (line[0] == '#')
  41.                             {
  42.                                 line = reader.ReadLine();
  43.  
  44.                             }
  45.  
  46.                             line1 = line.Split(' ');
  47.  
  48.                             Nodes.Add(int.Parse(line1[0]), new Node(int.Parse(line1[1]), int.Parse(line1[2]), int.Parse(line1[0])));
  49.  
  50.                         }
  51.                     }
  52.                     while (line[0] == '#')
  53.                     {
  54.                         line = reader.ReadLine();
  55.                         continue;
  56.                     }
  57.                     if (line1[0] == "LACZA")
  58.                     {
  59.                         int edgecnt = int.Parse(line1[2]);
  60.                         for (int i = 0; i < edgecnt; i++)
  61.                         {
  62.                             line = reader.ReadLine();
  63.                             while (line[0] == '#')
  64.                             {
  65.                                 line = reader.ReadLine();
  66.                             }
  67.  
  68.                             line1 = line.Split(' ');
  69.  
  70.                             Edges.Add(new Edge(Nodes[int.Parse(line1[1])], Nodes[int.Parse(line1[2])], int.Parse(line1[0])));
  71.  
  72.                         }
  73.                     }
  74.                 }
  75.             }
  76.         }
  77.  
  78.         public void DrawGraph()
  79.         {
  80.  
  81.             var mst = new MinimumSpanningTreePrim(Nodes.Values.ToList(), Edges);
  82.             //var dijkstra = new Dijkstra(Nodes.Values.ToList(), Edges);
  83.            
  84.             var minimalTreeEdges = new List<Edge>();
  85.  
  86.             List<Edge> shortestPath = mst.ProcessPath();
  87.             //List<Edge> dijkstralist = dijkstra.Algorithm(4, 5);
  88.  
  89.             //create a form
  90.             System.Windows.Forms.Form form = new System.Windows.Forms.Form();
  91.             //create a viewer object
  92.             Microsoft.Msagl.GraphViewerGdi.GViewer viewer = new Microsoft.Msagl.GraphViewerGdi.GViewer();
  93.             //create a graph object
  94.             Microsoft.Msagl.Drawing.Graph graph = new Microsoft.Msagl.Drawing.Graph("graph");
  95.             //create the graph content
  96.  
  97.             /*foreach (var edge in Edges)
  98.             {
  99.                 var gedge = graph.AddEdge(edge.GetBegin().GetName().ToString(), edge.GetEnd().GetName().ToString());
  100.                 gedge.Attr.ArrowheadAtTarget = Microsoft.Msagl.Drawing.ArrowStyle.None;
  101.                 gedge.LabelText = edge.GetWeight().ToString("0.00");
  102.                 if (shortestPath.Contains(edge))
  103.                 {
  104.                     gedge.Attr.Color = Microsoft.Msagl.Drawing.Color.Red;
  105.                 }
  106.             }*/
  107.  
  108.             foreach (var edge in Edges)
  109.             {
  110.                 if (shortestPath.Contains(edge))
  111.                 {
  112.                     minimalTreeEdges.Add(edge);
  113.                 }
  114.             }
  115.  
  116.             var dijkstra = new Dijkstra(Nodes.Values.ToList(), minimalTreeEdges);
  117.             List<Edge> dijkstralist = dijkstra.Algorithm(4, 5);
  118.  
  119.             foreach (var edge in Edges)
  120.             {
  121.                 var gedge = graph.AddEdge(edge.GetBegin().GetName().ToString(), edge.GetEnd().GetName().ToString());
  122.                 gedge.Attr.ArrowheadAtTarget = Microsoft.Msagl.Drawing.ArrowStyle.None;
  123.                 gedge.LabelText = edge.GetWeight().ToString("0.00");
  124.                 if (dijkstralist.Contains(edge))
  125.                 {
  126.                     gedge.Attr.Color = Microsoft.Msagl.Drawing.Color.Green;
  127.                 }
  128.             }
  129.  
  130.             //bind the graph to the viewer
  131.             viewer.Graph = graph;
  132.             //associate the viewer with the form
  133.             form.SuspendLayout();
  134.             viewer.Dock = System.Windows.Forms.DockStyle.Fill;
  135.             form.Controls.Add(viewer);
  136.             form.ResumeLayout();
  137.             //show the form
  138.             form.ShowDialog();
  139.         }
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement