Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Windows.Forms;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace AISDE
- {
- public class MyGraph
- {
- //public List<Node> Nodes = new List<Node>();
- public Dictionary<int, Node> Nodes = new Dictionary<int, Node>();
- public List<Edge> Edges = new List<Edge>();
- public MyGraph(string path)
- {
- using (StreamReader reader = new StreamReader(path))
- {
- string line;
- while ((line = reader.ReadLine()) != null)
- {
- while (line[0] == '#')
- {
- line = reader.ReadLine();
- continue;
- }
- Console.WriteLine(line);
- string[] line1 = line.Split(' ');
- Console.WriteLine(line1);
- if (line1[0] == "WEZLY")
- {
- int nodecnt = int.Parse(line1[2]);
- for (int i = 0; i < nodecnt; i++)
- {
- line = reader.ReadLine();
- while (line[0] == '#')
- {
- line = reader.ReadLine();
- }
- line1 = line.Split(' ');
- Nodes.Add(int.Parse(line1[0]), new Node(int.Parse(line1[1]), int.Parse(line1[2]), int.Parse(line1[0])));
- }
- }
- while (line[0] == '#')
- {
- line = reader.ReadLine();
- continue;
- }
- if (line1[0] == "LACZA")
- {
- int edgecnt = int.Parse(line1[2]);
- for (int i = 0; i < edgecnt; i++)
- {
- line = reader.ReadLine();
- while (line[0] == '#')
- {
- line = reader.ReadLine();
- }
- line1 = line.Split(' ');
- Edges.Add(new Edge(Nodes[int.Parse(line1[1])], Nodes[int.Parse(line1[2])], int.Parse(line1[0])));
- }
- }
- }
- }
- }
- public void DrawGraph()
- {
- var mst = new MinimumSpanningTreePrim(Nodes.Values.ToList(), Edges);
- //var dijkstra = new Dijkstra(Nodes.Values.ToList(), Edges);
- var minimalTreeEdges = new List<Edge>();
- List<Edge> shortestPath = mst.ProcessPath();
- //List<Edge> dijkstralist = dijkstra.Algorithm(4, 5);
- //create a form
- System.Windows.Forms.Form form = new System.Windows.Forms.Form();
- //create a viewer object
- Microsoft.Msagl.GraphViewerGdi.GViewer viewer = new Microsoft.Msagl.GraphViewerGdi.GViewer();
- //create a graph object
- Microsoft.Msagl.Drawing.Graph graph = new Microsoft.Msagl.Drawing.Graph("graph");
- //create the graph content
- /*foreach (var edge in Edges)
- {
- var gedge = graph.AddEdge(edge.GetBegin().GetName().ToString(), edge.GetEnd().GetName().ToString());
- gedge.Attr.ArrowheadAtTarget = Microsoft.Msagl.Drawing.ArrowStyle.None;
- gedge.LabelText = edge.GetWeight().ToString("0.00");
- if (shortestPath.Contains(edge))
- {
- gedge.Attr.Color = Microsoft.Msagl.Drawing.Color.Red;
- }
- }*/
- foreach (var edge in Edges)
- {
- if (shortestPath.Contains(edge))
- {
- minimalTreeEdges.Add(edge);
- }
- }
- var dijkstra = new Dijkstra(Nodes.Values.ToList(), minimalTreeEdges);
- List<Edge> dijkstralist = dijkstra.Algorithm(4, 5);
- foreach (var edge in Edges)
- {
- var gedge = graph.AddEdge(edge.GetBegin().GetName().ToString(), edge.GetEnd().GetName().ToString());
- gedge.Attr.ArrowheadAtTarget = Microsoft.Msagl.Drawing.ArrowStyle.None;
- gedge.LabelText = edge.GetWeight().ToString("0.00");
- if (dijkstralist.Contains(edge))
- {
- gedge.Attr.Color = Microsoft.Msagl.Drawing.Color.Green;
- }
- }
- //bind the graph to the viewer
- viewer.Graph = graph;
- //associate the viewer with the form
- form.SuspendLayout();
- viewer.Dock = System.Windows.Forms.DockStyle.Fill;
- form.Controls.Add(viewer);
- form.ResumeLayout();
- //show the form
- form.ShowDialog();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement